Skip to content

Commit 8d7bd58

Browse files
committed
Initial commit
Signed-off-by: Chris. Webster <[email protected]>
0 parents  commit 8d7bd58

File tree

6 files changed

+411
-0
lines changed

6 files changed

+411
-0
lines changed

.gitignore

Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
# test for durf
2+
test/
3+
4+
# Logs
5+
logs
6+
*.log
7+
npm-debug.log*
8+
yarn-debug.log*
9+
yarn-error.log*
10+
lerna-debug.log*
11+
12+
# Diagnostic reports (https://nodejs.org/api/report.html)
13+
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
14+
15+
# Runtime data
16+
pids
17+
*.pid
18+
*.seed
19+
*.pid.lock
20+
21+
# Directory for instrumented libs generated by jscoverage/JSCover
22+
lib-cov
23+
24+
# Coverage directory used by tools like istanbul
25+
coverage
26+
*.lcov
27+
28+
# nyc test coverage
29+
.nyc_output
30+
31+
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
32+
.grunt
33+
34+
# Bower dependency directory (https://bower.io/)
35+
bower_components
36+
37+
# node-waf configuration
38+
.lock-wscript
39+
40+
# Compiled binary addons (https://nodejs.org/api/addons.html)
41+
build/Release
42+
43+
# Dependency directories
44+
node_modules/
45+
jspm_packages/
46+
47+
# TypeScript v1 declaration files
48+
typings/
49+
50+
# TypeScript cache
51+
*.tsbuildinfo
52+
53+
# Optional npm cache directory
54+
.npm
55+
56+
# Optional eslint cache
57+
.eslintcache
58+
59+
# Microbundle cache
60+
.rpt2_cache/
61+
.rts2_cache_cjs/
62+
.rts2_cache_es/
63+
.rts2_cache_umd/
64+
65+
# Optional REPL history
66+
.node_repl_history
67+
68+
# Output of 'npm pack'
69+
*.tgz
70+
71+
# Yarn Integrity file
72+
.yarn-integrity
73+
74+
# dotenv environment variables file
75+
.env
76+
.env.test
77+
78+
# parcel-bundler cache (https://parceljs.org/)
79+
.cache
80+
81+
# next.js build output
82+
.next
83+
84+
# nuxt.js build output
85+
.nuxt
86+
87+
# gatsby files
88+
.cache/
89+
public
90+
91+
# vuepress build output
92+
.vuepress/dist
93+
94+
# Serverless directories
95+
.serverless/
96+
97+
# FuseBox cache
98+
.fusebox/
99+
100+
# DynamoDB Local files
101+
.dynamodb/
102+
103+
# TernJS port file
104+
.tern-port
105+
# Logs
106+
logs
107+
*.log
108+
npm-debug.log*
109+
yarn-debug.log*
110+
yarn-error.log*
111+
lerna-debug.log*
112+
113+
# Diagnostic reports (https://nodejs.org/api/report.html)
114+
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
115+
116+
# Runtime data
117+
pids
118+
*.pid
119+
*.seed
120+
*.pid.lock
121+
122+
# Directory for instrumented libs generated by jscoverage/JSCover
123+
lib-cov
124+
125+
# Coverage directory used by tools like istanbul
126+
coverage
127+
*.lcov
128+
129+
# nyc test coverage
130+
.nyc_output
131+
132+
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
133+
.grunt
134+
135+
# Bower dependency directory (https://bower.io/)
136+
bower_components
137+
138+
# node-waf configuration
139+
.lock-wscript
140+
141+
# Compiled binary addons (https://nodejs.org/api/addons.html)
142+
build/Release
143+
144+
# Dependency directories
145+
node_modules/
146+
jspm_packages/
147+
148+
# TypeScript v1 declaration files
149+
typings/
150+
151+
# TypeScript cache
152+
*.tsbuildinfo
153+
154+
# Optional npm cache directory
155+
.npm
156+
157+
# Optional eslint cache
158+
.eslintcache
159+
160+
# Microbundle cache
161+
.rpt2_cache/
162+
.rts2_cache_cjs/
163+
.rts2_cache_es/
164+
.rts2_cache_umd/
165+
166+
# Optional REPL history
167+
.node_repl_history
168+
169+
# Output of 'npm pack'
170+
*.tgz
171+
172+
# Yarn Integrity file
173+
.yarn-integrity
174+
175+
# dotenv environment variables file
176+
.env
177+
.env.test
178+
179+
# parcel-bundler cache (https://parceljs.org/)
180+
.cache
181+
182+
# next.js build output
183+
.next
184+
185+
# nuxt.js build output
186+
.nuxt
187+
188+
# gatsby files
189+
.cache/
190+
public
191+
192+
# vuepress build output
193+
.vuepress/dist
194+
195+
# Serverless directories
196+
.serverless/
197+
198+
# FuseBox cache
199+
.fusebox/
200+
201+
# DynamoDB Local files
202+
.dynamodb/
203+
204+
# TernJS port file
205+
.tern-port

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2021 Chris. Webster
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# fix-has-install-script
2+
3+
Tool to check if a `package-lock.json` needs updating following conversion
4+
to lockfileVersion 2. All dependent packages will be checked for the
5+
`hasInstallScript` property. If it is not present, the `package.json` for
6+
that package will be check for `install`, `preinstall` or `postinstall`
7+
properties in the `scripts` section.
8+
9+
It will report on packages missing `hasInstallScript` set in `package-lock.json`.
10+
The lock file will be updated unless the `--dry-run` option is specfified.
11+
12+
This must be run from the directory where `package-lock.json` exists.
13+
14+
## Getting Started
15+
16+
### Prerequisites
17+
18+
Tested on Node v16 with npm v7.
19+
20+
### Installing
21+
22+
Due to low usage, `fix-has-install-script` can usually be run using `npx`:
23+
24+
```
25+
npx fix-has-install-script
26+
```
27+
28+
If it is installed globally there will be a `fix-has-install-script` command.
29+
30+
Run using:
31+
32+
```
33+
fix-has-install-script
34+
```
35+
36+
### Options:
37+
+ --dry-run report missing entries only. Do not update the lockfile. (default: false)
38+
+ --file-in <fileName> override JSON file to be read. Mostly used for testing.
39+
+ --file <fileName> JSON file to be processed. (default: "package-lock.json")
40+
41+
## License
42+
43+
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details

