From d8bb5aa0091f107804904fc8bebab64c0078ba3f Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Wed, 17 Feb 2021 19:54:49 -0500 Subject: [PATCH] plugins: Eliminate unnecessary `run_npm.js` I had anticipated more shared logic than we actually need (the abstraction in `run_npm.js` is YAGNI). --- src/node/utils/run_npm.js | 39 ------------------------------- src/static/js/pluginfw/plugins.js | 26 +++++++++++++++++---- 2 files changed, 22 insertions(+), 43 deletions(-) delete mode 100644 src/node/utils/run_npm.js diff --git a/src/node/utils/run_npm.js b/src/node/utils/run_npm.js deleted file mode 100644 index 869e10877..000000000 --- a/src/node/utils/run_npm.js +++ /dev/null @@ -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. -}); diff --git a/src/static/js/pluginfw/plugins.js b/src/static/js/pluginfw/plugins.js index a65b4f08e..0860a65b1 100644 --- a/src/static/js/pluginfw/plugins.js +++ b/src/static/js/pluginfw/plugins.js @@ -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]) => {