#!/usr/bin/env node var chalk = require('chalk'); var interpret = require('interpret'); var v8flags = require('v8flags'); var Liftoff = require('liftoff'); var argv = require('minimist')(process.argv.slice(2)); var Shipit = require('../lib/shipit'); var pkg = require('../package.json'); var Promise = require('bluebird'); // Initialize cli. var cli = new Liftoff({ name: 'shipit', extensions: interpret.jsVariants, v8flags: v8flags }); // Launch cli. cli.launch({ cwd: argv.cwd, configPath: argv.shipitfile, require: argv.require, completion: argv.completion }, invoke); /** * Invoke CLI. * * @param {object} env CLI environment */ function invoke(env) { if (argv.version) { console.info('v%s', pkg.version); exit(0); } if (!env.configPath) { console.error(chalk.red('shipitfile not found')); exit(1); } if (argv._.length === 0) { console.error(chalk.red('environment not found')); exit(1); } // Run the 'default' task if no task is specified var tasks = argv._.slice(1); if (tasks.length === 0) { tasks.push('default'); } try { initShipit(argv._[0], env.configPath, tasks); } catch(e) { console.error(chalk.red(e.message)); exit(1); } } /** * Initialize shipit. * * @param {string} env Shipit environement * @param {string} shipfile Shipitfile path * @param {string[]} tasks Tasks */ function initShipit(env, shipfile, tasks) { // Create. var shipit = new Shipit({environment: env}); // Load shipfile. var pendingConfig = require(shipfile)(shipit); var done = function() { // Initialize shipit. shipit.initialize(); // Run tasks. shipit.start(tasks); shipit.on('task_err', function() { exit(1); }); shipit.on('task_not_found', function() { exit(1); }); } Promise.resolve(pendingConfig) .then(done) .catch(function(err) { console.error('Could not load async config.', err); } ) } /** * Properly exit. * Even on Windows. * * @param {number} code Exit code */ function exit(code) { if (process.platform === 'win32' && process.stdout.bufferSize) { process.stdout.once('drain', function() { process.exit(code); }); return; } process.exit(code); }