1 | const fs = require("fs-extra");
|
2 | const ts = require("typescript");
|
3 | const path = require("path");
|
4 |
|
5 | const packageName = process.argv[2];
|
6 | const watch = process.argv[3] === "watch";
|
7 | const tsConfigPath = process.argv[4];
|
8 |
|
9 | const contextPath = path.resolve(process.cwd(), "packages", packageName).replace(/\\/g, "/");
|
10 | const configPath = tsConfigPath || path.resolve(contextPath, "tsconfig.json").replace(/\\/g, "/");
|
11 | const parsedConfig = ts.parseJsonConfigFileContent(fs.readJsonSync(configPath), ts.sys, contextPath);
|
12 | const outDir = parsedConfig.options.outDir || path.resolve(contextPath, "dist");
|
13 |
|
14 | if (watch) {
|
15 | const host = ts.createWatchCompilerHost(
|
16 | configPath,
|
17 | {
|
18 | outDir,
|
19 | sourceMap: false,
|
20 | noEmit: !parsedConfig.options.declaration,
|
21 | emitDeclarationOnly: parsedConfig.options.declaration
|
22 | },
|
23 | {
|
24 | ...ts.sys,
|
25 | writeFile: (filePath, content) => {
|
26 | writeFile(filePath, content);
|
27 | },
|
28 | createDirectory: () => {
|
29 | }
|
30 | },
|
31 | ts.createEmitAndSemanticDiagnosticsBuilderProgram,
|
32 | diagnostic => {
|
33 | printDiagnostic(diagnostic);
|
34 | },
|
35 | () => {
|
36 | }
|
37 | );
|
38 |
|
39 | ts.createWatchProgram(host);
|
40 | }
|
41 | else {
|
42 | const host = ts.createCompilerHost(parsedConfig.options);
|
43 | host.writeFile = (filePath, content) => {
|
44 | writeFile(filePath, content);
|
45 | };
|
46 |
|
47 | const program = ts.createProgram(
|
48 | parsedConfig.fileNames,
|
49 | {
|
50 | ...parsedConfig.options,
|
51 | outDir: outDir,
|
52 | sourceMap: false,
|
53 | noEmit: !parsedConfig.options.declaration,
|
54 | emitDeclarationOnly: parsedConfig.options.declaration
|
55 | },
|
56 | host
|
57 | );
|
58 |
|
59 | let diagnostics = parsedConfig.options.declaration
|
60 | ? ts.getPreEmitDiagnostics(program)
|
61 | : program.getSemanticDiagnostics();
|
62 |
|
63 | if (parsedConfig.options.declaration) {
|
64 | diagnostics = diagnostics.concat(program.emit(undefined, undefined, undefined, true).diagnostics);
|
65 | }
|
66 | else {
|
67 | diagnostics = diagnostics.concat(program.getSyntacticDiagnostics());
|
68 | }
|
69 |
|
70 | for (const diagnostic of diagnostics) {
|
71 | printDiagnostic(diagnostic);
|
72 | }
|
73 | }
|
74 |
|
75 | function writeFile(filePath, content) {
|
76 | if (!parsedConfig.options.declaration) return;
|
77 |
|
78 | let newFilePath = filePath.replace(/\\/g, "/");
|
79 | if (newFilePath.includes("src")) {
|
80 | const prevOutDir = path.resolve(outDir, packageName, "src").replace(/\\/g, "/");
|
81 |
|
82 | if (!newFilePath.startsWith(prevOutDir)) {
|
83 | return;
|
84 | }
|
85 |
|
86 | newFilePath = path.resolve(outDir, path.relative(prevOutDir, filePath));
|
87 | }
|
88 |
|
89 | fs.mkdirsSync(path.dirname(newFilePath));
|
90 | fs.writeFileSync(newFilePath, content);
|
91 | }
|
92 |
|
93 | function printDiagnostic(diagnostic) {
|
94 | if (diagnostic.file) {
|
95 | if (diagnostic.file.fileName.startsWith(contextPath.replace(/\\/g, "/"))) {
|
96 | const position = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start);
|
97 | const tsMessage = ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n");
|
98 | const message = `${diagnostic.file.fileName}(${position.line + 1},${position.character + 1}): error: ${tsMessage}`;
|
99 | sendMessage(message);
|
100 | }
|
101 | }
|
102 | else {
|
103 | const message = `error: ${ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n")}`;
|
104 | sendMessage(message);
|
105 | }
|
106 | }
|
107 |
|
108 | function sendMessage(message) {
|
109 | process.send(message, err => {
|
110 | if (err) throw err;
|
111 | });
|
112 | } |
\ | No newline at end of file |