|
1 | 1 | import EmscriptenModule from "./python.mjs"; |
2 | | -import { dirname } from 'node:path'; |
3 | | -import { fileURLToPath } from 'node:url'; |
| 2 | +import fs from "node:fs"; |
4 | 3 |
|
5 | 4 | if (process?.versions?.node) { |
6 | 5 | const nodeVersion = Number(process.versions.node.split(".", 1)[0]); |
7 | 6 | if (nodeVersion < 18) { |
8 | | - process.stderr.write( |
9 | | - `Node version must be >= 18, got version ${process.version}\n`, |
10 | | - ); |
11 | | - process.exit(1); |
| 7 | + process.stderr.write( |
| 8 | + `Node version must be >= 18, got version ${process.version}\n`, |
| 9 | + ); |
| 10 | + process.exit(1); |
12 | 11 | } |
13 | 12 | } |
14 | 13 |
|
| 14 | +function rootDirsToMount(Module) { |
| 15 | + return fs |
| 16 | + .readdirSync("/") |
| 17 | + .filter((dir) => !["dev", "lib", "proc"].includes(dir)) |
| 18 | + .map((dir) => "/" + dir); |
| 19 | +} |
| 20 | + |
| 21 | +function mountDirectories(Module) { |
| 22 | + for (const dir of rootDirsToMount(Module)) { |
| 23 | + Module.FS.mkdirTree(dir); |
| 24 | + Module.FS.mount(Module.FS.filesystems.NODEFS, { root: dir }, dir); |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +const thisProgram = "--this-program="; |
| 29 | +const thisProgramIndex = process.argv.findIndex((x) => |
| 30 | + x.startsWith(thisProgram), |
| 31 | +); |
| 32 | + |
15 | 33 | const settings = { |
16 | 34 | preRun(Module) { |
17 | | - const __dirname = dirname(fileURLToPath(import.meta.url)); |
18 | | - Module.FS.mkdirTree("/lib/"); |
19 | | - Module.FS.mount(Module.FS.filesystems.NODEFS, { root: __dirname + "/lib/" }, "/lib/"); |
| 35 | + mountDirectories(Module); |
| 36 | + Module.FS.chdir(process.cwd()); |
| 37 | + Object.assign(Module.ENV, process.env); |
20 | 38 | }, |
21 | | - // The first three arguments are: "node", path to this file, path to |
22 | | - // python.sh. After that come the arguments the user passed to python.sh. |
23 | | - arguments: process.argv.slice(3), |
24 | 39 | // Ensure that sys.executable, sys._base_executable, etc point to python.sh |
25 | 40 | // not to this file. To properly handle symlinks, python.sh needs to compute |
26 | 41 | // its own path. |
27 | | - thisProgram: process.argv[2], |
| 42 | + thisProgram: process.argv[thisProgramIndex], |
| 43 | + // After python.sh come the arguments thatthe user passed to python.sh. |
| 44 | + arguments: process.argv.slice(thisProgramIndex + 1), |
28 | 45 | }; |
29 | 46 |
|
30 | 47 | await EmscriptenModule(settings); |
0 commit comments