-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathcompile.ts
More file actions
55 lines (48 loc) · 1.66 KB
/
compile.ts
File metadata and controls
55 lines (48 loc) · 1.66 KB
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
import { resolve } from 'path';
import * as typescript from './typescript';
import { isFile } from '../../common/utils';
/**
* @param tscOutput
* Converts
* TscOutput [
"fileA(10,15): error TS2532: Object is possibly 'undefined'.",
"fileA(11,15): error TS2532: Object is possibly 'undefined'.",
"fileB(14,15): error TS2532: Object is possibly 'undefined'.",
"fileB(15,15): error TS2532: Object is possibly 'undefined'."
]
to:
Map(2) {
'/Users/User/project/src/fileA.ts' => [
"fileA(10,15): error TS2532: Object is possibly 'undefined'.",
"fileA(11,15): error TS2532: Object is possibly 'undefined'."
],
'/Users/User/project/src/fileB.ts' => [
"fileB(14,15): error TS2532: Object is possibly 'undefined'.",
"fileB(15,15): error TS2532: Object is possibly 'undefined'."
]
}
*/
function getPathToErrorsMap(tscOutput: string[]): Map<string, string[]> {
const result = new Map<string, string[]>();
tscOutput.forEach((error) => {
const match = error.match(/^(.*?)(?=\(\d+,\d+\))/);
const beforePattern = match ? match[1] : error;
const path = resolve(process.cwd(), beforePattern);
if (result.has(path)) {
result.set(path, [...result.get(path)!, error]);
} else {
result.set(path, [error]);
}
});
return result;
}
export async function compile(): Promise<Map<string, string[]>> {
const tscOutput: string[] = (await typescript.compile())
.split(/\r?\n/)
.filter((it) => !isFile(it));
if (tscOutput.some((it) => it.startsWith('error'))) {
console.log(`💥 Typescript did not compile due to some errors. Errors: `, tscOutput);
process.exit(1);
}
return getPathToErrorsMap(tscOutput);
}