Skip to content

Instantly share code, notes, and snippets.

@sub314xxl
Forked from ArtemAvramenko/install.js
Created June 22, 2024 14:34
Show Gist options
  • Save sub314xxl/f68709e11560064ca1205d4af589c774 to your computer and use it in GitHub Desktop.
Save sub314xxl/f68709e11560064ca1205d4af589c774 to your computer and use it in GitHub Desktop.
Fastest install script for npm 7.0+, speed up npm ci
// 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