index.js

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#!/usr/bin/env node
2+
3+
import commander from "commander";
4+
import { readFile, writeFile } from 'fs/promises';
5+
6+
commander.version("1.0.0")
7+
.usage("[options]")
8+
.description("Tool to report on packages missing `hasInstallScript` set in `package-lock.json`. Optionally update the lock file.")
9+
.option("--debug",
10+
"trace extra scum messages")
11+
.option("--file-in <fileName>",
12+
"override JSON file to be read. Mostly used for testing.",
13+
undefined)
14+
.option("--file <fileName>",
15+
"JSON file to be processed.",
16+
"package-lock.json")
17+
.option("--dry-run", "report missing entries only. Do not update the lockfile.", false)
18+
.parse(process.argv);
19+
20+
const commandOptions = commander.opts();
21+
const debugLog = (body) => {
22+
return console.log(body);
23+
};
24+
const nodebugLog = () => { };
25+
26+
const debug = commandOptions.debug ? debugLog : nodebugLog;
27+
28+
if (!commandOptions.fileIn) {
29+
commandOptions.fileIn = commandOptions.file;
30+
}
31+
32+
(async () => {
33+
const lockFile = await getData(commandOptions.fileIn);
34+
let updated = false;
35+
if (lockFile.lockfileVersion && lockFile.lockfileVersion === 2) {
36+
const packages = lockFile.packages;
37+
for (const entry in packages) {
38+
if (entry != "") {
39+
debug(`Checking ${entry}`);
40+
41+
const depsEntry = packages[entry];
42+
if (!packages[entry].hasOwnProperty('hasInstallScript')) {
43+
const depsPackage = await getData(`${entry}/package.json`);
44+
45+
if (depsPackage.hasOwnProperty('scripts') && (
46+
depsPackage.scripts.hasOwnProperty('install') ||
47+
depsPackage.scripts.hasOwnProperty('preinstall') ||
48+
depsPackage.scripts.hasOwnProperty('postinstall'))) {
49+
console.log(`Package '${depsPackage.name}' requires `+
50+
"'hasInstallScript' to be set");
51+
52+
if (!commandOptions.dryRun) {
53+
packages[entry].hasInstallScript = true;
54+
updated = true;
55+
}
56+
}
57+
}
58+
}
59+
}
60+
61+
if (updated) {
62+
await putData(commandOptions.file, lockFile);
63+
}
64+
} else {
65+
console.log('Only lockfileVersion 2 is supported');
66+
return;
67+
}
68+
})().catch((reason) => {
69+
console.log(`Caught error ${reason}:\n${reason.stack}\n`);
70+
process.stderr.write(`Caught error ${reason}:\n${reason.stack}\n`);
71+
process.exit(1);
72+
});
73+
74+
async function getData( file ) {
75+
const rawdata = await readFile( file, 'utf8' );
76+
const records = JSON.parse( rawdata );
77+
return records;
78+
}
79+
80+
async function putData( file, data ) {
81+
const recordsOut = JSON.stringify( data, null, 2 );
82+
await writeFile( file, recordsOut );
83+
}

package-lock.json

Lines changed: 33 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)