Skip to content

Commit d825e90

Browse files
committed
ci: run install scripts for root project
`npm ci` should run all the same preinstall/install/postinstall/prepare scripts for the root project just like `npm install`. Fixes: #1905 PR-URL: #2316 Credit: @isaacs Close: #2316 Reviewed-by: @ruyadorno
1 parent 7ff6efb commit d825e90

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

lib/ci.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const util = require('util')
22
const Arborist = require('@npmcli/arborist')
33
const rimraf = util.promisify(require('rimraf'))
44
const reifyFinish = require('./utils/reify-finish.js')
5+
const runScript = require('@npmcli/run-script')
56

67
const log = require('npmlog')
78
const npm = require('./npm.js')
@@ -20,6 +21,7 @@ const ci = async () => {
2021
}
2122

2223
const where = npm.prefix
24+
const { scriptShell } = npm.flatOptions
2325
const arb = new Arborist({ ...npm.flatOptions, path: where })
2426

2527
await Promise.all([
@@ -35,6 +37,27 @@ const ci = async () => {
3537
])
3638
// npm ci should never modify the lockfile or package.json
3739
await arb.reify({ ...npm.flatOptions, save: false })
40+
41+
// run the same set of scripts that `npm install` runs.
42+
const scripts = [
43+
'preinstall',
44+
'install',
45+
'postinstall',
46+
'prepublish', // XXX should we remove this finally??
47+
'preprepare',
48+
'prepare',
49+
'postprepare',
50+
]
51+
for (const event of scripts) {
52+
await runScript({
53+
path: where,
54+
args: [],
55+
scriptShell,
56+
stdio: 'inherit',
57+
stdioString: true,
58+
event,
59+
})
60+
}
3861
await reifyFinish(arb)
3962
}
4063

test/lib/ci.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,16 @@ const { test } = require('tap')
66

77
const requireInject = require('require-inject')
88

9-
test('should use Arborist', (t) => {
9+
test('should use Arborist and run-script', (t) => {
10+
const scripts = [
11+
'preinstall',
12+
'install',
13+
'postinstall',
14+
'prepublish', // XXX should we remove this finally??
15+
'preprepare',
16+
'prepare',
17+
'postprepare',
18+
]
1019
const ci = requireInject('../../lib/ci.js', {
1120
'../../lib/npm.js': {
1221
prefix: 'foo',
@@ -15,6 +24,9 @@ test('should use Arborist', (t) => {
1524
},
1625
},
1726
'../../lib/utils/reify-finish.js': async () => {},
27+
'@npmcli/run-script': opts => {
28+
t.match(opts, { event: scripts.shift() })
29+
},
1830
'@npmcli/arborist': function (args) {
1931
t.ok(args, 'gets options object')
2032
this.loadVirtual = () => {
@@ -40,6 +52,7 @@ test('should use Arborist', (t) => {
4052
ci(null, er => {
4153
if (er)
4254
throw er
55+
t.strictSame(scripts, [], 'called all scripts')
4356
t.end()
4457
})
4558
})
@@ -53,6 +66,7 @@ test('should pass flatOptions to Arborist.reify', (t) => {
5366
},
5467
},
5568
'../../lib/utils/reify-finish.js': async () => {},
69+
'@npmcli/run-script': opts => {},
5670
'@npmcli/arborist': function () {
5771
this.loadVirtual = () => Promise.resolve(true)
5872
this.reify = async (options) => {
@@ -80,6 +94,7 @@ test('should throw if package-lock.json or npm-shrinkwrap missing', (t) => {
8094
global: false,
8195
},
8296
},
97+
'@npmcli/run-script': opts => {},
8398
'../../lib/utils/reify-finish.js': async () => {},
8499
npmlog: {
85100
verbose: () => {
@@ -102,6 +117,7 @@ test('should throw ECIGLOBAL', (t) => {
102117
global: true,
103118
},
104119
},
120+
'@npmcli/run-script': opts => {},
105121
'../../lib/utils/reify-finish.js': async () => {},
106122
})
107123
ci(null, (err, res) => {
@@ -125,6 +141,7 @@ test('should remove existing node_modules before installing', (t) => {
125141
global: false,
126142
},
127143
},
144+
'@npmcli/run-script': opts => {},
128145
'../../lib/utils/reify-finish.js': async () => {},
129146
'@npmcli/arborist': function () {
130147
this.loadVirtual = () => Promise.resolve(true)

0 commit comments

Comments
 (0)