Skip to content

Instantly share code, notes, and snippets.

@wffranco
Created January 14, 2023 17:26
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 wffranco/61edcbb3111b4c929741f7f9de3ff830 to your computer and use it in GitHub Desktop.
Save wffranco/61edcbb3111b4c929741f7f9de3ff830 to your computer and use it in GitHub Desktop.
Node check (install/update packages if required)
/* eslint-disable @typescript-eslint/no-var-requires */
const { execSync } = require('child_process');
const fs = require('fs');
const path = require('path');
const paths = [
path.resolve(__dirname, 'package.json'),
path.resolve(__dirname, 'node_modules', 'last.package.json'),
];
function exec(path, options = {}) {
// {stdio: "ignore"} to ignore response
try {
const result = execSync(path, options);
return result.toString();
} catch (e) {
return '';
}
}
function readFile(name) {
try {
const file = fs.readFileSync(name, 'utf8');
return file.toString();
} catch (e) {
return '';
}
}
function updateNPM() {
const original = readFile(paths[0]);
if (!original) {
console.error(`${paths[0]} not found.`);
return;
}
const copy = readFile(paths[1]);
if (!copy) {
console.log('First time running prerun, or missing node_modules.');
} else if (original === copy) {
console.log('No changes in package.json');
return;
}
console.log('Runing npm install...');
if (exec('npm i')) {
fs.writeFileSync(paths[1], original);
console.log('Done.');
}
}
try {
updateNPM();
} catch (err) {
console.log(err);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment