Preliminary support for the --save and --save-dev flags

This commit is contained in:
DaveJ
2016-01-30 18:36:55 +00:00
parent 5f18e2e210
commit 9803579af1
3 changed files with 41 additions and 31 deletions

View File

@@ -12,6 +12,7 @@ var assign = require('object-assign')
var logger = require('../lib/logger')
var installMultiple = require('../lib/install_multiple')
var config = require('../lib/config')
var save = require('../lib/save')
const cli = require('meow')([
'Usage:',
@@ -59,25 +60,28 @@ if (cli.flags.debug) {
function run (cli) {
var ctx = {}
var pkg
var packagesToInstall
var installType
return readPkgUp()
.then(pkg_ => { pkg = pkg_ })
.then(_ => updateContext(pkg.path))
.then(_ => install())
.then(_ => savePkg())
function install () {
var packages
installType = cli.input && cli.input.length ? 'named' : 'general'
if (cli.input && cli.input.length) {
packages = cli.input
if (installType === 'named') {
packagesToInstall = cli.input
} else {
packages = assign({},
packagesToInstall = assign({},
pkg.pkg.dependencies || {},
pkg.pkg.devDependencies || {})
}
return installMultiple(ctx,
packages,
packagesToInstall,
join(ctx.root, 'node_modules'),
cli.flags)
}
@@ -89,6 +93,13 @@ function run (cli) {
if (!cli.flags.quiet) ctx.log = logger()
else ctx.log = function () { return function () {} }
}
function savePkg () {
var saveType = cli.flags.save ? 'dependencies' : cli.flags.saveDev ? 'devDependencies' : null
if (saveType && installType === 'named') {
return save(pkg, packagesToInstall, saveType)
}
}
}
module.exports = run

18
lib/save.js Normal file
View File

@@ -0,0 +1,18 @@
var thenify = require('thenify')
var fs = require('fs')
var writeFile = thenify(fs.writeFile)
module.exports = function save (pkg, installedPackages, saveType) {
var packageJson = pkg.pkg
packageJson[saveType] = packageJson[saveType] || {}
installedPackages.forEach(function (dependency) {
// Assumes package name of in the format of "package-name@x.x.x"
// TODO: Support other install sources (Github, scoped pkgs etc.)
var splitDep = dependency.split('@')
var depName = splitDep[0]
var depVersion = splitDep[1]
packageJson[saveType][depName] = '^' + depVersion
})
return writeFile(pkg.path, JSON.stringify(packageJson, null, 2), 'utf8')
}

View File

@@ -130,36 +130,17 @@ test('compiled modules (ursa@0.9.1)', function (t) {
}, t.end)
})
test('tarballs (is-array-1.0.1.tgz)', function (t) {
test('save to package.json (rimraf)', function (t) {
prepare()
install({ input: ['http://registry.npmjs.org/is-array/-/is-array-1.0.1.tgz'], flags: { quiet: true } })
install({ input: ['rimraf@2.5.1'], flags: { quiet: true, saveDev: true } })
.then(function () {
var isArray = require(
join(process.cwd(), 'node_modules', 'is-array'))
var rimraf = require(join(process.cwd(), 'node_modules', 'rimraf'))
t.ok(typeof rimraf === 'function', 'rimraf() is available')
t.ok(isArray, 'isArray() is available')
var pkgJson = fs.readFileSync(join(process.cwd(), 'package.json'), 'utf8')
var devDependencies = JSON.parse(pkgJson).devDependencies
t.deepEqual(devDependencies, {rimraf: '^2.5.1'}, 'rimraf has been added to devDependencies')
stat = fs.statSync(
join(process.cwd(), 'node_modules', '.store',
'is-array-1.0.1@a83102a9c117983e6ff4d85311fb322231abe3d6'))
t.ok(stat.isDirectory(), 'stored in the proper location')
t.end()
}, t.end)
})
test('shrinkwrap compatibility', function (t) {
prepare()
fs.writeFileSync('package.json',
JSON.stringify({ dependencies: { rimraf: '*' } }),
'utf-8')
install({ input: ['rimraf@2.5.1'], flags: { quiet: true } })
.then(function () {
var npm = JSON.stringify(require.resolve('npm/bin/npm-cli.js'))
require('child_process').execSync('node ' + npm + ' shrinkwrap')
var wrap = JSON.parse(fs.readFileSync('npm-shrinkwrap.json', 'utf-8'))
t.ok(wrap.dependencies.rimraf.version === '2.5.1',
'npm shrinkwrap is successful')
t.end()
}, t.end)
})