Skip to content

Commit

Permalink
chore: move piping subprocess output to util fn
Browse files Browse the repository at this point in the history
  • Loading branch information
wemeetagain committed Oct 5, 2023
1 parent 363b7a7 commit c20b25c
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 16 deletions.
6 changes: 2 additions & 4 deletions src/exec.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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) {
Expand Down
11 changes: 3 additions & 8 deletions src/release-rc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -114,19 +113,15 @@ 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 () => {
console.info(`npm publish --tag ${ctx.tag} --dry-run ${!process.env.CI}`)

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')) {
Expand Down
6 changes: 2 additions & 4 deletions src/run.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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) {
Expand Down
12 changes: 12 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`))
}

0 comments on commit c20b25c

Please sign in to comment.