Permalink
Fetching contributors…
Cannot retrieve contributors at this time
executable file 109 lines (88 sloc) 2.75 KB
#!/usr/bin/env node
/* eslint-disable no-console */
'use strict';
const fs = require('fs');
const gypkg = require('../');
const colors = require('colors/safe');
const genOptions = (argv) => {
return argv
.alias('f', 'freeze')
.boolean('f')
.describe('f', 'generate .gypkg-freeze file')
.alias('v', 'verbose')
.boolean('v')
.describe('v', 'print verbose output')
.boolean('insecure')
.describe('insecure', 'do not verify GPG signatures');
};
const yargs = require('yargs')
.usage('$0 build project.gyp -- [optional extra args to `gyp`]')
.command('build [gyp-file]',
'build all projects in a .gyp file', (argv) => {
return genOptions(argv)
.alias('c', 'config')
.default('c', 'Release')
.describe('c', 'configuration to build');
})
.command('gen [gyp-file]',
'generate build configuration out of .gyp file', genOptions)
.command('init [gyp-file]',
'initialize boilerplate .gyp file')
.command('deps', 'dependency helper to be used in `dependencies`')
.command('type', 'dependency helper to be used in `type`')
.alias('h', 'help')
.help();
const argv = yargs.argv;
const cmd = argv._[0];
function findBuildFile(cwd) {
const files = fs.readdirSync(cwd)
.filter(file => /\.gyp$/.test(file))
// Do not attempt to build npm files
.filter(file => file !== 'binding.gyp');
if (files.length === 0)
throw new Error('No .gyp file found in current directory');
return files[0];
}
if (cmd === 'gen' || cmd === 'build') {
const server = new gypkg.Server({
verbose: argv.verbose,
freeze: argv.freeze,
insecure: argv.insecure,
config: cmd === 'build' && argv.config,
resultType: argv.type
});
const gypFile = argv['gyp-file'] || findBuildFile(process.cwd());
return server.generate(gypFile, argv._.slice(1), (err) => {
if (err)
throw err;
}).on('log', (line, progress) => {
console.error(colors.grey(`[${(progress * 100).toFixed(0)}%]`) +
` ${line}`);
});
}
function escape(str) {
// Escape `\` on Windows, because shlex.split will eat them
if (process.platform === 'win32')
return str.replace(/\\/g, '\\\\');
else
return str;
}
if (cmd === 'deps' || cmd === 'type') {
const client = new gypkg.Client({ verbose: argv.verbose });
return client.cmd(cmd, argv._.slice(1), (err, out) => {
if (err)
throw err;
if (Array.isArray(out))
out = out.map(escape).join('\n');
else
out = escape(out);
console.log(out);
});
}
if (cmd === 'init') {
gypkg.commands.init(argv['gyp-file']);
return;
}
yargs.showHelp();
console.error(`Unknown command: ${cmd}`);
process.exit(1);