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

Commit d96ec3a

Browse files
committed
feat: validate semver range
Closes #6
1 parent 5728a44 commit d96ec3a

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

lib/index.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const Cp = require('child_process');
44
const Fs = require('fs');
55
const Npm = require('libnpm');
66
const Path = require('path');
7+
const Semver = require('semver');
78
const Topo = require('topo');
89

910

@@ -131,11 +132,24 @@ exports.run = async (options) => {
131132
throw new Error(`No entry for ${name}`);
132133
}
133134

134-
if (!allowScripts[name]) {
135+
if (allowScripts[name] === false) {
135136
console.warn(`==========> skip ${path} (because it is not allowed in package.json)`);
136137
}
137138

138-
return allowScripts[name];
139+
if (allowScripts[name] === true) {
140+
return true;
141+
}
142+
143+
if (!Semver.validRange(allowScripts[name])) {
144+
throw new Error(`Invalid version range in allowedScripts[${name}]: ${allowScripts[name]}`);
145+
}
146+
147+
if (!Semver.satisfies(childPkg.version, allowScripts[name])) {
148+
console.warn(`==========> skip ${path} (because ${childPkg.version} is outside of allowed range: ${allowScripts[name]})`);
149+
return false;
150+
}
151+
152+
return true;
139153
});
140154

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

package.json

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "allow-scripts",
33
"version": "0.0.0",
4-
"description": "Execute allowed lifecycle scripts",
4+
"description": "Execute allowed `npm install` lifecycle scripts",
55
"main": "lib/index.js",
66
"repository": {
77
"url": "https://github.com/dominykas/allow-scripts.git"
@@ -15,12 +15,13 @@
1515
"author": "Dominykas Blyžė <hello@dominykas.com>",
1616
"license": "MIT",
1717
"dependencies": {
18-
"libnpm": "^2.0.1",
19-
"topo": "^3.0.3"
18+
"libnpm": "2.x.x",
19+
"semver": "5.x.x",
20+
"topo": "3.x.x"
2021
},
2122
"devDependencies": {
22-
"lab": "^18.0.1",
23-
"semantic-release": "^15.13.3"
23+
"lab": "18.x.x",
24+
"semantic-release": "15.x.x"
2425
},
2526
"files": [
2627
"bin",

0 commit comments

Comments
 (0)