Skip to content

Instantly share code, notes, and snippets.

@dongyuwei
Created October 20, 2021 05:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dongyuwei/d2234eed1f15479025b35db1af9fac4a to your computer and use it in GitHub Desktop.
Save dongyuwei/d2234eed1f15479025b35db1af9fac4a to your computer and use it in GitHub Desktop.
const path = require('path');
const fs = require('fs');
const shell = require('shelljs'); // Todo: `npm install --save-dev shelljs@0.8.4` for the first time on CI
const crypto = require('crypto');
// 如果 package-lock.json 都没有变化,就 skip `npm install`,节省ci时间。
const dirPath = path.join(__dirname, '..');
shell.cd(dirPath);
const hashFunc = crypto.createHash('md5');
shell
.exec(`fd package-lock.json`)
.toString()
.split('\n')
.filter((item) => !!item)
.map((lockFile) => {
hashFunc.update(fs.readFileSync(lockFile));
});
const checksum = hashFunc.digest('hex');
console.log('All package-lock.json checksum is:', checksum);
const file = '.node_modules_checksum.txt';
if (fs.existsSync(file)) {
const hash = fs.readFileSync(file, 'UTF-8');
if (hash === checksum) {
console.info('>>> No changes in package-lock.json, skip npm install!');
process.exit(0);
}
}
fs.writeFileSync(file, checksum);
shell.exec(`npm install`);
process.exit(0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment