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

Commit 6169171

Browse files
committed
feat: fall back to package-lock.json or npm ls
Closes #10
1 parent beab25e commit 6169171

File tree

1 file changed

+30
-3
lines changed

1 file changed

+30
-3
lines changed

lib/index.js

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
'use strict';
22

3+
const Cp = require('child_process');
4+
const Fs = require('fs');
35
const Npm = require('libnpm');
46
const Path = require('path');
57
const Topo = require('topo');
@@ -66,16 +68,41 @@ internals.runScript = (stage, { pkg, path, cwd, unsafePerm }) => {
6668
});
6769
};
6870

71+
internals.getLockFile = (cwd) => {
72+
73+
if (Fs.existsSync(Path.join(cwd, 'npm-shrinkwrap.json'))) {
74+
return require(Path.join(cwd, 'npm-shrinkwrap.json'));
75+
}
76+
77+
if (Fs.existsSync(Path.join(cwd, 'package-lock.json'))) {
78+
return require(Path.join(cwd, 'package-lock.json'));
79+
}
80+
81+
let output;
82+
try {
83+
output = Cp.execSync('npm ls --json', { cwd });
84+
}
85+
catch (err) {
86+
output = err.output[1]; // npm will exist with an error when e.g. there's peer deps missing - attempt to ignore that
87+
}
88+
89+
try {
90+
return JSON.parse(output.toString());
91+
}
92+
catch (err) {
93+
console.error(err);
94+
throw new Error('Failed to read the contents of node_modules');
95+
}
96+
};
97+
6998
exports.run = async (cmd = 'install') => {
7099

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

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

76-
const shrinkwrap = require(Path.join(cwd, 'npm-shrinkwrap.json'));
77-
78-
const tree = Npm.logicalTree(pkg, shrinkwrap);
105+
const tree = Npm.logicalTree(pkg, internals.getLockFile(cwd));
79106
const queue = internals.queue(tree);
80107

81108
const allowScripts = pkg.allowScripts || {};

0 commit comments

Comments
 (0)