zapier-platform-cli
Version:
The CLI for apps in the Zapier Developer Platform.
110 lines (90 loc) • 4.05 kB
JavaScript
;
var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }();
/*eslint no-process-exit: 0 */
require('babel-polyfill');
var _ = require('lodash');
var colors = require('colors/safe');
var _require = require('./constants'),
DEBUG = _require.DEBUG,
PLATFORM_PACKAGE = _require.PLATFORM_PACKAGE;
var commands = require('./commands');
var utils = require('./utils');
module.exports = function (argv) {
process.on('exit', utils.clearSpinner);
process.on('SIGINT', function () {
utils.clearSpinner();
process.exit();
});
if (!utils.isValidNodeVersion()) {
console.error(colors.red('Requires node version >= ' + utils.readNvmVersion() + ', found ' + process.versions.node + '. Please upgrade node.'));
process.exit(1);
}
require('update-notifier')({ pkg: require('../package.json') }).notify();
if (DEBUG) {
console.log('running in:', process.cwd());
console.log('raw argv:', argv);
console.log('\n--------------------------------------------------\n\n');
}
argv = argv.slice(2); // strip path, zapier.js
var _utils$argParse = utils.argParse(argv),
_utils$argParse2 = _slicedToArray(_utils$argParse, 2),
args = _utils$argParse2[0],
argOpts = _utils$argParse2[1];
global.argOpts = argOpts;
// when `zapier invitees --help`, swap to `zapier help invitees`
if (argOpts.help || args.length === 0) {
args = ['help'].concat(args);
}
var command = args[0];
args = args.slice(1);
// create the context, logs thread through this
var context = utils.createContext({ command: command, args: args, argOpts: argOpts });
if (command === 'help' && (argOpts.version || argOpts.v)) {
context.line(['zapier-platform-cli/' + require('../package.json').version, 'node/' + process.version].join('\n'));
return;
}
var commandFunc = commands[command];
if (!commandFunc) {
context.line('`zapier ' + command + '` is not a command! Try running `zapier help`?');
return;
}
if (!utils.isValidAppInstall(command)) {
console.error(colors.red('Looks like your package.json is missing `' + PLATFORM_PACKAGE + '`, its not restricted to a single version, or you haven\'t run npm install yet!'));
process.exit(1);
}
var spec = {
argsSpec: commandFunc.argsSpec,
argOptsSpec: _.extend({}, utils.globalArgOptsSpec, commandFunc.argOptsSpec)
};
var errors = utils.enforceArgSpec(spec, args, argOpts);
if (errors.length) {
utils.clearSpinner();
context.line();
context.line(colors.red('Errors running command `' + ['zapier'].concat(argv).join(' ') + '`:'));
context.line();
errors.forEach(function (error) {
return context.line(colors.red('!!! ' + error));
});
context.line();
context.line('For more information, run `zapier help ' + command + '`.');
context.line();
process.exit(1);
}
commandFunc.apply(commands, [context].concat(args)).then(function () {
utils.clearSpinner();
}).catch(function (err) {
utils.clearSpinner();
if (DEBUG || global.argOpts.debug) {
context.line();
context.line(err.stack);
context.line();
context.line(colors.red('Error!'));
} else {
context.line();
context.line();
context.line(colors.red('Error!') + ' ' + colors.red(err.message));
context.line(colors.grey('(Use --debug flag and run this command again to get more details.)'));
}
process.exit(1);
});
};