Skip to content

Commit e77b7bf

Browse files
Peter Bengtssonrsese
andauthored
Automatically run npm install when running npm start (#35283)
Co-authored-by: Robert Sese <[email protected]>
1 parent 222be16 commit e77b7bf

File tree

4 files changed

+38
-0
lines changed

4 files changed

+38
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,4 @@ user-code/
3232
# Logs from scripts
3333
script/logs/
3434
external-link-checker-db.json
35+
.installed.package-lock.json

.npmrc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
11
# skip installing optional dependencies to avoid issues with troublesome `fsevents` module
22
omit=optional
3+
4+
# For 15-25% faster npm install
5+
# https://www.peterbe.com/plog/benchmarking-npm-install-with-or-without-audit
6+
# Also we have Dependabot alerts configured in the GitHub repo.
7+
audit=false

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@
198198
"prevent-pushes-to-main": "node script/prevent-pushes-to-main.js",
199199
"rest-dev": "src/rest/scripts/update-files.js",
200200
"show-action-deps": "echo 'Action Dependencies:' && rg '^[\\s|-]*(uses:.*)$' .github -I -N --no-heading -r '$1$2' | sort | uniq | cut -c 7-",
201+
"prestart": "script/cmp-files.js package-lock.json .installed.package-lock.json || npm install && cp package-lock.json .installed.package-lock.json",
201202
"start": "cross-env NODE_ENV=development ENABLED_LANGUAGES=en nodemon server.js",
202203
"start-all-languages": "cross-env NODE_ENV=development nodemon server.js",
203204
"sync-search": "cross-env NODE_OPTIONS='--max_old_space_size=8192' start-server-and-test sync-search-server 4002 sync-search-indices",

script/cmp-files.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/usr/bin/env node
2+
3+
// [start-readme]
4+
//
5+
// Given N files. Exit 0 if they all exist and are identical in content.
6+
//
7+
// [end-readme]
8+
9+
import fs from 'fs'
10+
11+
import { program } from 'commander'
12+
13+
program.description('Compare N files').arguments('[files...]', '').parse(process.argv)
14+
15+
main(program.args)
16+
17+
function main(files) {
18+
if (files.length < 2) throw new Error('Must be at least 2 files')
19+
try {
20+
const contents = files.map((file) => fs.readFileSync(file, 'utf-8'))
21+
if (new Set(contents).size > 1) {
22+
process.exit(1)
23+
}
24+
} catch (error) {
25+
if (error.code === 'ENOENT') {
26+
process.exit(1)
27+
} else {
28+
throw error
29+
}
30+
}
31+
}

0 commit comments

Comments
 (0)