UNPKG

post-npm-install

Version:

npm install when package.json dependencies have changed post-merge

3 lines (2 loc) 1.37 kB
#!/usr/bin/env node var child_process=require("child_process"),fs=require("fs");function postNpmInstall(e=!1){checkChanges().then(s=>{if(s)return canUseCi(e);process.exit(0)}).then(e=>{child_process.execSync(e?"npm ci":"npm install",{stdio:["pipe","pipe",process.stderr]}),console.log("Please restart the server.")}).then(()=>process.exit(0),e=>{console.error(e),process.exit(1)})}const checkChanges=()=>Promise.all([exec("git show ORIG_HEAD:package.json"),exec("git show HEAD:package.json")]).then(e=>{const s=JSON.parse(e[0]),c=JSON.parse(e[1]);return haveObjectValuesChanged(s.dependencies,c.dependencies)||haveObjectValuesChanged(s.devDependencies,c.devDependencies)},e=>{const s=/^fatal: Invalid object name/;if(!s.test(e))throw new Error(s)}),canUseCi=e=>Promise.all([checkNpmCiExist(),doesPackageLockJsonExist()]).then(([s,c])=>(!c&&e&&console.warn("Please ensure you have a lockfile before attempting to use npm ci.\nFalling back to npm install."),s&&c&&e)),exec=(e,s)=>new Promise((c,n)=>{child_process.exec(e,Object(s),(e,s,t)=>{e?n(t):c(s)})}),checkNpmCiExist=()=>exec("npm ci --help").then(e=>"npm ci"===e.toString()),doesPackageLockJsonExist=()=>new Promise(e=>{fs.access("package-lock.json",fs.constants.R_OK,s=>{e(!s)})}),haveObjectValuesChanged=(e,s)=>Object.keys(Object(e)).concat(Object.keys(Object(s))).some(c=>e[c]!==s[c]);module.exports=postNpmInstall;