Created
October 20, 2021 05:25
-
-
Save dongyuwei/d2234eed1f15479025b35db1af9fac4a to your computer and use it in GitHub Desktop.
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
const path = require('path'); | |
const fs = require('fs'); | |
const shell = require('shelljs'); // Todo: `npm install --save-dev [email protected]` 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