post-npm-install
Version:
npm install when package.json dependencies have changed post-merge
3 lines (2 loc) • 1.13 kB
JavaScript
import{exec as e,execSync as n}from"child_process";import{access as s,constants as t}from"fs";const o=()=>Promise.all([c("git show ORIG_HEAD:package.json"),c("git show HEAD:package.json")]).then(e=>{const n=JSON.parse(e[0]),s=JSON.parse(e[1]);return a(n.dependencies,s.dependencies)||a(n.devDependencies,s.devDependencies)},e=>{const n=/^fatal: Invalid object name/;if(!n.test(e))throw new Error(n)}),r=e=>Promise.all([i(),p()]).then(([n,s])=>(!s&&e&&console.warn("Please ensure you have a lockfile before attempting to use npm ci.\nFalling back to npm install."),n&&s&&e)),c=(n,s)=>new Promise((t,o)=>{e(n,Object(s),(e,n,s)=>{e?o(s):t(n)})}),i=()=>c("npm ci --help").then(e=>"npm ci"===e.toString()),p=()=>new Promise(e=>{s("package-lock.json",t.R_OK,n=>{e(!n)})}),a=(e,n)=>Object.keys(Object(e)).concat(Object.keys(Object(n))).some(s=>e[s]!==n[s]);export default function(e=!1){o().then(n=>{if(n)return r(e);process.exit(0)}).then(e=>{n(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)})}