@nestia/migrate 0.6.3 → 0.7.0-dev.20240201
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.
- package/lib/IMigrateConfig.d.ts +1 -0
- package/lib/MigrateApplication.d.ts +3 -15
- package/lib/MigrateApplication.js +21 -62
- package/lib/MigrateApplication.js.map +1 -1
- package/lib/bundles/NEST_TEMPLATE.d.ts +5 -0
- package/{src/bundles/TEMPLATE.ts → lib/bundles/NEST_TEMPLATE.js} +71 -27
- package/lib/bundles/NEST_TEMPLATE.js.map +1 -0
- package/lib/bundles/SDK_TEMPLATE.d.ts +5 -0
- package/lib/bundles/SDK_TEMPLATE.js +61 -0
- package/lib/bundles/SDK_TEMPLATE.js.map +1 -0
- package/lib/bundles/TEMPLATE.d.ts +1 -1
- package/lib/bundles/TEMPLATE.js +23 -23
- package/lib/bundles/TEMPLATE.js.map +1 -1
- package/lib/executable/bundle.js +70 -51
- package/lib/executable/bundle.js.map +1 -1
- package/lib/executable/migrate.js +2 -62
- package/lib/executable/migrate.js.map +1 -1
- package/lib/internal/MigrateCli.d.ts +3 -0
- package/lib/internal/MigrateCli.js +59 -0
- package/lib/internal/MigrateCli.js.map +1 -0
- package/lib/internal/{MigrateArguments.d.ts → MigrateCommander.d.ts} +2 -1
- package/lib/internal/{MigrateArguments.js → MigrateCommander.js} +23 -20
- package/lib/internal/MigrateCommander.js.map +1 -0
- package/lib/programmers/ApiProgrammer.js +47 -2
- package/lib/programmers/ApiProgrammer.js.map +1 -1
- package/package.json +3 -2
- package/src/IMigrateConfig.ts +1 -0
- package/src/MigrateApplication.ts +20 -67
- package/src/bundles/NEST_TEMPLATE.ts +202 -0
- package/src/bundles/SDK_TEMPLATE.ts +57 -0
- package/src/executable/bundle.ts +80 -50
- package/src/executable/migrate.ts +2 -60
- package/src/internal/MigrateCommander.ts +52 -0
- package/src/internal/{MigrateArguments.ts → MigrateInquirer.ts} +12 -7
- package/src/programmers/ApiProgrammer.ts +60 -2
- package/lib/internal/MigrateArguments.js.map +0 -1
@@ -1,65 +1,7 @@
|
|
1
1
|
#!/usr/bin/env node
|
2
|
-
import
|
3
|
-
import path from "path";
|
2
|
+
import { MigrateCommander } from "../internal/MigrateCommander";
|
4
3
|
|
5
|
-
|
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 <nest|sdk> --input <swagger.json> --output <directory>
|
12
|
-
|
13
|
-
ex) npx @nestia/migrate nest --input swagger.json --output my-nest-project
|
14
|
-
ex) npx @nestia/migrate sdk --input swagger.json --output my-sdk-library
|
15
|
-
`;
|
16
|
-
|
17
|
-
function halt(desc: string): never {
|
18
|
-
console.error(desc);
|
19
|
-
process.exit(-1);
|
20
|
-
}
|
21
|
-
|
22
|
-
const main = async (argv: string[]): Promise<void> => {
|
23
|
-
const resolve = (str: string | undefined) =>
|
24
|
-
str ? path.resolve(str).split("\\").join("/") : undefined;
|
25
|
-
const input: string | undefined = resolve(argv[0]);
|
26
|
-
const output: string | undefined = resolve(argv[1]);
|
27
|
-
|
28
|
-
// VALIDATE ARGUMENTS
|
29
|
-
if (input === undefined || output === undefined) halt(USAGE);
|
30
|
-
|
31
|
-
// VALIDATE OUTPUT DIRECTORY
|
32
|
-
const parent: string = resolve(output + "/..")!;
|
33
|
-
if (fs.existsSync(output)) halt("Output directory alreay exists.");
|
34
|
-
else if (fs.existsSync(parent) === false)
|
35
|
-
halt("Output directory's parent directory does not exist.");
|
36
|
-
else if (fs.statSync(parent).isDirectory() === false)
|
37
|
-
halt("Output directory's parent is not a directory.");
|
38
|
-
|
39
|
-
// READ SWAGGER
|
40
|
-
const swagger: ISwagger = (() => {
|
41
|
-
if (fs.existsSync(input) === false)
|
42
|
-
halt("Unable to find the input swagger.json file.");
|
43
|
-
const stats: fs.Stats = fs.statSync(input);
|
44
|
-
if (stats.isFile() === false) 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: MigrateApplication = new MigrateApplication(
|
52
|
-
{
|
53
|
-
simulate: false,
|
54
|
-
},
|
55
|
-
swagger,
|
56
|
-
);
|
57
|
-
await app.generate(output);
|
58
|
-
|
59
|
-
// RUN SCRIPTS
|
60
|
-
SetupWizard.setup(output);
|
61
|
-
};
|
62
|
-
main(process.argv.slice(2)).catch((exp) => {
|
4
|
+
MigrateCommander.main().catch((exp) => {
|
63
5
|
console.error(exp);
|
64
6
|
process.exit(-1);
|
65
7
|
});
|
@@ -0,0 +1,52 @@
|
|
1
|
+
import fs from "fs";
|
2
|
+
import path from "path";
|
3
|
+
|
4
|
+
import { FileArchiver } from "../archivers/FileArchiver";
|
5
|
+
import { MigrateApplication } from "../module";
|
6
|
+
import { IMigrateFile } from "../structures/IMigrateFile";
|
7
|
+
import { ISwagger } from "../structures/ISwagger";
|
8
|
+
import { MigrateInquirer } from "./MigrateInquirer";
|
9
|
+
|
10
|
+
export namespace MigrateCommander {
|
11
|
+
export const main = async (): Promise<void> => {
|
12
|
+
const resolve = (str: string | undefined) =>
|
13
|
+
str ? path.resolve(str).split("\\").join("/") : undefined;
|
14
|
+
const options: MigrateInquirer.IOutput = await MigrateInquirer.parse();
|
15
|
+
|
16
|
+
// VALIDATE OUTPUT DIRECTORY
|
17
|
+
const parent: string = resolve(options.output + "/..")!;
|
18
|
+
if (fs.existsSync(options.output)) halt("Output directory alreay exists.");
|
19
|
+
else if (fs.existsSync(parent) === false)
|
20
|
+
halt("Output directory's parent directory does not exist.");
|
21
|
+
else if (fs.statSync(parent).isDirectory() === false)
|
22
|
+
halt("Output directory's parent is not a directory.");
|
23
|
+
|
24
|
+
// READ SWAGGER
|
25
|
+
const swagger: ISwagger = (() => {
|
26
|
+
if (fs.existsSync(options.input) === false)
|
27
|
+
halt("Unable to find the input swagger.json file.");
|
28
|
+
const stats: fs.Stats = fs.statSync(options.input);
|
29
|
+
if (stats.isFile() === false)
|
30
|
+
halt("The input swagger.json is not a file.");
|
31
|
+
const content: string = fs.readFileSync(options.input, "utf-8");
|
32
|
+
const swagger: ISwagger = JSON.parse(content);
|
33
|
+
return swagger;
|
34
|
+
})();
|
35
|
+
|
36
|
+
const app: MigrateApplication = new MigrateApplication(swagger);
|
37
|
+
const files: IMigrateFile[] =
|
38
|
+
options.mode === "nest"
|
39
|
+
? app.nest(options.simulate)
|
40
|
+
: app.sdk(options.simulate);
|
41
|
+
await FileArchiver.archive({
|
42
|
+
mkdir: fs.promises.mkdir,
|
43
|
+
writeFile: (file, content) =>
|
44
|
+
fs.promises.writeFile(file, content, "utf-8"),
|
45
|
+
})(options.output)(files);
|
46
|
+
};
|
47
|
+
|
48
|
+
const halt = (desc: string): never => {
|
49
|
+
console.error(desc);
|
50
|
+
process.exit(-1);
|
51
|
+
};
|
52
|
+
}
|
@@ -1,28 +1,28 @@
|
|
1
1
|
import commander from "commander";
|
2
2
|
import inquirer from "inquirer";
|
3
3
|
|
4
|
-
export namespace
|
4
|
+
export namespace MigrateInquirer {
|
5
5
|
export interface IOutput {
|
6
6
|
mode: "nest" | "sdk";
|
7
|
+
simulate: boolean;
|
7
8
|
input: string;
|
8
9
|
output: string;
|
9
10
|
}
|
10
11
|
|
11
12
|
export const parse = async (): Promise<IOutput> => {
|
12
13
|
// PREPARE ASSETS
|
13
|
-
commander.program.option("--
|
14
|
+
commander.program.option("--mode [nest/sdk]", "migration mode");
|
14
15
|
commander.program.option(
|
15
16
|
"--input [swagger.json]",
|
16
17
|
"location of target swagger.json file",
|
17
18
|
);
|
18
19
|
commander.program.option("--output [directory]", "output directory path");
|
20
|
+
commander.program.option("--simulate", "Mockup simulator");
|
19
21
|
|
20
22
|
// INTERNAL PROCEDURES
|
21
23
|
const questioned = { value: false };
|
22
|
-
const action = (
|
23
|
-
|
24
|
-
) => {
|
25
|
-
return new Promise<IOutput>((resolve, reject) => {
|
24
|
+
const action = (closure: (options: Partial<IOutput>) => Promise<IOutput>) =>
|
25
|
+
new Promise<IOutput>((resolve, reject) => {
|
26
26
|
commander.program.action(async (options) => {
|
27
27
|
try {
|
28
28
|
resolve(await closure(options));
|
@@ -32,7 +32,6 @@ export namespace MigrateArguments {
|
|
32
32
|
});
|
33
33
|
commander.program.parseAsync().catch(reject);
|
34
34
|
});
|
35
|
-
};
|
36
35
|
const select =
|
37
36
|
(name: string) =>
|
38
37
|
(message: string) =>
|
@@ -68,6 +67,12 @@ export namespace MigrateArguments {
|
|
68
67
|
);
|
69
68
|
partial.input ??= await input("input")("Swagger file location");
|
70
69
|
partial.output ??= await input("output")("Output directory path");
|
70
|
+
if (partial.simulate)
|
71
|
+
partial.simulate = (partial.simulate as any) === "true";
|
72
|
+
else
|
73
|
+
partial.simulate =
|
74
|
+
(await select("simulate")("Mokup Simulator")(["true", "false"])) ===
|
75
|
+
"true";
|
71
76
|
return partial as IOutput;
|
72
77
|
});
|
73
78
|
};
|
@@ -1,4 +1,5 @@
|
|
1
1
|
import { HashMap, IPointer, hash } from "tstl";
|
2
|
+
import ts from "typescript";
|
2
3
|
import { Escaper } from "typia/lib/utils/Escaper";
|
3
4
|
|
4
5
|
import { IMigrateProgram } from "../module";
|
@@ -6,6 +7,8 @@ import { IMigrateFile } from "../structures/IMigrateFile";
|
|
6
7
|
import { FilePrinter } from "../utils/FilePrinter";
|
7
8
|
import { StringUtil } from "../utils/StringUtil";
|
8
9
|
import { ApiFileProgrammer } from "./ApiFileProgrammer";
|
10
|
+
import { DtoProgrammer } from "./DtoProgrammer";
|
11
|
+
import { ImportProgrammer } from "./ImportProgrammer";
|
9
12
|
|
10
13
|
export namespace ApiProgrammer {
|
11
14
|
export const write = (program: IMigrateProgram): IMigrateFile[] => {
|
@@ -44,6 +47,12 @@ export namespace ApiProgrammer {
|
|
44
47
|
props.children.add(last.value.namespace.at(-1)!);
|
45
48
|
last.value = props;
|
46
49
|
});
|
50
|
+
const top = dict.take([], () => ({
|
51
|
+
namespace: [],
|
52
|
+
children: new Set(),
|
53
|
+
entries: [],
|
54
|
+
}));
|
55
|
+
if (namespace.length) top.children.add(namespace[0]);
|
47
56
|
}
|
48
57
|
for (const { second: props } of dict)
|
49
58
|
props.entries.forEach(
|
@@ -57,8 +66,8 @@ export namespace ApiProgrammer {
|
|
57
66
|
])(entry.alias)),
|
58
67
|
);
|
59
68
|
|
60
|
-
|
61
|
-
location: `src
|
69
|
+
const output: IMigrateFile[] = [...dict].map(({ second: props }) => ({
|
70
|
+
location: `src/${program.config.mode === "nest" ? "api/" : ""}functional/${props.namespace.join("/")}`,
|
62
71
|
file: "index.ts",
|
63
72
|
content: FilePrinter.write({
|
64
73
|
statements: ApiFileProgrammer.write(program.config)(
|
@@ -66,5 +75,54 @@ export namespace ApiProgrammer {
|
|
66
75
|
)(props),
|
67
76
|
}),
|
68
77
|
}));
|
78
|
+
if (program.config.mode === "sdk")
|
79
|
+
output.push(
|
80
|
+
...[...DtoProgrammer.write(program.swagger.components).entries()].map(
|
81
|
+
([key, value]) => ({
|
82
|
+
location: "src/structures",
|
83
|
+
file: `${key}.ts`,
|
84
|
+
content: FilePrinter.write({
|
85
|
+
statements: writeDtoFile(key, value),
|
86
|
+
}),
|
87
|
+
}),
|
88
|
+
),
|
89
|
+
);
|
90
|
+
return output;
|
91
|
+
};
|
92
|
+
|
93
|
+
const writeDtoFile = (
|
94
|
+
key: string,
|
95
|
+
modulo: DtoProgrammer.IModule,
|
96
|
+
): ts.Statement[] => {
|
97
|
+
const importer = new ImportProgrammer();
|
98
|
+
const statements: ts.Statement[] = iterate(importer)(modulo);
|
99
|
+
if (statements.length === 0) return [];
|
100
|
+
|
101
|
+
return [
|
102
|
+
...importer.toStatements((name) => `./${name}`, key),
|
103
|
+
...(importer.empty() ? [] : [FilePrinter.enter()]),
|
104
|
+
...statements,
|
105
|
+
];
|
69
106
|
};
|
107
|
+
|
108
|
+
const iterate =
|
109
|
+
(importer: ImportProgrammer) =>
|
110
|
+
(modulo: DtoProgrammer.IModule): ts.Statement[] => {
|
111
|
+
const output: ts.Statement[] = [];
|
112
|
+
if (modulo.programmer !== null) output.push(modulo.programmer(importer));
|
113
|
+
if (modulo.children.size) {
|
114
|
+
const internal: ts.Statement[] = [];
|
115
|
+
for (const child of modulo.children.values())
|
116
|
+
internal.push(...iterate(importer)(child));
|
117
|
+
output.push(
|
118
|
+
ts.factory.createModuleDeclaration(
|
119
|
+
[ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)],
|
120
|
+
ts.factory.createIdentifier(modulo.name),
|
121
|
+
ts.factory.createModuleBlock(internal),
|
122
|
+
ts.NodeFlags.Namespace,
|
123
|
+
),
|
124
|
+
);
|
125
|
+
}
|
126
|
+
return output;
|
127
|
+
};
|
70
128
|
}
|
@@ -1 +0,0 @@
|
|
1
|
-
{"version":3,"file":"MigrateArguments.js","sourceRoot":"","sources":["../../src/internal/MigrateArguments.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,0DAAkC;AAClC,wDAAgC;AAEhC,IAAiB,gBAAgB,CAsEhC;AAtED,WAAiB,gBAAgB;IAOlB,sBAAK,GAAG,GAA2B,EAAE;QAChD,iBAAiB;QACjB,mBAAS,CAAC,OAAO,CAAC,MAAM,CAAC,mBAAmB,EAAE,gBAAgB,CAAC,CAAC;QAChE,mBAAS,CAAC,OAAO,CAAC,MAAM,CACtB,wBAAwB,EACxB,sCAAsC,CACvC,CAAC;QACF,mBAAS,CAAC,OAAO,CAAC,MAAM,CAAC,sBAAsB,EAAE,uBAAuB,CAAC,CAAC;QAE1E,sBAAsB;QACtB,MAAM,UAAU,GAAG,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC;QACpC,MAAM,MAAM,GAAG,CACb,OAAwD,EACxD,EAAE;YACF,OAAO,IAAI,OAAO,CAAU,CAAC,OAAO,EA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