UNPKG

post-checkout-npm-install

Version:

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

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