From be9471355d2eda8b32d22343074534097ec5d1e6 Mon Sep 17 00:00:00 2001 From: Peter LeBrun Date: Wed, 16 Jan 2019 16:12:17 -0500 Subject: [PATCH 1/3] Prefix workspace commands with workspace name --- CHANGELOG.md | 6 ++++-- package.json | 1 + .../pkg-tests/pkg-tests-core/package.json | 3 +++ .../pkg-tests/pkg-tests-fixtures/package.json | 3 ++- .../pkg-tests/pkg-tests-specs/package.json | 3 +++ src/cli/commands/workspaces.js | 3 ++- src/util/child.js | 21 +++++++++++++++++-- yarn.lock | 16 +++++++++++++- 8 files changed, 49 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c941e3ef8f..4a6cb6c2db 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,14 +4,16 @@ Please add one entry in this file for each change in Yarn's behavior. Use the sa ## Master +- Prefix `yarn workspaces run` output with workspace name + - Improves PnP compatibility with Node 6 [#6871](https://github.com/yarnpkg/yarn/pull/6871) - [**Robert Jackson**](https://github.com/rwjblue) - + - Fixes PnP detection with workspaces (`installConfig` is now read at the top-level) [#6878](https://github.com/yarnpkg/yarn/pull/6878) - [**Maƫl Nison**](https://twitter.com/arcanis) - + - Fixes an interaction between `yarn pack` and bundled dependencies [#6908](https://github.com/yarnpkg/yarn/pull/6908) - [**Travis Hoover**](https://twitter.com/thoov) diff --git a/package.json b/package.json index 8b7e9fffcd..24a43351ce 100644 --- a/package.json +++ b/package.json @@ -46,6 +46,7 @@ "ssri": "^5.3.0", "strip-ansi": "^4.0.0", "strip-bom": "^3.0.0", + "strong-log-transformer": "2.1.0", "tar-fs": "^1.16.0", "tar-stream": "^1.6.1", "uuid": "^3.0.1", diff --git a/packages/pkg-tests/pkg-tests-core/package.json b/packages/pkg-tests/pkg-tests-core/package.json index 0477d1a82d..9b8449e45d 100644 --- a/packages/pkg-tests/pkg-tests-core/package.json +++ b/packages/pkg-tests/pkg-tests-core/package.json @@ -10,5 +10,8 @@ "super-resolve": "^1.0.0", "tar-fs": "^1.16.0", "tmp": "^0.0.33" + }, + "scripts": { + "test-script": "echo pkg-tests-core test-script" } } diff --git a/packages/pkg-tests/pkg-tests-fixtures/package.json b/packages/pkg-tests/pkg-tests-fixtures/package.json index 3d7ae8fd88..868c5329f0 100644 --- a/packages/pkg-tests/pkg-tests-fixtures/package.json +++ b/packages/pkg-tests/pkg-tests-fixtures/package.json @@ -2,6 +2,7 @@ "name": "pkg-tests-fixtures", "version": "1.0.0", "scripts": { - "copy-index": "find packages -type d -mindepth 1 -maxdepth 1 | while read pkg; do cp default-index.js \"$pkg\/index.js\"; done" + "copy-index": "find packages -type d -mindepth 1 -maxdepth 1 | while read pkg; do cp default-index.js \"$pkg\/index.js\"; done", + "test-script": "echo pkg-tests-fixtures test-script" } } diff --git a/packages/pkg-tests/pkg-tests-specs/package.json b/packages/pkg-tests/pkg-tests-specs/package.json index 5b84ce264f..15941bc76d 100644 --- a/packages/pkg-tests/pkg-tests-specs/package.json +++ b/packages/pkg-tests/pkg-tests-specs/package.json @@ -6,5 +6,8 @@ "fs-extra": "^7.0.0", "pkg-tests-core": "1.0.0", "semver": "^5.6.0" + }, + "scripts": { + "test-script": "echo pkg-tests-specs test-script" } } diff --git a/src/cli/commands/workspaces.js b/src/cli/commands/workspaces.js index 616cffe041..a3f48db9ba 100644 --- a/src/cli/commands/workspaces.js +++ b/src/cli/commands/workspaces.js @@ -81,8 +81,9 @@ export async function runScript(config: Config, reporter: Reporter, flags: Objec const {loc} = workspaces[workspaceName]; await child.spawn(NODE_BIN_PATH, [YARN_BIN_PATH, ...rest], { - stdio: 'inherit', + stdio: 'pipe', cwd: loc, + workspaceName, }); } } catch (err) { diff --git a/src/util/child.js b/src/util/child.js index e83b819745..ee68b63abc 100644 --- a/src/util/child.js +++ b/src/util/child.js @@ -8,6 +8,11 @@ import {promisify} from './promise.js'; const child = require('child_process'); +const logTransformer = require('strong-log-transformer'); +const chalk = require('chalk'); +const colors = ['red', 'yellow', 'green', 'cyan', 'blue', 'magenta', 'white', 'gray']; +const workspaceColors = {}; + export const queue = new BlockingQueue('child', constants.CHILD_CONCURRENCY); // TODO: this uid check is kinda whack @@ -61,7 +66,7 @@ type ProcessFn = ( export function spawn( program: string, args: Array, - opts?: child_process$spawnOpts & {detached?: boolean, process?: ProcessFn} = {}, + opts?: child_process$spawnOpts & {detached?: boolean, process?: ProcessFn, workspaceName?: string} = {}, onData?: (chunk: Buffer | string) => void, ): Promise { const key = opts.cwd || String(++uid); @@ -116,7 +121,19 @@ export function spawn( } if (proc.stdout) { - proc.stdout.on('data', updateStdout); + // Prefix output with workspace name, if we have it + if (opts.workspaceName) { + // Stay consistent by using the key that is mapped to proc + if (!workspaceColors[key]) { + const randInt = Math.floor(Math.random() * colors.length); + workspaceColors[key] = colors[randInt]; + } + const wsColor = chalk[workspaceColors[key]]; + const tag = `${wsColor.bold(opts.workspaceName)}:`; + proc.stdout.pipe(logTransformer({tag})).pipe(process.stdout); + } else { + proc.stdout.on('data', updateStdout); + } } processingDone = true; diff --git a/yarn.lock b/yarn.lock index a270478e36..0a3a362f2a 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2387,6 +2387,11 @@ domexception@^1.0.1: dependencies: webidl-conversions "^4.0.2" +duplexer@^0.1.1: + version "0.1.1" + resolved "https://registry.npmjs.org/duplexer/-/duplexer-0.1.1.tgz#ace6ff808c1ce66b57d1ebf97977acb02334cfc1" + integrity sha1-rOb/gIwc5mtX0ev5eXessCM0z8E= + duplexify@^3.5.0, duplexify@^3.6.0: version "3.6.0" resolved "https://registry.yarnpkg.com/duplexify/-/duplexify-3.6.0.tgz#592903f5d80b38d037220541264d69a198fb3410" @@ -7077,6 +7082,15 @@ strip-json-comments@2.0.1, strip-json-comments@~2.0.1: resolved "https://registry.yarnpkg.com/strip-json-comments/-/strip-json-comments-2.0.1.tgz#3c531942e908c2697c0ec344858c286c7ca0a60a" integrity sha1-PFMZQukIwml8DsNEhYwobHygpgo= +strong-log-transformer@2.1.0: + version "2.1.0" + resolved "https://registry.npmjs.org/strong-log-transformer/-/strong-log-transformer-2.1.0.tgz#0f5ed78d325e0421ac6f90f7f10e691d6ae3ae10" + integrity sha512-B3Hgul+z0L9a236FAUC9iZsL+nVHgoCJnqCbN588DjYxvGXaXaaFbfmQ/JhvKjZwsOukuR72XbHv71Qkug0HxA== + dependencies: + duplexer "^0.1.1" + minimist "^1.2.0" + through "^2.3.4" + supports-color@^2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/supports-color/-/supports-color-2.0.0.tgz#535d045ce6b6363fa40117084629995e9df324c7" @@ -7231,7 +7245,7 @@ through2@2.X, through2@^2.0.0, through2@^2.0.1, through2@^2.0.3, through2@~2.0.0 readable-stream "^2.1.5" xtend "~4.0.1" -through@^2.3.6: +through@^2.3.4, through@^2.3.6: version "2.3.8" resolved "https://registry.yarnpkg.com/through/-/through-2.3.8.tgz#0dd4c9ffaabc357960b1b724115d7e0e86a2e1f5" integrity sha1-DdTJ/6q8NXlgsbckEV1+Doai4fU= From c7480784ae8308e9d2d9752130249116725555b1 Mon Sep 17 00:00:00 2001 From: peterlebrun Date: Thu, 17 Jan 2019 16:40:00 -0500 Subject: [PATCH 2/3] fix for tests --- __tests__/commands/workspaces.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/__tests__/commands/workspaces.js b/__tests__/commands/workspaces.js index ad81f2df76..3bd6d0aa68 100644 --- a/__tests__/commands/workspaces.js +++ b/__tests__/commands/workspaces.js @@ -52,12 +52,14 @@ test('workspaces run should spawn command for each workspace', (): Promise const originalArgs = ['run', 'script', 'arg1', '--flag1']; return runWorkspaces({originalArgs}, ['run', 'script', 'arg1', '--flag1'], 'run-basic', config => { expect(spawn).toHaveBeenCalledWith(NODE_BIN_PATH, [YARN_BIN_PATH, 'script', 'arg1', '--flag1'], { - stdio: 'inherit', + stdio: 'pipe', cwd: path.join(fixturesLoc, 'run-basic', 'packages', 'workspace-child-1'), + workspaceName: 'workspace-child-1', }); expect(spawn).toHaveBeenCalledWith(NODE_BIN_PATH, [YARN_BIN_PATH, 'script', 'arg1', '--flag1'], { - stdio: 'inherit', + stdio: 'pipe', cwd: path.join(fixturesLoc, 'run-basic', 'packages', 'workspace-child-2'), + workspaceName: 'workspace-child-2', }); }); }); From 31b01f5ed97837b760c5eab1ec43c1d53ef51a47 Mon Sep 17 00:00:00 2001 From: peterlebrun Date: Wed, 23 Jan 2019 10:49:17 -0500 Subject: [PATCH 3/3] Fix test output --- __tests__/commands/workspaces.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/__tests__/commands/workspaces.js b/__tests__/commands/workspaces.js index 3bd6d0aa68..c2fc25f613 100644 --- a/__tests__/commands/workspaces.js +++ b/__tests__/commands/workspaces.js @@ -54,12 +54,12 @@ test('workspaces run should spawn command for each workspace', (): Promise expect(spawn).toHaveBeenCalledWith(NODE_BIN_PATH, [YARN_BIN_PATH, 'script', 'arg1', '--flag1'], { stdio: 'pipe', cwd: path.join(fixturesLoc, 'run-basic', 'packages', 'workspace-child-1'), - workspaceName: 'workspace-child-1', + workspaceName: 'workspace-1', }); expect(spawn).toHaveBeenCalledWith(NODE_BIN_PATH, [YARN_BIN_PATH, 'script', 'arg1', '--flag1'], { stdio: 'pipe', cwd: path.join(fixturesLoc, 'run-basic', 'packages', 'workspace-child-2'), - workspaceName: 'workspace-child-2', + workspaceName: 'workspace-2', }); }); });