Skip to content

Instantly share code, notes, and snippets.

@yak1ex
Last active June 7, 2017 07:02
Show Gist options
  • Select an option

  • Save yak1ex/800d09dcd09dd633b83fa1827517e218 to your computer and use it in GitHub Desktop.

Select an option

Save yak1ex/800d09dcd09dd633b83fa1827517e218 to your computer and use it in GitHub Desktop.
node-bootstrap
{
"versions": {
"nwjs": "0.21.5",
"node": "7.8.0"
},
"dependencies": {
"bluebird": "^3.5.0",
"canvas": {
"version": "^1.6.5",
"arg": "--with_jpeg=true"
},
"chai": "^3.5.0",
"command-line-args": "^4.0.4",
"command-line-usage": "^4.0.0",
"follow-redirects": "^1.2.3",
"fs-extra": "^2.1.2",
"http-proxy-agent": "^1.0.0",
"https-proxy-agent": "^1.0.0",
"iconv-lite": "^0.4.17",
"jsdoc": "^3.4.3",
"mocha": "^3.2.0",
"nexe": "^1.1.2",
"nwjs": "^1.4.3",
"split2": "^2.1.1"
}
}
#!/usr/bin/env node
const fs = require('fs');
const vm = require('vm');
const path = require('path');
const child_process = require('child_process');
const bootstrap = JSON.parse(fs.readFileSync(path.join(__dirname, 'bootstrap.json')));
const files = fs.readdirSync(__dirname);
const pre = files.filter(x => x.match(/^pre-.*\.js$/)).sort();
const post = files.filter(x => x.match(/^post-.*\.js$/)).sort();
const exec = (command) => {
console.log(command);
child_process.execSync(command, { stdio: 'inherit' });
};
const exec_get = (command) => {
// FIXME: Should handle Windows codepage by iconv-lite & chcp result
return child_process.execSync(command, { encoding: 'ascii' });
}
const find_path = (target) => {
for(let dir of process.env['PATH'].split(/;/)) {
if(fs.existsSync(path.join(dir, target))) {
return dir;
}
}
return null;
}
const LENGTH = 1024 * 1024;
const copy = (src, dest) => {
const rfd = fs.openSync(src, 'r');
const rst = fs.fstatSync(rfd);
const wfd = fs.openSync(dest, 'w', rst.mode);
const buf = new Buffer(LENGTH)
let read_bytes;
do {
read_bytes = fs.readSync(rfd, buf, 0, LENGTH)
fs.writeSync(wfd, buf, 0, read_bytes);
} while(read_bytes > 0);
fs.futimesSync(wfd, rst.atime, rst.mtime);
fs.closeSync(rfd);
fs.closeSync(wfd);
};
const sandbox_base = {
bootstrap, console, process, fs, path, exec, exec_get, find_path, copy
};
pre.forEach(filename => {
const script = fs.readFileSync(path.join(__dirname, filename));
const sandbox = Object.assign({}, sandbox_base);
vm.runInNewContext(script, sandbox, { filename, displayErrors: true });
});
for(let key in bootstrap.dependencies) {
let version = bootstrap.dependencies[key];
let arg = '';
if(typeof version === 'object') {
arg = version.arg;
version = version.version;
}
exec(`npm -g install ${arg} ${key}@${version}`);
}
post.forEach(filename => {
const script = fs.readFileSync(path.join(__dirname, filename));
const sandbox = Object.assign({}, sandbox_base);
vm.runInNewContext(script, sandbox, { filename, displayErrors: true });
});
{
"name": "node-bootstrap",
"version": "0.1.0",
"description": "Bootstrap Node environment on Windows",
"bin": { "node-bootstrap": "./node-bootstrap.js" },
"private": true
}
const gtk_prefix = path.dirname(find_path('libgtk-win32-2.0-0.dll'));
const target = path.join(process.env.npm_config_prefix, 'node_modules/canvas/build/Release');
if(!fs.existsSync(path.join(target, 'libfreetype-6.dll'))) {
console.log('copy freetype6.dll');
copy(path.join(gtk_prefix, 'bin/freetype6.dll'), path.join(target, 'freetype6.dll'));
}
exec(`nw install ${bootstrap.versions.nwjs}`);
exec(`nw install ${bootstrap.versions.nwjs}-sdk`);
exec(`nw use ${bootstrap.versions.nwjs}-sdk`);
exec(`nodist global ${bootstrap.versions.node}`);
const candidates = Object.keys(process.env).filter(name => name.match(/^VS\d+COMNTOOLS$/)).sort();
if(candidates.length === 0) {
console.error('No VS installed?');
process.exit(1);
}
const key = candidates[candidates.length-1].match(/^VS(\d+)COMNTOOLS$/)[1];
const versions = {
'100': '2010',
'110': '2012',
'120': '2013',
'140': '2015',
'150': '2017'
};
if(!(key in versions)) {
console.error(`Unknown version identifier ${key}`);
process.exit(1);
}
exec(`npm config set msvs_version ${versions[key]}`);
const gtk_prefix = path.dirname(find_path('libgtk-win32-2.0-0.dll'));
exec(`npm config set GTK_Root ${path.normalize(gtk_prefix)}`);
const libjpeg_turbo_prefix = path.dirname(find_path('jpeg62.dll'));
const gtk_prefix = path.dirname(find_path('libgtk-win32-2.0-0.dll'));
const targets = [
'include/jconfig.h',
'include/jerror.h',
'include/jmorecfg.h',
'include/jpeglib.h',
'lib/jpeg.lib'
];
for(let file of targets) {
console.log(`copying ${file}`);
copy(path.join(libjpeg_turbo_prefix, file), path.join(gtk_prefix, file));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment