@nestia/migrate 0.5.0 → 0.6.0

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 (72) hide show
  1. package/lib/NestiaMigrateApplication.js +15 -15
  2. package/lib/NestiaMigrateApplication.js.map +1 -1
  3. package/lib/archivers/FileArchiver.js.map +1 -1
  4. package/lib/bundles/TEMPLATE.js +11 -11
  5. package/lib/bundles/TEMPLATE.js.map +1 -1
  6. package/lib/executable/bundle.js.map +1 -1
  7. package/lib/executable/migrate.js +5 -5
  8. package/lib/executable/migrate.js.map +1 -1
  9. package/lib/programmers/ControllerProgrammer.d.ts +2 -1
  10. package/lib/programmers/ControllerProgrammer.js +21 -18
  11. package/lib/programmers/ControllerProgrammer.js.map +1 -1
  12. package/lib/programmers/DtoProgrammer.d.ts +8 -4
  13. package/lib/programmers/DtoProgrammer.js +36 -77
  14. package/lib/programmers/DtoProgrammer.js.map +1 -1
  15. package/lib/programmers/ImportProgrammer.d.ts +5 -5
  16. package/lib/programmers/ImportProgrammer.js +27 -19
  17. package/lib/programmers/ImportProgrammer.js.map +1 -1
  18. package/lib/programmers/MigrateProgrammer.d.ts +1 -2
  19. package/lib/programmers/MigrateProgrammer.js +52 -36
  20. package/lib/programmers/MigrateProgrammer.js.map +1 -1
  21. package/lib/programmers/ModuleProgrammer.d.ts +5 -0
  22. package/lib/programmers/ModuleProgrammer.js +29 -0
  23. package/lib/programmers/ModuleProgrammer.js.map +1 -0
  24. package/lib/programmers/RouteProgrammer.d.ts +2 -2
  25. package/lib/programmers/RouteProgrammer.js +108 -111
  26. package/lib/programmers/RouteProgrammer.js.map +1 -1
  27. package/lib/programmers/SchemaProgrammer.d.ts +2 -1
  28. package/lib/programmers/SchemaProgrammer.js +122 -189
  29. package/lib/programmers/SchemaProgrammer.js.map +1 -1
  30. package/lib/structures/IMigrateProgram.d.ts +2 -2
  31. package/lib/structures/ISwaggerInfo.d.ts +3 -3
  32. package/lib/utils/FilePrinter.d.ts +9 -0
  33. package/lib/utils/FilePrinter.js +25 -0
  34. package/lib/utils/FilePrinter.js.map +1 -0
  35. package/lib/utils/JsonTypeChecker.d.ts +3 -1
  36. package/lib/utils/JsonTypeChecker.js +31 -18
  37. package/lib/utils/JsonTypeChecker.js.map +1 -1
  38. package/lib/utils/MapUtil.js.map +1 -1
  39. package/lib/utils/SetupWizard.js.map +1 -1
  40. package/lib/utils/StringUtil.js.map +1 -1
  41. package/package.json +11 -9
  42. package/src/NestiaMigrateApplication.ts +68 -73
  43. package/src/archivers/FileArchiver.ts +35 -38
  44. package/src/bundles/TEMPLATE.ts +11 -11
  45. package/src/executable/bundle.ts +72 -78
  46. package/src/executable/migrate.ts +59 -60
  47. package/src/index.ts +4 -4
  48. package/src/module.ts +4 -4
  49. package/src/programmers/ControllerProgrammer.ts +155 -157
  50. package/src/programmers/DtoProgrammer.ts +74 -118
  51. package/src/programmers/ImportProgrammer.ts +98 -60
  52. package/src/programmers/MigrateProgrammer.ts +75 -62
  53. package/src/programmers/ModuleProgrammer.ts +62 -0
  54. package/src/programmers/RouteProgrammer.ts +486 -466
  55. package/src/programmers/SchemaProgrammer.ts +247 -339
  56. package/src/structures/IMigrateController.ts +8 -8
  57. package/src/structures/IMigrateDto.ts +8 -8
  58. package/src/structures/IMigrateFile.ts +5 -5
  59. package/src/structures/IMigrateProgram.ts +7 -7
  60. package/src/structures/IMigrateRoute.ts +36 -36
  61. package/src/structures/IMigrateSchema.ts +4 -4
  62. package/src/structures/ISwaggeSchema.ts +82 -82
  63. package/src/structures/ISwagger.ts +20 -20
  64. package/src/structures/ISwaggerComponents.ts +7 -7
  65. package/src/structures/ISwaggerInfo.ts +57 -57
  66. package/src/structures/ISwaggerRoute.ts +52 -52
  67. package/src/structures/ISwaggerSecurity.ts +47 -47
  68. package/src/utils/FilePrinter.ts +36 -0
  69. package/src/utils/JsonTypeChecker.ts +67 -52
  70. package/src/utils/MapUtil.ts +13 -13
  71. package/src/utils/SetupWizard.ts +15 -15
  72. package/src/utils/StringUtil.ts +51 -51
