-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathrun.ts
51 lines (46 loc) · 1.4 KB
/
run.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import { getPackages } from "@manypkg/get-packages";
import path from "path";
import spawn from "spawndamnit";
import * as logger from "./logger";
import { ExitError } from "./errors";
export async function runCmd(args: string[], cwd: string) {
let { packages, root } = await getPackages(cwd);
const exactMatchingPackage = packages.find(pkg => {
return (
pkg.packageJson.name === args[0] ||
path.relative(root.dir, pkg.dir) === args[0]
);
});
if (exactMatchingPackage) {
const { code } = await spawn("yarn", args.slice(1), {
cwd: exactMatchingPackage.dir,
stdio: "inherit"
});
throw new ExitError(code);
}
const matchingPackages = packages.filter(pkg => {
return (
pkg.packageJson.name.includes(args[0]) ||
path.relative(root.dir, pkg.dir).includes(args[0])
);
});
if (matchingPackages.length > 1) {
logger.error(
`an identifier must only match a single package but "${
args[0]
}" matches the following packages: \n${matchingPackages
.map(x => x.packageJson.name)
.join("\n")}`
);
throw new ExitError(1);
} else if (matchingPackages.length === 0) {
logger.error("No matching packages found");
throw new ExitError(1);
} else {
const { code } = await spawn("yarn", args.slice(1), {
cwd: matchingPackages[0].dir,
stdio: "inherit"
});
throw new ExitError(code);
}
}