-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
92 lines (80 loc) · 2.55 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/usr/bin/env node
import commander from "commander";
import { readFile, writeFile } from 'fs/promises';
commander.version("1.0.2")
.usage("[options]")
.description("Tool to report on packages missing `hasInstallScript` set in `package-lock.json`. Optionally update the lock file.")
.option("--debug",
"trace extra scum messages")
.option("--file-in <fileName>",
"override JSON file to be read. Mostly used for testing.",
undefined)
.option("--file <fileName>",
"JSON file to be processed.",
"package-lock.json")
.option("--dry-run", "report missing entries only. Do not update the lockfile.", false)
.parse(process.argv);
const commandOptions = commander.opts();
const debugLog = (body) => {
return console.log(body);
};
const nodebugLog = () => { };
const debug = commandOptions.debug ? debugLog : nodebugLog;
if (!commandOptions.fileIn) {
commandOptions.fileIn = commandOptions.file;
}
(async () => {
const lockFile = await getData(commandOptions.fileIn);
let updated = false;
if (lockFile.lockfileVersion && lockFile.lockfileVersion === 2) {
const packages = lockFile.packages;
for (const entry in packages) {
if (entry != "") {
debug(`Checking ${entry}`);
const depsEntry = packages[entry];
if (!packages[entry].hasOwnProperty('hasInstallScript')) {
try {
const depsPackage = await getData(`${entry}/package.json`);
if (depsPackage.hasOwnProperty('scripts') && (
depsPackage.scripts.hasOwnProperty('install') ||
depsPackage.scripts.hasOwnProperty('preinstall') ||
depsPackage.scripts.hasOwnProperty('postinstall'))) {
console.log(`Package '${depsPackage.name}' requires `+
"'hasInstallScript' to be set");
if (!commandOptions.dryRun) {
packages[entry].hasInstallScript = true;
updated = true;
}
}
} catch(reason) {
debug(`Failed on ${entry}`);
if (reason.code === "ENOENT") {
console.log(`package-lock.json may need rebuilding. ${entry} not found.`);
}
else {
throw reason;
}
}
}
}
}
if (updated) {
await putData(commandOptions.file, lockFile);
}
} else {
console.log('Only lockfileVersion 2 is supported');
return;
}
})().catch((reason) => {
console.error(`Caught error: ${reason}`);
process.exit(1);
});
async function getData( file ) {
const rawdata = await readFile( file, 'utf8' );
const records = JSON.parse( rawdata );
return records;
}
async function putData( file, data ) {
const recordsOut = JSON.stringify( data, null, 2 );
await writeFile( file, recordsOut );
}