Skip to content

Instantly share code, notes, and snippets.

@wffranco
Created January 14, 2023 17:26
Show Gist options
  • Save wffranco/61edcbb3111b4c929741f7f9de3ff830 to your computer and use it in GitHub Desktop.
Save wffranco/61edcbb3111b4c929741f7f9de3ff830 to your computer and use it in GitHub Desktop.

Revisions

  1. wffranco created this gist Jan 14, 2023.
    56 changes: 56 additions & 0 deletions node-check.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,56 @@
    /* 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);
    }