very-express 0.0.11 → 0.1.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.
- package/.env +2 -1
- package/.eslintrc.json +37 -0
- package/dist/cli.js +36 -33
- package/dist/generators/controller.template.d.ts +5 -4
- package/dist/generators/controller.template.js +174 -131
- package/dist/generators/controllers.generator.d.ts +6 -1
- package/dist/generators/controllers.generator.js +99 -77
- package/dist/generators/index.d.ts +3 -3
- package/dist/generators/openapi.generator.d.ts +5 -1
- package/dist/generators/openapi.generator.js +125 -121
- package/dist/generators/packageJson.generator.js +5 -12
- package/dist/generators/routes.generator.d.ts +10 -5
- package/dist/generators/routes.generator.js +6 -7
- package/dist/generators/routes.template.d.ts +4 -5
- package/dist/generators/routes.template.js +49 -27
- package/dist/generators/server.generator.d.ts +1 -1
- package/dist/generators/server.generator.js +9 -13
- package/dist/generators/server.template.d.ts +1 -2
- package/dist/generators/server.template.js +24 -25
- package/dist/index.d.ts +2 -2
- package/dist/index.js +45 -45
- package/dist/preprocess/jsonschemaFormat.d.ts +2 -0
- package/dist/preprocess/jsonschemaFormat.js +94 -0
- package/dist/templates/root/package.json +7 -4
- package/dist/templates/utils/common.gen.ts +35 -11
- package/dist/test.d.ts +1 -0
- package/dist/test.js +10 -0
- package/dist/types/controller.d.ts +0 -0
- package/dist/types/controller.js +1 -0
- package/dist/types/openapi.d.ts +21 -18
- package/dist/types/openapi.js +0 -24
- package/dist/types/types.d.ts +36 -21
- package/dist/types/types.js +3 -13
- package/dist/utils/common.d.ts +8 -6
- package/dist/utils/common.js +81 -35
- package/dist/utils/log.d.ts +1 -1
- package/dist/utils/log.js +5 -5
- package/package.json +60 -56
- package/vex.config.json +7 -0
|
@@ -9,14 +9,13 @@ const log_1 = __importDefault(require("../utils/log"));
|
|
|
9
9
|
function compile(packageOutPath, schemaDir, openapiDir, outDir) {
|
|
10
10
|
log_1.default.process(`Project : ${packageOutPath}`);
|
|
11
11
|
let writePackage = false;
|
|
12
|
-
let packjson =
|
|
12
|
+
let packjson = "";
|
|
13
13
|
if (!fs_1.default.existsSync(packageOutPath)) {
|
|
14
|
-
packjson = fs_1.default.readFileSync(__dirname +
|
|
14
|
+
packjson = fs_1.default.readFileSync(__dirname + "/../templates/root/package.json").toString();
|
|
15
15
|
}
|
|
16
16
|
else {
|
|
17
17
|
packjson = fs_1.default.readFileSync(packageOutPath).toString();
|
|
18
18
|
}
|
|
19
|
-
;
|
|
20
19
|
const packageJson = JSON.parse(packjson);
|
|
21
20
|
const vexgenScript = `vex -j ${schemaDir} -a ${openapiDir} -o ${outDir}`;
|
|
22
21
|
if (packageJson.scripts.vexgen !== vexgenScript) {
|
|
@@ -24,36 +23,30 @@ function compile(packageOutPath, schemaDir, openapiDir, outDir) {
|
|
|
24
23
|
writePackage = true;
|
|
25
24
|
log_1.default.process(`package.json : Update script : vexgen > ${packageJson.scripts.vexgen}`);
|
|
26
25
|
}
|
|
27
|
-
;
|
|
28
26
|
if (!packageJson.scripts.build) {
|
|
29
|
-
packageJson.scripts.build =
|
|
27
|
+
packageJson.scripts.build = "tsc -p .";
|
|
30
28
|
writePackage = true;
|
|
31
29
|
log_1.default.process(`package.json : Update script : build > ${packageJson.scripts.build}`);
|
|
32
30
|
}
|
|
33
|
-
;
|
|
34
31
|
if (!packageJson.scripts.dev) {
|
|
35
32
|
packageJson.scripts.dev = `nodemon --watch src --exec ts-node ${outDir}/server.ts`;
|
|
36
33
|
writePackage = true;
|
|
37
34
|
log_1.default.process(`package.json : Update script : dev > ${packageJson.scripts.dev}`);
|
|
38
35
|
}
|
|
39
|
-
;
|
|
40
36
|
if (!packageJson.scripts.start) {
|
|
41
|
-
packageJson.scripts.start =
|
|
37
|
+
packageJson.scripts.start = "node ./dist/server.js";
|
|
42
38
|
writePackage = true;
|
|
43
39
|
log_1.default.process(`package.json : Update script : start > ${packageJson.scripts.start}`);
|
|
44
40
|
}
|
|
45
|
-
;
|
|
46
41
|
if (writePackage) {
|
|
47
42
|
log_1.default.writing(`Project : ${packageOutPath}`);
|
|
48
43
|
fs_1.default.writeFileSync(packageOutPath, JSON.stringify(packageJson, null, 4));
|
|
49
44
|
}
|
|
50
45
|
else {
|
|
51
|
-
log_1.default.info(
|
|
46
|
+
log_1.default.info("package.json : No changes");
|
|
52
47
|
}
|
|
53
|
-
;
|
|
54
48
|
}
|
|
55
49
|
exports.compile = compile;
|
|
56
|
-
;
|
|
57
50
|
exports.default = {
|
|
58
51
|
compile
|
|
59
52
|
};
|
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
import * as types from "../types/types";
|
|
2
|
-
export declare function compile(
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
export declare function compile(options: {
|
|
3
|
+
routesArr: {
|
|
4
|
+
route: string;
|
|
5
|
+
interfaceName: string;
|
|
6
|
+
controllerPath: string;
|
|
7
|
+
}[];
|
|
8
|
+
routesOutPath: string;
|
|
9
|
+
openapiFile: string;
|
|
10
|
+
compilerOptions: types.compilerOptions;
|
|
11
|
+
}): void;
|
|
@@ -7,13 +7,12 @@ exports.compile = void 0;
|
|
|
7
7
|
const fs_1 = __importDefault(require("fs"));
|
|
8
8
|
const routes_template_1 = __importDefault(require("./routes.template"));
|
|
9
9
|
const log_1 = __importDefault(require("../utils/log"));
|
|
10
|
-
function compile(
|
|
11
|
-
log_1.default.writing(`Router : ${
|
|
12
|
-
fs_1.default.writeFileSync(
|
|
13
|
-
routes: routesArr,
|
|
14
|
-
|
|
15
|
-
|
|
10
|
+
function compile(options) {
|
|
11
|
+
log_1.default.writing(`Router : ${options.routesOutPath}`);
|
|
12
|
+
fs_1.default.writeFileSync(options.routesOutPath, (0, routes_template_1.default)({
|
|
13
|
+
routes: options.routesArr,
|
|
14
|
+
openapiFile: options.openapiFile,
|
|
15
|
+
compilerOptions: options.compilerOptions,
|
|
16
16
|
}));
|
|
17
17
|
}
|
|
18
18
|
exports.compile = compile;
|
|
19
|
-
;
|
|
@@ -1,13 +1,12 @@
|
|
|
1
|
-
import * as types from
|
|
1
|
+
import * as types from "../types/types";
|
|
2
2
|
export default function routesTemplate(templateOptions: {
|
|
3
|
-
headerComment?: string;
|
|
4
3
|
template?: string;
|
|
4
|
+
openapiFile?: string;
|
|
5
5
|
enableOpenApi?: boolean;
|
|
6
|
-
openapiPath?: string;
|
|
7
6
|
routes: {
|
|
8
7
|
route: string;
|
|
9
|
-
|
|
8
|
+
interfaceName: string;
|
|
10
9
|
controllerPath: string;
|
|
11
10
|
}[];
|
|
12
|
-
|
|
11
|
+
compilerOptions: types.compilerOptions;
|
|
13
12
|
}): string;
|
|
@@ -1,43 +1,65 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
2
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
const utils = __importStar(require("../utils/common"));
|
|
3
27
|
function routesTemplate(templateOptions) {
|
|
4
|
-
var _a
|
|
5
|
-
if (!templateOptions.headerComment) {
|
|
6
|
-
templateOptions.headerComment =
|
|
28
|
+
var _a;
|
|
29
|
+
if (!templateOptions.compilerOptions.headerComment) {
|
|
30
|
+
templateOptions.compilerOptions.headerComment = templateOptions.compilerOptions.headerComment || "// generated files by very-express";
|
|
7
31
|
}
|
|
8
|
-
;
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
throw new Error('OpenApi : routers.generator > compile() : Args "openapiPath" is required while enableOpenApi is true.');
|
|
32
|
+
const enableOpenApi = templateOptions.enableOpenApi || true;
|
|
33
|
+
if (enableOpenApi && !templateOptions.compilerOptions.openapiDir) {
|
|
34
|
+
console.log("\x1b[31m%s\x1b[0m", "[Error]", "OpenApi : routers.generator > compile() : Args \"openapiPath\" is required while enableOpenApi is true.");
|
|
35
|
+
throw new Error("OpenApi : routers.generator > compile() : Args \"openapiPath\" is required while enableOpenApi is true.");
|
|
13
36
|
}
|
|
14
|
-
;
|
|
15
|
-
let
|
|
16
|
-
|
|
17
|
-
import {
|
|
18
|
-
import
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
37
|
+
const headerComment = ((_a = templateOptions.compilerOptions) === null || _a === void 0 ? void 0 : _a.headerComment) || "// generated files by very-express";
|
|
38
|
+
let template = templateOptions.template || `{{headerComment}}
|
|
39
|
+
import { Router } from 'express';
|
|
40
|
+
import swaggerUi, { JsonObject } from 'swagger-ui-express';
|
|
41
|
+
import { loadYaml } from '../utils/common.gen';
|
|
42
|
+
|
|
43
|
+
{{importRoutes}}
|
|
44
|
+
|
|
45
|
+
const router :Router = Router();
|
|
46
|
+
|
|
47
|
+
{{openapiRoute}}
|
|
48
|
+
|
|
49
|
+
{{useRoutes}}
|
|
50
|
+
|
|
29
51
|
export default router;`;
|
|
52
|
+
const openapiPath = utils.relativePath(templateOptions.compilerOptions.rootDir, templateOptions.compilerOptions.openapiDir) + (templateOptions.openapiFile || "/openapi.gen.yaml");
|
|
30
53
|
let importRoutes = "";
|
|
31
54
|
let useRoutes = "";
|
|
32
55
|
templateOptions === null || templateOptions === void 0 ? void 0 : templateOptions.routes.forEach((obj) => {
|
|
33
|
-
importRoutes += `import ${obj.
|
|
34
|
-
useRoutes += `router.use('${obj.route}', ${obj.
|
|
56
|
+
importRoutes += `import ${obj.interfaceName}Controller from '${obj.controllerPath}';\n`;
|
|
57
|
+
useRoutes += `router.use('${obj.route}', ${obj.interfaceName}Controller);\n`;
|
|
35
58
|
});
|
|
36
59
|
template = template.replace(/{{headerComment}}/g, headerComment);
|
|
37
60
|
template = template.replace(/{{importRoutes}}/g, importRoutes);
|
|
38
|
-
template = template.replace(/{{openapiRoute}}/g, enableOpenApi ? `router.use('/api', swaggerUi.serve, swaggerUi.setup(loadYaml('
|
|
61
|
+
template = template.replace(/{{openapiRoute}}/g, enableOpenApi ? `router.use('/api', swaggerUi.serve, swaggerUi.setup(loadYaml('./${openapiPath}') as JsonObject));` : "");
|
|
39
62
|
template = template.replace(/{{useRoutes}}/g, useRoutes);
|
|
40
63
|
return template;
|
|
41
64
|
}
|
|
42
65
|
exports.default = routesTemplate;
|
|
43
|
-
;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import * as types from "../types/types";
|
|
2
|
-
export declare function compile(
|
|
2
|
+
export declare function compile(options: types.compilerOptions): void;
|
|
@@ -8,29 +8,25 @@ const fs_1 = __importDefault(require("fs"));
|
|
|
8
8
|
const server_template_1 = __importDefault(require("./server.template"));
|
|
9
9
|
const packageJson_generator_1 = __importDefault(require("./packageJson.generator"));
|
|
10
10
|
const log_1 = __importDefault(require("../utils/log"));
|
|
11
|
-
function compile(
|
|
12
|
-
const serverOutPath =
|
|
13
|
-
const packageOutPath =
|
|
14
|
-
const envOutPath =
|
|
15
|
-
const tsconfigOutPath =
|
|
16
|
-
const gitignoreOutPath = `./.gitignore`;
|
|
11
|
+
function compile(options) {
|
|
12
|
+
const serverOutPath = options.srcDir + "/server.ts";
|
|
13
|
+
const packageOutPath = options.rootDir + "/package.json";
|
|
14
|
+
const envOutPath = options.rootDir + "/.env";
|
|
15
|
+
const tsconfigOutPath = options.rootDir + "/tsconfig.json";
|
|
17
16
|
if (!fs_1.default.existsSync(serverOutPath)) {
|
|
18
17
|
log_1.default.writing(`Server : ${serverOutPath}`);
|
|
19
|
-
fs_1.default.writeFileSync(
|
|
18
|
+
fs_1.default.writeFileSync(serverOutPath, (0, server_template_1.default)({
|
|
20
19
|
options: options,
|
|
21
20
|
}));
|
|
22
21
|
}
|
|
23
22
|
if (!fs_1.default.existsSync(envOutPath)) {
|
|
24
23
|
log_1.default.writing(`Project : ${envOutPath}`);
|
|
25
|
-
fs_1.default.copyFileSync(__dirname +
|
|
24
|
+
fs_1.default.copyFileSync(__dirname + "/../templates/root/.env", envOutPath);
|
|
26
25
|
}
|
|
27
|
-
;
|
|
28
26
|
if (!fs_1.default.existsSync(tsconfigOutPath)) {
|
|
29
27
|
log_1.default.writing(`Project : ${tsconfigOutPath}`);
|
|
30
|
-
fs_1.default.copyFileSync(__dirname +
|
|
28
|
+
fs_1.default.copyFileSync(__dirname + "/../templates/root/tsconfig.json", tsconfigOutPath);
|
|
31
29
|
}
|
|
32
|
-
;
|
|
33
|
-
packageJson_generator_1.default.compile(packageOutPath, schemaDir, openapiDir, outDir);
|
|
30
|
+
packageJson_generator_1.default.compile(packageOutPath, options.jsonSchemaDir, options.openapiDir, options.rootDir);
|
|
34
31
|
}
|
|
35
32
|
exports.compile = compile;
|
|
36
|
-
;
|
|
@@ -2,33 +2,32 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
function serverTemplate(templateOptions) {
|
|
4
4
|
var _a;
|
|
5
|
-
|
|
6
|
-
let template = templateOptions.template || `{{headerComment}}
|
|
7
|
-
|
|
8
|
-
import dotenv from 'dotenv';
|
|
9
|
-
import express from 'express';
|
|
10
|
-
import helmet from 'helmet';
|
|
11
|
-
import routesGenerated from './routes/routes.gen';
|
|
12
|
-
|
|
13
|
-
dotenv.config();
|
|
14
|
-
|
|
15
|
-
const app = express();
|
|
16
|
-
app.disable("x-powered-by");
|
|
17
|
-
app.use(express.json());
|
|
18
|
-
|
|
19
|
-
app.use(routesGenerated);
|
|
20
|
-
|
|
21
|
-
app.use(helmet({
|
|
22
|
-
xPoweredBy: false,
|
|
23
|
-
xDnsPrefetchControl: { allow: false },
|
|
24
|
-
}));
|
|
25
|
-
|
|
26
|
-
app.listen(process.env.EXPRESS_PORT, () => {
|
|
27
|
-
console.log('Server is running on port ' + process.env.EXPRESS_PORT );
|
|
28
|
-
});
|
|
5
|
+
const headerComment = ((_a = templateOptions.options) === null || _a === void 0 ? void 0 : _a.headerComment) || "// generated files by very-express";
|
|
6
|
+
let template = templateOptions.template || `{{headerComment}}
|
|
7
|
+
|
|
8
|
+
import dotenv from 'dotenv';
|
|
9
|
+
import express from 'express';
|
|
10
|
+
import helmet from 'helmet';
|
|
11
|
+
import routesGenerated from './routes/routes.gen';
|
|
12
|
+
|
|
13
|
+
dotenv.config();
|
|
14
|
+
|
|
15
|
+
const app = express();
|
|
16
|
+
app.disable("x-powered-by");
|
|
17
|
+
app.use(express.json());
|
|
18
|
+
|
|
19
|
+
app.use(routesGenerated);
|
|
20
|
+
|
|
21
|
+
app.use(helmet({
|
|
22
|
+
xPoweredBy: false,
|
|
23
|
+
xDnsPrefetchControl: { allow: false },
|
|
24
|
+
}));
|
|
25
|
+
|
|
26
|
+
app.listen(process.env.EXPRESS_PORT, () => {
|
|
27
|
+
console.log('Server is running on port ' + process.env.EXPRESS_PORT );
|
|
28
|
+
});
|
|
29
29
|
`;
|
|
30
30
|
template = template.replace(/{{headerComment}}/g, headerComment);
|
|
31
31
|
return template;
|
|
32
32
|
}
|
|
33
33
|
exports.default = serverTemplate;
|
|
34
|
-
;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import * as types from
|
|
2
|
-
export default function generate(
|
|
1
|
+
import * as types from "./types/types";
|
|
2
|
+
export default function generate(options: types.compilerOptions): void;
|
package/dist/index.js
CHANGED
|
@@ -34,55 +34,46 @@ const common_1 = __importDefault(require("./utils/common"));
|
|
|
34
34
|
const controllerGen = __importStar(require("./generators/controllers.generator"));
|
|
35
35
|
const routeGen = __importStar(require("./generators/routes.generator"));
|
|
36
36
|
const serverGen = __importStar(require("./generators/server.generator"));
|
|
37
|
-
|
|
37
|
+
const jsonschemaFormat_1 = require("./preprocess/jsonschemaFormat");
|
|
38
|
+
function generate(options) {
|
|
38
39
|
const dir = {
|
|
39
|
-
routeDir: `${
|
|
40
|
-
middlewareDir: `${
|
|
41
|
-
controllerDir: `${
|
|
42
|
-
modelDir: `${
|
|
43
|
-
typeDir: `${
|
|
44
|
-
serviceDir: `${
|
|
45
|
-
utilsDir: `${
|
|
40
|
+
routeDir: `${options.srcDir}/routes`,
|
|
41
|
+
middlewareDir: `${options.srcDir}/middlewares`,
|
|
42
|
+
controllerDir: `${options.srcDir}/controllers`,
|
|
43
|
+
modelDir: `${options.srcDir}/models`,
|
|
44
|
+
typeDir: `${options.srcDir}/types`,
|
|
45
|
+
serviceDir: `${options.srcDir}/services`,
|
|
46
|
+
utilsDir: `${options.srcDir}/utils`,
|
|
46
47
|
};
|
|
47
|
-
const
|
|
48
|
-
|
|
49
|
-
|
|
48
|
+
const openapiFile = "/openapi.gen.yaml";
|
|
49
|
+
const openapipath = options.openapiDir + openapiFile;
|
|
50
|
+
if (!fs_1.default.existsSync(options.rootDir)) {
|
|
51
|
+
fs_1.default.mkdirSync(options.rootDir);
|
|
50
52
|
}
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
53
|
+
if (!fs_1.default.existsSync(options.srcDir)) {
|
|
54
|
+
fs_1.default.mkdirSync(options.srcDir);
|
|
55
|
+
}
|
|
56
|
+
if (!fs_1.default.existsSync(options.openapiDir)) {
|
|
57
|
+
fs_1.default.mkdirSync(options.openapiDir);
|
|
54
58
|
}
|
|
55
|
-
;
|
|
56
59
|
Object.values(dir).forEach((path) => {
|
|
57
60
|
if (!fs_1.default.existsSync(path)) {
|
|
58
61
|
fs_1.default.mkdirSync(path);
|
|
59
62
|
}
|
|
60
|
-
;
|
|
61
63
|
});
|
|
62
64
|
log_1.default.process(`Router : ${openapipath}`);
|
|
63
|
-
|
|
64
|
-
const files = fs_1.default.readdirSync(
|
|
65
|
+
const routeData = [];
|
|
66
|
+
const files = fs_1.default.readdirSync(options.jsonSchemaDir);
|
|
65
67
|
files.forEach((schemaFileName) => {
|
|
66
|
-
const schemaPath = `${
|
|
68
|
+
const schemaPath = `${options.jsonSchemaDir}/${schemaFileName}`;
|
|
67
69
|
try {
|
|
68
|
-
if (!schemaFileName.endsWith(
|
|
70
|
+
if (!schemaFileName.endsWith(".json")) {
|
|
69
71
|
log_1.default.warn(`Skipping Non Json File : ${schemaPath}`);
|
|
70
72
|
return;
|
|
71
73
|
}
|
|
72
|
-
const
|
|
73
|
-
const jsonSchema = JSON.parse(file.toString());
|
|
74
|
+
const jsonSchema = (0, jsonschemaFormat_1.formatJsonSchema)(schemaPath);
|
|
74
75
|
const documentConfig = jsonSchema["x-documentConfig"];
|
|
75
|
-
if (
|
|
76
|
-
log_1.default.error(`x-documentConfig not found in ${schemaPath}`);
|
|
77
|
-
if (!documentConfig.documentName)
|
|
78
|
-
log_1.default.error(`x-documentConfig.documentName not found in ${schemaPath}`);
|
|
79
|
-
if (!documentConfig.interfaceName)
|
|
80
|
-
log_1.default.error(`x-documentConfig.interfaceName not found in ${schemaPath}`);
|
|
81
|
-
if (!documentConfig.methods)
|
|
82
|
-
log_1.default.error(`x-documentConfig.methods is not found in ${schemaPath}`);
|
|
83
|
-
if (!Array.isArray(documentConfig.methods))
|
|
84
|
-
log_1.default.error(`x-documentConfig.methods is invalid in ${schemaPath}`);
|
|
85
|
-
if (typeof jsonSchema.properties !== 'object')
|
|
76
|
+
if (typeof jsonSchema.properties !== "object")
|
|
86
77
|
log_1.default.error(`properties is invalid in ${schemaPath}`);
|
|
87
78
|
if (!fs_1.default.existsSync(`${schemaPath}.nogen.ts`)) {
|
|
88
79
|
json2mongoose_1.default.typesGen.compileFromFile(`${schemaPath}`, `${dir.typeDir}/${documentConfig.interfaceName}.gen.ts`, options || common_1.default.defaultCompilerOptions);
|
|
@@ -90,27 +81,36 @@ function generate(schemaDir, openapiDir, outDir, options) {
|
|
|
90
81
|
if (!fs_1.default.existsSync(`${schemaPath}Model.nogen.ts`)) {
|
|
91
82
|
json2mongoose_1.default.modelsGen.compileFromFile(`${schemaPath}`, `${common_1.default.relativePath(dir.modelDir, dir.typeDir)}/${documentConfig.interfaceName}.gen`, `${dir.modelDir}/${documentConfig.interfaceName}Model.gen.ts`, options || common_1.default.defaultCompilerOptions);
|
|
92
83
|
}
|
|
93
|
-
;
|
|
94
84
|
routeData.push({
|
|
95
85
|
route: `/${documentConfig.documentName}`,
|
|
96
|
-
|
|
97
|
-
controllerPath: common_1.default.relativePath(dir.routeDir, dir.controllerDir) +
|
|
86
|
+
interfaceName: documentConfig.interfaceName,
|
|
87
|
+
controllerPath: common_1.default.relativePath(dir.routeDir, dir.controllerDir) + "/" + documentConfig.interfaceName + "Controller.gen",
|
|
98
88
|
});
|
|
99
89
|
}
|
|
100
90
|
catch (err) {
|
|
101
91
|
log_1.default.error(`Processing File : ${schemaPath}\n`, err);
|
|
102
92
|
}
|
|
103
|
-
;
|
|
104
93
|
});
|
|
105
|
-
if (!fs_1.default.existsSync(openapiDir +
|
|
106
|
-
openapiGen.compile(
|
|
94
|
+
if (!fs_1.default.existsSync(options.openapiDir + "/openapi.nogen.yaml")) {
|
|
95
|
+
openapiGen.compile({
|
|
96
|
+
openapiOutFileName: openapiFile,
|
|
97
|
+
compilerOptions: options || common_1.default.defaultCompilerOptions
|
|
98
|
+
});
|
|
107
99
|
}
|
|
108
|
-
;
|
|
109
|
-
common_1.default.copyDir(`${openapiDir}`, outDir + '/openapi');
|
|
100
|
+
common_1.default.copyDir(`${options.openapiDir}`, options.rootDir + "/openapi");
|
|
110
101
|
common_1.default.copyDir(`${__dirname}/templates/utils`, dir.utilsDir);
|
|
111
|
-
controllerGen.compile(
|
|
112
|
-
|
|
113
|
-
|
|
102
|
+
controllerGen.compile({
|
|
103
|
+
openapiFile: openapiFile,
|
|
104
|
+
controllerOutDir: dir.controllerDir,
|
|
105
|
+
modelDir: dir.modelDir,
|
|
106
|
+
compilerOptions: options || common_1.default.defaultCompilerOptions
|
|
107
|
+
});
|
|
108
|
+
routeGen.compile({
|
|
109
|
+
routesArr: routeData,
|
|
110
|
+
openapiFile: openapiFile,
|
|
111
|
+
routesOutPath: `${dir.routeDir}/routes.gen.ts`,
|
|
112
|
+
compilerOptions: options || common_1.default.defaultCompilerOptions
|
|
113
|
+
});
|
|
114
|
+
serverGen.compile(options);
|
|
114
115
|
}
|
|
115
116
|
exports.default = generate;
|
|
116
|
-
;
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
|
+
};
|
|
28
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
+
exports.formatJsonSchema = void 0;
|
|
30
|
+
const fs_1 = __importDefault(require("fs"));
|
|
31
|
+
const types = __importStar(require("../types/types"));
|
|
32
|
+
const common = __importStar(require("../utils/common"));
|
|
33
|
+
const log_1 = __importDefault(require("../utils/log"));
|
|
34
|
+
function formatJsonSchema(jsonSchemaPath) {
|
|
35
|
+
const jsonSchema = common.loadJsonSchema(jsonSchemaPath);
|
|
36
|
+
if (jsonSchema == undefined) {
|
|
37
|
+
log_1.default.error("formatJsonSchema : error at loadJsonSchema()");
|
|
38
|
+
}
|
|
39
|
+
if (!jsonSchema["x-documentConfig"]) {
|
|
40
|
+
log_1.default.error(`formatJsonSchema : x-documentConfig not found in ${jsonSchemaPath}`);
|
|
41
|
+
}
|
|
42
|
+
else if (!jsonSchema["x-documentConfig"].documentName) {
|
|
43
|
+
log_1.default.error(`formatJsonSchema : x-documentConfig.documentName not found in ${jsonSchemaPath}`);
|
|
44
|
+
}
|
|
45
|
+
else if (!jsonSchema["x-documentConfig"].interfaceName) {
|
|
46
|
+
log_1.default.error(`formatJsonSchema : x-documentConfig.interfaceName not found in ${jsonSchemaPath}`);
|
|
47
|
+
}
|
|
48
|
+
else if (!jsonSchema["x-documentConfig"].methods) {
|
|
49
|
+
log_1.default.warn(`formatJsonSchema : x-documentConfig.methods is not found in ${jsonSchemaPath}, initialize all supported methods.`);
|
|
50
|
+
jsonSchema["x-documentConfig"].methods = Object.assign([], types.schemaMethodArr);
|
|
51
|
+
}
|
|
52
|
+
jsonSchema.required = getRequiredArrStr(jsonSchema, jsonSchemaPath);
|
|
53
|
+
fs_1.default.writeFileSync(jsonSchemaPath, JSON.stringify(jsonSchema, null, 4));
|
|
54
|
+
return jsonSchema;
|
|
55
|
+
}
|
|
56
|
+
exports.formatJsonSchema = formatJsonSchema;
|
|
57
|
+
function getRequiredArrStr(schema, jsonSchemaPath) {
|
|
58
|
+
let properties;
|
|
59
|
+
let requiredArr = [];
|
|
60
|
+
const isArrObj = (props) => Boolean(props.type == "array" && props.items && props.items.type == "object");
|
|
61
|
+
if (isArrObj(schema)) {
|
|
62
|
+
properties = schema.items.properties;
|
|
63
|
+
requiredArr = typeof schema.items.required == "object" ? schema.items.required : [];
|
|
64
|
+
}
|
|
65
|
+
else if (schema.type == "object") {
|
|
66
|
+
properties = schema.properties;
|
|
67
|
+
requiredArr = typeof schema.required == "object" ? schema.required : [];
|
|
68
|
+
}
|
|
69
|
+
else {
|
|
70
|
+
return undefined;
|
|
71
|
+
}
|
|
72
|
+
if (properties == undefined) {
|
|
73
|
+
log_1.default.error(`formatJsonSchema > formatRequired : invalid "schema.properties" in ${jsonSchemaPath}`, schema);
|
|
74
|
+
}
|
|
75
|
+
else {
|
|
76
|
+
Object.keys(properties).forEach((propKey) => {
|
|
77
|
+
const prop = properties === null || properties === void 0 ? void 0 : properties[propKey];
|
|
78
|
+
const keyNotInRequiredArr = !requiredArr.includes(propKey);
|
|
79
|
+
if (prop && prop.type !== "object" && prop.required === true && keyNotInRequiredArr) {
|
|
80
|
+
requiredArr.push(propKey);
|
|
81
|
+
}
|
|
82
|
+
else if (prop && prop.items && isArrObj(prop)) {
|
|
83
|
+
prop.items.required = getRequiredArrStr(prop.items, jsonSchemaPath);
|
|
84
|
+
}
|
|
85
|
+
else if (prop && prop.type == "object") {
|
|
86
|
+
prop.required = getRequiredArrStr(prop, jsonSchemaPath);
|
|
87
|
+
if (prop.required && prop.required.length > 0 && keyNotInRequiredArr) {
|
|
88
|
+
requiredArr.push(propKey);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
return requiredArr;
|
|
94
|
+
}
|
|
@@ -8,21 +8,24 @@
|
|
|
8
8
|
},
|
|
9
9
|
"dependencies": {
|
|
10
10
|
"dotenv": "^16.3.1",
|
|
11
|
-
"nodemon": "^2.0.22",
|
|
12
11
|
"express": "^4.18.2",
|
|
13
12
|
"express-validator": "^7.0.1",
|
|
14
13
|
"helmet": "^7.1.0",
|
|
14
|
+
"js-yaml": "^4.1.0",
|
|
15
|
+
"mongoose": "^8.0.3",
|
|
16
|
+
"nodemon": "^2.0.22",
|
|
15
17
|
"passport": "^0.7.0",
|
|
16
18
|
"passport-google-oauth20": "^2.0.0",
|
|
17
19
|
"swagger-ui-express": "^5.0.0"
|
|
18
|
-
|
|
19
|
-
|
|
20
|
+
},
|
|
21
|
+
"devDependencies": {
|
|
20
22
|
"@types/express": "^4.17.21",
|
|
23
|
+
"@types/js-yaml": "^4.0.9",
|
|
21
24
|
"@types/node": "^20.10.5",
|
|
22
25
|
"@types/passport": "^1.0.16",
|
|
23
26
|
"@types/passport-google-oauth20": "^2.0.14",
|
|
24
27
|
"@types/swagger-ui-express": "^4.1.6",
|
|
25
28
|
"ts-node": "^10.9.2",
|
|
26
29
|
"typescript": "^5.3.3"
|
|
27
|
-
|
|
30
|
+
}
|
|
28
31
|
}
|
|
@@ -1,11 +1,35 @@
|
|
|
1
|
-
import jsYaml from
|
|
2
|
-
import fs from
|
|
3
|
-
|
|
4
|
-
export function loadYaml(yamlFilePath: string) {
|
|
5
|
-
try {
|
|
6
|
-
return jsYaml.load(fs.readFileSync(yamlFilePath,
|
|
7
|
-
} catch (e: any) {
|
|
8
|
-
console.log(
|
|
9
|
-
process.exit(0);
|
|
10
|
-
}
|
|
11
|
-
}
|
|
1
|
+
import jsYaml from "js-yaml";
|
|
2
|
+
import fs from "fs";
|
|
3
|
+
|
|
4
|
+
export function loadYaml(yamlFilePath: string) {
|
|
5
|
+
try {
|
|
6
|
+
return jsYaml.load(fs.readFileSync(yamlFilePath, "utf8"));
|
|
7
|
+
} catch (e: any) {
|
|
8
|
+
console.log("\x1b[41m%s\x1b[0m", "[ERROR]", "Error Load OpenApi File :\n", e);
|
|
9
|
+
process.exit(0);
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* build mongoose select object
|
|
15
|
+
* @param fieldsString stringified array of FieldPath
|
|
16
|
+
* @error may throw an error if the fieldsString is not a valid JSON
|
|
17
|
+
*/
|
|
18
|
+
export function parseFieldsSelect(fieldsString:string): Promise<{[key:string]:number}> {
|
|
19
|
+
return new Promise((resolve, reject) => {
|
|
20
|
+
let fieldArr: string[] = [];
|
|
21
|
+
try {
|
|
22
|
+
fieldArr = JSON.parse(fieldsString);
|
|
23
|
+
} catch (err) {
|
|
24
|
+
return reject("Invalid fields string");
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// Convert the fieldArr to an object that can be used in the select method
|
|
28
|
+
const selectFields = fieldArr.reduce((obj:any, field:string) => {
|
|
29
|
+
obj[field] = 1;
|
|
30
|
+
return obj;
|
|
31
|
+
}, {} as Record<string, number>);
|
|
32
|
+
|
|
33
|
+
resolve(selectFields);
|
|
34
|
+
});
|
|
35
|
+
}
|
package/dist/test.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/test.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const common_1 = __importDefault(require("./utils/common"));
|
|
7
|
+
const util_1 = __importDefault(require("util"));
|
|
8
|
+
const jsonSchema = common_1.default.loadJsonSchema("./output/jsonSchema/User.json");
|
|
9
|
+
const formattedSchema = common_1.default.cleanXcustomValue(jsonSchema.properties, { _id: "string", index: "boolean", unique: "boolean", required: "boolean" });
|
|
10
|
+
console.log(util_1.default.inspect(formattedSchema, { depth: null, colors: true }));
|