Skip to content
This repository was archived by the owner on Aug 11, 2022. It is now read-only.

Commit 1549106

Browse files
committed
shrinkwrap: npm shrinkwrap --dev means prod too
Due to 448efd0, running `npm shrinkwrap --dev` caused production dependencies to no longer be included in npm-shrinkwrap-.json. Whoopsie! Added a regression test to go with the fix. Fixes #7641.
1 parent 61915a8 commit 1549106

File tree

5 files changed

+144
-50
lines changed

5 files changed

+144
-50
lines changed

lib/shrinkwrap.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ function shrinkwrap (args, silent, cb) {
2020
log.warn("shrinkwrap", "doesn't take positional args")
2121
}
2222

23+
// https://github.com/npm/npm/issues/7641
24+
// introduced because `npm ls` can now show dev and prod depenednecy
25+
// trees separately
26+
if (npm.config.get("dev")) {
27+
npm.config.set("production", true)
28+
}
2329
npm.commands.ls([], true, function (er, _, pkginfo) {
2430
if (er) return cb(er)
2531
shrinkwrap_(pkginfo, silent, npm.config.get("dev"), cb)
@@ -45,7 +51,7 @@ function shrinkwrap_ (pkginfo, silent, dev, cb) {
4551
return
4652
}
4753

48-
log.warn("shrinkwrap", "Excluding devDependency: %s", dep)
54+
log.warn("shrinkwrap", "Excluding devDependency: %s", dep, data.dependencies)
4955
delete pkginfo.dependencies[dep]
5056
})
5157
}

test/tap/shrinkwrap-dev-dependency.js

+48-25
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,78 @@
1-
var npm = npm = require("../../")
2-
var test = require("tap").test
3-
var path = require("path")
4-
var fs = require("fs")
5-
var osenv = require("osenv")
6-
var rimraf = require("rimraf")
7-
var mr = require("npm-registry-mock")
8-
var common = require("../common-tap.js")
9-
10-
var pkg = path.resolve(__dirname, "shrinkwrap-dev-dependency")
11-
var desiredResultsPath = path.resolve(pkg, "desired-shrinkwrap-results.json")
1+
var fs = require('fs')
2+
var path = require('path')
3+
4+
var mkdirp = require('mkdirp')
5+
var mr = require('npm-registry-mock')
6+
var osenv = require('osenv')
7+
var rimraf = require('rimraf')
8+
var test = require('tap').test
9+
10+
var npm = npm = require('../../')
11+
12+
var common = require('../common-tap.js')
13+
var pkg = path.resolve(__dirname, 'shrinkwrap-dev-dependency')
1214

