run_cmd: Log to Etherpad logs by default

This commit is contained in:
Richard Hansen 2021-02-17 18:22:01 -05:00 committed by John McLear
parent 689a75b381
commit 426c025127
2 changed files with 13 additions and 13 deletions

View file

@ -38,8 +38,9 @@ const logLines = (readable, logLineFn) => {
* @param opts Optional options that will be passed to `child_process.spawn()` with two extensions: * @param opts Optional options that will be passed to `child_process.spawn()` with two extensions:
* - `stdoutLogger`: Callback that is called each time a line of text is written to stdout (utf8 * - `stdoutLogger`: Callback that is called each time a line of text is written to stdout (utf8
* is assumed). The line (without trailing newline) is passed as the only argument. If null, * is assumed). The line (without trailing newline) is passed as the only argument. If null,
* stdout is not logged. If unset, defaults to no-op. Ignored if stdout is not a pipe. * stdout is not logged. If unset, defaults to logging to the Etherpad logs at log level INFO.
* - `stderrLogger`: Like `stdoutLogger` but for stderr. * Ignored if stdout is not a pipe.
* - `stderrLogger`: Like `stdoutLogger` but for stderr and log level ERROR.
* *
* @returns A Promise with `stdout`, `stderr`, and `child` properties containing the stdout stream, * @returns A Promise with `stdout`, `stderr`, and `child` properties containing the stdout stream,
* stderr stream, and ChildProcess objects, respectively. * stderr stream, and ChildProcess objects, respectively.
@ -47,7 +48,11 @@ const logLines = (readable, logLineFn) => {
module.exports = exports = (args, opts = {}) => { module.exports = exports = (args, opts = {}) => {
logger.debug(`Executing command: ${args.join(' ')}`); logger.debug(`Executing command: ${args.join(' ')}`);
const {stdoutLogger = () => {}, stderrLogger = () => {}} = opts; const cmdLogger = log4js.getLogger(`runCmd|${args[0]}`);
const {
stdoutLogger = (line) => cmdLogger.info(line),
stderrLogger = (line) => cmdLogger.error(line),
} = opts;
// Avoid confusing child_process.spawn() with our extensions. // Avoid confusing child_process.spawn() with our extensions.
opts = {...opts}; // Make a copy to avoid mutating the caller's copy. opts = {...opts}; // Make a copy to avoid mutating the caller's copy.
delete opts.stdoutLogger; delete opts.stdoutLogger;

View file

@ -4,17 +4,12 @@ const log4js = require('log4js');
const runCmd = require('./run_cmd'); const runCmd = require('./run_cmd');
const logger = log4js.getLogger('runNpm'); const logger = log4js.getLogger('runNpm');
const npmLogger = log4js.getLogger('npm');
const stdoutLogger = (line) => npmLogger.info(line);
const stderrLogger = (line) => npmLogger.error(line);
/** /**
* Wrapper around `runCmd()` that logs output to an npm logger by default. * Wrapper around `runCmd()` to make it easier to run npm.
* *
* @param args Command-line arguments to pass to npm. * @param args Command-line arguments to pass to npm.
* @param opts See the documentation for `runCmd()`. The `stdoutLogger` and `stderrLogger` options * @param opts See the documentation for `runCmd()`.
* default to a log4js logger.
* *
* @returns A Promise with additional `stdout`, `stderr`, and `child` properties. See the * @returns A Promise with additional `stdout`, `stderr`, and `child` properties. See the
* documentation for `runCmd()`. * documentation for `runCmd()`.
@ -22,7 +17,7 @@ const stderrLogger = (line) => npmLogger.error(line);
module.exports = exports = (args, opts = {}) => { module.exports = exports = (args, opts = {}) => {
const cmd = ['npm', ...args]; const cmd = ['npm', ...args];
// MUST return the original Promise returned from runCmd so that the caller can access stdout. // MUST return the original Promise returned from runCmd so that the caller can access stdout.
return runCmd(cmd, {stdoutLogger, stderrLogger, ...opts}); return runCmd(cmd, opts);
}; };
// Log the version of npm at startup. // Log the version of npm at startup.
@ -30,11 +25,11 @@ let loggedVersion = false;
(async () => { (async () => {
if (loggedVersion) return; if (loggedVersion) return;
loggedVersion = true; loggedVersion = true;
const p = runCmd(['npm', '--version'], {stdoutLogger: null, stderrLogger}); const p = runCmd(['npm', '--version'], {stdoutLogger: null});
const chunks = []; const chunks = [];
await Promise.all([ await Promise.all([
(async () => { for await (const chunk of p.stdout) chunks.push(chunk); })(), (async () => { for await (const chunk of p.stdout) chunks.push(chunk); })(),
p, // Await in parallel to avoid unhandled rejection if np rejects during chunk read. p, // Await in parallel to avoid unhandled rejection if p rejects during chunk read.
]); ]);
const version = Buffer.concat(chunks).toString().replace(/\n+$/g, ''); const version = Buffer.concat(chunks).toString().replace(/\n+$/g, '');
logger.info(`npm --version: ${version}`); logger.info(`npm --version: ${version}`);