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

Commit 467c6e7

Browse files
committed
feat: report all errors at once, if any
1 parent 1036a6c commit 467c6e7

File tree

2 files changed

+38
-16
lines changed

2 files changed

+38
-16
lines changed

lib/index.js

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict';
22

3+
const Assert = require('assert');
34
const Cp = require('child_process');
45
const Fs = require('fs');
56
const Npm = require('libnpm');
@@ -115,7 +116,7 @@ exports.run = async (options) => {
115116

116117
const allowScripts = pkg.allowScripts || {};
117118

118-
const allowedPackages = queue
119+
const packagesWithScripts = queue
119120
.map((path) => {
120121

121122
const childPkg = require(Path.join(cwd, path, 'package.json'));
@@ -125,36 +126,57 @@ exports.run = async (options) => {
125126
.filter(({ childPkg }) => {
126127

127128
return childPkg.scripts && (childPkg.scripts.install || childPkg.scripts.preinstall || childPkg.scripts.postinstall);
128-
})
129-
.filter(({ path, childPkg }) => {
129+
});
130+
131+
const allowedPackages = [];
132+
const warnings = [];
133+
const errors = [];
130134

131-
const name = childPkg.name;
135+
packagesWithScripts
136+
.forEach((entry) => {
137+
138+
const path = entry.path;
139+
const childPkg = entry.childPkg;
140+
const name = entry.childPkg.name;
132141

133142
if (allowScripts[name] === undefined) {
134-
throw new Error(`No entry for ${name}`);
143+
errors.push(`${name} (no entry)`);
144+
return;
135145
}
136146

137147
if (allowScripts[name] === false) {
138-
console.warn(`==========> skip ${path} (because it is not allowed in package.json)`);
139-
return false;
148+
warnings.push(`==========> skip ${path} (because it is not allowed in package.json)`);
149+
return;
140150
}
141151

142152
if (allowScripts[name] === true) {
143-
return true;
153+
allowedPackages.push(entry);
154+
return;
144155
}
145156

146157
if (!Semver.validRange(allowScripts[name])) {
147-
throw new Error(`Invalid version range in allowScripts[${name}]: ${allowScripts[name]}`);
158+
errors.push(`${name} (invalid semver range)`);
159+
return;
148160
}
149161

150-
if (!Semver.satisfies(childPkg.version, allowScripts[name])) {
151-
console.warn(`==========> skip ${path} (because ${childPkg.version} is outside of allowed range: ${allowScripts[name]})`);
152-
return false;
162+
if (!Semver.satisfies(entry.childPkg.version, allowScripts[name])) {
163+
warnings.push(`==========> skip ${path} (because ${childPkg.version} is outside of allowed range: ${allowScripts[name]})`);
164+
return;
153165
}
154166

155-
return true;
167+
allowedPackages.push(entry);
156168
});
157169

170+
Assert.strictEqual(packagesWithScripts.length, errors.length + warnings.length + allowedPackages.length, 'Package count does not match. Please raise an issue at https://github.com/dominykas/allow-scripts');
171+
172+
if (errors.length) {
173+
throw new Error(`Mis-configured allowedScripts: ${errors.join(', ')}`);
174+
}
175+
176+
if (warnings.length) {
177+
console.warn(warnings.join('\n'));
178+
}
179+
158180
await internals.runScript('preinstall', { pkg, path: '', cwd, unsafePerm: true }, options);
159181

160182
for (const { path, childPkg } of allowedPackages) {

test/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ describe('allow-scripts', () => {
146146
'without-scripts'
147147
]);
148148

149-
await expect(Allow.run({})).to.reject('No entry for @example/with-install-script');
149+
await expect(Allow.run({})).to.reject('Mis-configured allowedScripts: @example/with-install-script (no entry)');
150150

151151
expect(fixture.getActualResult()).to.equal('');
152152
expect(fixture.getLog()).to.equal('');
@@ -190,7 +190,7 @@ describe('allow-scripts', () => {
190190
'without-scripts'
191191
]);
192192

193-
await expect(Allow.run({})).to.reject('Invalid version range in allowScripts[@example/with-install-script]: not-a-semver-range');
193+
await expect(Allow.run({})).to.reject('Mis-configured allowedScripts: @example/with-install-script (invalid semver range)');
194194

195195
expect(fixture.getActualResult()).to.equal('');
196196
expect(fixture.getLog()).to.equal('');
@@ -204,7 +204,7 @@ describe('allow-scripts', () => {
204204
'without-scripts'
205205
]);
206206

207-
await expect(Allow.run({})).to.reject('No entry for @example/with-install-script');
207+
await expect(Allow.run({})).to.reject('Mis-configured allowedScripts: @example/with-install-script (no entry), @example/with-postinstall-script (no entry)');
208208

209209
expect(fixture.getActualResult()).to.equal('');
210210
expect(fixture.getLog()).to.equal('');

0 commit comments

Comments
 (0)