typia 3.8.0-dev.20230416 → 3.8.0-dev.20230418

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,120 +1,129 @@
1
- import fs from "fs";
2
- import path from "path";
3
- import ts from "typescript";
4
-
5
- import { ImportTransformer } from "../transformers/ImportTransformer";
6
-
7
- import transform from "../transform";
8
-
9
- export namespace TypiaFileFactory {
10
- export interface IProps {
11
- input: string;
12
- output: string;
13
- project: string;
14
- }
15
-
16
- export async function generate(
17
- props: TypiaFileFactory.IProps,
18
- ): Promise<void> {
19
- props.input = path.resolve(props.input);
20
- props.output = path.resolve(props.output);
21
-
22
- if ((await is_directory(props.input)) === false)
23
- throw new Error(
24
- "Error on TypiaGenerator.generate(): input path is not a directory.",
25
- );
26
- else if (fs.existsSync(props.output) === false)
27
- await fs.promises.mkdir(props.output, { recursive: true });
28
- else if ((await is_directory(props.output)) === false) {
29
- const parent: string = path.join(props.output, "..");
30
- if ((await is_directory(parent)) === false)
31
- throw new Error(
32
- "Error on TypiaGenerator.generate(): output path is not a directory.",
33
- );
34
- await fs.promises.mkdir(props.output);
35
- }
36
-
37
- // CREATE PROGRAM
38
- const { config } = ts.readConfigFile(props.project, ts.sys.readFile);
39
-
40
- const program: ts.Program = ts.createProgram(
41
- await (async () => {
42
- const container: string[] = [];
43
- await gather(props)(container)(props.input)(props.output);
44
- return container;
45
- })(),
46
- config.compilerOptions,
47
- );
48
-
49
- // DO TRANSFORM
50
- const result: ts.TransformationResult<ts.SourceFile> = ts.transform(
51
- program
52
- .getSourceFiles()
53
- .filter(
54
- (file) =>
55
- !file.isDeclarationFile &&
56
- path.resolve(file.fileName).indexOf(props.input) !== -1,
57
- ),
58
- [
59
- ImportTransformer.transform(props.input)(props.output),
60
- transform(
61
- program,
62
- (config.compilerOptions.plugins ?? []).find(
63
- (p: any) =>
64
- p.transform === "typia/lib/transform" ||
65
- p.transform === "../src/transform.ts",
66
- ) ?? {},
67
- ),
68
- ],
69
- program.getCompilerOptions(),
70
- );
71
-
72
- // ARCHIVE TRANSFORMED FILES
73
- const printer: ts.Printer = ts.createPrinter({
74
- newLine: ts.NewLineKind.LineFeed,
75
- });
76
- for (const file of result.transformed) {
77
- const to: string = path
78
- .resolve(file.fileName)
79
- .replace(props.input, props.output);
80
-
81
- const content: string = printer.printFile(file);
82
- await fs.promises.writeFile(to, emend(content), "utf8");
83
- }
84
- }
85
-
86
- const emend = (content: string): string => {
87
- if (
88
- content.indexOf("typia.") === -1 ||
89
- content.indexOf("import typia") !== -1 ||
90
- content.indexOf("import * as typia") !== -1
91
- )
92
- return content;
93
- return `import typia from "typia";\n\n${content}`;
94
- };
95
-
96
- const is_directory = async (current: string): Promise<boolean> => {
97
- const stat: fs.Stats = await fs.promises.stat(current);
98
- return stat.isDirectory();
99
- };
100
-
101
- const gather =
102
- (props: IProps) =>
103
- (container: string[]) =>
104
- (from: string) =>
105
- async (to: string) => {
106
- if (from === props.output) return;
107
- else if (fs.existsSync(to) === false) await fs.promises.mkdir(to);
108
-
109
- for (const file of await fs.promises.readdir(from)) {
110
- const next: string = path.join(from, file);
111
- const stat: fs.Stats = await fs.promises.stat(next);
112
-
113
- if (stat.isDirectory()) {
114
- await gather(props)(container)(next)(path.join(to, file));
115
- continue;
116
- } else if (file.substring(file.length - 3) === ".ts")
117
- container.push(next);
118
- }
119
- };
120
- }
1
+ import fs from "fs";
2
+ import path from "path";
3
+ import ts from "typescript";
4
+
5
+ import { ImportTransformer } from "../transformers/ImportTransformer";
6
+
7
+ import transform from "../transform";
8
+
9
+ export namespace TypiaFileFactory {
10
+ export interface IProps {
11
+ input: string;
12
+ output: string;
13
+ project: string;
14
+ }
15
+
16
+ export async function generate(
17
+ props: TypiaFileFactory.IProps,
18
+ ): Promise<void> {
19
+ props.input = path.resolve(props.input);
20
+ props.output = path.resolve(props.output);
21
+
22
+ if ((await is_directory(props.input)) === false)
23
+ throw new Error(
24
+ "Error on TypiaGenerator.generate(): input path is not a directory.",
25
+ );
26
+ else if (fs.existsSync(props.output) === false)
27
+ await fs.promises.mkdir(props.output, { recursive: true });
28
+ else if ((await is_directory(props.output)) === false) {
29
+ const parent: string = path.join(props.output, "..");
30
+ if ((await is_directory(parent)) === false)
31
+ throw new Error(
32
+ "Error on TypiaGenerator.generate(): output path is not a directory.",
33
+ );
34
+ await fs.promises.mkdir(props.output);
35
+ }
36
+
37
+ // CREATE PROGRAM
38
+ const { options: compilerOptions } = ts.parseJsonConfigFileContent(
39
+ ts.readConfigFile(props.project, ts.sys.readFile).config,
40
+ {
41
+ fileExists: ts.sys.fileExists,
42
+ readFile: ts.sys.readFile,
43
+ readDirectory: ts.sys.readDirectory,
44
+ useCaseSensitiveFileNames: ts.sys.useCaseSensitiveFileNames,
45
+ },
46
+ path.dirname(props.project),
47
+ );
48
+
49
+ const program: ts.Program = ts.createProgram(
50
+ await (async () => {
51
+ const container: string[] = [];
52
+ await gather(props)(container)(props.input)(props.output);
53
+ return container;
54
+ })(),
55
+ compilerOptions,
56
+ );
57
+
58
+ // DO TRANSFORM
59
+ const result: ts.TransformationResult<ts.SourceFile> = ts.transform(
60
+ program
61
+ .getSourceFiles()
62
+ .filter(
63
+ (file) =>
64
+ !file.isDeclarationFile &&
65
+ path.resolve(file.fileName).indexOf(props.input) !== -1,
66
+ ),
67
+ [
68
+ ImportTransformer.transform(props.input)(props.output),
69
+ transform(
70
+ program,
71
+ ((compilerOptions.plugins as any[]) ?? []).find(
72
+ (p: any) =>
73
+ p.transform === "typia/lib/transform" ||
74
+ p.transform === "../src/transform.ts",
75
+ ) ?? {},
76
+ ),
77
+ ],
78
+ program.getCompilerOptions(),
79
+ );
80
+
81
+ // ARCHIVE TRANSFORMED FILES
82
+ const printer: ts.Printer = ts.createPrinter({
83
+ newLine: ts.NewLineKind.LineFeed,
84
+ });
85
+ for (const file of result.transformed) {
86
+ const to: string = path
87
+ .resolve(file.fileName)
88
+ .replace(props.input, props.output);
89
+
90
+ const content: string = printer.printFile(file);
91
+ await fs.promises.writeFile(to, emend(content), "utf8");
92
+ }
93
+ }
94
+
95
+ const emend = (content: string): string => {
96
+ if (
97
+ content.indexOf("typia.") === -1 ||
98
+ content.indexOf("import typia") !== -1 ||
99
+ content.indexOf("import * as typia") !== -1
100
+ )
101
+ return content;
102
+ return `import typia from "typia";\n\n${content}`;
103
+ };
104
+
105
+ const is_directory = async (current: string): Promise<boolean> => {
106
+ const stat: fs.Stats = await fs.promises.stat(current);
107
+ return stat.isDirectory();
108
+ };
109
+
110
+ const gather =
111
+ (props: IProps) =>
112
+ (container: string[]) =>
113
+ (from: string) =>
114
+ async (to: string) => {
115
+ if (from === props.output) return;
116
+ else if (fs.existsSync(to) === false) await fs.promises.mkdir(to);
117
+
118
+ for (const file of await fs.promises.readdir(from)) {
119
+ const next: string = path.join(from, file);
120
+ const stat: fs.Stats = await fs.promises.stat(next);
121
+
122
+ if (stat.isDirectory()) {
123
+ await gather(props)(container)(next)(path.join(to, file));
124
+ continue;
125
+ } else if (file.substring(file.length - 3) === ".ts")
126
+ container.push(next);
127
+ }
128
+ };
129
+ }