From c20b25c1407a10c29d672f60b7c6936201498d84 Mon Sep 17 00:00:00 2001 From: Cayman Date: Thu, 5 Oct 2023 18:25:32 -0400 Subject: [PATCH] chore: move piping subprocess output to util fn --- src/exec.js | 6 ++---- src/release-rc.js | 11 +++-------- src/run.js | 6 ++---- src/utils.js | 12 ++++++++++++ 4 files changed, 19 insertions(+), 16 deletions(-) diff --git a/src/exec.js b/src/exec.js index cf5078692..cd26d7613 100644 --- a/src/exec.js +++ b/src/exec.js @@ -1,6 +1,6 @@ import { execa } from 'execa' import kleur from 'kleur' -import { everyMonorepoProject } from './utils.js' +import { everyMonorepoProject, pipeOutput } from './utils.js' /** * @typedef {import("./types").GlobalOptions} GlobalOptions @@ -22,9 +22,7 @@ export default { const subprocess = execa(ctx.command, forwardOptions, { cwd: project.dir }) - const prefix = ctx.noPrefix ? '' : kleur.gray(project.manifest.name + ': ') - subprocess.stdout?.on('data', (data) => process.stdout.write(`${prefix}${data}`)) - subprocess.stderr?.on('data', (data) => process.stderr.write(`${prefix}${data}`)) + pipeOutput(subprocess, project.manifest.name, ctx.noPrefix) await subprocess } catch (/** @type {any} */ err) { if (ctx.bail !== false) { diff --git a/src/release-rc.js b/src/release-rc.js index 74e20d9cf..03dfcda25 100644 --- a/src/release-rc.js +++ b/src/release-rc.js @@ -5,8 +5,7 @@ import { execa } from 'execa' import fs from 'fs-extra' import Listr from 'listr' import retry from 'p-retry' -import { isMonorepoParent, pkg, everyMonorepoProject } from './utils.js' -import kleur from 'kleur' +import { isMonorepoParent, pkg, everyMonorepoProject, pipeOutput } from './utils.js' /** * @typedef {import("./types").GlobalOptions} GlobalOptions @@ -114,9 +113,7 @@ async function releaseRc (commit, ctx) { console.info(`npm version ${pkg.version}-${commit} --no-git-tag-version`) const subprocess = execa('npm', ['version', `${pkg.version}-${commit}`, '--no-git-tag-version']) - const prefix = ctx.noPrefix ? '' : kleur.gray(pkg.name + ': ') - subprocess.stdout?.on('data', (data) => process.stdout.write(`${prefix}${data}`)) - subprocess.stderr?.on('data', (data) => process.stderr.write(`${prefix}${data}`)) + pipeOutput(subprocess, pkg.version, ctx.noPrefix) await subprocess await retry(async () => { @@ -124,9 +121,7 @@ async function releaseRc (commit, ctx) { try { const subprocess = execa('npm', ['publish', '--tag', ctx.tag, '--dry-run', `${!process.env.CI}`]) - const prefix = ctx.noPrefix ? '' : kleur.gray(pkg.name + ': ') - subprocess.stdout?.on('data', (data) => process.stdout.write(`${prefix}${data}`)) - subprocess.stderr?.on('data', (data) => process.stderr.write(`${prefix}${data}`)) + pipeOutput(subprocess, pkg.version, ctx.noPrefix) await subprocess } catch (/** @type {any} */ err) { if (err.all?.includes('You cannot publish over the previously published versions')) { diff --git a/src/run.js b/src/run.js index 9cc6b8306..caefb0b5f 100644 --- a/src/run.js +++ b/src/run.js @@ -1,6 +1,6 @@ import { execa } from 'execa' import kleur from 'kleur' -import { everyMonorepoProject } from './utils.js' +import { everyMonorepoProject, pipeOutput } from './utils.js' /** * @typedef {import("./types").GlobalOptions} GlobalOptions @@ -33,9 +33,7 @@ export default { const subprocess = execa('npm', ['run', script, ...forwardArgs], { cwd: project.dir }) - const prefix = ctx.noPrefix ? '' : kleur.gray(project.manifest.name + ': ') - subprocess.stdout?.on('data', (data) => process.stdout.write(`${prefix}${data}`)) - subprocess.stderr?.on('data', (data) => process.stderr.write(`${prefix}${data}`)) + pipeOutput(subprocess, project.manifest.name, ctx.noPrefix) await subprocess } catch (/** @type {any} */ err) { if (ctx.bail !== false) { diff --git a/src/utils.js b/src/utils.js index 298920b71..bb485097c 100644 --- a/src/utils.js +++ b/src/utils.js @@ -575,3 +575,15 @@ export const formatCode = (code, errorLines) => { }) return ' ' + lines.join('\n ') } + +/** + * Pipe subprocess output to stdio + * @param {import('execa').ExecaChildProcess} subprocess + * @param {string} prefix + * @param {boolean} [noPrefix] + */ +export function pipeOutput(subprocess, prefix, noPrefix) { + prefix = noPrefix ? '' : kleur.gray(prefix + ': ') + subprocess.stdout?.on('data', (data) => process.stdout.write(`${prefix}${data}`)) + subprocess.stderr?.on('data', (data) => process.stderr.write(`${prefix}${data}`)) +} \ No newline at end of file