@nestia/migrate 0.5.1 → 0.6.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.
Files changed (103) hide show
  1. package/lib/{NestiaMigrateApplication.d.ts → MigrateApplication.d.ts} +1 -1
  2. package/lib/{NestiaMigrateApplication.js → MigrateApplication.js} +38 -32
  3. package/lib/MigrateApplication.js.map +1 -0
  4. package/lib/analyzers/ControllerAnalyzer.d.ts +5 -0
  5. package/lib/{programmers/ControllerProgrammer.js → analyzers/ControllerAnalyzer.js} +8 -32
  6. package/lib/analyzers/ControllerAnalyzer.js.map +1 -0
  7. package/lib/analyzers/MethodAnalyzer.d.ts +9 -0
  8. package/lib/{programmers/RouteProgrammer.js → analyzers/MethodAnalyzer.js} +30 -134
  9. package/lib/analyzers/MethodAnalyzer.js.map +1 -0
  10. package/lib/analyzers/MigrateAnalyzer.d.ts +4 -0
  11. package/lib/analyzers/MigrateAnalyzer.js +12 -0
  12. package/lib/analyzers/MigrateAnalyzer.js.map +1 -0
  13. package/lib/analyzers/RouteAnalyzer.d.ts +0 -0
  14. package/lib/analyzers/RouteAnalyzer.js +2 -0
  15. package/lib/analyzers/RouteAnalyzer.js.map +1 -0
  16. package/lib/archivers/FileArchiver.js.map +1 -1
  17. package/lib/bundles/TEMPLATE.js +10 -10
  18. package/lib/bundles/TEMPLATE.js.map +1 -1
  19. package/lib/executable/bundle.js.map +1 -1
  20. package/lib/executable/migrate.js +7 -7
  21. package/lib/executable/migrate.js.map +1 -1
  22. package/lib/module.d.ts +1 -1
  23. package/lib/module.js +1 -1
  24. package/lib/module.js.map +1 -1
  25. package/lib/programmers/DtoProgrammer.d.ts +8 -4
  26. package/lib/programmers/DtoProgrammer.js +36 -77
  27. package/lib/programmers/DtoProgrammer.js.map +1 -1
  28. package/lib/programmers/ImportProgrammer.d.ts +5 -5
  29. package/lib/programmers/ImportProgrammer.js +27 -19
  30. package/lib/programmers/ImportProgrammer.js.map +1 -1
  31. package/lib/programmers/{ControllerProgrammer.d.ts → NestControllerProgrammer.d.ts} +3 -4
  32. package/lib/programmers/NestControllerProgrammer.js +30 -0
  33. package/lib/programmers/NestControllerProgrammer.js.map +1 -0
  34. package/lib/programmers/NestMethodProgrammer.d.ts +7 -0
  35. package/lib/programmers/NestMethodProgrammer.js +103 -0
  36. package/lib/programmers/NestMethodProgrammer.js.map +1 -0
  37. package/lib/programmers/NestModuleProgrammer.d.ts +5 -0
  38. package/lib/programmers/NestModuleProgrammer.js +29 -0
  39. package/lib/programmers/NestModuleProgrammer.js.map +1 -0
  40. package/lib/programmers/NestProgrammer.d.ts +5 -0
  41. package/lib/programmers/NestProgrammer.js +60 -0
  42. package/lib/programmers/NestProgrammer.js.map +1 -0
  43. package/lib/programmers/SchemaProgrammer.d.ts +2 -1
  44. package/lib/programmers/SchemaProgrammer.js +122 -189
  45. package/lib/programmers/SchemaProgrammer.js.map +1 -1
  46. package/lib/structures/IMigrateProgram.d.ts +2 -2
  47. package/lib/structures/ISwaggerInfo.d.ts +3 -3
  48. package/lib/utils/FilePrinter.d.ts +9 -0
  49. package/lib/utils/FilePrinter.js +25 -0
  50. package/lib/utils/FilePrinter.js.map +1 -0
  51. package/lib/utils/JsonTypeChecker.d.ts +3 -1
  52. package/lib/utils/JsonTypeChecker.js +31 -18
  53. package/lib/utils/JsonTypeChecker.js.map +1 -1
  54. package/lib/utils/MapUtil.js.map +1 -1
  55. package/lib/utils/SetupWizard.js.map +1 -1
  56. package/lib/utils/StringUtil.js.map +1 -1
  57. package/package.json +8 -6
  58. package/src/MigrateApplication.ts +73 -0
  59. package/src/analyzers/ControllerAnalyzer.ts +107 -0
  60. package/src/analyzers/MethodAnalyzer.ts +315 -0
  61. package/src/analyzers/MigrateAnalyzer.ts +9 -0
  62. package/src/analyzers/RouteAnalyzer.ts +0 -0
  63. package/src/archivers/FileArchiver.ts +35 -38
  64. package/src/bundles/TEMPLATE.ts +10 -10
  65. package/src/executable/bundle.ts +72 -78
  66. package/src/executable/migrate.ts +59 -60
  67. package/src/index.ts +4 -4
  68. package/src/module.ts +4 -4
  69. package/src/programmers/DtoProgrammer.ts +74 -118
  70. package/src/programmers/ImportProgrammer.ts +98 -60
  71. package/src/programmers/NestControllerProgrammer.ts +47 -0
  72. package/src/programmers/NestMethodProgrammer.ts +211 -0
  73. package/src/programmers/NestModuleProgrammer.ts +62 -0
  74. package/src/programmers/NestProgrammer.ts +74 -0
  75. package/src/programmers/SchemaProgrammer.ts +247 -339
  76. package/src/structures/IMigrateController.ts +8 -8
  77. package/src/structures/IMigrateDto.ts +8 -8
  78. package/src/structures/IMigrateFile.ts +5 -5
  79. package/src/structures/IMigrateProgram.ts +7 -7
  80. package/src/structures/IMigrateRoute.ts +36 -36
  81. package/src/structures/IMigrateSchema.ts +4 -4
  82. package/src/structures/ISwaggeSchema.ts +82 -82
  83. package/src/structures/ISwagger.ts +20 -20
  84. package/src/structures/ISwaggerComponents.ts +7 -7
  85. package/src/structures/ISwaggerInfo.ts +57 -57
  86. package/src/structures/ISwaggerRoute.ts +52 -52
  87. package/src/structures/ISwaggerSecurity.ts +47 -47
  88. package/src/utils/FilePrinter.ts +36 -0
  89. package/src/utils/JsonTypeChecker.ts +67 -52
  90. package/src/utils/MapUtil.ts +13 -13
  91. package/src/utils/SetupWizard.ts +15 -15
  92. package/src/utils/StringUtil.ts +51 -51
  93. package/lib/NestiaMigrateApplication.js.map +0 -1
  94. package/lib/programmers/ControllerProgrammer.js.map +0 -1
  95. package/lib/programmers/MigrateProgrammer.d.ts +0 -8
  96. package/lib/programmers/MigrateProgrammer.js +0 -48
  97. package/lib/programmers/MigrateProgrammer.js.map +0 -1
  98. package/lib/programmers/RouteProgrammer.d.ts +0 -13
  99. package/lib/programmers/RouteProgrammer.js.map +0 -1
  100. package/src/NestiaMigrateApplication.ts +0 -73
  101. package/src/programmers/ControllerProgrammer.ts +0 -157
  102. package/src/programmers/MigrateProgrammer.ts +0 -62
  103. package/src/programmers/RouteProgrammer.ts +0 -506
