@nestia/migrate 0.7.0-dev.20240201 → 0.7.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.
@@ -1,74 +1,74 @@
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(components)(importer)(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
- (components: ISwaggerComponents) =>
52
- (importer: ImportProgrammer) =>
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(components)(importer)(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
+ 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(components)(importer)(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
+ (components: ISwaggerComponents) =>
52
+ (importer: ImportProgrammer) =>
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(components)(importer)(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,114 +1,114 @@
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<
9
- string,
10
- {
11
- default: string | null;
12
- instances: Set<string>;
13
- }
14
- > = new Map();
15
- private dtos_: Set<string> = new Set();
16
-
17
- public constructor() {}
18
-
19
- public empty(): boolean {
20
- return this.external_.size === 0 && this.dtos_.size === 0;
21
- }
22
-
23
- public external(props: ImportProgrammer.IProps): string {
24
- const element = MapUtil.take(this.external_)(props.library)(() => ({
25
- default: null,
26
- instances: new Set(),
27
- }));
28
- const name: string = props.name.split(".")[0];
29
- if (props.type === "default") element.default = props.name;
30
- else element.instances.add(name);
31
- return name;
32
- }
33
-
34
- public dto(name: string): ts.TypeReferenceNode {
35
- const file: string = name.split(".")[0];
36
- this.dtos_.add(file);
37
- return ts.factory.createTypeReferenceNode(name);
38
- }
39
-
40
- public tag(type: string, arg: number | string): ts.TypeReferenceNode {
41
- this.external({
42
- type: "instance",
43
- library: "typia",
44
- name: "tags",
45
- });
46
- return ts.factory.createTypeReferenceNode(`tags.${type}`, [
47
- ts.factory.createLiteralTypeNode(
48
- typeof arg === "string"
49
- ? ts.factory.createStringLiteral(arg)
50
- : ExpressionFactory.number(arg),
51
- ),
52
- ]);
53
- }
54
-
55
- public toStatements(
56
- dtoPath: (name: string) => string,
57
- current?: string,
58
- ): ts.Statement[] {
59
- return [
60
- ...[...this.external_.entries()].map(([library, props]) => {
61
- return ts.factory.createImportDeclaration(
62
- undefined,
63
- ts.factory.createImportClause(
64
- false,
65
- props.default !== null
66
- ? ts.factory.createIdentifier(props.default)
67
- : undefined,
68
- props.instances.size
69
- ? ts.factory.createNamedImports(
70
- [...props.instances].map((i) =>
71
- ts.factory.createImportSpecifier(
72
- false,
73
- undefined,
74
- ts.factory.createIdentifier(i),
75
- ),
76
- ),
77
- )
78
- : undefined,
79
- ),
80
- ts.factory.createStringLiteral(library),
81
- );
82
- }),
83
- ...(this.external_.size && this.dtos_.size ? [FilePrinter.enter()] : []),
84
- ...[...this.dtos_]
85
- .filter(
86
- current ? (name) => name !== current!.split(".")[0] : () => true,
87
- )
88
- .map((i) =>
89
- ts.factory.createImportDeclaration(
90
- undefined,
91
- ts.factory.createImportClause(
92
- false,
93
- undefined,
94
- ts.factory.createNamedImports([
95
- ts.factory.createImportSpecifier(
96
- false,
97
- undefined,
98
- ts.factory.createIdentifier(i),
99
- ),
100
- ]),
101
- ),
102
- ts.factory.createStringLiteral(dtoPath(i)),
103
- ),
104
- ),
105
- ];
106
- }
107
- }
108
- export namespace ImportProgrammer {
109
- export interface IProps {
110
- type: "default" | "instance";
111
- library: string;
112
- name: string;
113
- }
114
- }
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<
9
+ string,
10
+ {
11
+ default: string | null;
12
+ instances: Set<string>;
13
+ }
14
+ > = new Map();
15
+ private dtos_: Set<string> = new Set();
16
+
17
+ public constructor() {}
18
+
19
+ public empty(): boolean {
20
+ return this.external_.size === 0 && this.dtos_.size === 0;
21
+ }
22
+
23
+ public external(props: ImportProgrammer.IProps): string {
24
+ const element = MapUtil.take(this.external_)(props.library)(() => ({
25
+ default: null,
26
+ instances: new Set(),
27
+ }));
28
+ const name: string = props.name.split(".")[0];
29
+ if (props.type === "default") element.default = props.name;
30
+ else element.instances.add(name);
31
+ return name;
32
+ }
33
+
34
+ public dto(name: string): ts.TypeReferenceNode {
35
+ const file: string = name.split(".")[0];
36
+ this.dtos_.add(file);
37
+ return ts.factory.createTypeReferenceNode(name);
38
+ }
39
+
40
+ public tag(type: string, arg: number | string): ts.TypeReferenceNode {
41
+ this.external({
42
+ type: "instance",
43
+ library: "typia",
44
+ name: "tags",
45
+ });
46
+ return ts.factory.createTypeReferenceNode(`tags.${type}`, [
47
+ ts.factory.createLiteralTypeNode(
48
+ typeof arg === "string"
49
+ ? ts.factory.createStringLiteral(arg)
50
+ : ExpressionFactory.number(arg),
51
+ ),
52
+ ]);
53
+ }
54
+
55
+ public toStatements(
56
+ dtoPath: (name: string) => string,
57
+ current?: string,
58
+ ): ts.Statement[] {
59
+ return [
60
+ ...[...this.external_.entries()].map(([library, props]) => {
61
+ return ts.factory.createImportDeclaration(
62
+ undefined,
63
+ ts.factory.createImportClause(
64
+ false,
65
+ props.default !== null
66
+ ? ts.factory.createIdentifier(props.default)
67
+ : undefined,
68
+ props.instances.size
69
+ ? ts.factory.createNamedImports(
70
+ [...props.instances].map((i) =>
71
+ ts.factory.createImportSpecifier(
72
+ false,
73
+ undefined,
74
+ ts.factory.createIdentifier(i),
75
+ ),
76
+ ),
77
+ )
78
+ : undefined,
79
+ ),
80
+ ts.factory.createStringLiteral(library),
81
+ );
82
+ }),
83
+ ...(this.external_.size && this.dtos_.size ? [FilePrinter.enter()] : []),
84
+ ...[...this.dtos_]
85
+ .filter(
86
+ current ? (name) => name !== current!.split(".")[0] : () => true,
87
+ )
88
+ .map((i) =>
89
+ ts.factory.createImportDeclaration(
90
+ undefined,
91
+ ts.factory.createImportClause(
92
+ false,
93
+ undefined,
94
+ ts.factory.createNamedImports([
95
+ ts.factory.createImportSpecifier(
96
+ false,
97
+ undefined,
98
+ ts.factory.createIdentifier(i),
99
+ ),
100
+ ]),
101
+ ),
102
+ ts.factory.createStringLiteral(dtoPath(i)),
103
+ ),
104
+ ),
105
+ ];
106
+ }
107
+ }
108
+ export namespace ImportProgrammer {
109
+ export interface IProps {
110
+ type: "default" | "instance";
111
+ library: string;
112
+ name: string;
113
+ }
114
+ }
@@ -1,48 +1,48 @@
1
- import ts from "typescript";
2
-
3
- import { IMigrateController } from "../structures/IMigrateController";
4
- import { ISwaggerComponents } from "../structures/ISwaggerComponents";
5
- import { FilePrinter } from "../utils/FilePrinter";
6
- import { StringUtil } from "../utils/StringUtil";
7
- import { ImportProgrammer } from "./ImportProgrammer";
8
- import { NestMethodProgrammer } from "./NestMethodProgrammer";
9
-
10
- export namespace NestControllerProgrammer {
11
- export const write =
12
- (components: ISwaggerComponents) =>
13
- (controller: IMigrateController): ts.Statement[] => {
14
- const importer: ImportProgrammer = new ImportProgrammer();
15
- const $class = ts.factory.createClassDeclaration(
16
- [
17
- ts.factory.createDecorator(
18
- ts.factory.createCallExpression(
19
- ts.factory.createIdentifier(
20
- importer.external({
21
- type: "instance",
22
- library: "@nestjs/common",
23
- name: "Controller",
24
- }),
25
- ),
26
- [],
27
- [ts.factory.createStringLiteral(controller.path)],
28
- ),
29
- ),
30
- ts.factory.createToken(ts.SyntaxKind.ExportKeyword),
31
- ],
32
- controller.name,
33
- [],
34
- [],
35
- controller.routes.map(NestMethodProgrammer.write(components)(importer)),
36
- );
37
- return [
38
- ...importer.toStatements(
39
- (ref) =>
40
- `${"../".repeat(
41
- StringUtil.splitWithNormalization(controller.location).length - 1,
42
- )}api/structures/${ref}`,
43
- ),
44
- ...(importer.empty() ? [] : [FilePrinter.enter()]),
45
- $class,
46
- ];
47
- };
48
- }
1
+ import ts from "typescript";
2
+
3
+ import { IMigrateController } from "../structures/IMigrateController";
4
+ import { ISwaggerComponents } from "../structures/ISwaggerComponents";
5
+ import { FilePrinter } from "../utils/FilePrinter";
6
+ import { StringUtil } from "../utils/StringUtil";
7
+ import { ImportProgrammer } from "./ImportProgrammer";
8
+ import { NestMethodProgrammer } from "./NestMethodProgrammer";
9
+
10
+ export namespace NestControllerProgrammer {
11
+ export const write =
12
+ (components: ISwaggerComponents) =>
13
+ (controller: IMigrateController): ts.Statement[] => {
14
+ const importer: ImportProgrammer = new ImportProgrammer();
15
+ const $class = ts.factory.createClassDeclaration(
16
+ [
17
+ ts.factory.createDecorator(
18
+ ts.factory.createCallExpression(
19
+ ts.factory.createIdentifier(
20
+ importer.external({
21
+ type: "instance",
22
+ library: "@nestjs/common",
23
+ name: "Controller",
24
+ }),
25
+ ),
26
+ [],
27
+ [ts.factory.createStringLiteral(controller.path)],
28
+ ),
29
+ ),
30
+ ts.factory.createToken(ts.SyntaxKind.ExportKeyword),
31
+ ],
32
+ controller.name,
33
+ [],
34
+ [],
35
+ controller.routes.map(NestMethodProgrammer.write(components)(importer)),
36
+ );
37
+ return [
38
+ ...importer.toStatements(
39
+ (ref) =>
40
+ `${"../".repeat(
41
+ StringUtil.splitWithNormalization(controller.location).length - 1,
42
+ )}api/structures/${ref}`,
43
+ ),
44
+ ...(importer.empty() ? [] : [FilePrinter.enter()]),
45
+ $class,
46
+ ];
47
+ };
48
+ }
@@ -1,74 +1,74 @@
1
- import ts from "typescript";
2
-
3
- import { IMigrateFile } from "../structures/IMigrateFile";
4
- import { IMigrateProgram } from "../structures/IMigrateProgram";
5
- import { FilePrinter } from "../utils/FilePrinter";
6
- import { DtoProgrammer } from "./DtoProgrammer";
7
- import { ImportProgrammer } from "./ImportProgrammer";
8
- import { NestControllerProgrammer } from "./NestControllerProgrammer";
9
- import { NestModuleProgrammer } from "./NestModuleProgrammer";
10
-
11
- export namespace NestProgrammer {
12
- export const write = (program: IMigrateProgram): IMigrateFile[] =>
13
- [
14
- {
15
- location: "src",
16
- file: "MyModule.ts",
17
- statements: NestModuleProgrammer.write(program.controllers),
18
- },
19
- ...program.controllers.map((c) => ({
20
- location: c.location,
21
- file: `${c.name}.ts`,
22
- statements: NestControllerProgrammer.write(program.swagger.components)(
23
- c,
24
- ),
25
- })),
26
- ...[...DtoProgrammer.write(program.swagger.components).entries()].map(
27
- ([key, value]) => ({
28
- location: "src/api/structures",
29
- file: `${key}.ts`,
30
- statements: writeDtoFile(key, value),
31
- }),
32
- ),
33
- ].map((o) => ({
34
- location: o.location,
35
- file: o.file,
36
- content: FilePrinter.write({ statements: o.statements }),
37
- }));
38
-
39
- const writeDtoFile = (
40
- key: string,
41
- modulo: DtoProgrammer.IModule,
42
- ): ts.Statement[] => {
43
- const importer = new ImportProgrammer();
44
- const statements: ts.Statement[] = iterate(importer)(modulo);
45
- if (statements.length === 0) return [];
46
-
47
- return [
48
- ...importer.toStatements((name) => `./${name}`, key),
49
- ...(importer.empty() ? [] : [FilePrinter.enter()]),
50
- ...statements,
51
- ];
52
- };
53
-
54
- const iterate =
55
- (importer: ImportProgrammer) =>
56
- (modulo: DtoProgrammer.IModule): ts.Statement[] => {
57
- const output: ts.Statement[] = [];
58
- if (modulo.programmer !== null) output.push(modulo.programmer(importer));
59
- if (modulo.children.size) {
60
- const internal: ts.Statement[] = [];
61
- for (const child of modulo.children.values())
62
- internal.push(...iterate(importer)(child));
63
- output.push(
64
- ts.factory.createModuleDeclaration(
65
- [ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)],
66
- ts.factory.createIdentifier(modulo.name),
67
- ts.factory.createModuleBlock(internal),
68
- ts.NodeFlags.Namespace,
69
- ),
70
- );
71
- }
72
- return output;
73
- };
74
- }
1
+ import ts from "typescript";
2
+
3
+ import { IMigrateFile } from "../structures/IMigrateFile";
4
+ import { IMigrateProgram } from "../structures/IMigrateProgram";
5
+ import { FilePrinter } from "../utils/FilePrinter";
6
+ import { DtoProgrammer } from "./DtoProgrammer";
7
+ import { ImportProgrammer } from "./ImportProgrammer";
8
+ import { NestControllerProgrammer } from "./NestControllerProgrammer";
9
+ import { NestModuleProgrammer } from "./NestModuleProgrammer";
10
+
11
+ export namespace NestProgrammer {
12
+ export const write = (program: IMigrateProgram): IMigrateFile[] =>
13
+ [
14
+ {
15
+ location: "src",
16
+ file: "MyModule.ts",
17
+ statements: NestModuleProgrammer.write(program.controllers),
18
+ },
19
+ ...program.controllers.map((c) => ({
20
+ location: c.location,
21
+ file: `${c.name}.ts`,
22
+ statements: NestControllerProgrammer.write(program.swagger.components)(
23
+ c,
24
+ ),
25
+ })),
26
+ ...[...DtoProgrammer.write(program.swagger.components).entries()].map(
27
+ ([key, value]) => ({
28
+ location: "src/api/structures",
29
+ file: `${key}.ts`,
30
+ statements: writeDtoFile(key, value),
31
+ }),
32
+ ),
33
+ ].map((o) => ({
34
+ location: o.location,
35
+ file: o.file,
36
+ content: FilePrinter.write({ statements: o.statements }),
37
+ }));
38
+
39
+ const writeDtoFile = (
40
+ key: string,
41
+ modulo: DtoProgrammer.IModule,
42
+ ): ts.Statement[] => {
43
+ const importer = new ImportProgrammer();
44
+ const statements: ts.Statement[] = iterate(importer)(modulo);
45
+ if (statements.length === 0) return [];
46
+
47
+ return [
48
+ ...importer.toStatements((name) => `./${name}`, key),
49
+ ...(importer.empty() ? [] : [FilePrinter.enter()]),
50
+ ...statements,
51
+ ];
52
+ };
53
+
54
+ const iterate =
55
+ (importer: ImportProgrammer) =>
56
+ (modulo: DtoProgrammer.IModule): ts.Statement[] => {
57
+ const output: ts.Statement[] = [];
58
+ if (modulo.programmer !== null) output.push(modulo.programmer(importer));
59
+ if (modulo.children.size) {
60
+ const internal: ts.Statement[] = [];
61
+ for (const child of modulo.children.values())
62
+ internal.push(...iterate(importer)(child));
63
+ output.push(
64
+ ts.factory.createModuleDeclaration(
65
+ [ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)],
66
+ ts.factory.createIdentifier(modulo.name),
67
+ ts.factory.createModuleBlock(internal),
68
+ ts.NodeFlags.Namespace,
69
+ ),
70
+ );
71
+ }
72
+ return output;
73
+ };
74
+ }
@@ -1,9 +1,9 @@
1
- import { IMigrateConfig } from "../IMigrateConfig";
2
- import { IMigrateController } from "./IMigrateController";
3
- import { ISwagger } from "./ISwagger";
4
-
5
- export interface IMigrateProgram {
6
- config: IMigrateConfig;
7
- controllers: IMigrateController[];
8
- swagger: ISwagger;
9
- }
1
+ import { IMigrateConfig } from "../IMigrateConfig";
2
+ import { IMigrateController } from "./IMigrateController";
3
+ import { ISwagger } from "./ISwagger";
4
+
5
+ export interface IMigrateProgram {
6
+ config: IMigrateConfig;
7
+ controllers: IMigrateController[];
8
+ swagger: ISwagger;
9
+ }