This repository has been archived by the owner on Jul 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(add): add tink add, to add new dependencies
- Loading branch information
Showing
2 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
}) | ||
}) | ||
} |