From a7eca3246fbbcbb05434cb6677f65d14c945d74f Mon Sep 17 00:00:00 2001 From: Bryan English Date: Mon, 2 Nov 2015 18:49:36 -0800 Subject: [PATCH] shrinkwrap: Record if dep is dev-only and honor the annotation Credit: @bengl Reviewed-By: @iarna PR-URL: https://github.com/npm/npm/pull/10073 --- lib/install/inflate-shrinkwrap.js | 3 + lib/shrinkwrap.js | 4 +- test/tap/install-cli-only-shrinkwrap.js | 139 ++++++++++++++++++++ test/tap/shrinkwrap-prod-dependency-also.js | 1 + test/tap/shrinkwrap-prod-dependency.js | 1 + 5 files changed, 147 insertions(+), 1 deletion(-) create mode 100644 test/tap/install-cli-only-shrinkwrap.js diff --git a/lib/install/inflate-shrinkwrap.js b/lib/install/inflate-shrinkwrap.js index 11c19979d8566..ab1bdd1f1912f 100644 --- a/lib/install/inflate-shrinkwrap.js +++ b/lib/install/inflate-shrinkwrap.js @@ -24,6 +24,8 @@ function inflateShrinkwrap (topPath, tree, swdeps, finishInflating) { var onDisk = {} tree.children.forEach(function (child) { onDisk[moduleName(child)] = child }) tree.children = [] + var dev = npm.config.get('dev') || (!/^prod(uction)?$/.test(npm.config.get('only')) && !npm.config.get('production')) || /^dev(elopment)?$/.test(npm.config.get('only')) + var prod = !/^dev(elopment)?$/.test(npm.config.get('only')) return asyncMap(Object.keys(swdeps), doRealizeAndInflate, finishInflating) function doRealizeAndInflate (name, next) { @@ -34,6 +36,7 @@ function inflateShrinkwrap (topPath, tree, swdeps, finishInflating) { return function (requested) { var sw = swdeps[name] var dependencies = sw.dependencies || {} + if ((!prod && !sw.dev) || (!dev && sw.dev)) return next() var child = onDisk[name] if (childIsEquivalent(sw, requested, child)) { if (!child.fromShrinkwrap) child.fromShrinkwrap = requested.raw diff --git a/lib/shrinkwrap.js b/lib/shrinkwrap.js index ecf020ad11ac7..5f4f560215980 100644 --- a/lib/shrinkwrap.js +++ b/lib/shrinkwrap.js @@ -99,7 +99,8 @@ function shrinkwrapDeps (dev, problems, deps, tree, seen) { } }) tree.children.sort(function (aa, bb) { return moduleName(aa).localeCompare(moduleName(bb)) }).forEach(function (child) { - if (!dev && isOnlyDev(child)) { + var childIsOnlyDev = isOnlyDev(child) + if (!dev && childIsOnlyDev) { log.warn('shrinkwrap', 'Excluding devDependency: %s', child.location) return } @@ -107,6 +108,7 @@ function shrinkwrapDeps (dev, problems, deps, tree, seen) { pkginfo.version = child.package.version pkginfo.from = child.package._from pkginfo.resolved = child.package._resolved + if (dev && childIsOnlyDev) pkginfo.dev = true if (isExtraneous(child)) { problems.push('extraneous: ' + child.package._id + ' ' + child.path) } diff --git a/test/tap/install-cli-only-shrinkwrap.js b/test/tap/install-cli-only-shrinkwrap.js new file mode 100644 index 0000000000000..17ff1ec9559ec --- /dev/null +++ b/test/tap/install-cli-only-shrinkwrap.js @@ -0,0 +1,139 @@ +var fs = require('graceful-fs') +var path = require('path') +var existsSync = fs.existsSync || path.existsSync + +var mkdirp = require('mkdirp') +var osenv = require('osenv') +var rimraf = require('rimraf') +var test = require('tap').test + +var common = require('../common-tap.js') + +var pkg = path.join(__dirname, path.basename(__filename, '.js')) + +var EXEC_OPTS = { cwd: pkg } + +var json = { + name: 'install-cli-only-shrinkwrap', + description: 'fixture', + version: '0.0.0', + dependencies: { + dependency: 'file:./dependency' + }, + devDependencies: { + 'dev-dependency': 'file:./dev-dependency' + } +} + +var shrinkwrap = { + name: 'install-cli-only-shrinkwrap', + description: 'fixture', + version: '0.0.0', + dependencies: { + dependency: { + version: '0.0.0', + from: 'file:./dependency' + }, + 'dev-dependency': { + version: '0.0.0', + from: 'file:./dev-dependency', + dev: true + } + } +} + +var dependency = { + name: 'dependency', + description: 'fixture', + version: '0.0.0' +} + +var devDependency = { + name: 'dev-dependency', + description: 'fixture', + version: '0.0.0' +} + +test('setup', function (t) { + setup() + t.pass('setup ran') + t.end() +}) + +test('\'npm install --only=development\' should only install devDependencies', function (t) { + common.npm(['install', '--only=development'], EXEC_OPTS, function (err, code, stderr, stdout) { + if (err) throw err + t.comment(stdout.trim()) + t.comment(stderr.trim()) + t.is(code, 0, 'npm install did not raise error code') + t.ok( + existsSync( + path.resolve(pkg, 'node_modules/dev-dependency/package.json') + ), + 'devDependency was installed' + ) + t.notOk( + existsSync(path.resolve(pkg, 'node_modules/dependency/package.json')), + 'dependency was NOT installed' + ) + t.end() + }) +}) + +test('\'npm install --only=production\' should only install dependencies', function (t) { + cleanup() + setup() + common.npm(['install', '--only=production'], EXEC_OPTS, function (err, code, stdout, stderr) { + if (err) throw err + t.comment(stdout.trim()) + t.comment(stderr.trim()) + t.is(code, 0, 'npm install did not raise error code') + t.ok( + existsSync( + path.resolve(pkg, 'node_modules/dependency/package.json') + ), + 'dependency was installed' + ) + t.notOk( + existsSync(path.resolve(pkg, 'node_modules/dev-dependency/package.json')), + 'devDependency was NOT installed' + ) + t.end() + }) +}) + +test('cleanup', function (t) { + cleanup() + t.pass('cleaned up') + t.end() +}) + +function setup () { + mkdirp.sync(path.join(pkg, 'dependency')) + fs.writeFileSync( + path.join(pkg, 'dependency', 'package.json'), + JSON.stringify(dependency, null, 2) + ) + + mkdirp.sync(path.join(pkg, 'dev-dependency')) + fs.writeFileSync( + path.join(pkg, 'dev-dependency', 'package.json'), + JSON.stringify(devDependency, null, 2) + ) + + mkdirp.sync(path.join(pkg, 'node_modules')) + fs.writeFileSync( + path.join(pkg, 'package.json'), + JSON.stringify(json, null, 2) + ) + fs.writeFileSync( + path.join(pkg, 'npm-shrinkwrap.json'), + JSON.stringify(shrinkwrap, null, 2) + ) + process.chdir(pkg) +} + +function cleanup () { + process.chdir(osenv.tmpdir()) + rimraf.sync(pkg) +} diff --git a/test/tap/shrinkwrap-prod-dependency-also.js b/test/tap/shrinkwrap-prod-dependency-also.js index 723960ac32169..d3e86fcd659c1 100644 --- a/test/tap/shrinkwrap-prod-dependency-also.js +++ b/test/tap/shrinkwrap-prod-dependency-also.js @@ -70,6 +70,7 @@ var desired = { resolved: common.registry + '/request/-/request-0.9.0.tgz' }, underscore: { + dev: true, version: '1.5.1', from: 'underscore@1.5.1', resolved: common.registry + '/underscore/-/underscore-1.5.1.tgz' diff --git a/test/tap/shrinkwrap-prod-dependency.js b/test/tap/shrinkwrap-prod-dependency.js index 5bc834376eb1a..57d6ecd3b19d9 100644 --- a/test/tap/shrinkwrap-prod-dependency.js +++ b/test/tap/shrinkwrap-prod-dependency.js @@ -50,6 +50,7 @@ var desired = { resolved: common.registry + '/request/-/request-0.9.0.tgz' }, underscore: { + dev: true, version: '1.5.1', from: 'underscore@1.5.1', resolved: common.registry + '/underscore/-/underscore-1.5.1.tgz'