From a2d36e66f345f53b03997e212c0a5d4f5e4be77d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kat=20March=C3=A1n?= Date: Sun, 11 Nov 2018 10:32:38 -0800 Subject: [PATCH] feat(add): add tink add, to add new dependencies --- bin/tink.js | 1 + lib/commands/add.js | 71 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 lib/commands/add.js diff --git a/bin/tink.js b/bin/tink.js index 3d4fa0d..0391d04 100755 --- a/bin/tink.js +++ b/bin/tink.js @@ -3,6 +3,7 @@ require('../lib/node/index.js') const CMDS = new Map([ + ['add', require('../lib/commands/add.js')], ['shell', require('../lib/commands/shell.js')], ['org', require('../lib/commands/org.jsx')], ['prepare', require('../lib/commands/prepare.js')], diff --git a/lib/commands/add.js b/lib/commands/add.js new file mode 100644 index 0000000..abe692f --- /dev/null +++ b/lib/commands/add.js @@ -0,0 +1,71 @@ +'use strict' + +'use strict' + +const Add = module.exports = { + command: 'add ', + describe: 'Add a dependency.', + builder (y) { + return y.help().alias('help', 'h') + .options(Add.options) + }, + options: Object.assign(require('../common-opts.js', { + 'development': { + alias: ['dev', 'D'], + describe: 'Add this dependency as a devDependency', + type: 'boolean' + }, + 'production': { + alias: ['prod', 'P'], + describe: 'Add this dependency as a regular dependency', + type: 'boolean', + default: true + }, + 'bundle': { + alias: ['bundled', 'B'], + describe: 'Add this dependency as a bundledDependency', + type: 'boolean' + } + })), + handler: async argv => add(argv) +} + +async function add (argv) { + const BB = require('bluebird') + + const figgyPudding = require('figgy-pudding') + const log = require('npmlog') + const npmConfig = require('../config.js') + const spawn = require('child_process').spawn + + const AddConfig = figgyPudding({ + log: { default: () => log }, + silent: {} + }, { other () { return true } }) + + const opts = AddConfig(npmConfig().concat(argv).concat({ log })) + + await new BB((resolve, reject) => { + const npmBin = process.platform === 'win32' ? 'npm.cmd' : 'npm' + const child = spawn(npmBin, [ + 'add', opts.pkg, '--package-lock-only' + // We add argv here to get npm to parse those options for us :D + ].concat(process.argv.slice(3).filter(x => { + return !['--bundle', '--development', '--production'].find(y => y === x) + }) || []), { + env: process.env, + cwd: process.cwd(), + stdio: 'inherit' + }) + child.on('error', reject) + child.on('close', code => { + if (code === 127) { + reject(new Error('`npm` command not found. Please ensure you have npm@5.4.0 or later installed.')) + } else if (code) { + reject(new Error('non-zero exit code: ' + code)) + } else { + resolve() + } + }) + }) +}