Skip to content

Commit 6985bff

Browse files
committed
init recursively searching workspace configs
1 parent 1338a2a commit 6985bff

7 files changed

Lines changed: 73 additions & 6 deletions

File tree

‎example/packages/foo/package.json‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
"postinstall": "echo postinstall",
88
"prepublish": "echo prepublish"
99
},
10+
"pworkspaces": [
11+
"packages/*"
12+
],
1013
"dependencies": {
1114
"react": "^15.6.1",
1215
"bar-workspace-package": "^1.0.0"
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "baz",
3+
"version": "1.0.0"
4+
}

‎package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"globby": "^6.1.0",
3939
"make-dir": "^1.0.0",
4040
"meow": "^3.7.0",
41+
"multimatch": "^2.1.0",
4142
"p-limit": "^1.1.0",
4243
"parse-json": "^2.2.0",
4344
"path-is-inside": "^1.0.2",

‎src/commands/index.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ export {default as normalize} from './normalize';
55
export {default as publish} from './publish';
66
export {default as workspace} from './workspace';
77
export {default as workspaces} from './workspaces';
8+
export {default as run} from './run';

‎src/utils/config.js‎

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import pkgUp from 'pkg-up';
33
import detectIndent from 'detect-indent';
44
import parseJson from 'parse-json';
5+
import multimatch from 'multimatch';
56
import * as path from 'path';
67
import * as fs from './fs';
78
import type {Config} from '../types';
@@ -23,20 +24,48 @@ export async function writeConfigFile(filePath: string, config: Config) {
2324
await fs.writeFile(filePath, contents);
2425
}
2526

26-
export async function getProjectConfig(cwd: string) {
27+
export async function getPackageStack(cwd: string) {
28+
let stack = [];
2729
let searching = cwd;
2830

2931
while (searching) {
3032
let filePath = await findConfigFile(searching);
3133

3234
if (filePath) {
3335
let config = await readConfigFile(filePath);
34-
if (config && config.pworkspaces) {
35-
return filePath;
36-
}
36+
stack.unshift({ filePath, config });
3737
searching = path.dirname(path.dirname(filePath));
3838
} else {
39-
return null;
39+
searching = null;
4040
}
4141
}
42+
43+
return stack;
44+
}
45+
46+
export async function getProjectConfig(cwd: string) {
47+
let stack = await getPackageStack(cwd);
48+
49+
let highest = stack.pop();
50+
let matches = [highest];
51+
52+
while (stack.length) {
53+
let current = stack.pop();
54+
55+
if (current.config.pworkspaces) {
56+
let patterns = current.config.pworkspaces;
57+
let filePaths = matches.map(match => {
58+
return path.relative(path.dirname(current.filePath), path.dirname(match.filePath));
59+
});
60+
61+
let found = multimatch(filePaths, patterns);
62+
63+
if (found.length) {
64+
matches.push(current);
65+
highest = current;
66+
}
67+
}
68+
}
69+
70+
return highest.filePath;
4271
}

‎src/utils/yarn.js‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// @flow
2+
import type Package from '../Package';
3+
import * as processes from './processes';
4+
5+
export async function run(pkg: Package, script: string, args: Array<string> = []) {
6+
let spawnArgs = ['run', script, '-s'];
7+
8+
if (args.length) {
9+
spawnArgs = spawnArgs.concat('--', args);
10+
}
11+
12+
await processes.spawn('yarn', spawnArgs, {
13+
cwd: pkg.dir,
14+
pkg: pkg,
15+
});
16+
}

‎yarn.lock‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@ arr-union@^3.1.0:
105105
version "3.1.0"
106106
resolved "https://registry.yarnpkg.com/arr-union/-/arr-union-3.1.0.tgz#e39b09aea9def866a8f206e288af63919bae39c4"
107107

108+
array-differ@^1.0.0:
109+
version "1.0.0"
110+
resolved "https://registry.yarnpkg.com/array-differ/-/array-differ-1.0.0.tgz#eff52e3758249d33be402b8bb8e564bb2b5d4031"
111+
108112
array-equal@^1.0.0:
109113
version "1.0.0"
110114
resolved "https://registry.yarnpkg.com/array-equal/-/array-equal-1.0.0.tgz#8c2a5ef2472fd9ea742b04c77a75093ba2757c93"
@@ -127,7 +131,7 @@ array-unique@^0.2.1:
127131
version "0.2.1"
128132
resolved "https://registry.yarnpkg.com/array-unique/-/array-unique-0.2.1.tgz#a1d97ccafcbc2625cc70fadceb36a50c58b01a53"
129133

130-
arrify@^1.0.1:
134+
arrify@^1.0.0, arrify@^1.0.1:
131135
version "1.0.1"
132136
resolved "https://registry.yarnpkg.com/arrify/-/arrify-1.0.1.tgz#898508da2226f380df904728456849c1501a4b0d"
133137

@@ -2084,6 +2088,15 @@ ms@2.0.0:
20842088
version "2.0.0"
20852089
resolved "https://registry.yarnpkg.com/ms/-/ms-2.0.0.tgz#5608aeadfc00be6c2901df5f9861788de0d597c8"
20862090

2091+
multimatch@^2.1.0:
2092+
version "2.1.0"
2093+
resolved "https://registry.yarnpkg.com/multimatch/-/multimatch-2.1.0.tgz#9c7906a22fb4c02919e2f5f75161b4cdbd4b2a2b"
2094+
dependencies:
2095+
array-differ "^1.0.0"
2096+
array-union "^1.0.1"
2097+
arrify "^1.0.0"
2098+
minimatch "^3.0.0"
2099+
20872100
nan@^2.3.0:
20882101
version "2.6.2"
20892102
resolved "https://registry.yarnpkg.com/nan/-/nan-2.6.2.tgz#e4ff34e6c95fdfb5aecc08de6596f43605a7db45"

0 commit comments

Comments
 (0)