|
1 | 1 | 'use strict';
|
2 | 2 |
|
| 3 | +const Cp = require('child_process'); |
| 4 | +const Fs = require('fs'); |
3 | 5 | const Npm = require('libnpm');
|
4 | 6 | const Path = require('path');
|
5 | 7 | const Topo = require('topo');
|
@@ -66,16 +68,41 @@ internals.runScript = (stage, { pkg, path, cwd, unsafePerm }) => {
|
66 | 68 | });
|
67 | 69 | };
|
68 | 70 |
|
| 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 | + |
69 | 98 | exports.run = async (cmd = 'install') => {
|
70 | 99 |
|
71 | 100 | const cwd = process.cwd();
|
72 | 101 | const pkg = require(Path.join(cwd, 'package.json'));
|
73 | 102 |
|
74 | 103 | pkg._id = `${pkg.name}@${pkg.version}`; // @todo: find an official way to do this for top level package
|
75 | 104 |
|
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)); |
79 | 106 | const queue = internals.queue(tree);
|
80 | 107 |
|
81 | 108 | const allowScripts = pkg.allowScripts || {};
|
|
0 commit comments