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

Commit c86dc14

Browse files
authored
Merge pull request #18 from dominykas/list-all
Report all errors at once
2 parents 1036a6c + 04c92ba commit c86dc14

File tree

4 files changed

+48
-17
lines changed

4 files changed

+48
-17
lines changed

bin/allow-scripts.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
'use strict';
44

5+
const Ansicolors = require('ansicolors');
6+
57
const Allow = require('..');
68

79
const options = {
@@ -10,6 +12,13 @@ const options = {
1012

1113
Allow.run(options).catch((err) => {
1214

13-
console.error(err);
15+
let log = err;
16+
if (err.stack) {
17+
18+
const [message, ...rest] = err.stack.split('\n');
19+
log = `${Ansicolors.red(message)}\n${rest.join('\n')}`;
20+
}
21+
22+
console.error(`\n\n${log}`);
1423
process.exit(1);
1524
});

lib/index.js

Lines changed: 34 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,56 @@ 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, childPkg } = entry;
139+
const name = entry.childPkg.name;
132140

133141
if (allowScripts[name] === undefined) {
134-
throw new Error(`No entry for ${name}`);
142+
errors.push(`${name} (no entry)`);
143+
return;
135144
}
136145

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

142151
if (allowScripts[name] === true) {
143-
return true;
152+
allowedPackages.push(entry);
153+
return;
144154
}
145155

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

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;
161+
if (!Semver.satisfies(entry.childPkg.version, allowScripts[name])) {
162+
warnings.push(`==========> skip ${path} (because ${childPkg.version} is outside of allowed range: ${allowScripts[name]})`);
163+
return;
153164
}
154165

155-
return true;
166+
allowedPackages.push(entry);
156167
});
157168

169+
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');
170+
171+
if (errors.length) {
172+
throw new Error(`Mis-configured allowedScripts: ${errors.join(', ')}`);
173+
}
174+
175+
if (warnings.length) {
176+
console.warn(warnings.join('\n'));
177+
}
178+
158179
await internals.runScript('preinstall', { pkg, path: '', cwd, unsafePerm: true }, options);
159180

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

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"author": "Dominykas Blyžė <[email protected]>",
1717
"license": "MIT",
1818
"dependencies": {
19+
"ansicolors": "0.3.x",
1920
"libnpm": "2.x.x",
2021
"semver": "5.x.x",
2122
"topo": "3.x.x"

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: not-a-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)