@@ -1,78 +1,72 @@
1
- import cp from "child_process";
2
- import fs from "fs";
3
-
4
- import { IMigrateFile } from "../structures/IMigrateFile";
5
-
6
- const ROOT = __dirname + "/../..";
7
- const ASSETS = ROOT + "/assets";
8
- const TEMPLATE = ASSETS + "/template";
9
-
10
- const clone = async (): Promise<void> => {
11
- console.log("Preparing bundles...");
12
-
13
- // CLONE REPOSITORY
14
- if (fs.existsSync(TEMPLATE))
15
- await fs.promises.rm(TEMPLATE, { recursive: true });
16
- else
17
- try {
18
- await fs.promises.mkdir(ASSETS);
19
- } catch {}
20
-
21
- cp.execSync(
22
- "git clone https://github.com/samchon/nestia-template template",
23
- {
24
- cwd: __dirname + "/../../assets",
25
- },
26
- );
27
-
28
- // REMOVE VUNLERABLE FILES
29
- for (const path of [
30
- `${TEMPLATE}/.git`,
31
- `${TEMPLATE}/src/api`,
32
- `${TEMPLATE}/src/controllers`,
33
- `${TEMPLATE}/src/providers`,
34
- `${TEMPLATE}/test/features`,
35
- ])
36
- await fs.promises.rm(path, { recursive: true });
37
- };
38
-
39
- const iterate = (collection: IMigrateFile[]) => async (location: string) => {
40
- const directory: string[] = await fs.promises.readdir(location);
41
- for (const file of directory) {
42
- const absolute: string = location + "/" + file;
43
- const stats: fs.Stats = await fs.promises.stat(absolute);
44
- if (stats.isDirectory()) await iterate(collection)(absolute);
45
- else {
46
- const content: string = await fs.promises.readFile(
47
- absolute,
48
- "utf-8",
49
- );
50
- collection.push({
51
- location: location.replace(TEMPLATE, ""),
52
- file,
53
- content,
54
- });
55
- }
56
- }
57
- };
58
-
59
- const archive = async (collection: IMigrateFile[]): Promise<void> => {
60
- const body: string = JSON.stringify(collection, null, 4);
61
- const content: string = `export const TEMPLATE = ${body}`;
62
-
63
- try {
64
- await fs.promises.mkdir(`${ROOT}/src/bundles`);
65
- } catch {}
66
- await fs.promises.writeFile(`${ROOT}/src/bundles/TEMPLATE.ts`, content);
67
- };
68
-
69
- const main = async (): Promise<void> => {
70
- const collection: IMigrateFile[] = [];
71
- await clone();
72
- await iterate(collection)(TEMPLATE);
73
- await archive(collection);
74
- };
75
- main().catch((exp) => {
76
- console.error(exp);
77
- process.exit(-1);
78
- });
1
+ import cp from "child_process";
2
+ import fs from "fs";
3
+
4
+ import { IMigrateFile } from "../structures/IMigrateFile";
5
+
6
+ const ROOT = __dirname + "/../..";
7
+ const ASSETS = ROOT + "/assets";
8
+ const TEMPLATE = ASSETS + "/template";
9
+
10
+ const clone = async (): Promise<void> => {
11
+ console.log("Preparing bundles...");
12
+
13
+ // CLONE REPOSITORY
14
+ if (fs.existsSync(TEMPLATE))
15
+ await fs.promises.rm(TEMPLATE, { recursive: true });
16
+ else
17
+ try {
18
+ await fs.promises.mkdir(ASSETS);
19
+ } catch {}
20
+
21
+ cp.execSync("git clone https://github.com/samchon/nestia-template template", {
22
+ cwd: __dirname + "/../../assets",
23
+ });
24
+
25
+ // REMOVE VUNLERABLE FILES
26
+ for (const path of [
27
+ `${TEMPLATE}/.git`,
28
+ `${TEMPLATE}/src/api`,
29
+ `${TEMPLATE}/src/controllers`,
30
+ `${TEMPLATE}/src/providers`,
31
+ `${TEMPLATE}/test/features`,
32
+ ])
33
+ await fs.promises.rm(path, { recursive: true });
34
+ };
35
+
36
+ const iterate = (collection: IMigrateFile[]) => async (location: string) => {
37
+ const directory: string[] = await fs.promises.readdir(location);
38
+ for (const file of directory) {
39
+ const absolute: string = location + "/" + file;
40
+ const stats: fs.Stats = await fs.promises.stat(absolute);
41
+ if (stats.isDirectory()) await iterate(collection)(absolute);
42
+ else {
43
+ const content: string = await fs.promises.readFile(absolute, "utf-8");
44
+ collection.push({
45
+ location: location.replace(TEMPLATE, ""),
46
+ file,
47
+ content,
48
+ });
49
+ }
50
+ }
51
+ };
52
+
53
+ const archive = async (collection: IMigrateFile[]): Promise<void> => {
54
+ const body: string = JSON.stringify(collection, null, 4);
55
+ const content: string = `export const TEMPLATE = ${body}`;
56
+
57
+ try {
58
+ await fs.promises.mkdir(`${ROOT}/src/bundles`);
59
+ } catch {}
60
+ await fs.promises.writeFile(`${ROOT}/src/bundles/TEMPLATE.ts`, content);
61
+ };
62
+
63
+ const main = async (): Promise<void> => {
64
+ const collection: IMigrateFile[] = [];
65
+ await clone();
66
+ await iterate(collection)(TEMPLATE);
67
+ await archive(collection);
68
+ };
69
+ main().catch((exp) => {
70
+ console.error(exp);
71
+ process.exit(-1);
72
+ });
@@ -1,60 +1,59 @@
1
- #!/usr/bin/env node
2
- import fs from "fs";
3
- import path from "path";
4
-
5
- import { NestiaMigrateApplication } from "../NestiaMigrateApplication";
6
- import { ISwagger } from "../structures/ISwagger";
7
- import { SetupWizard } from "../utils/SetupWizard";
8
-
9
- const USAGE = `Wrong command has been detected. Use like below:
10
-
11
- npx @nestia/migrate [input] [output]
12
-
13
- ex) npx @nestia/migrate swagger.json my-new-project
14
- `;
15
-
16
- function halt(desc: string): never {
17
- console.error(desc);
18
- process.exit(-1);
19
- }
20
-
21
- const main = (argv: string[]) => {
22
- const resolve = (str: string | undefined) =>
23
- str ? path.resolve(str).split("\\").join("/") : undefined;
24
- const input: string | undefined = resolve(argv[0]);
25
- const output: string | undefined = resolve(argv[1]);
26
-
27
- // VALIDATE ARGUMENTS
28
- if (input === undefined || output === undefined) halt(USAGE);
29
-
30
- // VALIDATE OUTPUT DIRECTORY
31
- const parent: string = resolve(output + "/..")!;
32
- if (fs.existsSync(output)) halt("Output directory alreay exists.");
33
- else if (fs.existsSync(parent) === false)
34
- halt("Output directory's parent directory does not exist.");
35
- else if (fs.statSync(parent).isDirectory() === false)
36
- halt("Output directory's parent is not a directory.");
37
-
38
- // READ SWAGGER
39
- const swagger: ISwagger = (() => {
40
- if (fs.existsSync(input) === false)
41
- halt("Unable to find the input swagger.json file.");
42
- const stats: fs.Stats = fs.statSync(input);
43
- if (stats.isFile() === false)
44
- halt("The input swagger.json is not a file.");
45
- const content: string = fs.readFileSync(input, "utf-8");
46
- const swagger: ISwagger = JSON.parse(content);
47
- return swagger;
48
- })();
49
-
50
- // DO GENERATE
51
- const app = new NestiaMigrateApplication(swagger);
52
- app.generate({
53
- mkdir: fs.mkdirSync,
54
- writeFile: (path, content) => fs.writeFileSync(path, content, "utf8"),
55
- })(output);
56
-
57
- // RUN SCRIPTS
58
- SetupWizard.setup(output);
59
- };
60
- main(process.argv.slice(2));
1
+ #!/usr/bin/env node
2
+ import fs from "fs";
3
+ import path from "path";
4
+
5
+ import { MigrateApplication } from "../MigrateApplication";
6
+ import { ISwagger } from "../structures/ISwagger";
7
+ import { SetupWizard } from "../utils/SetupWizard";
8
+
9
+ const USAGE = `Wrong command has been detected. Use like below:
10
+
11
+ npx @nestia/migrate [input] [output]
12
+
13
+ ex) npx @nestia/migrate swagger.json my-new-project
14
+ `;
15
+
16
+ function halt(desc: string): never {
17
+ console.error(desc);
18
+ process.exit(-1);
19
+ }
20
+
21
+ const main = (argv: string[]) => {
22
+ const resolve = (str: string | undefined) =>
23
+ str ? path.resolve(str).split("\\").join("/") : undefined;
24
+ const input: string | undefined = resolve(argv[0]);
25
+ const output: string | undefined = resolve(argv[1]);
26
+
27
+ // VALIDATE ARGUMENTS
28
+ if (input === undefined || output === undefined) halt(USAGE);
29
+
30
+ // VALIDATE OUTPUT DIRECTORY
31
+ const parent: string = resolve(output + "/..")!;
32
+ if (fs.existsSync(output)) halt("Output directory alreay exists.");
33
+ else if (fs.existsSync(parent) === false)
34
+ halt("Output directory's parent directory does not exist.");
35
+ else if (fs.statSync(parent).isDirectory() === false)
36
+ halt("Output directory's parent is not a directory.");
37
+
38
+ // READ SWAGGER
39
+ const swagger: ISwagger = (() => {
40
+ if (fs.existsSync(input) === false)
41
+ halt("Unable to find the input swagger.json file.");
42
+ const stats: fs.Stats = fs.statSync(input);
43
+ if (stats.isFile() === false) halt("The input swagger.json is not a file.");
44
+ const content: string = fs.readFileSync(input, "utf-8");
45
+ const swagger: ISwagger = JSON.parse(content);
46
+ return swagger;
47
+ })();
48
+
49
+ // DO GENERATE
50
+ const app = new MigrateApplication(swagger);
51
+ app.generate({
52
+ mkdir: fs.mkdirSync,
53
+ writeFile: (path, content) => fs.writeFileSync(path, content, "utf8"),
54
+ })(output);
55
+
56
+ // RUN SCRIPTS
57
+ SetupWizard.setup(output);
58
+ };
59
+ main(process.argv.slice(2));
package/src/index.ts CHANGED
@@ -1,4 +1,4 @@
1
- import * as migrate from "./module";
2
-
3
- export default migrate;
4
- export * from "./module";
1
+ import * as migrate from "./module";
2
+
3
+ export default migrate;
4
+ export * from "./module";
package/src/module.ts CHANGED
@@ -1,4 +1,4 @@
1
- export * from "./NestiaMigrateApplication";
2
-
3
- export * from "./structures/ISwagger";
4
- export * from "./structures/IMigrateProgram";
1
+ export * from "./MigrateApplication";
2
+
3
+ export * from "./structures/ISwagger";
4
+ export * from "./structures/IMigrateProgram";
@@ -1,118 +1,74 @@
1
- import { IMigrateDto } from "../structures/IMigrateDto";
2
- import { ISwaggerSchema } from "../structures/ISwaggeSchema";
3
- import { ISwagger } from "../structures/ISwagger";
4
- import { ISwaggerComponents } from "../structures/ISwaggerComponents";
5
- import { MapUtil } from "../utils/MapUtil";
6
- import { ImportProgrammer } from "./ImportProgrammer";
7
- import { SchemaProgrammer } from "./SchemaProgrammer";
8
-
9
- export namespace DtoProgrammer {
10
- export const analyze = (swagger: ISwagger): IMigrateDto[] => {
11
- const root: Modulo = new Modulo("");
12
-
13
- // COMPONENTS
14
- for (const [id, schema] of Object.entries(
15
- swagger.components.schemas ?? {},
16
- )) {
17
- const modulo = emplace(root)(id);
18
- modulo.dto.schema = schema;
19
- }
20
- return root.toDto().children;
21
- };
22
-
23
- const emplace = (modulo: Modulo) => (name: string) => {
24
- const identifiers: string[] = name.split(".");
25
- for (const key of identifiers)
26
- modulo = MapUtil.take(modulo.children)(key)(() => new Modulo(key));
27
- return modulo;
28
- };
29
-
30
- export const write =
31
- (components: ISwaggerComponents) =>
32
- (dto: IMigrateDto): string => {
33
- const references: ISwaggerSchema.IReference[] = [];
34
- const importer: ImportProgrammer = new ImportProgrammer(dto.name);
35
- const body: string = iterate(components)(references)(importer)(dto);
36
-
37
- const content: string[] = [
38
- ...importer.toScript((name) => `./${name}`, dto.name),
39
- ...(importer.empty() ? [] : [""]),
40
- body,
41
- ];
42
- return content.join("\n");
43
- };
44
-
45
- const iterate =
46
- (components: ISwaggerComponents) =>
47
- (references: ISwaggerSchema.IReference[]) =>
48
- (importer: ImportProgrammer) =>
49
- (dto: IMigrateDto): string => {
50
- const content: string[] = [];
51
- if (dto.schema) {
52
- const description: string | undefined = describe(dto.schema);
53
- content.push(
54
- ...(description
55
- ? [
56
- "/**",
57
- ...description.split("\n").map((l) => ` * ${l}`),
58
- " */",
59
- ]
60
- : []),
61
- `export type ${dto.name} = ${SchemaProgrammer.write(
62
- components,
63
- )(references)(importer)(dto.schema)}`,
64
- );
65
- }
66
- if (dto.children.length) {
67
- content.push(
68
- `export namespace ${dto.name} {`,
69
- ...dto.children.map((c) =>
70
- iterate(components)(references)(importer)(c)
71
- .split("\n")
72
- .map((l) => ` ${l}`)
73
- .join("\n"),
74
- ),
75
- `}`,
76
- );
77
- }
78
- return content.join("\n");
79
- };
80
-
81
- const describe = (schema: ISwaggerSchema): string | undefined => {
82
- const content: string[] = [];
83
- const add = (text: string) => {
84
- if (schema.description && !schema.description.includes(text))
85
- content.push(text);
86
- };
87
- if (schema.description) {
88
- content.push(...schema.description.split("\n"));
89
- if (!schema.description.split("\n").at(-1)?.startsWith("@"))
90
- content.push("");
91
- }
92
- if (schema.deprecated) add("@deprecated");
93
- if (schema.title) add(`@title ${schema.title}`);
94
- return content.length ? content.join("\n") : undefined;
95
- };
96
- }
97
-
98
- class Modulo {
99
- public readonly dto: IMigrateDto;
100
- public readonly children: Map<string, Modulo>;
101
-
102
- public constructor(name: string) {
103
- this.dto = {
104
- name,
105
- location: "src/api/structures",
106
- schema: null,
107
- children: [],
108
- };
109
- this.children = new Map();
110
- }
111
-
112
- public toDto(): IMigrateDto {
113
- this.dto.children = Array.from(this.children.values()).map((modulo) =>
114
- modulo.toDto(),
115
- );
116
- return this.dto;
117
- }
118
- }
1
+ import { IPointer } from "tstl";
2
+ import ts from "typescript";
3
+
4
+ import { ISwaggerSchema } from "../structures/ISwaggeSchema";
5
+ import { ISwaggerComponents } from "../structures/ISwaggerComponents";
6
+ import { FilePrinter } from "../utils/FilePrinter";
7
+ import { MapUtil } from "../utils/MapUtil";
8
+ import { ImportProgrammer } from "./ImportProgrammer";
9
+ import { SchemaProgrammer } from "./SchemaProgrammer";
10
+
11
+ export namespace DtoProgrammer {
12
+ export interface IModule {
13
+ name: string;
14
+ children: Map<string, IModule>;
15
+ programmer:
16
+ | null
17
+ | ((importer: ImportProgrammer) => ts.TypeAliasDeclaration);
18
+ }
19
+
20
+ export const write = (
21
+ components: ISwaggerComponents,
22
+ ): Map<string, IModule> => {
23
+ const dict: Map<string, IModule> = new Map();
24
+ for (const [key, value] of Object.entries(components.schemas ?? {}))
25
+ prepare(dict)(key)((importer) =>
26
+ writeAlias(importer)(components)(key, value),
27
+ );
28
+ return dict;
29
+ };
30
+
31
+ const prepare =
32
+ (dict: Map<string, IModule>) =>
33
+ (name: string) =>
34
+ (programmer: (importer: ImportProgrammer) => ts.TypeAliasDeclaration) => {
35
+ const accessors: string[] = name.split(".");
36
+ const modulo: IPointer<IModule> = { value: null! };
37
+
38
+ accessors.forEach((acc, i) => {
39
+ modulo.value = MapUtil.take(dict)(acc)(() => ({
40
+ name: acc,
41
+ children: new Map(),
42
+ programmer: null,
43
+ }));
44
+ if (i === accessors.length - 1) modulo.value.programmer = programmer;
45
+ dict = modulo.value.children;
46
+ });
47
+ return modulo!;
48
+ };
49
+
50
+ const writeAlias =
51
+ (importer: ImportProgrammer) =>
52
+ (components: ISwaggerComponents) =>
53
+ (key: string, value: ISwaggerSchema) =>
54
+ FilePrinter.description(
55
+ ts.factory.createTypeAliasDeclaration(
56
+ [ts.factory.createToken(ts.SyntaxKind.ExportKeyword)],
57
+ key.split(".").at(-1)!,
58
+ [],
59
+ SchemaProgrammer.write(importer)(components)(value),
60
+ ),
61
+ writeComment(value),
62
+ );
63
+ }
64
+
65
+ const writeComment = (schema: ISwaggerSchema): string =>
66
+ [
67
+ ...(schema.description?.length ? [schema.description] : []),
68
+ ...(schema.description?.length &&
69
+ (schema.title !== undefined || schema.deprecated === true)
70
+ ? [""]
71
+ : []),
72
+ ...(schema.title !== undefined ? [`@title ${schema.title}`] : []),
73
+ ...(schema.deprecated === true ? [`@deprecated`] : []),
74
+ ].join("\n");
@@ -1,60 +1,98 @@
1
- import { MapUtil } from "../utils/MapUtil";
2
-
3
- export class ImportProgrammer {
4
- private external_: Map<string, Set<string>> = new Map();
5
- private dtos_: Set<string> = new Set();
6
-
7
- public constructor(public readonly name: string | null) {}
8
-
9
- public empty(): boolean {
10
- return this.external_.size === 0 && this.dtos_.size === 0;
11
- }
12
-
13
- public external(props: ImportProgrammer.IProps): string {
14
- MapUtil.take(this.external_)(props.library)(() => new Set()).add(
15
- props.instance.split(".")[0],
16
- );
17
- return props.instance;
18
- }
19
-
20
- public tag(type: string, arg: number | string): string {
21
- this.external({
22
- library: "typia",
23
- instance: "tags",
24
- });
25
- return `tags.${type}<${JSON.stringify(arg)}>`;
26
- }
27
-
28
- public dto(name: string): string {
29
- const file: string = name.split(".")[0];
30
- if (this.name !== file) this.dtos_.add(file);
31
- return name;
32
- }
33
-
34
- public toScript(
35
- dtoPath: (name: string) => string,
36
- current?: string,
37
- ): string[] {
38
- const content: string[] = [...this.external_.entries()].map(
39
- ([library, properties]) =>
40
- `import { ${[...properties].join(", ")} } from "${library}";`,
41
- );
42
- if (this.external_.size && this.dtos_.size) content.push("");
43
- content.push(
44
- ...[...this.dtos_]
45
- .filter(
46
- current
47
- ? (name) => name !== current!.split(".")[0]
48
- : () => true,
49
- )
50
- .map((i) => `import { ${i} } from "${dtoPath(i)}";`),
51
- );
52
- return content;
53
- }
54
- }
55
- export namespace ImportProgrammer {
56
- export interface IProps {
57
- library: string;
58
- instance: string;
59
- }
60
- }
1
+ import ts from "typescript";
2
+ import { ExpressionFactory } from "typia/lib/factories/ExpressionFactory";
3
+
4
+ import { FilePrinter } from "../utils/FilePrinter";
5
+ import { MapUtil } from "../utils/MapUtil";
6
+
7
+ export class ImportProgrammer {
8
+ private external_: Map<string, Set<string>> = new Map();
9
+ private dtos_: Set<string> = new Set();
10
+
11
+ public constructor() {}
12
+
13
+ public empty(): boolean {
14
+ return this.external_.size === 0 && this.dtos_.size === 0;
15
+ }
16
+
17
+ public external(props: ImportProgrammer.IProps): string {
18
+ MapUtil.take(this.external_)(props.library)(() => new Set()).add(
19
+ props.instance.split(".")[0],
20
+ );
21
+ return props.instance;
22
+ }
23
+
24
+ public dto(name: string): ts.TypeReferenceNode {
25
+ const file: string = name.split(".")[0];
26
+ this.dtos_.add(file);
27
+ return ts.factory.createTypeReferenceNode(name);
28
+ }
29
+
30
+ public tag(type: string, arg: number | string): ts.TypeReferenceNode {
31
+ this.external({
32
+ library: "typia",
33
+ instance: "tags",
34
+ });
35
+ return ts.factory.createTypeReferenceNode(`tags.${type}`, [
36
+ ts.factory.createLiteralTypeNode(
37
+ typeof arg === "string"
38
+ ? ts.factory.createStringLiteral(arg)
39
+ : ExpressionFactory.number(arg),
40
+ ),
41
+ ]);
42
+ }
43
+
44
+ public toStatements(
45
+ dtoPath: (name: string) => string,
46
+ current?: string,
47
+ ): ts.Statement[] {
48
+ return [
49
+ ...[...this.external_.entries()].map(([library, properties]) =>
50
+ ts.factory.createImportDeclaration(
51
+ undefined,
52
+ ts.factory.createImportClause(
53
+ false,
54
+ undefined,
55
+ ts.factory.createNamedImports(
56
+ [...properties].map((i) =>
57
+ ts.factory.createImportSpecifier(
58
+ false,
59
+ undefined,
60
+ ts.factory.createIdentifier(i),
61
+ ),
62
+ ),
63
+ ),
64
+ ),
65
+ ts.factory.createStringLiteral(library),
66
+ ),
67
+ ),
68
+ ...(this.external_.size && this.dtos_.size ? [FilePrinter.enter()] : []),
69
+ ...[...this.dtos_]
70
+ .filter(
71
+ current ? (name) => name !== current!.split(".")[0] : () => true,
72
+ )
73
+ .map((i) =>
74
+ ts.factory.createImportDeclaration(
75
+ undefined,
76
+ ts.factory.createImportClause(
77
+ false,
78
+ undefined,
79
+ ts.factory.createNamedImports([
80
+ ts.factory.createImportSpecifier(
81
+ false,
82
+ undefined,
83
+ ts.factory.createIdentifier(i),
84
+ ),
85
+ ]),
86
+ ),
87
+ ts.factory.createStringLiteral(dtoPath(i)),
88
+ ),
89
+ ),
90
+ ];
91
+ }
92
+ }
93
+ export namespace ImportProgrammer {
94
+ export interface IProps {
95
+ library: string;
96
+ instance: string;
97
+ }
98
+ }