17
17
import { join } from "path"
18
18
import { encodeComponent } from "@kui-shell/core"
19
19
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
+
20
41
/**
21
42
* In electron production builds, if the user launches by double
22
43
* clicking, or via spotlight (macos), then the CODEFLARE_HEADLESS and
@@ -33,44 +54,34 @@ import { encodeComponent } from "@kui-shell/core"
33
54
* @return the absolute path to the directory that includes the
34
55
* headless bundle.js.
35
56
*/
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" )
43
59
}
44
60
45
61
/**
46
62
* @return same as with `electronProductionBuildHeadlessRoot()`, except
47
63
* returning the guidebook store absolute path
48
64
*/
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" )
56
67
}
57
68
58
69
/** @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 ( ) )
61
72
}
62
73
63
74
/** @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 ( ) )
66
77
}
67
78
68
79
/** 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 [ ] ) {
70
81
return {
71
82
argv : [
72
83
encodeComponent ( process . argv [ 0 ] ) ,
73
- encodeComponent ( headlessRoot ( ) + "/codeflare.min.js" ) ,
84
+ encodeComponent ( ( await headlessRoot ( ) ) + "/codeflare.min.js" ) ,
74
85
"--" ,
75
86
...( typeof cmdline === "string" ? [ cmdline ] : cmdline ) ,
76
87
] ,
@@ -79,7 +90,7 @@ export default function respawnCommand(cmdline: string | string[]) {
79
90
KUI_HEADLESS : "true" ,
80
91
KUI_HEADLESS_WEBPACK : "true" ,
81
92
ELECTRON_RUN_AS_NODE : "true" ,
82
- GUIDEBOOK_STORE : guidebookStore ( ) ,
93
+ GUIDEBOOK_STORE : await guidebookStore ( ) ,
83
94
DEBUG : process . env . DEBUG || "" ,
84
95
HOME : process . env . HOME || "" ,
85
96
PATH : process . env . PATH || "" ,
0 commit comments