Skip to content

Commit e155a01

Browse files
authored
Disable path rewritting by default; allow WSRUN_* args; bump to 4.0.0 (#53)
Disable path rewritting by default; allow WSRUN_* args; bump to 4.0.0
2 parents e59c71f + c4e0172 commit e155a01

File tree

11 files changed

+91
-39
lines changed

11 files changed

+91
-39
lines changed

.prettierrc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"printWidth": 100,
3+
"singleQuote": true,
4+
"semi": false
5+
}

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
dist: xenial
22
language: node_js
33
node_js: 'lts/*'
4-
script: yarn build && chmod +x build/index.js && yarn test
4+
script: yarn test -i --ci

bin/wsrun.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env node
2+
3+
require('../build/index.js')

package.json

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,14 @@
11
{
22
"name": "wsrun",
3-
"version": "3.6.6",
3+
"version": "4.0.0",
44
"description": "executes commands on packages in parallel, but is aware of the dependencies between them",
55
"main": "./build/index.js",
66
"author": "hfour",
77
"license": "MIT",
88
"repository": "github:whoeverest/wsrun",
99
"jest": {
1010
"verbose": false,
11-
"transform": {
12-
"^.+\\.tsx?$": "ts-jest"
13-
},
14-
"moduleFileExtensions": [
15-
"ts",
16-
"tsx",
17-
"js",
18-
"jsx",
19-
"json"
20-
],
11+
"preset": "ts-jest",
2112
"testPathIgnorePatterns": [
2213
"<rootDir>/tmp",
2314
"<rootDir>/node_modules/",
@@ -33,7 +24,7 @@
3324
]
3425
},
3526
"bin": {
36-
"wsrun": "./build/index.js"
27+
"wsrun": "./bin/wsrun.js"
3728
},
3829
"files": [
3930
"build/"
@@ -61,7 +52,7 @@
6152
"scripts": {
6253
"build": "tsc",
6354
"watch": "tsc -w",
64-
"test": "jest",
55+
"test": "yarn build && jest",
6556
"test:watch": "jest --watch",
6657
"dev": "run-p test:watch watch",
6758
"prepublish": "tsc"

src/cmd-process.ts

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@ import * as Bromise from 'bluebird'
33

44
import originalSplit = require('split')
55

6-
import { Result, ResultSpecialValues } from './enums';
6+
import { Result, ResultSpecialValues } from './enums'
77
import { defer } from './utils'
88

99
export interface CmdOptions {
1010
rejectOnNonZeroExit: boolean
1111
silent?: boolean
1212
collectLogs: boolean
1313
prefixer?: (basePath: string, pkg: string, line: string) => string
14+
pathRewriter?: (basePath: string, pkg: string, line: string) => string
1415
doneCriteria?: string
1516
path: string
1617
}
@@ -47,7 +48,7 @@ export class CmdProcess {
4748
}
4849

4950
get result() {
50-
return Bromise.race([this._exitCode.promise, this._cancelled.promise]);
51+
return Bromise.race([this._exitCode.promise, this._cancelled.promise])
5152
}
5253

5354
/**
@@ -82,14 +83,14 @@ export class CmdProcess {
8283
this.exitCode.then(code => {
8384
if (code > 0) {
8485
const msg = '`' + this.cmdString + '` failed with exit code ' + code
85-
if (!this.opts.silent) console.error(this.autoPrefix(msg))
86+
if (!this.opts.silent) console.error(this.autoAugmentLine(msg))
8687
if (this.opts.rejectOnNonZeroExit) return this._finished.reject(new Error(msg))
8788
}
8889
this._finished.resolve()
8990
})
9091

9192
// ignore if unhandled
92-
this._finished.promise.catch(() => { })
93+
this._finished.promise.catch(() => {})
9394
}
9495

9596
stop() {
@@ -98,13 +99,25 @@ export class CmdProcess {
9899
this.cp.removeAllListeners('exit')
99100
this.cp.kill('SIGINT')
100101
}
101-
this._cancelled.resolve(ResultSpecialValues.Cancelled);
102+
this._cancelled.resolve(ResultSpecialValues.Cancelled)
102103
}
103104

104105
private autoPrefix(line: string) {
105106
return this.opts.prefixer ? this.opts.prefixer(this.opts.path, this.pkgName, line) : line
106107
}
107108

109+
private autoPathRewrite(line: string) {
110+
return this.opts.pathRewriter
111+
? this.opts.pathRewriter(this.opts.path, this.pkgName, line)
112+
: line
113+
}
114+
115+
private autoAugmentLine(line: string) {
116+
line = this.autoPathRewrite(line)
117+
line = this.autoPrefix(line)
118+
return line
119+
}
120+
108121
private _start(cmd: string[]) {
109122
let sh: string
110123
let args: string[]
@@ -137,21 +150,21 @@ export class CmdProcess {
137150
if (this.cp.stdout)
138151
this.cp.stdout.pipe(split()).on('data', (line: string) => {
139152
if (this.opts.collectLogs) stdOutBuffer.push(line)
140-
else console.log(this.autoPrefix(line))
153+
else console.log(this.autoAugmentLine(line))
141154
if (this.doneCriteria && this.doneCriteria.test(line)) this._finished.resolve()
142155
})
143156
if (this.cp.stderr)
144157
this.cp.stderr.pipe(split()).on('data', (line: string) => {
145158
if (this.opts.collectLogs) stdErrBuffer.push(line)
146-
else console.error(this.autoPrefix(line))
159+
else console.error(this.autoAugmentLine(line))
147160
if (this.doneCriteria && this.doneCriteria.test(line)) this._finished.resolve()
148161
})
149162
if (this.opts.collectLogs)
150163
this._closed.promise.then(() => {
151164
if (stdOutBuffer.length)
152-
console.log(stdOutBuffer.map(line => this.autoPrefix(line)).join('\n'))
165+
console.log(stdOutBuffer.map(line => this.autoAugmentLine(line)).join('\n'))
153166
if (stdErrBuffer.length)
154-
console.error(stdErrBuffer.map(line => this.autoPrefix(line)).join('\n'))
167+
console.error(stdErrBuffer.map(line => this.autoAugmentLine(line)).join('\n'))
155168
})
156169
}
157170
}

src/fix-paths.spec.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import 'jest'
22
import { fixPaths } from './fix-paths'
3-
import chalk from 'chalk';
3+
import chalk from 'chalk'
44

55
describe('fix paths', () => {
66
it('works', () => {
@@ -18,8 +18,8 @@ describe('fix paths', () => {
1818
})
1919

2020
it('ignores absolute paths that contain @', () => {
21-
let logLine = 'at (/Thing/src/node_modules/@h4bff/backend/src/rpc/serviceRegistry.ts:54:54)';
22-
let res = fixPaths('/Thing/src/','/Thing/src/packages/app-lib-ca-backend', logLine)
21+
let logLine = 'at (/Thing/src/node_modules/@h4bff/backend/src/rpc/serviceRegistry.ts:54:54)'
22+
let res = fixPaths('/Thing/src/', '/Thing/src/packages/app-lib-ca-backend', logLine)
2323
expect(res).toEqual(logLine)
2424
})
2525

@@ -37,4 +37,9 @@ describe('fix paths', () => {
3737
let res = fixPaths('/a/b/c', '/a/b/c/packages/p', 'Testing ' + chalk.blue('src/test.ts:12'))
3838
expect(res).toEqual('Testing ' + chalk.blue('packages/p/src/test.ts:12'))
3939
})
40+
41+
it('doesnt rewrite URLs', () => {
42+
let res = fixPaths('/a/b/c', '/a/b/c/packages/p', 'Testing http://src/test.ts')
43+
expect(res).toEqual('Testing http://src/test.ts')
44+
})
4045
})

src/fix-paths.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as path from 'path'
22

33
export function fixPaths(workspacePath: string, packagePath: string, logLine: string) {
44
return logLine.replace(
5-
/([\u001b][^m]*m|[^/_@0-9a-zA-Z])(([-_0-9a-zA-Z.]([^\s/'"*]*[/]){1,})([^/'"*]+)\.[0-9a-zA-Z]{1,6})/,
5+
/([\u001b][^m]*m|[^/_@0-9a-zA-Z])(([-_0-9a-zA-Z.]([^\s/'"*:]*[/]){1,})([^/'"*]+)\.[0-9a-zA-Z]{1,6})/,
66
(_m, before, file) => {
77
return before + path.relative(workspacePath, path.resolve(packagePath, file))
88
}

src/index.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#!/usr/bin/env node
2-
31
/**
42
* Tool for running command in yarn workspaces.
53
*/
@@ -13,6 +11,7 @@ import { RunGraph } from './run-graph'
1311
import { listPkgs } from './workspace'
1412

1513
let yargsParser = yargs
14+
.env('WSRUN')
1615
.wrap(yargs.terminalWidth() - 1)
1716
.updateStrings({
1817
'Options:': 'Other Options:'
@@ -30,7 +29,6 @@ let yargsParser = yargs
3029
'Runs "yarn watch" in each of the packages in stages, continuing when the process outputs "Finished"'
3130
)
3231
.example('$0 --exclude-missing test', 'Runs "yarn test" in all packages that have such a script')
33-
3432
.group(['parallel', 'stages', 'serial'], 'Mode (choose one):')
3533
.options({
3634
parallel: {
@@ -69,6 +67,7 @@ let yargsParser = yargs
6967
'fast-exit',
7068
'collect-logs',
7169
'no-prefix',
70+
'no-path-rewrite',
7271
'bin',
7372
'done-criteria',
7473
'exclude',
@@ -100,6 +99,11 @@ let yargsParser = yargs
10099
boolean: true,
101100
describe: "Don't prefix output"
102101
},
102+
'rewrite-paths': {
103+
boolean: true,
104+
describe:
105+
'Rewrite relative paths in the standard output, by prepending the <root_folder>/<package_name>.'
106+
},
103107
bin: {
104108
default: 'yarn',
105109
describe: 'The program to pass the command to',
@@ -156,6 +160,8 @@ const recursive: boolean = argv.recursive || argv.r || false
156160
const fastExit: boolean = argv.fastExit || false
157161
const collectLogs: boolean = argv.collectLogs || false
158162
const addPrefix: boolean = argv.prefix === undefined ? true : false
163+
console.log(argv)
164+
const rewritePaths: boolean = Boolean(argv.rewritePaths)
159165
const doneCriteria: string = argv.doneCriteria
160166
const exclude: string[] =
161167
(argv.exclude && (Array.isArray(argv.exclude) ? argv.exclude : [argv.exclude])) || []
@@ -197,6 +203,7 @@ let runner = new RunGraph(
197203
fastExit,
198204
collectLogs,
199205
addPrefix,
206+
rewritePaths,
200207
mode: mode as any,
201208
recursive,
202209
doneCriteria,

src/run-graph.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as Bromise from 'bluebird'
22
import chalk from 'chalk'
33

44
import { PkgJson, Dict } from './workspace'
5-
import { ResultSpecialValues, Result, ProcResolution } from './enums';
5+
import { ResultSpecialValues, Result, ProcResolution } from './enums'
66
import { uniq } from 'lodash'
77
import { CmdProcess } from './cmd-process'
88
import minimatch = require('minimatch')
@@ -35,6 +35,7 @@ export interface GraphOptions {
3535
fastExit: boolean
3636
collectLogs: boolean
3737
addPrefix: boolean
38+
rewritePaths: boolean
3839
mode: 'parallel' | 'serial' | 'stages'
3940
recursive: boolean
4041
doneCriteria: string | undefined
@@ -56,12 +57,14 @@ export class RunGraph {
5657
private resultMap = new Map<string, Result>()
5758
private throat: PromiseFnRunner = passThrough
5859
prefixer = new Prefixer(this.opts.workspacePath).prefixer
60+
pathRewriter = fixPaths
5961

6062
constructor(
6163
public pkgJsons: PkgJson[],
6264
public opts: GraphOptions,
6365
public pkgPaths: Dict<string>
6466
) {
67+
6568
this.checkResultsAndReport = this.checkResultsAndReport.bind(this)
6669
this.closeAll = this.closeAll.bind(this)
6770

@@ -187,6 +190,7 @@ export class RunGraph {
187190
rejectOnNonZeroExit: this.opts.fastExit,
188191
collectLogs: this.opts.collectLogs,
189192
prefixer: this.opts.addPrefix ? this.prefixer : undefined,
193+
pathRewriter: this.opts.rewritePaths ? this.pathRewriter : undefined,
190194
doneCriteria: this.opts.doneCriteria,
191195
path: this.pkgPaths[pkg]
192196
})

tests/basic.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,17 +185,35 @@ describe('basic', () => {
185185
)
186186
})
187187

188-
it('should show an error for pkgs without name', async () => {
188+
it('should not rewrite paths by default', async () => {
189189
await withScaffold(
190190
{
191191
packages: [
192-
echo.makePkg({ path: 'packages/p1', dependencies: {} }),
192+
echo.makePkg(
193+
{ name: 'app-x-frontend', dependencies: {} },
194+
'',
195+
'Output for path src/index.ts testing'
196+
)
193197
]
194198
},
199+
async () => {
200+
let tst = await wsrun('printthis', { WSRUN_REWRITE_PATHS: 'true' })
201+
expect(tst.output.toString()).toContain('app-x-frontend/src/index.ts')
202+
}
203+
)
204+
})
205+
206+
it('should show an error for pkgs without name', async () => {
207+
await withScaffold(
208+
{
209+
packages: [echo.makePkg({ path: 'packages/p1', dependencies: {} })]
210+
},
195211
async () => {
196212
let tst = await wsrun('doecho')
197213
expect(tst.status).toBeTruthy()
198-
expect(String(tst.output[2])).toEqual('\nERROR: Package in directory packages/p1 has no name in package.json\n')
214+
expect(String(tst.output[2])).toEqual(
215+
'\nERROR: Package in directory packages/p1 has no name in package.json\n'
216+
)
199217
}
200218
)
201219
})

0 commit comments

Comments
 (0)