1717import { join } from "path"
1818import { encodeComponent } from "@kui-shell/core"
1919
20+ async function getAppPath ( ) {
21+ try {
22+ const { app } = await import ( "electron" )
23+ if ( app ) {
24+ const appPath = app . getAppPath ( )
25+ if ( appPath ) {
26+ return appPath
27+ }
28+ }
29+ } catch ( err ) {
30+ console . error ( "Error fetching app path" , err )
31+ }
32+
33+ const appPath = process . argv . find ( ( _ ) => / a p p - p a t h / . test ( _ ) )
34+ if ( appPath ) {
35+ return appPath . replace ( / ^ - - a p p - p a t h = / , "" )
36+ } else {
37+ return "."
38+ }
39+ }
40+
2041/**
2142 * In electron production builds, if the user launches by double
2243 * clicking, or via spotlight (macos), then the CODEFLARE_HEADLESS and
@@ -33,44 +54,34 @@ import { encodeComponent } from "@kui-shell/core"
3354 * @return the absolute path to the directory that includes the
3455 * headless bundle.js.
3556 */
36- function electronProductionBuildHeadlessRoot ( ) {
37- const appPath = process . argv . find ( ( _ ) => / a p p - p a t h / . test ( _ ) )
38- if ( appPath ) {
39- return join ( appPath . replace ( / ^ - - a p p - p a t h = / , "" ) , "dist/headless" )
40- } else {
41- return "."
42- }
57+ async function electronProductionBuildHeadlessRoot ( ) {
58+ return join ( await getAppPath ( ) , "dist/headless" )
4359}
4460
4561/**
4662 * @return same as with `electronProductionBuildHeadlessRoot()`, except
4763 * returning the guidebook store absolute path
4864 */
49- function electronProductionBuildGuidebookStore ( ) {
50- const appPath = process . argv . find ( ( _ ) => / a p p - p a t h / . test ( _ ) )
51- if ( appPath ) {
52- return join ( appPath . replace ( / ^ - - a p p - p a t h = / , "" ) , "store" )
53- } else {
54- return ""
55- }
65+ async function electronProductionBuildGuidebookStore ( ) {
66+ return join ( await getAppPath ( ) , "store" )
5667}
5768
5869/** @return the absolute path to the directory that contains the headless bundle.js */
59- function headlessRoot ( ) {
60- return process . env . CODEFLARE_HEADLESS || electronProductionBuildHeadlessRoot ( )
70+ async function headlessRoot ( ) {
71+ return process . env . CODEFLARE_HEADLESS || ( await electronProductionBuildHeadlessRoot ( ) )
6172}
6273
6374/** @return the absolute path to the directory that contains the guidebook store for this build */
64- function guidebookStore ( ) {
65- return process . env . GUIDEBOOK_STORE || electronProductionBuildGuidebookStore ( )
75+ async function guidebookStore ( ) {
76+ return process . env . GUIDEBOOK_STORE || ( await electronProductionBuildGuidebookStore ( ) )
6677}
6778
6879/** Fill in the given command line to spawn ourselves as a subprocess */
69- export default function respawnCommand ( cmdline : string | string [ ] ) {
80+ export default async function respawnCommand ( cmdline : string | string [ ] ) {
7081 return {
7182 argv : [
7283 encodeComponent ( process . argv [ 0 ] ) ,
73- encodeComponent ( headlessRoot ( ) + "/codeflare.min.js" ) ,
84+ encodeComponent ( ( await headlessRoot ( ) ) + "/codeflare.min.js" ) ,
7485 "--" ,
7586 ...( typeof cmdline === "string" ? [ cmdline ] : cmdline ) ,
7687 ] ,
@@ -79,7 +90,7 @@ export default function respawnCommand(cmdline: string | string[]) {
7990 KUI_HEADLESS : "true" ,
8091 KUI_HEADLESS_WEBPACK : "true" ,
8192 ELECTRON_RUN_AS_NODE : "true" ,
82- GUIDEBOOK_STORE : guidebookStore ( ) ,
93+ GUIDEBOOK_STORE : await guidebookStore ( ) ,
8394 DEBUG : process . env . DEBUG || "" ,
8495 HOME : process . env . HOME || "" ,
8596 PATH : process . env . PATH || "" ,
0 commit comments