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

Commit 575fa8e

Browse files
committed
first draft
0 parents  commit 575fa8e

File tree

6 files changed

+147
-0
lines changed

6 files changed

+147
-0
lines changed

.eslintrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": "./node_modules/lab/lib/linter/.eslintrc.js"
3+
}

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.idea
2+
node_modules
3+

bin/allow-scripts.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/usr/bin/env node
2+
3+
'use strict';
4+
5+
const Allow = require('..');
6+
7+
Allow.run();

lib/index.js

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
'use strict';
2+
3+
const Npm = require('libnpm');
4+
const Path = require('path');
5+
const Topo = require('topo');
6+
7+
8+
9+
const internals = {};
10+
11+
12+
internals.scan = (tree, parent, map = new Map(), scanned = new Set()) => {
13+
14+
for (const [k, v] of tree.dependencies) {
15+
16+
if (v.hasCycle()) {
17+
console.warn(`Cycle in ${k} - skipping`);
18+
continue;
19+
}
20+
21+
const path = v.path();
22+
if (!map.has(path)) {
23+
map.set(path, []);
24+
}
25+
26+
const node = map.get(path);
27+
node.push(parent);
28+
29+
if (!scanned.has(v)) {
30+
scanned.add(v);
31+
internals.scan(v, v.path(), map, scanned);
32+
}
33+
}
34+
35+
return map;
36+
};
37+
38+
39+
internals.queue = (tree) => {
40+
41+
const map = internals.scan(tree);
42+
const topo = new Topo();
43+
for (const [group, before] of map) {
44+
topo.add(group, { group, before });
45+
}
46+
47+
return topo.nodes;
48+
};
49+
50+
exports.run = (cmd = 'install') => {
51+
52+
const cwd = process.cwd();
53+
const pkg = require(Path.join(cwd, 'package.json'));
54+
const shrinkwrap = require(Path.join(cwd, 'npm-shrinkwrap.json'));
55+
56+
const tree = Npm.logicalTree(pkg, shrinkwrap);
57+
const queue = internals.queue(tree);
58+
59+
const allowed = (pkg.allowScripts || {})[cmd] || {};
60+
61+
queue
62+
.map((path) => {
63+
64+
const childPkg = require(Path.join(cwd, path, 'package.json'));
65+
66+
return [path, childPkg];
67+
})
68+
.filter(([,childPkg]) => {
69+
70+
return childPkg.scripts && (childPkg.scripts[cmd] || childPkg.scripts[`pre${cmd}`] || childPkg.scripts[`post${childPkg}`]);
71+
})
72+
.filter(([,childPkg]) => {
73+
74+
const name = childPkg.name;
75+
if (allowed[name] === undefined) {
76+
throw new Error(`No entry for ${name}`);
77+
}
78+
79+
return allowed[name];
80+
})
81+
.forEach(([path, childPkg]) => {
82+
83+
console.log(`==========> install ${path}...`);
84+
85+
Npm.runScript(childPkg, 'install', Path.join(cwd, path), {
86+
dir: cwd,
87+
log: Object.assign({
88+
pause: () => {},
89+
clearProgress: () => {},
90+
showProgress: () => {},
91+
verbose: () => {},
92+
silly: () => {},
93+
resume: () => {}
94+
}, console),
95+
config: {}
96+
});
97+
98+
console.log(`==========> postinstall ${path}...`);
99+
100+
Npm.runScript(childPkg, 'postinstall', Path.join(cwd, path), {
101+
dir: cwd,
102+
log: Object.assign({
103+
pause: () => {},
104+
clearProgress: () => {},
105+
showProgress: () => {},
106+
verbose: () => {},
107+
silly: () => {},
108+
resume: () => {}
109+
}, console),
110+
config: {}
111+
});
112+
});
113+
};

package.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "allow-scripts",
3+
"version": "0.0.0",
4+
"description": "Execute allowed lifecycle scripts",
5+
"main": "lib/index.js",
6+
"scripts": {
7+
"test": "lab -L -n"
8+
},
9+
"bin": {
10+
"allow-scripts": "./bin/allow-scripts.js"
11+
},
12+
"author": "",
13+
"license": "MIT",
14+
"dependencies": {
15+
"libnpm": "^2.0.1",
16+
"topo": "^3.0.3"
17+
},
18+
"devDependencies": {
19+
"lab": "^18.0.1"
20+
}
21+
}

test/index.js

Whitespace-only changes.

0 commit comments

Comments
 (0)