1315
test("shrinkwrap doesn't strip out the dependency", function (t) {
1416
t.plan(1)
1517

16-
mr({port : common.port}, function (er, s) {
18+
mr({port: common.port}, function (er, s) {
1719
setup({}, function (err) {
1820
if (err) return t.fail(err)
1921

20-
npm.install(".", function (err) {
22+
npm.install('.', function (err) {
2123
if (err) return t.fail(err)
2224

2325
npm.commands.shrinkwrap([], true, function (err, results) {
2426
if (err) return t.fail(err)
2527

26-
fs.readFile(desiredResultsPath, function (err, desired) {
27-
if (err) return t.fail(err)
28-
29-
t.deepEqual(results, JSON.parse(desired))
30-
s.close()
31-
t.end()
32-
})
28+
t.deepEqual(results, desired)
29+
s.close()
30+
t.end()
3331
})
3432
})
3533
})
3634
})
3735
})
3836

39-
test("cleanup", function (t) {
37+
test('cleanup', function (t) {
4038
cleanup()
4139
t.end()
4240
})
4341

42+
var desired = {
43+
name: 'npm-test-shrinkwrap-dev-dependency',
44+
version: '0.0.0',
45+
dependencies: {
46+
request: {
47+
version: '0.9.0'
48+
},
49+
underscore: {
50+
version: '1.3.1'
51+
}
52+
}
53+
}
54+
55+
var json = {
56+
author: 'Domenic Denicola',
57+
name: 'npm-test-shrinkwrap-dev-dependency',
58+
version: '0.0.0',
59+
dependencies: {
60+
request: '0.9.0',
61+
underscore: '1.3.1'
62+
},
63+
devDependencies: {
64+
underscore: '1.5.1'
65+
}
66+
}
4467

4568
function setup (opts, cb) {
4669
cleanup()
70+
mkdirp.sync(pkg)
71+
fs.writeFileSync(path.join(pkg, 'package.json'), JSON.stringify(json, null, 2))
4772
process.chdir(pkg)
4873

4974
var allOpts = {
50-
cache: path.resolve(pkg, "cache"),
75+
cache: path.resolve(pkg, 'cache'),
5176
registry: common.registry
5277
}
5378

@@ -60,7 +85,5 @@ function setup (opts, cb) {
6085

6186
function cleanup () {
6287
process.chdir(osenv.tmpdir())
63-
rimraf.sync(path.resolve(pkg, "node_modules"))
64-
rimraf.sync(path.resolve(pkg, "cache"))
65-
rimraf.sync(path.resolve(pkg, "npm-shrinkwrap.json"))
88+
rimraf.sync(pkg)
6689
}

test/tap/shrinkwrap-dev-dependency/desired-shrinkwrap-results.json

-12
This file was deleted.

test/tap/shrinkwrap-dev-dependency/package.json

-12
This file was deleted.
+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
var fs = require('fs')
2+
var path = require('path')
3+
4+
var mkdirp = require('mkdirp')
5+
var mr = require('npm-registry-mock')
6+
var osenv = require('osenv')
7+
var rimraf = require('rimraf')
8+
var test = require('tap').test
9+
10+
var npm = npm = require('../../')
11+
12+
var common = require('../common-tap.js')
13+
var pkg = path.resolve(__dirname, 'shrinkwrap-prod-dependency')
14+
15+
test("shrinkwrap --dev doesn't strip out prod dependencies", function (t) {
16+
t.plan(1)
17+
18+
mr({port: common.port}, function (er, s) {
19+
setup({}, function (err) {
20+
if (err) return t.fail(err)
21+
22+
npm.install('.', function (err) {
23+
if (err) return t.fail(err)
24+
25+
npm.config.set('dev', true)
26+
npm.commands.shrinkwrap([], true, function (err, results) {
27+
if (err) return t.fail(err)
28+
29+
t.deepEqual(results, desired)
30+
s.close()
31+
t.end()
32+
})
33+
})
34+
})
35+
})
36+
})
37+
38+
test('cleanup', function (t) {
39+
cleanup()
40+
t.end()
41+
})
42+
43+
var desired = {
44+
name: 'npm-test-shrinkwrap-prod-dependency',
45+
version: '0.0.0',
46+
dependencies: {
47+
request: {
48+
version: '0.9.0'
49+
},
50+
underscore: {
51+
version: '1.5.1'
52+
}
53+
}
54+
}
55+
56+
var json = {
57+
author: 'Domenic Denicola',
58+
name: 'npm-test-shrinkwrap-prod-dependency',
59+
version: '0.0.0',
60+
dependencies: {
61+
request: '0.9.0'
62+
},
63+
devDependencies: {
64+
underscore: '1.5.1'
65+
}
66+
}
67+
68+
function setup (opts, cb) {
69+
cleanup()
70+
mkdirp.sync(pkg)
71+
fs.writeFileSync(path.join(pkg, 'package.json'), JSON.stringify(json, null, 2))
72+
process.chdir(pkg)
73+
74+
var allOpts = {
75+
cache: path.resolve(pkg, 'cache'),
76+
registry: common.registry
77+
}
78+
79+
for (var key in opts) {
80+
allOpts[key] = opts[key]
81+
}
82+
83+
npm.load(allOpts, cb)
84+
}
85+
86+
function cleanup () {
87+
process.chdir(osenv.tmpdir())
88+
rimraf.sync(pkg)
89+
}

0 commit comments

Comments
 (0)