-
Notifications
You must be signed in to change notification settings - Fork 0
/
turbo-run.ts
68 lines (59 loc) · 2.58 KB
/
turbo-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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import ci from 'ci-info';
import type { ExecaError } from 'execa';
import c from 'picocolors';
import stripAnsi from 'strip-ansi';
import { consola, execaWithOutput, isESMain, logWithBox } from './utils';
export async function runTurboTask(scriptName: string, ...args: string[]) {
try {
await execaWithOutput('turbo', ['run', scriptName, ...args]);
} catch (_error) {
const error = _error as ExecaError;
const match = (error.stdout ?? '')
.trimEnd()
.match(/\nFailed: {4}(?<packageName>[\w\-@/]+)#(?<scriptName>[\w:-]+)/);
if (match?.groups) {
const { packageName, scriptName } = match.groups;
if (packageName && scriptName) {
consola.error(
`执行包 ${c.yellow(packageName)} 的 ${c.yellow(scriptName)} 脚本失败!`,
);
const outputPrefix = `${packageName}:${scriptName}: `;
const errorTaskOutput = error.stdout
.split(/\n/)
.filter((line) => stripAnsi(line).startsWith(outputPrefix))
.map((line) => line.slice(line.indexOf(' ') + 1))
.join('\n');
console.log(
c.yellow('------------------------ 运行输出 -----------------------------\n'),
);
console.log(errorTaskOutput);
console.log(
c.yellow(
'\n----------------------------------------------------------------------\n',
),
);
const command = `pnpm --filter ${packageName} ${scriptName}`;
logWithBox(c.red('可以在本地运行下面命令复现该错误'), c.green(command));
if (ci.isCI) {
const prTittle = process.env.CHANGE_TITLE;
const skipCacheFlag = '[skip cache]';
if (prTittle && !prTittle.includes(skipCacheFlag)) {
console.log();
logWithBox(
c.red(
'若本地运行没有问题,可以尝试修改 pr 标题为下面这样以跳过 CI 缓存',
),
c.green(`${prTittle} ${skipCacheFlag}`),
);
}
}
process.exit(1);
}
}
throw error;
}
}
if (isESMain(import.meta)) {
const taskName = process.argv[2];
await runTurboTask(taskName, ...process.argv.slice(3));
}