|
| 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 | +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 }); |
| 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, ""); |
| 46 | +} |
| 47 | + |
| 48 | +const Module = require("node:module"); |
| 49 | + |
| 50 | +const looksLikeScript = process.argv[2] && !process.argv[2].startsWith("-"); |
| 51 | + |
| 52 | +if (looksLikeScript) { |
| 53 | + process.argv = [process.execPath, ...process.argv.slice(2)]; |
| 54 | +} else { |
| 55 | + console.log("Starting CoCalc Project Runner"); |
| 56 | + |
| 57 | + const script = path.join(destDir, "src/packages/project-runner/bin/start.js"); |
| 58 | + |
| 59 | + if (!fs.existsSync(script)) { |
| 60 | + console.error("missing start.js at", script); |
| 61 | + process.exit(1); |
| 62 | + } |
| 63 | + |
| 64 | + // set up argv and cwd as if launched directly |
| 65 | + process.chdir(path.dirname(script)); |
| 66 | + process.argv = [process.execPath, script, ...process.argv.slice(2)]; |
| 67 | + |
| 68 | + // make sure PATH (and any other env) includes your extracted tools |
| 69 | + process.env.PATH = |
| 70 | + path.join(destDir, "src/packages/project-runner/bin/") + |
| 71 | + path.delimiter + |
| 72 | + process.env.PATH; |
| 73 | + |
| 74 | + process.env.AUTH_TOKEN = "random"; |
| 75 | +} |
| 76 | + |
| 77 | +// run like “node start.js” |
| 78 | +Module.runMain(); // loads process.argv[1] as the main script |
0 commit comments