typia 5.2.0 → 5.2.1

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,169 +1,169 @@
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 TypiaProgrammer {
10
- export interface IProps {
11
- input: string;
12
- output: string;
13
- project: string;
14
- }
15
-
16
- export const build = async (
17
- props: TypiaProgrammer.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 URIError(
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 URIError(
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 diagnostics: ts.Diagnostic[] = [];
60
- const result: ts.TransformationResult<ts.SourceFile> = ts.transform(
61
- program
62
- .getSourceFiles()
63
- .filter(
64
- (file) =>
65
- !file.isDeclarationFile &&
66
- path.resolve(file.fileName).indexOf(props.input) !== -1,
67
- ),
68
- [
69
- ImportTransformer.transform(props.input)(props.output),
70
- transform(
71
- program,
72
- ((compilerOptions.plugins as any[]) ?? []).find(
73
- (p: any) =>
74
- p.transform === "typia/lib/transform" ||
75
- p.transform === "../src/transform.ts",
76
- ) ?? {},
77
- {
78
- addDiagnostic: (diag) => diagnostics.push(diag),
79
- },
80
- ),
81
- ],
82
- program.getCompilerOptions(),
83
- );
84
-
85
- // TRACE ERRORS
86
- for (const diag of diagnostics) {
87
- const file: string = diag.file
88
- ? path.relative(diag.file.fileName, process.cwd())
89
- : "(unknown file)";
90
- const category: string =
91
- diag.category === ts.DiagnosticCategory.Warning
92
- ? "warning"
93
- : diag.category === ts.DiagnosticCategory.Error
94
- ? "error"
95
- : diag.category === ts.DiagnosticCategory.Suggestion
96
- ? "suggestion"
97
- : diag.category === ts.DiagnosticCategory.Message
98
- ? "message"
99
- : "unkown";
100
- const [line, pos] = diag.file
101
- ? (() => {
102
- const lines: string[] = diag
103
- .file!.text.substring(0, diag.start)
104
- .split("\n");
105
- if (lines.length === 0) return [0, 0];
106
- return [lines.length, lines.at(-1)!.length + 1];
107
- })()
108
- : [0, 0];
109
- console.error(
110
- `${file}:${line}:${pos} - ${category} TS${diag.code}: ${diag.messageText}`,
111
- );
112
- }
113
- if (diagnostics.length) process.exit(-1);
114
-
115
- // ARCHIVE TRANSFORMED FILES
116
- const printer: ts.Printer = ts.createPrinter({
117
- newLine: ts.NewLineKind.LineFeed,
118
- });
119
- for (const file of result.transformed) {
120
- const to: string = path
121
- .resolve(file.fileName)
122
- .replace(props.input, props.output);
123
-
124
- const content: string = printer.printFile(file);
125
- await fs.promises.writeFile(to, emend(content), "utf8");
126
- }
127
- };
128
-
129
- const emend = (content: string): string => {
130
- if (
131
- content.indexOf("typia.") === -1 ||
132
- content.indexOf("import typia") !== -1 ||
133
- content.indexOf("import * as typia") !== -1
134
- )
135
- return content;
136
- return `import typia from "typia";\n\n${content}`;
137
- };
138
-
139
- const is_directory = async (current: string): Promise<boolean> => {
140
- const stat: fs.Stats = await fs.promises.stat(current);
141
- return stat.isDirectory();
142
- };
143
-
144
- const gather =
145
- (props: IProps) =>
146
- (container: string[]) =>
147
- (from: string) =>
148
- async (to: string) => {
149
- if (from === props.output) return;
150
- else if (fs.existsSync(to) === false) await fs.promises.mkdir(to);
151
-
152
- for (const file of await fs.promises.readdir(from)) {
153
- const next: string = path.join(from, file);
154
- const stat: fs.Stats = await fs.promises.stat(next);
155
-
156
- if (stat.isDirectory()) {
157
- await gather(props)(container)(next)(path.join(to, file));
158
- continue;
159
- } else if (is_supported_extension(file)) container.push(next);
160
- }
161
- };
162
-
163
- const is_supported_extension = (filename: string): boolean => {
164
- return (
165
- (filename.endsWith(".ts") && !filename.endsWith(".d.ts")) ||
166
- (filename.endsWith(".tsx") && !filename.endsWith(".d.tsx"))
167
- );
168
- };
169
- }
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 TypiaProgrammer {
10
+ export interface IProps {
11
+ input: string;
12
+ output: string;
13
+ project: string;
14
+ }
15
+
16
+ export const build = async (
17
+ props: TypiaProgrammer.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 URIError(
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 URIError(
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 diagnostics: ts.Diagnostic[] = [];
60
+ const result: ts.TransformationResult<ts.SourceFile> = ts.transform(
61
+ program
62
+ .getSourceFiles()
63
+ .filter(
64
+ (file) =>
65
+ !file.isDeclarationFile &&
66
+ path.resolve(file.fileName).indexOf(props.input) !== -1,
67
+ ),
68
+ [
69
+ ImportTransformer.transform(props.input)(props.output),
70
+ transform(
71
+ program,
72
+ ((compilerOptions.plugins as any[]) ?? []).find(
73
+ (p: any) =>
74
+ p.transform === "typia/lib/transform" ||
75
+ p.transform === "../src/transform.ts",
76
+ ) ?? {},
77
+ {
78
+ addDiagnostic: (diag) => diagnostics.push(diag),
79
+ },
80
+ ),
81
+ ],
82
+ program.getCompilerOptions(),
83
+ );
84
+
85
+ // TRACE ERRORS
86
+ for (const diag of diagnostics) {
87
+ const file: string = diag.file
88
+ ? path.relative(diag.file.fileName, process.cwd())
89
+ : "(unknown file)";
90
+ const category: string =
91
+ diag.category === ts.DiagnosticCategory.Warning
92
+ ? "warning"
93
+ : diag.category === ts.DiagnosticCategory.Error
94
+ ? "error"
95
+ : diag.category === ts.DiagnosticCategory.Suggestion
96
+ ? "suggestion"
97
+ : diag.category === ts.DiagnosticCategory.Message
98
+ ? "message"
99
+ : "unkown";
100
+ const [line, pos] = diag.file
101
+ ? (() => {
102
+ const lines: string[] = diag
103
+ .file!.text.substring(0, diag.start)
104
+ .split("\n");
105
+ if (lines.length === 0) return [0, 0];
106
+ return [lines.length, lines.at(-1)!.length + 1];
107
+ })()
108
+ : [0, 0];
109
+ console.error(
110
+ `${file}:${line}:${pos} - ${category} TS${diag.code}: ${diag.messageText}`,
111
+ );
112
+ }
113
+ if (diagnostics.length) process.exit(-1);
114
+
115
+ // ARCHIVE TRANSFORMED FILES
116
+ const printer: ts.Printer = ts.createPrinter({
117
+ newLine: ts.NewLineKind.LineFeed,
118
+ });
119
+ for (const file of result.transformed) {
120
+ const to: string = path
121
+ .resolve(file.fileName)
122
+ .replace(props.input, props.output);
123
+
124
+ const content: string = printer.printFile(file);
125
+ await fs.promises.writeFile(to, emend(content), "utf8");
126
+ }
127
+ };
128
+
129
+ const emend = (content: string): string => {
130
+ if (
131
+ content.indexOf("typia.") === -1 ||
132
+ content.indexOf("import typia") !== -1 ||
133
+ content.indexOf("import * as typia") !== -1
134
+ )
135
+ return content;
136
+ return `import typia from "typia";\n\n${content}`;
137
+ };
138
+
139
+ const is_directory = async (current: string): Promise<boolean> => {
140
+ const stat: fs.Stats = await fs.promises.stat(current);
141
+ return stat.isDirectory();
142
+ };
143
+
144
+ const gather =
145
+ (props: IProps) =>
146
+ (container: string[]) =>
147
+ (from: string) =>
148
+ async (to: string) => {
149
+ if (from === props.output) return;
150
+ else if (fs.existsSync(to) === false) await fs.promises.mkdir(to);
151
+
152
+ for (const file of await fs.promises.readdir(from)) {
153
+ const next: string = path.join(from, file);
154
+ const stat: fs.Stats = await fs.promises.stat(next);
155
+
156
+ if (stat.isDirectory()) {
157
+ await gather(props)(container)(next)(path.join(to, file));
158
+ continue;
159
+ } else if (is_supported_extension(file)) container.push(next);
160
+ }
161
+ };
162
+
163
+ const is_supported_extension = (filename: string): boolean => {
164
+ return (
165
+ (filename.endsWith(".ts") && !filename.endsWith(".d.ts")) ||
166
+ (filename.endsWith(".tsx") && !filename.endsWith(".d.tsx"))
167
+ );
168
+ };
169
+ }
@@ -1,15 +1,15 @@
1
- import { ITransformOptions } from "../../transformers/ITransformOptions";
2
-
3
- export namespace OptionPredicator {
4
- export const numeric = (options: ITransformOptions): boolean =>
5
- finite(options) || options.numeric === true;
6
-
7
- export const functional = (options: ITransformOptions): boolean =>
8
- options.functional === true;
9
-
10
- export const finite = (options: ITransformOptions): boolean =>
11
- options.finite === true;
12
-
13
- export const undefined = (options: ITransformOptions): boolean =>
14
- options.undefined !== false;
15
- }
1
+ import { ITransformOptions } from "../../transformers/ITransformOptions";
2
+
3
+ export namespace OptionPredicator {
4
+ export const numeric = (options: ITransformOptions): boolean =>
5
+ finite(options) || options.numeric === true;
6
+
7
+ export const functional = (options: ITransformOptions): boolean =>
8
+ options.functional === true;
9
+
10
+ export const finite = (options: ITransformOptions): boolean =>
11
+ options.finite === true;
12
+
13
+ export const undefined = (options: ITransformOptions): boolean =>
14
+ options.undefined !== false;
15
+ }