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

Commit d167ca5

Browse files
committed
fix: only call runScript when relevant script actually exists
1 parent d86370b commit d167ca5

File tree

4 files changed

+42
-5
lines changed

4 files changed

+42
-5
lines changed

lib/index.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ internals.runScript = (stage, { pkg, path, cwd, unsafePerm }, options) => {
5353
console.log();
5454

5555
if (options.dryRun) {
56-
console.log(`DRY RUN ==> ${stage} ${path || pkg.name}...`);
56+
console.log(`DRY RUN ==> ${stage} ${path || pkg.name}`);
5757
return;
5858
}
5959

@@ -146,15 +146,21 @@ exports.run = async (options) => {
146146
await internals.runScript('preinstall', { pkg, path: '', cwd, unsafePerm: true }, options);
147147

148148
for (const { path, childPkg } of allowedPackages) {
149-
await internals.runScript('preinstall', { pkg: childPkg, path, cwd }, options);
149+
if (childPkg.scripts.preinstall) {
150+
await internals.runScript('preinstall', { pkg: childPkg, path, cwd }, options);
151+
}
150152
}
151153

152154
for (const { path, childPkg } of allowedPackages) {
153-
await internals.runScript('install', { pkg: childPkg, path, cwd }, options);
155+
if (childPkg.scripts.install) {
156+
await internals.runScript('install', { pkg: childPkg, path, cwd }, options);
157+
}
154158
}
155159

156160
for (const { path, childPkg } of allowedPackages) {
157-
await internals.runScript('postinstall', { pkg: childPkg, path, cwd }, options);
161+
if (childPkg.scripts.postinstall) {
162+
await internals.runScript('postinstall', { pkg: childPkg, path, cwd }, options);
163+
}
158164
}
159165

160166
await internals.runScript('install', { pkg, path: '', cwd, unsafePerm: true }, options);

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"url": "https://github.com/dominykas/allow-scripts.git"
88
},
99
"scripts": {
10-
"test": "lab -L -t 93 -m 5000"
10+
"test": "lab -L -t 96 -m 5000"
1111
},
1212
"bin": {
1313
"allow-scripts": "./bin/allow-scripts.js"

test/fixtures/basic.dry-run.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
DRY RUN ==> preinstall @example/basic
2+
3+
DRY RUN ==> preinstall node_modules/@example/with-preinstall-script
4+
5+
DRY RUN ==> install node_modules/@example/with-install-script
6+
7+
DRY RUN ==> postinstall node_modules/@example/with-postinstall-script
8+
9+
DRY RUN ==> install @example/basic
10+
11+
DRY RUN ==> postinstall @example/basic
12+
13+
DRY RUN ==> prepublish @example/basic
14+
15+
DRY RUN ==> prepare @example/basic

test/index.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,22 @@ describe('allow-scripts', () => {
4545
expect(fixture.getLog()).not.to.contain('without-install-scripts');
4646
});
4747

48+
it('dry run: reports allowed scripts', async () => {
49+
50+
const fixture = Fixtures.setup('basic', [
51+
'with-preinstall-script',
52+
'with-install-script',
53+
'with-postinstall-script',
54+
'without-scripts',
55+
'without-install-scripts'
56+
]);
57+
58+
await Allow.run({ dryRun: true });
59+
60+
expect(fixture.getActualResult()).to.equal('');
61+
expect(fixture.getLog()).to.equal(Fs.readFileSync(Path.join(__dirname, 'fixtures', 'basic.dry-run.txt')).toString().trim());
62+
});
63+
4864
it('crashes on script not in allowed list', async () => {
4965

5066
const fixture = Fixtures.setup('not-in-allowed', [

0 commit comments

Comments
 (0)