Skip to content
This repository has been archived by the owner on Aug 18, 2024. It is now read-only.

Commit

Permalink
feat: fall back to package-lock.json or npm ls
Browse files Browse the repository at this point in the history
Closes #10
  • Loading branch information
dominykas committed Jan 29, 2019
1 parent beab25e commit 6169171
Showing 1 changed file with 30 additions and 3 deletions.
33 changes: 30 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

const Cp = require('child_process');
const Fs = require('fs');
const Npm = require('libnpm');
const Path = require('path');
const Topo = require('topo');
Expand Down Expand Up @@ -66,16 +68,41 @@ internals.runScript = (stage, { pkg, path, cwd, unsafePerm }) => {
});
};

internals.getLockFile = (cwd) => {

if (Fs.existsSync(Path.join(cwd, 'npm-shrinkwrap.json'))) {
return require(Path.join(cwd, 'npm-shrinkwrap.json'));
}

if (Fs.existsSync(Path.join(cwd, 'package-lock.json'))) {
return require(Path.join(cwd, 'package-lock.json'));
}

let output;
try {
output = Cp.execSync('npm ls --json', { cwd });
}
catch (err) {
output = err.output[1]; // npm will exist with an error when e.g. there's peer deps missing - attempt to ignore that
}

try {
return JSON.parse(output.toString());
}
catch (err) {
console.error(err);
throw new Error('Failed to read the contents of node_modules');
}
};

exports.run = async (cmd = 'install') => {

const cwd = process.cwd();
const pkg = require(Path.join(cwd, 'package.json'));

pkg._id = `${pkg.name}@${pkg.version}`; // @todo: find an official way to do this for top level package

const shrinkwrap = require(Path.join(cwd, 'npm-shrinkwrap.json'));

const tree = Npm.logicalTree(pkg, shrinkwrap);
const tree = Npm.logicalTree(pkg, internals.getLockFile(cwd));
const queue = internals.queue(tree);

const allowScripts = pkg.allowScripts || {};
Expand Down

0 comments on commit 6169171

Please sign in to comment.