Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add js2bin-version-info #19

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ jobs:
condition: contains(variables['Agent.JobName'], 'windows')
- script: npm ci
displayName: Install dependencies
- script: node js2bin.js --ci --node=14.15.3 --size=2MB --size=4MB --upload --clean
- script: npm run deploy
displayName: Build base node binaries
env:
GITHUB_TOKEN: $(PersonalGithubToken)
Expand Down
133 changes: 76 additions & 57 deletions js2bin.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,46 @@
#!/usr/bin/env node

'use strict';

const { NodeJsBuilder } = require('./src/NodeBuilder');
const { VersionInfo } = require('js2bin-version-info');
const { log } = require('./src/util');
const fs = require('fs');

function usage(msg) {
if (msg) { console.log(`ERROR: ${msg}`); }
if (msg) {
console.log(`ERROR: ${msg}`);
}
console.log(`usage: ${process.argv[1]} command <command-args>
command: --build, --ci, --help
command-args: take the form of --name=value

--build: embed your application into the precompiled NodeJS binary.
--node: NodeJS version(s) to use, can specify more than one.
e.g. --node=10.16.0 --node=12.4.0
--platform: Platform(s) to build for, can specifiy more than one.
--platform: Platform(s) to build for, can specifiy more than one.
e.g. --platform=linux --plaform=darwin
--app: Path to your (bundled) application.
e.g. --app=/path/to/app/index.js
--name: (opt) Application name
--node: (opt) NodeJS version(s) to use, can specify more than one.
If not specified use the latest precompiled NodeJS binary.
e.g. --node=10.16.0 --node=12.4.0
--name: (opt) Application name.
e.g --name=MyAppSoCool
--dir: (opt) Working directory, if not specified use cwd
--dir: (opt) Working directory, if not specified use cwd.
e.g. --dir=/tmp/js2bin
--cache (opt) Cache any pre-built binaries used, to avoid redownload
--cache (opt) Cache any pre-built binaries used, to avoid redownload.


--ci: build NodeJS with preallocated space for embedding applications
--node: NodeJS version to build from source, can specify more than one.
e.g. --node=10.16.0
--size: Amount of preallocated space, can specify more than one.
e.g. --size=2MB --size==4MB
--dir: (opt) Working directory, if not specified use cwd
--cache: (opt) whether to keep build in the cache (to be reused by --build)
--upload: (opt) whether to upload node build to github releases
--clean: (opt) whether to clean up after the build
--ci: build NodeJS with preallocated space for embedding applications.
--size: Amount of preallocated space, can specify more than one.
e.g. --size=2MB --size==4MB
--node: (opt) NodeJS version to build from source, can specify more than one.
e.g. --node=10.16.0
--dir: (opt) Working directory, if not specified use cwd.
--cache: (opt) Whether to keep build in the cache (to be reused by --build).
--upload: (opt) Whether to upload node build to github releases.
--clean: (opt) Whether to clean up after the build.

--help: print this help message
--help: print this help message.
`);
process.exit(1);
}
Expand All @@ -46,7 +52,6 @@ function parseArgs() {
if (!arg.startsWith('--')) {
return usage(`invalid argument: ${arg}`);
}

if (arg === '--help') {
return usage();
}
Expand All @@ -55,7 +60,11 @@ function parseArgs() {
const name = parts[0];
const value = parts.length === 1 ? true : parts[1];
if (args[name] !== undefined) {
if (Array.isArray(args[name])) { args[name].push(value); } else { args[name] = [args[name], value]; }
if (Array.isArray(args[name])) {
args[name].push(value);
} else {
args[name] = [args[name], value];
}
} else {
args[name] = value;
}
Expand All @@ -65,7 +74,6 @@ function parseArgs() {
if (!args.build && !args.ci) {
return usage('must use either --build or --ci');
}
args.node = (args.node || '10.16.0');
args.platform = (args.platform || NodeJsBuilder.platform());
return args;
}
Expand All @@ -74,45 +82,56 @@ function asArray(val) {
return Array.isArray(val) ? val : [val];
}

const args = parseArgs();
let p = Promise.resolve();

if (args.build) {
const app = args.app;
if (!app) usage('missing required arg: --app');
if (!fs.existsSync(app)) {
console.log(`ERROR: file not found: ${app}`);
process.exit(1);
}
(async () => {
const args = parseArgs();
let p = Promise.resolve();

const versions = asArray(args.node);
const plats = asArray(args.platform);
versions.forEach(version => {
plats.forEach(plat => {
const builder = new NodeJsBuilder(args.dir, version, app, args.name);
p = p.then(() => {
log(`building for version=${version}, plat=${plat} app=${app}}`);
const arch = 'x64';
const outName = args.name ? `${args.name}-${plat}-${arch}` : undefined;
return builder.buildFromCached(plat, arch, outName, args.cache);
if (args.build) {
const app = args.app;
if (!app) {
return usage('missing required arg: --app');
}
if (!fs.existsSync(app)) {
console.log(`ERROR: file not found: ${app}`);
process.exit(1);
}
const versions = asArray(args.node || await new VersionInfo().get('build'));
const plats = asArray(args.platform);
versions.forEach(version => {
plats.forEach(plat => {
const builder = new NodeJsBuilder(args.dir, version, app, args.name);
p = p.then(() => {
log(`building for version=${version}, plat=${plat} app=${app}}`);
const arch = 'x64';
const outName = args.name
? `${args.name}-${plat}-${arch}`
: undefined;
return builder.buildFromCached(plat, arch, outName, args.cache);
});
});
});
});
} else if (args.ci) {
const versions = asArray(args.node);
const sizes = asArray(args.size || '2MB').map(v => `__${v.trim().toUpperCase()}__`);
versions.forEach(version => {
let lastBuilder;
sizes.forEach(size => {
const builder = new NodeJsBuilder(args.dir, version, size);
lastBuilder = builder;
p = p.then(() => {
log(`building for version=${version}, size=${size}`);
return builder.buildFromSource(args.upload, args.cache);
} else if (args.ci) {
const versions = asArray(args.node || await new VersionInfo().get('ci'));
const sizes =
asArray(args.size || '2MB').map(v => `__${v.trim().toUpperCase()}__`);
versions.forEach(version => {
let lastBuilder;
sizes.forEach(size => {
const builder = new NodeJsBuilder(args.dir, version, size);
lastBuilder = builder;
p = p.then(() => {
log(`building for version=${version}, size=${size}`);
return builder.buildFromSource(args.upload, args.cache);
});
});
if (args.clean) {
p = p.then(() => lastBuilder.cleanupBuild().catch(err => log(err)));
}
});
if (args.clean) { p = p.then(() => lastBuilder.cleanupBuild().catch(err => log(err))); }
});
} else {
usage();
}
} else {
return usage();
}
})().catch(e => {
console.error(e);
process.exit(1);
});
49 changes: 34 additions & 15 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"description": "Native binary for your NodeJS apps",
"main": "index.js",
"scripts": {
"deploy": "node js2bin.js --ci --size=2MB --size=4MB --upload --clean",
"lint": "eslint --fix .",
"test": "echo \"Error: no test specified\" && exit 1"
},
Expand All @@ -26,7 +27,8 @@
},
"homepage": "https://github.com/criblio/js2bin#readme",
"dependencies": {
"tar-fs": "^2.1.0"
"js2bin-version-info": "^1.0.1",
"tar-fs": "^2.1.1"
},
"devDependencies": {
"eslint": "^7.13.0",
Expand Down