14
14
* limitations under the License.
15
15
*/
16
16
17
+ import { join } from "path"
17
18
import { encodeComponent } from "@kui-shell/core"
18
19
20
+ /**
21
+ * In electron production builds, if the user launches by double
22
+ * clicking, or via spotlight (macos), then the CODEFLARE_HEADLESS and
23
+ * GUIDEBOOK_STORE env vars are not defined. However, we can still
24
+ * find these relative to the electron appPath. Note: here, we take a
25
+ * shortcut, and extract the appPath from the `argv`. The proper way
26
+ * to do this would be to invoke
27
+ * `import('electron').app.getAppPath()`. Doing so would require
28
+ * communicating with the electron main process, since this API is not
29
+ * available in the renderer processes. See src/tray/renderer.ts for
30
+ * how this is done; except that we would need to consume a return
31
+ * value from the main process.
32
+ *
33
+ * @return the absolute path to the directory that includes the
34
+ * headless bundle.js.
35
+ */
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
+ }
43
+ }
44
+
45
+ /**
46
+ * @return same as with `electronProductionBuildHeadlessRoot()`, except
47
+ * returning the guidebook store absolute path
48
+ */
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
+ }
56
+ }
57
+
58
+ /** @return the absolute path to the directory that contains the headless bundle.js */
59
+ function headlessRoot ( ) {
60
+ return process . env . CODEFLARE_HEADLESS || electronProductionBuildHeadlessRoot ( )
61
+ }
62
+
63
+ /** @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 ( )
66
+ }
67
+
19
68
/** Fill in the given command line to spawn ourselves as a subprocess */
20
69
export default function respawnCommand ( cmdline : string | string [ ] ) {
21
70
return {
22
71
argv : [
23
72
encodeComponent ( process . argv [ 0 ] ) ,
24
- encodeComponent ( process . env . CODEFLARE_HEADLESS + "/codeflare.min.js" ) ,
73
+ encodeComponent ( headlessRoot ( ) + "/codeflare.min.js" ) ,
25
74
"--" ,
26
75
...( typeof cmdline === "string" ? [ cmdline ] : cmdline ) ,
27
76
] ,
@@ -30,7 +79,7 @@ export default function respawnCommand(cmdline: string | string[]) {
30
79
KUI_HEADLESS : "true" ,
31
80
KUI_HEADLESS_WEBPACK : "true" ,
32
81
ELECTRON_RUN_AS_NODE : "true" ,
33
- GUIDEBOOK_STORE : process . env . GUIDEBOOK_STORE || "" ,
82
+ GUIDEBOOK_STORE : guidebookStore ( ) ,
34
83
DEBUG : process . env . DEBUG || "" ,
35
84
HOME : process . env . HOME || "" ,
36
85
PATH : process . env . PATH || "" ,
0 commit comments