Skip to content

Commit 050ca21

Browse files
authored
feat: allow extracting custom Node.js source tarballs (#35)
1 parent 03086c5 commit 050ca21

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

bin/boxednode.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const argv = require('yargs')
1313
alias: 't', type: 'string', demandOption: true, desc: 'Target executable file'
1414
})
1515
.option('node-version', {
16-
alias: 'n', type: 'string', desc: 'Node.js version or semver version range', default: '*'
16+
alias: 'n', type: 'string', desc: 'Node.js version or semver version range or tarball file url', default: '*'
1717
})
1818
.option('configure-args', {
1919
alias: 'C', type: 'string', desc: 'Extra ./configure or vcbuild arguments, comma-separated'

src/index.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,35 @@ import { ExecutableMetadata, generateRCFile } from './executable-metadata';
1414
import { spawnBuildCommand, ProcessEnv, pipeline, createCppJsStringDefinition } from './helpers';
1515
import { Readable } from 'stream';
1616
import nv from '@pkgjs/nv';
17+
import { fileURLToPath, URL } from 'url';
1718

1819
// Download and unpack a tarball containing the code for a specific Node.js version.
1920
async function getNodeSourceForVersion (range: string, dir: string, logger: Logger, retries = 2): Promise<string> {
2021
logger.stepStarting(`Looking for Node.js version matching ${JSON.stringify(range)}`);
22+
23+
let inputIsFileUrl = false;
24+
try {
25+
inputIsFileUrl = new URL(range).protocol === 'file:';
26+
} catch { /* not a valid URL */ }
27+
28+
if (inputIsFileUrl) {
29+
logger.stepStarting(`Extracting tarball from ${range}`);
30+
await pipeline(
31+
createReadStream(fileURLToPath(range)),
32+
zlib.createGunzip(),
33+
tar.x({
34+
cwd: dir
35+
})
36+
);
37+
logger.stepCompleted();
38+
const filesInDir = await fs.readdir(dir, { withFileTypes: true });
39+
const dirsInDir = filesInDir.filter(f => f.isDirectory());
40+
if (dirsInDir.length !== 1) {
41+
throw new Error('Node.js tarballs should contain exactly one directory');
42+
}
43+
return path.join(dir, dirsInDir[0].name);
44+
}
45+
2146
const ver = (await nv(range)).pop();
2247
if (!ver) {
2348
throw new Error(`No node version found for ${range}`);

0 commit comments

Comments
 (0)