1
- // cocalc.js
2
- const VERSION = "${VERSION}" ;
3
-
4
- const { getRawAsset } = require ( "node:sea" ) ;
5
- const fs = require ( "node:fs" ) ;
6
- const os = require ( "node:os" ) ;
7
1
const path = require ( "node:path" ) ;
8
- const { spawnSync } = require ( "node:child_process" ) ;
9
-
10
- const destDir = path . join (
11
- process . env . XDG_CACHE_HOME || path . join ( os . homedir ( ) , ".cache" ) ,
12
- "cocalc" ,
13
- "project-runner" ,
14
- VERSION ,
15
- ) ;
16
-
17
- const stamp = path . join ( destDir , ".ok" ) ;
18
- if ( ! fs . existsSync ( stamp ) ) {
19
- console . log ( "Unpacking..." ) ;
20
- // Read the SEA asset into a Buffer
21
- const ab = getRawAsset ( "cocalc-project-runner.tar.xz" ) ; // ArrayBuffer (no copy)
22
- const buf = Buffer . from ( new Uint8Array ( ab ) ) ; // turn into Node Buffer
23
-
24
- fs . mkdirSync ( destDir , { recursive : true } ) ;
2
+ const fs = require ( "node:fs" ) ;
25
3
26
- const child = spawnSync (
27
- "tar" ,
28
- [ "-Jxf" , "-" , "-C" , destDir , "--strip-components=1" ] ,
29
- {
30
- input : buf ,
31
- stdio : [ "pipe" , "inherit" , "inherit" ] ,
32
- } ,
4
+ function extractAssetsSync ( ) {
5
+ const VERSION = "${VERSION}" ;
6
+ const { getRawAsset } = require ( "node:sea" ) ;
7
+ const os = require ( "node:os" ) ;
8
+ const { spawnSync } = require ( "node:child_process" ) ;
9
+
10
+ const destDir = path . join (
11
+ process . env . XDG_CACHE_HOME || path . join ( os . homedir ( ) , ".cache" ) ,
12
+ "cocalc" ,
13
+ "project-runner" ,
14
+ VERSION ,
33
15
) ;
34
16
35
- if ( child . error ) {
36
- console . error ( "Failed to run tar:" , child . error ) ;
37
- process . exit ( 1 ) ;
38
- }
39
- if ( child . status !== 0 ) {
40
- console . error ( `tar exited with code ${ child . status } ` ) ;
41
- process . exit ( child . status ) ;
17
+ const stamp = path . join ( destDir , ".ok" ) ;
18
+ if ( ! fs . existsSync ( stamp ) ) {
19
+ console . log ( "Unpacking..." ) ;
20
+ // Read the SEA asset into a Buffer
21
+ const ab = getRawAsset ( "cocalc-project-runner.tar.xz" ) ; // ArrayBuffer (no copy)
22
+ const buf = Buffer . from ( new Uint8Array ( ab ) ) ; // turn into Node Buffer
23
+
24
+ fs . mkdirSync ( destDir , { recursive : true } ) ;
25
+
26
+ const child = spawnSync (
27
+ "tar" ,
28
+ [ "-Jxf" , "-" , "-C" , destDir , "--strip-components=1" ] ,
29
+ {
30
+ input : buf ,
31
+ stdio : [ "pipe" , "inherit" , "inherit" ] ,
32
+ } ,
33
+ ) ;
34
+
35
+ if ( child . error ) {
36
+ console . error ( "Failed to run tar:" , child . error ) ;
37
+ process . exit ( 1 ) ;
38
+ }
39
+ if ( child . status !== 0 ) {
40
+ console . error ( `tar exited with code ${ child . status } ` ) ;
41
+ process . exit ( child . status ) ;
42
+ }
43
+
44
+ console . log ( "Assets ready at:" , destDir ) ;
45
+ fs . writeFileSync ( stamp , "" ) ;
42
46
}
43
-
44
- console . log ( "Assets ready at:" , destDir ) ;
45
- fs . writeFileSync ( stamp , "" ) ;
47
+ return destDir ;
46
48
}
47
49
48
50
const Module = require ( "node:module" ) ;
49
-
50
51
const looksLikeScript = process . argv [ 2 ] && ! process . argv [ 2 ] . startsWith ( "-" ) ;
51
52
52
53
if ( looksLikeScript ) {
53
54
process . argv = [ process . execPath , ...process . argv . slice ( 2 ) ] ;
54
55
} else {
56
+ const destDir = extractAssetsSync ( ) ;
55
57
console . log ( "Starting CoCalc Project Runner" ) ;
56
58
57
59
const script = path . join ( destDir , "src/packages/project-runner/bin/start.js" ) ;
@@ -74,5 +76,4 @@ if (looksLikeScript) {
74
76
process . env . AUTH_TOKEN = "random" ;
75
77
}
76
78
77
- // run like “node start.js”
78
- Module . runMain ( ) ; // loads process.argv[1] as the main script
79
+ Module . runMain ( ) ;
0 commit comments