-
-
Notifications
You must be signed in to change notification settings - Fork 163
/
TypiaProgrammer.ts
163 lines (147 loc) · 5.92 KB
/
TypiaProgrammer.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import fs from "fs";
import path from "path";
import ts from "typescript";
import { ImportTransformer } from "../transformers/ImportTransformer";
import transform from "../transform";
export namespace TypiaProgrammer {
export interface IProps {
input: string;
output: string;
project: string;
}
export const build = async (
props: TypiaProgrammer.IProps,
): Promise<void> => {
props.input = path.resolve(props.input);
props.output = path.resolve(props.output);
if ((await is_directory(props.input)) === false)
throw new URIError(
"Error on TypiaGenerator.generate(): input path is not a directory.",
);
else if (fs.existsSync(props.output) === false)
await fs.promises.mkdir(props.output, { recursive: true });
else if ((await is_directory(props.output)) === false) {
const parent: string = path.join(props.output, "..");
if ((await is_directory(parent)) === false)
throw new URIError(
"Error on TypiaGenerator.generate(): output path is not a directory.",
);
await fs.promises.mkdir(props.output);
}
// CREATE PROGRAM
const { options: compilerOptions } = ts.parseJsonConfigFileContent(
ts.readConfigFile(props.project, ts.sys.readFile).config,
{
fileExists: ts.sys.fileExists,
readFile: ts.sys.readFile,
readDirectory: ts.sys.readDirectory,
useCaseSensitiveFileNames: ts.sys.useCaseSensitiveFileNames,
},
path.dirname(props.project),
);
const program: ts.Program = ts.createProgram(
await (async () => {
const container: string[] = [];
await gather(props)(container)(props.input)(props.output);
return container;
})(),
compilerOptions,
);
// DO TRANSFORM
const diagnostics: ts.Diagnostic[] = [];
const result: ts.TransformationResult<ts.SourceFile> = ts.transform(
program
.getSourceFiles()
.filter(
(file) =>
!file.isDeclarationFile &&
path.resolve(file.fileName).indexOf(props.input) !== -1,
),
[
ImportTransformer.transform(props.input)(props.output),
transform(
program,
((compilerOptions.plugins as any[]) ?? []).find(
(p: any) =>
p.transform === "typia/lib/transform" ||
p.transform === "../src/transform.ts",
) ?? {},
{
addDiagnostic: (diag) => diagnostics.push(diag),
},
),
],
program.getCompilerOptions(),
);
// TRACE ERRORS
for (const diag of diagnostics) {
const file: string = diag.file
? path.relative(diag.file.fileName, process.cwd())
: "(unknown file)";
const category: string =
diag.category === ts.DiagnosticCategory.Warning
? "warning"
: diag.category === ts.DiagnosticCategory.Error
? "error"
: diag.category === ts.DiagnosticCategory.Suggestion
? "suggestion"
: diag.category === ts.DiagnosticCategory.Message
? "message"
: "unkown";
const [line, pos] = diag.file
? (() => {
const lines: string[] = diag
.file!.text.substring(0, diag.start)
.split("\n");
if (lines.length === 0) return [0, 0];
return [lines.length, lines.at(-1)!.length + 1];
})()
: [0, 0];
console.error(
`${file}:${line}:${pos} - ${category} TS${diag.code}: ${diag.messageText}`,
);
}
if (diagnostics.length) process.exit(-1);
// ARCHIVE TRANSFORMED FILES
const printer: ts.Printer = ts.createPrinter({
newLine: ts.NewLineKind.LineFeed,
});
for (const file of result.transformed) {
const to: string = path
.resolve(file.fileName)
.replace(props.input, props.output);
const content: string = printer.printFile(file);
await fs.promises.writeFile(to, emend(content), "utf8");
}
};
const emend = (content: string): string => {
if (
content.indexOf("typia.") === -1 ||
content.indexOf("import typia") !== -1 ||
content.indexOf("import * as typia") !== -1
)
return content;
return `import typia from "typia";\n\n${content}`;
};
const is_directory = async (current: string): Promise<boolean> => {
const stat: fs.Stats = await fs.promises.stat(current);
return stat.isDirectory();
};
const gather =
(props: IProps) =>
(container: string[]) =>
(from: string) =>
async (to: string) => {
if (from === props.output) return;
else if (fs.existsSync(to) === false) await fs.promises.mkdir(to);
for (const file of await fs.promises.readdir(from)) {
const next: string = path.join(from, file);
const stat: fs.Stats = await fs.promises.stat(next);
if (stat.isDirectory()) {
await gather(props)(container)(next)(path.join(to, file));
continue;
} else if (file.substring(file.length - 3) === ".ts")
container.push(next);
}
};
}