1414 * limitations under the License.
1515 */
1616
17+ import { join } from "path"
1718import { encodeComponent } from "@kui-shell/core"
1819
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+
1968/** Fill in the given command line to spawn ourselves as a subprocess */
2069export default function respawnCommand ( cmdline : string | string [ ] ) {
2170 return {
2271 argv : [
2372 encodeComponent ( process . argv [ 0 ] ) ,
24- encodeComponent ( process . env . CODEFLARE_HEADLESS + "/codeflare.min.js" ) ,
73+ encodeComponent ( headlessRoot ( ) + "/codeflare.min.js" ) ,
2574 "--" ,
2675 ...( typeof cmdline === "string" ? [ cmdline ] : cmdline ) ,
2776 ] ,
@@ -30,7 +79,7 @@ export default function respawnCommand(cmdline: string | string[]) {
3079 KUI_HEADLESS : "true" ,
3180 KUI_HEADLESS_WEBPACK : "true" ,
3281 ELECTRON_RUN_AS_NODE : "true" ,
33- GUIDEBOOK_STORE : process . env . GUIDEBOOK_STORE || "" ,
82+ GUIDEBOOK_STORE : guidebookStore ( ) ,
3483 DEBUG : process . env . DEBUG || "" ,
3584 HOME : process . env . HOME || "" ,
3685 PATH : process . env . PATH || "" ,
0 commit comments