plugins: Eliminate unnecessary run_npm.js

I had anticipated more shared logic than we actually need (the
abstraction in `run_npm.js` is YAGNI).
This commit is contained in:
Richard Hansen 2021-02-17 19:54:49 -05:00 committed by John McLear
parent 426c025127
commit d8bb5aa009
2 changed files with 22 additions and 43 deletions

View file

@ -1,39 +0,0 @@
'use strict';
const log4js = require('log4js');
const runCmd = require('./run_cmd');
const logger = log4js.getLogger('runNpm');
/**
* Wrapper around `runCmd()` to make it easier to run npm.
*
* @param args Command-line arguments to pass to npm.
* @param opts See the documentation for `runCmd()`.
*
* @returns A Promise with additional `stdout`, `stderr`, and `child` properties. See the
* documentation for `runCmd()`.
*/
module.exports = exports = (args, opts = {}) => {
const cmd = ['npm', ...args];
// MUST return the original Promise returned from runCmd so that the caller can access stdout.
return runCmd(cmd, opts);
};
// Log the version of npm at startup.
let loggedVersion = false;
(async () => {
if (loggedVersion) return;
loggedVersion = true;
const p = runCmd(['npm', '--version'], {stdoutLogger: null});
const chunks = [];
await Promise.all([
(async () => { for await (const chunk of p.stdout) chunks.push(chunk); })(),
p, // Await in parallel to avoid unhandled rejection if p rejects during chunk read.
]);
const version = Buffer.concat(chunks).toString().replace(/\n+$/g, '');
logger.info(`npm --version: ${version}`);
})().catch((err) => {
logger.error(`Failed to get npm version: ${err.stack}`);
// This isn't a fatal error so don't re-throw.
});

View file

@ -4,13 +4,31 @@ const fs = require('fs').promises;
const hooks = require('./hooks');
const log4js = require('log4js');
const path = require('path');
const runNpm = require('../../../node/utils/run_npm');
const runCmd = require('../../../node/utils/run_cmd');
const tsort = require('./tsort');
const pluginUtils = require('./shared');
const defs = require('./plugin_defs');
const logger = log4js.getLogger('plugins');
// Log the version of npm at startup.
let loggedVersion = false;
(async () => {
if (loggedVersion) return;
loggedVersion = true;
const p = runCmd(['npm', '--version'], {stdoutLogger: null});
const chunks = [];
await Promise.all([
(async () => { for await (const chunk of p.stdout) chunks.push(chunk); })(),
p, // Await in parallel to avoid unhandled rejection if p rejects during chunk read.
]);
const version = Buffer.concat(chunks).toString().replace(/\n+$/g, '');
logger.info(`npm --version: ${version}`);
})().catch((err) => {
logger.error(`Failed to get npm version: ${err.stack || err}`);
// This isn't a fatal error so don't re-throw.
});
exports.prefix = 'ep_';
exports.formatPlugins = () => Object.keys(defs.plugins).join(', ');
@ -78,13 +96,13 @@ exports.getPackages = async () => {
// * The `--no-production` flag is required (or the `NODE_ENV` environment variable must be
// unset or set to `development`) because otherwise `npm ls` will not mention any packages
// that are not included in `package.json` (which is expected to not exist).
const np = runNpm(['ls', '--long', '--json', '--depth=0', '--no-production'], {
const p = runCmd(['npm', 'ls', '--long', '--json', '--depth=0', '--no-production'], {
stdoutLogger: null, // We want to capture stdout, so don't attempt to log it.
});
const chunks = [];
await Promise.all([
(async () => { for await (const chunk of np.stdout) chunks.push(chunk); })(),
np, // Await in parallel to avoid unhandled rejection if np rejects during chunk read.
(async () => { for await (const chunk of p.stdout) chunks.push(chunk); })(),
p, // Await in parallel to avoid unhandled rejection if p rejects during chunk read.
]);
const {dependencies = {}} = JSON.parse(Buffer.concat(chunks).toString());
await Promise.all(Object.entries(dependencies).map(async ([pkg, info]) => {