-
-
Save sub314xxl/f68709e11560064ca1205d4af589c774 to your computer and use it in GitHub Desktop.
Fastest install script for npm 7.0+, speed up npm ci
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Fastest install script for npm 7.0+ | |
// usage: node install | |
const fs = require('fs'); | |
const readLockFile = path => { | |
if (fs.existsSync(path)) { | |
const text = fs.readFileSync(path, { encoding:'utf8', flag:'r' }); | |
const lockFile = JSON.parse(text); | |
delete lockFile.dependencies; | |
delete lockFile.lockfileVersion; | |
Object.getOwnPropertyNames(lockFile.packages).forEach(p => { | |
if (p === '' || lockFile.packages[p].optional) { | |
delete lockFile.packages[p]; | |
} | |
}); | |
return lockFile; | |
} | |
} | |
const expected = readLockFile('./package-lock.json'); | |
// call npm ci only when package-lock changed | |
if (expected) { | |
const actual = readLockFile('./node_modules/.package-lock.json'); | |
if (!actual || JSON.stringify(actual) !== JSON.stringify(expected)) { | |
const { execSync } = require('child_process'); | |
execSync('npm ci', { stdio: 'inherit' }); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment