-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
39 lines (29 loc) · 1.01 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
const dirTree = require('directory-tree');
const fs = require('fs');
const scriptTypes = ['preinstall', 'postinstall', 'preuninstall', 'postuninstall'];
const reports = [];
const walkModules = ((path = 'node_modules') => {
dirTree(path, {extensions:/\.json$/}, (item) => {
if (item.name === 'package.json') {
let pkg = JSON.parse(fs.readFileSync(item.path, 'utf8'));
if (!pkg.scripts) return;
let scripts = Object.keys(pkg.scripts);
scripts.forEach((script) => {
if (scriptTypes.includes(script)) {
reports.push({name: pkg.name, script: script})
}
});
}
});
});
process.on('exit', () => {
if (!reports.length) {
console.log('No potentially unsafe scripts found.');
} else {
console.log('\x1b[31m', 'Potentially unsafe scripts found. These should be reviewed for safety', '\x1b[0m');
reports.forEach((report) => {
console.log(`Module name: ${report.name} Type: ${report.script}`);
});
}
});
module.exports = { walkModules };