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 14 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
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@
"source-map-support": "^0.5.20",
"strip-bom": "^5.0.0",
"strip-json-comments": "^5.0.0",
"strong-log-transformer": "^2.1.0",
"tempy": "^3.0.0",
"typedoc": "^0.25.0",
"typedoc-plugin-mdn-links": "^3.0.3",
Expand Down Expand Up @@ -335,6 +336,7 @@
"@types/prompt": "^1.1.2",
"@types/proper-lockfile": "^4.1.1",
"@types/semver": "^7.3.4",
"@types/strong-log-transformer": "^1.0.2",
"@types/update-notifier": "^6.0.1",
"@types/yargs": "^17.0.0",
"electron": "^26.1.0"
Expand Down
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 @@
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

Check warning on line 42 in src/cmds/release-rc.js

View check run for this annotation

Codecov / codecov/patch

src/cmds/release-rc.js#L38-L42

Added lines #L38 - L42 were not covered by tests
}
})
},
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 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 @@ -76,23 +76,22 @@

await retry(async () => {
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
// returns an error
return
}

throw err
}

Check warning on line 94 in src/release-rc.js

View check run for this annotation

Codecov / codecov/patch

src/release-rc.js#L79-L94

Added lines #L79 - L94 were not covered by tests
}, {
retries: ctx.retries
})
Expand All @@ -119,22 +118,22 @@

await retry(async () => {
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',
all: true
})
} 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
// returns an error
return
}

throw err
}

Check warning on line 136 in src/release-rc.js

View check run for this annotation

Codecov / codecov/patch

src/release-rc.js#L121-L136

Added lines #L121 - L136 were not covered by tests
}, {
retries: ctx.retries
})
Expand Down
11 changes: 6 additions & 5 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,15 @@ 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], {
const subprocess = execa('npm', ['run', script, ...forwardArgs], {
cwd: project.dir,
stderr: 'inherit',
stdout: 'inherit'
stdio: ['ignore', 'pipe', 'pipe']
})
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 @@ -22,6 +22,7 @@
import { readPackageUpSync } from 'read-pkg-up'
import stripBom from 'strip-bom'
import stripComments from 'strip-json-comments'
import logTransformer from 'strong-log-transformer'

const __dirname = path.dirname(fileURLToPath(import.meta.url))
const EnvPaths = envPaths('aegir', { suffix: '' })
Expand Down Expand Up @@ -374,14 +375,14 @@
const stat = fs.statSync(subProjectDir)

if (!stat.isDirectory()) {
continue
}

Check warning on line 379 in src/utils.js

View check run for this annotation

Codecov / codecov/patch

src/utils.js#L378-L379

Added lines #L378 - L379 were not covered by tests

const manfest = path.join(subProjectDir, 'package.json')

if (!fs.existsSync(manfest)) {
continue
}

Check warning on line 385 in src/utils.js

View check run for this annotation

Codecov / codecov/patch

src/utils.js#L384-L385

Added lines #L384 - L385 were not covered by tests

const pkg = fs.readJSONSync(manfest)

Expand Down Expand Up @@ -491,10 +492,10 @@

return
}

if (minimatch(relativeDir, pattern, options)) {
yield options.absolute === true ? absoluteDir : relativeDir
}

Check warning on line 498 in src/utils.js

View check run for this annotation

Codecov / codecov/patch

src/utils.js#L495-L498

Added lines #L495 - L498 were not covered by tests
}

/**
Expand All @@ -508,14 +509,14 @@
const p = path.join(base, dir)

if (!fs.existsSync(p)) {
return
}

Check warning on line 513 in src/utils.js

View check run for this annotation

Codecov / codecov/patch

src/utils.js#L512-L513

Added lines #L512 - L513 were not covered by tests

const stats = fs.statSync(p)

if (!stats.isDirectory()) {
return
}

Check warning on line 519 in src/utils.js

View check run for this annotation

Codecov / codecov/patch

src/utils.js#L518-L519

Added lines #L518 - L519 were not covered by tests

const d = fs.opendirSync(p)

Expand All @@ -535,8 +536,8 @@
const isDirectory = entry.isDirectory()

if (isDirectory && options.nodir === true) {
match = false
}

Check warning on line 540 in src/utils.js

View check run for this annotation

Codecov / codecov/patch

src/utils.js#L539-L540

Added lines #L539 - L540 were not covered by tests

if (match) {
yield options.absolute === true ? absoluteEntryPath : relativeEntryPath
Expand Down Expand Up @@ -575,3 +576,30 @@
})
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) {
if (shouldPrefix === false) {
subprocess.stdout?.pipe(process.stdout)
subprocess.stderr?.pipe(process.stderr)

return
}

const stdoutOpts = {
tag: kleur.gray(`${prefix}:`)
}

const stderrOpts = {
tag: kleur.gray(`${prefix}:`)
}

subprocess.stdout?.pipe(logTransformer(stdoutOpts)).pipe(process.stdout)
subprocess.stderr?.pipe(logTransformer(stderrOpts)).pipe(process.stderr)
}
87 changes: 87 additions & 0 deletions test/exec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/* 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

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

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
Loading