-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathtsc.ts
More file actions
80 lines (64 loc) 路 2.21 KB
/
Copy pathtsc.ts
File metadata and controls
80 lines (64 loc) 路 2.21 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import ts from 'typescript';
import { exec } from '../../utils/exec';
import type { Logger } from '../../utils/logging';
import { parseTscArgs } from './args';
const DEFAULT_ARGS = ['--project', 'tsconfig.build.json'] as const;
const formatHost: ts.FormatDiagnosticsHost = {
getCanonicalFileName: (fileName) => fileName,
getCurrentDirectory: ts.sys.getCurrentDirectory.bind(undefined),
getNewLine: () => ts.sys.newLine,
};
const tsconfigCache = new Map<string, ts.ParsedCommandLine>();
const computeCacheKey = (args: string[]) => Array.from(args).sort().toString();
export const tsc = async (args = process.argv.slice(2)) => {
const tscArgs = parseTscArgs(args);
// Build flag is incompatible with project flag.
const defaultArgs = tscArgs.build || tscArgs.project ? [] : DEFAULT_ARGS;
return exec('tsc', ...defaultArgs, ...args);
};
export const readTsconfig = (args = process.argv.slice(2), log: Logger) => {
const tscArgs = parseTscArgs(args);
let parsedCommandLine = tsconfigCache.get(computeCacheKey(args));
if (!parsedCommandLine) {
log.debug(
log.bold(
'tsconfig',
...(tscArgs.project ? ['--project', tscArgs.project] : []),
),
);
log.debug(tscArgs.pathname);
const tsconfigFile = ts.findConfigFile(
tscArgs.dirname,
ts.sys.fileExists.bind(undefined),
tscArgs.basename,
);
if (!tsconfigFile) {
log.err(`Could not find ${tscArgs.pathname}.`);
return;
}
const readConfigFile = ts.readConfigFile(
tsconfigFile,
ts.sys.readFile.bind(undefined),
);
if (readConfigFile.error) {
log.err(`Could not read ${tscArgs.pathname}.`);
log.subtle(ts.formatDiagnostic(readConfigFile.error, formatHost));
return;
}
parsedCommandLine = ts.parseJsonConfigFileContent(
readConfigFile.config,
ts.sys,
tscArgs.dirname,
);
tsconfigCache.set(computeCacheKey(args), parsedCommandLine);
}
if (parsedCommandLine.errors.length) {
log.err(`Could not parse ${tscArgs.pathname}.`);
log.subtle(ts.formatDiagnostics(parsedCommandLine.errors, formatHost));
return;
}
return {
compilerOptions: parsedCommandLine.options,
entryPoints: parsedCommandLine.fileNames,
};
};