@@ -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
+ }
@@ -1,62 +1,75 @@
1
- import { IMigrateController } from "../structures/IMigrateController";
2
- import { IMigrateDto } from "../structures/IMigrateDto";
3
- import { IMigrateFile } from "../structures/IMigrateFile";
4
- import { IMigrateProgram } from "../structures/IMigrateProgram";
5
- import { ISwagger } from "../structures/ISwagger";
6
- import { ISwaggerComponents } from "../structures/ISwaggerComponents";
7
- import { ControllerProgrammer } from "./ControllerProgrammer";
8
- import { DtoProgrammer } from "./DtoProgrammer";
9
-
10
- export namespace MigrateProgrammer {
11
- export const analyze = (swagger: ISwagger): IMigrateProgram => {
12
- const controllers: IMigrateController[] =
13
- ControllerProgrammer.analyze(swagger);
14
- const structures: IMigrateDto[] = DtoProgrammer.analyze(swagger);
15
- return {
16
- controllers,
17
- structures,
18
- };
19
- };
20
-
21
- export const write =
22
- (components: ISwaggerComponents) =>
23
- (program: IMigrateProgram): IMigrateFile[] => {
24
- return [
25
- ...program.structures.map((s) => ({
26
- location: s.location,
27
- file: `${s.name}.ts`,
28
- content: DtoProgrammer.write(components)(s),
29
- })),
30
- ...program.controllers.map((c) => ({
31
- location: c.location,
32
- file: `${c.name}.ts`,
33
- content: ControllerProgrammer.write(components)(c),
34
- })),
35
- {
36
- location: "src",
37
- file: "MyModule.ts",
38
- content: MyModule(program.controllers),
39
- },
40
- ];
41
- };
42
- }
43
-
44
- const MyModule = (controllers: IMigrateController[]): string =>
45
- [
46
- `import { Module } from "@nestjs/common";`,
47
- ``,
48
- ...controllers.map(
49
- (c) =>
50
- `import { ${c.name} } from "${c.location.replace(
51
- "src/",
52
- "./",
53
- )}/${c.name}";`,
54
- ),
55
- ``,
56
- `@Module({`,
57
- ` controllers: [`,
58
- ...controllers.map((c) => ` ${c.name},`),
59
- ` ],`,
60
- `})`,
61
- `export class MyModule {}`,
62
- ].join("\n");
1
+ import ts from "typescript";
2
+
3
+ import { IMigrateFile } from "../structures/IMigrateFile";
4
+ import { IMigrateProgram } from "../structures/IMigrateProgram";
5
+ import { ISwagger } from "../structures/ISwagger";
6
+ import { FilePrinter } from "../utils/FilePrinter";
7
+ import { ControllerProgrammer } from "./ControllerProgrammer";
8
+ import { DtoProgrammer } from "./DtoProgrammer";
9
+ import { ImportProgrammer } from "./ImportProgrammer";
10
+ import { ModuleProgrammer } from "./ModuleProgrammer";
11
+
12
+ export namespace MigrateProgrammer {
13
+ export const analyze = (swagger: ISwagger): IMigrateProgram => ({
14
+ swagger,
15
+ controllers: ControllerProgrammer.analyze(swagger),
16
+ });
17
+
18
+ export const write = (program: IMigrateProgram): IMigrateFile[] =>
19
+ [
20
+ {
21
+ location: "src",
22
+ file: "MyModule.ts",
23
+ statements: ModuleProgrammer.write(program.controllers),
24
+ },
25
+ ...program.controllers.map((c) => ({
26
+ location: c.location,
27
+ file: `${c.name}.ts`,
28
+ statements: ControllerProgrammer.write(program.swagger.components)(c),
29
+ })),
30
+ ...[...DtoProgrammer.write(program.swagger.components).entries()].map(
31
+ ([key, value]) => ({
32
+ location: "src/api/structures",
33
+ file: `${key}.ts`,
34
+ statements: writeDtoFile(key, value),
35
+ }),
36
+ ),
37
+ ].map((o) => ({
38
+ location: o.location,
39
+ file: o.file,
40
+ content: FilePrinter.write({ statements: o.statements }),
41
+ }));
42
+
43
+ const writeDtoFile = (key: string, modulo: DtoProgrammer.IModule): ts.Statement[] => {
44
+ const importer = new ImportProgrammer();
45
+ const statements: ts.Statement[] = iterate(importer)(modulo);
46
+ if (statements.length === 0) return [];
47
+
48
+ return [
49
+ ...importer.toStatements((name) => `./${name}`, key),
50
+ ...(importer.empty() ? [] : [FilePrinter.enter()]),
51
+ ...statements,
52
+ ];
53
+ };
54
+
55
+ const iterate =
56
+ (importer: ImportProgrammer) =>
57
+ (modulo: DtoProgrammer.IModule): ts.Statement[] => {
58
+ const output: ts.Statement[] = [];
59
+ if (modulo.programmer !== null) output.push(modulo.programmer(importer));
60
+ if (modulo.children.size) {
61
+ const internal: ts.Statement[] = [];
62
+ for (const child of modulo.children.values())
63
+ internal.push(...iterate(importer)(child));
64
+ output.push(
65
+ ts.factory.createModuleDeclaration(
66
+ [ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)],
67
+ ts.factory.createIdentifier(modulo.name),
68
+ ts.factory.createModuleBlock(internal),
69
+ ts.NodeFlags.Namespace,
70
+ ),
71
+ );
72
+ }
73
+ return output;
74
+ };
75
+ }
@@ -0,0 +1,62 @@
1
+ import ts from "typescript";
2
+
3
+ import { IMigrateController } from "../structures/IMigrateController";
4
+ import { FilePrinter } from "../utils/FilePrinter";
5
+
6
+ export namespace ModuleProgrammer {
7
+ export const write = (controllers: IMigrateController[]): ts.Statement[] => [
8
+ $import("@nestjs/common")("Module"),
9
+ ...(controllers.length ? [FilePrinter.enter()] : []),
10
+ ...controllers.map((c) =>
11
+ $import(`${c.location.replace("src/", "./")}/${c.name}`)(c.name),
12
+ ),
13
+ ...(controllers.length ? [FilePrinter.enter()] : []),
14
+ ts.factory.createClassDeclaration(
15
+ [
16
+ ts.factory.createDecorator(
17
+ ts.factory.createCallExpression(
18
+ ts.factory.createIdentifier("Module"),
19
+ undefined,
20
+ [
21
+ ts.factory.createObjectLiteralExpression(
22
+ [
23
+ ts.factory.createPropertyAssignment(
24
+ ts.factory.createIdentifier("controllers"),
25
+ ts.factory.createArrayLiteralExpression(
26
+ controllers.map((c) =>
27
+ ts.factory.createIdentifier(c.name),
28
+ ),
29
+ ),
30
+ ),
31
+ ],
32
+ true,
33
+ ),
34
+ ],
35
+ ),
36
+ ),
37
+ ts.factory.createToken(ts.SyntaxKind.ExportKeyword),
38
+ ],
39
+ "MyModule",
40
+ undefined,
41
+ undefined,
42
+ [],
43
+ ),
44
+ ];
45
+ }
46
+
47
+ const $import = (file: string) => (instance: string) =>
48
+ ts.factory.createImportDeclaration(
49
+ undefined,
50
+ ts.factory.createImportClause(
51
+ false,
52
+ undefined,
53
+ ts.factory.createNamedImports([
54
+ ts.factory.createImportSpecifier(
55
+ false,
56
+ undefined,
57
+ ts.factory.createIdentifier(instance),
58
+ ),
59
+ ]),
60
+ ),
61
+ ts.factory.createStringLiteral(file),
62
+ );