Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(monorepo): prefix output with package name #1378

Merged
merged 15 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/cmds/exec.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ export default {
type: 'boolean',
describe: '',
default: userConfig.exec.bail
},
prefix: {
type: 'boolean',
describe: 'Prefix output with the package name',
default: userConfig.exec.prefix
}
})
},
Expand Down
5 changes: 5 additions & 0 deletions src/cmds/release-rc.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ export default {
type: 'string',
describe: 'Which tag to publish the version as',
default: userConfig.releaseRc.tag
},
prefix: {
type: 'boolean',
describe: 'Prefix output with the package name',
default: userConfig.releaseRc.prefix
}
})
},
Expand Down
5 changes: 5 additions & 0 deletions src/cmds/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ export default {
type: 'boolean',
describe: '',
default: userConfig.run.bail
},
prefix: {
type: 'boolean',
describe: 'Prefix output with the package name',
default: userConfig.run.prefix
}
})
.positional('script', {
Expand Down
9 changes: 6 additions & 3 deletions src/config/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,18 +107,21 @@ const defaults = {
},
releaseRc: {
retries: 5,
tag: 'next'
tag: 'next',
prefix: true
},
// dependency check cmd options
dependencyCheck: {
unused: false,
ignore: []
},
exec: {
bail: true
bail: true,
prefix: true
},
run: {
bail: true
bail: true,
prefix: true
}
}

Expand Down
14 changes: 7 additions & 7 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 @@ -12,18 +12,18 @@ export default {
* @param {GlobalOptions & ExecOptions & { command: string }} ctx
*/
async run (ctx) {
const forwardOptions = ctx['--'] ? ctx['--'] : []
const forwardArgs = ctx['--'] ? ctx['--'] : []

await everyMonorepoProject(process.cwd(), async (project) => {
console.info('') // eslint-disable-line no-console
console.info(kleur.grey(`${project.manifest.name} > ${ctx.command} ${forwardOptions.join(' ')}`)) // eslint-disable-line no-console
console.info(kleur.grey(`${project.manifest.name}:`), `> ${ctx.command}${forwardArgs.length > 0 ? ` ${forwardArgs.join(' ')}` : ''}`) // eslint-disable-line no-console

try {
await execa(ctx.command, forwardOptions, {
cwd: project.dir,
stderr: 'inherit',
stdout: 'inherit'
const subprocess = execa(ctx.command, forwardArgs, {
cwd: project.dir
})
pipeOutput(subprocess, project.manifest.name, ctx.prefix)
await subprocess
} catch (/** @type {any} */ err) {
if (ctx.bail !== false) {
throw err
Expand Down
11 changes: 5 additions & 6 deletions src/release-rc.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +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 { isMonorepoParent, pkg, everyMonorepoProject, pipeOutput } from './utils.js'

/**
* @typedef {import("./types").GlobalOptions} GlobalOptions
Expand Down Expand Up @@ -78,12 +78,11 @@ async function releaseMonorepoRcs (commit, ctx) {
console.info(`npm publish --tag ${ctx.tag} --dry-run ${!process.env.CI}`)

try {
await execa('npm', ['publish', '--tag', ctx.tag, '--dry-run', `${!process.env.CI}`], {
stdout: 'inherit',
stderr: 'inherit',
cwd: project.dir,
all: true
const subprocess = execa('npm', ['publish', '--tag', ctx.tag, '--dry-run', `${!process.env.CI}`], {
cwd: project.dir
})
pipeOutput(subprocess, project.manifest.name, ctx.prefix)
await subprocess
} catch (/** @type {any} */ err) {
if (err.all?.includes('You cannot publish over the previously published versions')) {
// this appears to be a bug in npm, sometimes you publish successfully but it also
Expand Down
12 changes: 6 additions & 6 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 All @@ -27,14 +27,14 @@ export default {
}

console.info('') // eslint-disable-line no-console
console.info(kleur.grey(`${project.manifest.name} > npm run ${script} ${forwardArgs.join(' ')}`)) // eslint-disable-line no-console
console.info(kleur.grey(`${project.manifest.name}:`), `npm run ${script}${forwardArgs.length > 0 ? ` ${forwardArgs.join(' ')}` : ''}`) // eslint-disable-line no-console

try {
await execa('npm', ['run', script, ...forwardArgs], {
cwd: project.dir,
stderr: 'inherit',
stdout: 'inherit'
const subprocess = execa('npm', ['run', script, ...forwardArgs], {
cwd: project.dir
})
pipeOutput(subprocess, project.manifest.name, ctx.prefix)
await subprocess
} catch (/** @type {any} */ err) {
if (ctx.bail !== false) {
throw err
Expand Down
15 changes: 15 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,11 @@ interface ReleaseRcOptions {
* Which tag to publish the rc as
*/
tag: string

/**
* Prefix output with the package name
*/
prefix?: boolean
}

interface DependencyCheckOptions {
Expand All @@ -351,13 +356,23 @@ interface ExecOptions {
* If false, the command will continue to be run in other packages
*/
bail?: boolean

/**
* Prefix output with the package name
*/
prefix?: boolean
}

interface RunOptions {
/**
* If false, the command will continue to be run in other packages
*/
bail?: boolean

/**
* Prefix output with the package name
*/
prefix?: boolean
}

export type {
Expand Down
28 changes: 28 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -575,3 +575,31 @@ export const formatCode = (code, errorLines) => {
})
return ' ' + lines.join('\n ')
}

/**
* Pipe subprocess output to stdio
*
* @param {import('execa').ExecaChildProcess} subprocess
* @param {string} prefix
* @param {boolean} [shouldPrefix]
*/
export function pipeOutput (subprocess, prefix, shouldPrefix) {
prefix = shouldPrefix === false ? '' : kleur.gray(prefix + ':')
subprocess.stdout?.on('data', (data) => process.stdout.write(
data.toString('utf8')
.split('\n')
.map((/** @type {string} */ line) => [prefix, line].filter(Boolean)
.map(line => line.trim())
.join(' '))
.join('\n')
))
subprocess.stderr?.on('data', (data) => process.stderr.write(
data.toString('utf8')
.split('\n')
.map((/** @type {string} */ line) => [prefix, line]
.filter(Boolean)
.map(line => line.trim())
.join(' '))
.join('\n')
))
}
88 changes: 88 additions & 0 deletions test/exec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/* eslint-env mocha */

import { createRequire } from 'module'
import os from 'os'
import { execa } from 'execa'
import { expect } from '../utils/chai.js'
import { setUpProject } from './utils/set-up-project.js'

const require = createRequire(import.meta.url)
const bin = require.resolve('../src/index.js')

describe('exec', () => {
if (os.platform() === 'win32') {
describe.skip('Skipping tests on windows because commands are different', () => {})

return
}

let projectDir = ''

before(async () => {
projectDir = await setUpProject('a-monorepo')
})

it('should prefix monorepo output with the project name', async function () {
this.timeout(120 * 1000) // slow ci is slow

const result = await execa(bin, ['exec', 'ls'], {
cwd: projectDir
})

expect(result.stdout).to.equal(`
another-workspace-project: > ls
another-workspace-project: package.json
another-workspace-project: src
another-workspace-project: tsconfig.json
another-workspace-project: typedoc.json
another-workspace-project:
a-workspace-project: > ls
a-workspace-project: package.json
a-workspace-project: src
a-workspace-project: tsconfig.json
a-workspace-project: typedoc.json
a-workspace-project:`)
})

it('should not prefix monorepo output with the project name with --no-prefix', async function () {
this.timeout(120 * 1000) // slow ci is slow

const result = await execa(bin, ['exec', '--no-prefix', 'ls'], {
cwd: projectDir
})

expect(result.stdout).to.equal(`
another-workspace-project: > ls
package.json
src
tsconfig.json
typedoc.json

a-workspace-project: > ls
package.json
src
tsconfig.json
typedoc.json`)
})

it('should not prefix monorepo output with the project name with --prefix=false', async function () {
this.timeout(120 * 1000) // slow ci is slow

const result = await execa(bin, ['exec', '--prefix=false', 'ls'], {
cwd: projectDir
})

expect(result.stdout).to.equal(`
another-workspace-project: > ls
package.json
src
tsconfig.json
typedoc.json

a-workspace-project: > ls
package.json
src
tsconfig.json
typedoc.json`)
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
"import": "./src/index.js"
}
},
"scripts": {
"test": "echo very test"
},
"type": "module",
"author": "",
"license": "ISC"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
"import": "./src/index.js"
}
},
"scripts": {
"test": "echo very test"
},
"type": "module",
"author": "",
"license": "ISC"
Expand Down
2 changes: 2 additions & 0 deletions test/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import './fixtures.js'
import './dependants.js'
import './document-check.js'
import './dependency-check.js'
import './exec.js'
import './run.js'
import './utils/echo-server.js'
import './utils/get-port.js'
import './config/user.js'
Expand Down
Loading