Skip to content
This repository has been archived by the owner on Jul 28, 2021. It is now read-only.

Commit

Permalink
feat(add): add tink add, to add new dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
zkat committed Nov 11, 2018
1 parent 07cdab0 commit a2d36e6
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
1 change: 1 addition & 0 deletions bin/tink.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')],
Expand Down
71 changes: 71 additions & 0 deletions lib/commands/add.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
'use strict'

'use strict'

const Add = module.exports = {
command: 'add <pkg>',
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()
}
})
})
}

0 comments on commit a2d36e6

Please sign in to comment.