Skip to content

Instantly share code, notes, and snippets.

@ArtemAvramenko
Last active June 22, 2024 14:34

Revisions

  1. ArtemAvramenko revised this gist Jul 17, 2023. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions install.js
    Original file line number Diff line number Diff line change
    @@ -8,6 +8,7 @@ const readLockFile = path => {
    const text = fs.readFileSync(path, { encoding:'utf8', flag:'r' });
    const lockFile = JSON.parse(text);
    delete lockFile.dependencies;
    delete lockFile.lockfileVersion;
    Object.getOwnPropertyNames(lockFile.packages).forEach(p => {
    if (p === '' || lockFile.packages[p].optional) {
    delete lockFile.packages[p];
  2. ArtemAvramenko revised this gist Jan 5, 2023. No changes.
  3. ArtemAvramenko revised this gist Sep 2, 2022. 1 changed file with 1 addition and 19 deletions.
    20 changes: 1 addition & 19 deletions install.js
    Original file line number Diff line number Diff line change
    @@ -1,10 +1,8 @@
    // Fastest install script for npm 7.0+
    // usage: node install

    const fs = require('fs');

    const minNodeVer = 14;
    const minNpmVer = 7;

    const readLockFile = path => {
    if (fs.existsSync(path)) {
    const text = fs.readFileSync(path, { encoding:'utf8', flag:'r' });
    @@ -22,26 +20,10 @@ const readLockFile = path => {
    const expected = readLockFile('./package-lock.json');

    // call npm ci only when package-lock changed

    if (expected) {

    const actual = readLockFile('./node_modules/.package-lock.json');

    if (!actual || JSON.stringify(actual) !== JSON.stringify(expected)) {

    const { execSync } = require('child_process');
    const nodeVer = process.versions.node;
    if (+nodeVer.split('.')[0] < minNodeVer) {
    console.warn('\x1b[31mnode is outdated (v' + nodeVer +
    '), you should install node v' + minNodeVer + '.0 or newer\x1b[0m');
    }

    const npmVer = execSync('npm -v').toString().trim();
    if (+npmVer.split('.')[0] < minNpmVer) {
    console.warn('\x1b[31mnpm is outdated (v' + npmVer + '), you should run:\x1b[0m');
    console.warn('npm install -g npm')
    }

    execSync('npm ci', { stdio: 'inherit' });
    }
    }
  4. ArtemAvramenko revised this gist Sep 15, 2021. 1 changed file with 17 additions and 7 deletions.
    24 changes: 17 additions & 7 deletions install.js
    Original file line number Diff line number Diff line change
    @@ -5,17 +5,27 @@ const fs = require('fs');
    const minNodeVer = 14;
    const minNpmVer = 7;

    const readJSON = path =>
    fs.existsSync(path) && JSON.parse(fs.readFileSync(path), { encoding:'utf8', flag:'r' });
    const readLockFile = path => {
    if (fs.existsSync(path)) {
    const text = fs.readFileSync(path, { encoding:'utf8', flag:'r' });
    const lockFile = JSON.parse(text);
    delete lockFile.dependencies;
    Object.getOwnPropertyNames(lockFile.packages).forEach(p => {
    if (p === '' || lockFile.packages[p].optional) {
    delete lockFile.packages[p];
    }
    });
    return lockFile;
    }
    }

    const expected = readJSON('./package-lock.json');
    const expected = readLockFile('./package-lock.json');

    if (expected) {
    // call npm ci only when package-lock changed

    delete expected.packages[''];
    delete expected.dependencies;
    if (expected) {

    const actual = readJSON('./node_modules/.package-lock.json');
    const actual = readLockFile('./node_modules/.package-lock.json');

    if (!actual || JSON.stringify(actual) !== JSON.stringify(expected)) {

  5. ArtemAvramenko created this gist Sep 13, 2021.
    37 changes: 37 additions & 0 deletions install.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    // usage: node install

    const fs = require('fs');

    const minNodeVer = 14;
    const minNpmVer = 7;

    const readJSON = path =>
    fs.existsSync(path) && JSON.parse(fs.readFileSync(path), { encoding:'utf8', flag:'r' });

    const expected = readJSON('./package-lock.json');

    if (expected) {

    delete expected.packages[''];
    delete expected.dependencies;

    const actual = readJSON('./node_modules/.package-lock.json');

    if (!actual || JSON.stringify(actual) !== JSON.stringify(expected)) {

    const { execSync } = require('child_process');
    const nodeVer = process.versions.node;
    if (+nodeVer.split('.')[0] < minNodeVer) {
    console.warn('\x1b[31mnode is outdated (v' + nodeVer +
    '), you should install node v' + minNodeVer + '.0 or newer\x1b[0m');
    }

    const npmVer = execSync('npm -v').toString().trim();
    if (+npmVer.split('.')[0] < minNpmVer) {
    console.warn('\x1b[31mnpm is outdated (v' + npmVer + '), you should run:\x1b[0m');
    console.warn('npm install -g npm')
    }

    execSync('npm ci', { stdio: 'inherit' });
    }
    }