very-express 0.0.3 → 0.0.5
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/README.md +5 -10
- package/dist/cli.js +17 -4
- package/dist/generators/controllers.generator.js +3 -2
- package/dist/generators/openapi.generator.js +8 -7
- package/dist/generators/routes.generator.js +2 -1
- package/dist/generators/server.generator.js +4 -3
- package/dist/index.d.ts +2 -2
- package/dist/index.js +31 -15
- package/dist/templates/controller.template.js +41 -53
- package/dist/templates/utils/common.gen.js +2 -1
- package/dist/types/types.d.ts +1 -1
- package/dist/utils/common.d.ts +22 -0
- package/dist/utils/common.js +105 -0
- package/dist/utils/log.d.ts +9 -0
- package/dist/utils/log.js +21 -0
- package/package.json +5 -3
package/README.md
CHANGED
|
@@ -7,7 +7,7 @@ an generator that generate [Express](https://github.com/expressjs/express) rest-
|
|
|
7
7
|
- mongoDb
|
|
8
8
|
|
|
9
9
|
## TO DO
|
|
10
|
-
- [
|
|
10
|
+
- [x] genarate express server from template
|
|
11
11
|
|
|
12
12
|
- [x] generate openapi
|
|
13
13
|
- [x] path
|
|
@@ -17,13 +17,8 @@ an generator that generate [Express](https://github.com/expressjs/express) rest-
|
|
|
17
17
|
- [x] generate mongoose & collection interface
|
|
18
18
|
- [ ] generate sql interface
|
|
19
19
|
|
|
20
|
-
- [
|
|
21
|
-
- [
|
|
20
|
+
- [x] genarate controllers from template
|
|
21
|
+
- [x] based on openapi spec
|
|
22
22
|
|
|
23
|
-
- [
|
|
24
|
-
- [
|
|
25
|
-
|
|
26
|
-
## Dependencies
|
|
27
|
-
- [express](https://github.com/expressjs/express)
|
|
28
|
-
- [json2mongoose](https://github.com/Justin26l/json2mongoose)
|
|
29
|
-
- [js-yaml](https://github.com/nodeca/js-yaml)
|
|
23
|
+
- [x] genarate routes from template
|
|
24
|
+
- [x] based on controller
|
package/dist/cli.js
CHANGED
|
@@ -6,9 +6,22 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
6
6
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
7
|
const index_1 = __importDefault(require("./index"));
|
|
8
8
|
const fs_1 = __importDefault(require("fs"));
|
|
9
|
-
const
|
|
10
|
-
|
|
11
|
-
|
|
9
|
+
const minimist_1 = __importDefault(require("minimist"));
|
|
10
|
+
var args = (0, minimist_1.default)(process.argv.slice(2));
|
|
11
|
+
console.log('\x1b[35m%s\x1b[0m', '\n========== veryExpress CLI (vex) ==========\n');
|
|
12
|
+
if ('h' in args) {
|
|
13
|
+
console.log(`
|
|
14
|
+
Usage: vex [jsonSchemaDir] [openapiDir] [outputDir]
|
|
15
|
+
-h : Help
|
|
16
|
+
-j : jsonSchemaDir
|
|
17
|
+
-a : openApiDir
|
|
18
|
+
-o : outputDir
|
|
19
|
+
`);
|
|
20
|
+
process.exit(0);
|
|
21
|
+
}
|
|
22
|
+
const schemaDir = args.j || args.jsonSchemaDir || process.argv[2] || './jsonSchema';
|
|
23
|
+
const openapiDir = args.a || args.openApiDir || process.argv[3] || './openapi';
|
|
24
|
+
const outputDir = args.o || args.outputDir || process.argv[4] || './output';
|
|
12
25
|
if (!fs_1.default.existsSync(schemaDir)) {
|
|
13
26
|
console.log('\x1b[31m%s\x1b[0m', 'Schema Dir Not Found:', schemaDir);
|
|
14
27
|
process.exit(1);
|
|
@@ -18,4 +31,4 @@ if (!fs_1.default.existsSync(outputDir)) {
|
|
|
18
31
|
fs_1.default.mkdirSync(outputDir);
|
|
19
32
|
}
|
|
20
33
|
;
|
|
21
|
-
(0, index_1.default)(schemaDir,
|
|
34
|
+
(0, index_1.default)(schemaDir, openapiDir, outputDir);
|
|
@@ -31,6 +31,7 @@ const fs_1 = __importDefault(require("fs"));
|
|
|
31
31
|
const js_yaml_1 = __importDefault(require("js-yaml"));
|
|
32
32
|
const templates_1 = __importDefault(require("../templates"));
|
|
33
33
|
const types = __importStar(require("../types/types"));
|
|
34
|
+
const log_1 = __importDefault(require("../utils/log"));
|
|
34
35
|
function compile(openapiPath, controllerToModelDir, outDir, options) {
|
|
35
36
|
const file = fs_1.default.readFileSync(openapiPath, 'utf8');
|
|
36
37
|
const openApi = js_yaml_1.default.load(file);
|
|
@@ -38,7 +39,7 @@ function compile(openapiPath, controllerToModelDir, outDir, options) {
|
|
|
38
39
|
Object.keys(openApi.paths).forEach((endpoint) => {
|
|
39
40
|
const interfaceName = openApi.paths[endpoint].summary;
|
|
40
41
|
endpointsValidator[endpoint] = {};
|
|
41
|
-
|
|
42
|
+
log_1.default.process(`Controller : ${openapiPath} > ${endpoint}`);
|
|
42
43
|
Object.keys(openApi.paths[endpoint]).forEach((method) => {
|
|
43
44
|
if (!Object.values(types.method).includes(method))
|
|
44
45
|
return;
|
|
@@ -62,7 +63,7 @@ function compile(openapiPath, controllerToModelDir, outDir, options) {
|
|
|
62
63
|
;
|
|
63
64
|
const outPath = `${outDir}/${interfaceName}Controller.gen.ts`;
|
|
64
65
|
const controllerToModelPath = `${controllerToModelDir}/${interfaceName}Model.gen`;
|
|
65
|
-
|
|
66
|
+
log_1.default.writing(`Controller : ${outPath}`);
|
|
66
67
|
fs_1.default.writeFileSync(outPath, templates_1.default.controllerTemplate({
|
|
67
68
|
endpoint: endpoint,
|
|
68
69
|
modelPath: controllerToModelPath,
|
|
@@ -31,7 +31,8 @@ const json_schema_to_openapi_schema_1 = __importDefault(require("json-schema-to-
|
|
|
31
31
|
const js_yaml_1 = __importDefault(require("js-yaml"));
|
|
32
32
|
const fs_1 = __importDefault(require("fs"));
|
|
33
33
|
const types = __importStar(require("../types/types"));
|
|
34
|
-
const
|
|
34
|
+
const common_1 = __importDefault(require("../utils/common"));
|
|
35
|
+
const log_1 = __importDefault(require("../utils/log"));
|
|
35
36
|
function compile(schemaDir, outPath) {
|
|
36
37
|
let openapiJson = {
|
|
37
38
|
openapi: "3.0.0",
|
|
@@ -49,7 +50,7 @@ function compile(schemaDir, outPath) {
|
|
|
49
50
|
files.forEach((file) => {
|
|
50
51
|
if (!file.endsWith('.json'))
|
|
51
52
|
return;
|
|
52
|
-
|
|
53
|
+
log_1.default.process(`OpenApi : ${schemaDir + '/' + file}`);
|
|
53
54
|
const jsonSchemaBuffer = fs_1.default.readFileSync(`${schemaDir}/${file}`);
|
|
54
55
|
const jsonSchema = JSON.parse(jsonSchemaBuffer.toString());
|
|
55
56
|
openapiJson = jsonToOpenapiPath(openapiJson, jsonSchema);
|
|
@@ -57,7 +58,7 @@ function compile(schemaDir, outPath) {
|
|
|
57
58
|
});
|
|
58
59
|
const validOpenApi = (0, json_schema_to_openapi_schema_1.default)(openapiJson, { version: 3.0 });
|
|
59
60
|
const openapiYaml = js_yaml_1.default.dump(validOpenApi);
|
|
60
|
-
|
|
61
|
+
log_1.default.writing(`OpenApi : ${outPath}`);
|
|
61
62
|
fs_1.default.writeFileSync(outPath, openapiYaml);
|
|
62
63
|
}
|
|
63
64
|
exports.compile = compile;
|
|
@@ -67,7 +68,7 @@ function jsonToOpenapiPath(openapiJson, jsonSchema) {
|
|
|
67
68
|
const lowerDocName = documentConfig.documentName.toLowerCase();
|
|
68
69
|
const interfaceName = documentConfig.interfaceName;
|
|
69
70
|
const properties = jsonSchema.properties;
|
|
70
|
-
const methods = documentConfig.
|
|
71
|
+
const methods = documentConfig.methods;
|
|
71
72
|
let routes = {
|
|
72
73
|
['/' + lowerDocName]: {
|
|
73
74
|
summary: interfaceName,
|
|
@@ -155,14 +156,14 @@ function jsonToOpenapiComponentSchema(openapiJson, jsonSchema) {
|
|
|
155
156
|
const documentConfig = jsonSchema["x-documentConfig"];
|
|
156
157
|
const lowerDocName = documentConfig.documentName.toLowerCase();
|
|
157
158
|
const interfaceName = documentConfig.interfaceName;
|
|
158
|
-
const methods = documentConfig.
|
|
159
|
+
const methods = documentConfig.methods;
|
|
159
160
|
const componentSchemaResponse = {
|
|
160
161
|
type: 'object',
|
|
161
|
-
properties: (0, json_schema_to_openapi_schema_1.default)(
|
|
162
|
+
properties: (0, json_schema_to_openapi_schema_1.default)(common_1.default.cleanXcustomValue(jsonSchema.properties, ['index', 'unique', 'required']), { version: 3.0 }),
|
|
162
163
|
};
|
|
163
164
|
const componentSchemaBody = {
|
|
164
165
|
type: 'object',
|
|
165
|
-
properties: (0, json_schema_to_openapi_schema_1.default)(
|
|
166
|
+
properties: (0, json_schema_to_openapi_schema_1.default)(common_1.default.cleanXcustomValue(jsonSchema.properties, ['_id', 'index', 'unique', 'required']), { version: 3.0 }),
|
|
166
167
|
};
|
|
167
168
|
methods.forEach((method) => {
|
|
168
169
|
switch (method) {
|
|
@@ -6,8 +6,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
6
6
|
exports.compile = void 0;
|
|
7
7
|
const fs_1 = __importDefault(require("fs"));
|
|
8
8
|
const templates_1 = __importDefault(require("../templates"));
|
|
9
|
+
const log_1 = __importDefault(require("../utils/log"));
|
|
9
10
|
function compile(routesArr, openapiPath, outPath, options) {
|
|
10
|
-
|
|
11
|
+
log_1.default.writing(`Router : ${outPath}`);
|
|
11
12
|
fs_1.default.writeFileSync(outPath, templates_1.default.routesTemplate({
|
|
12
13
|
routes: routesArr,
|
|
13
14
|
openapiPath: openapiPath,
|
|
@@ -6,19 +6,20 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
6
6
|
exports.compile = void 0;
|
|
7
7
|
const fs_1 = __importDefault(require("fs"));
|
|
8
8
|
const templates_1 = __importDefault(require("../templates"));
|
|
9
|
+
const log_1 = __importDefault(require("../utils/log"));
|
|
9
10
|
function compile(outDir, options) {
|
|
10
11
|
const serverOutPath = `${outDir}/server.ts`;
|
|
11
12
|
const envOutPath = `${outDir}/.env`;
|
|
12
13
|
const gitignoreOutPath = `${outDir}/.gitignore`;
|
|
13
|
-
|
|
14
|
+
log_1.default.writing(`Server : ${serverOutPath}`);
|
|
14
15
|
fs_1.default.writeFileSync(outDir + '/server.ts', templates_1.default.serverTemplate({
|
|
15
16
|
options: options,
|
|
16
17
|
}));
|
|
17
|
-
|
|
18
|
+
log_1.default.writing(`Server : ${envOutPath}`);
|
|
18
19
|
fs_1.default.writeFileSync(`${outDir}/.env`, `# veryExpress generated
|
|
19
20
|
VERYEXPRESS_PORT=3000
|
|
20
21
|
`);
|
|
21
|
-
|
|
22
|
+
log_1.default.writing(`Server : ${gitignoreOutPath}`);
|
|
22
23
|
fs_1.default.writeFileSync(`${outDir}/.env`, `# veryExpress generated
|
|
23
24
|
node_modules
|
|
24
25
|
dist
|
package/dist/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import
|
|
2
|
-
export default function generate(schemaDir: string, openapiDir: string, outDir: string, options?: compilerOptions): void;
|
|
1
|
+
import * as types from './types/types';
|
|
2
|
+
export default function generate(schemaDir: string, openapiDir: string, outDir: string, options?: types.compilerOptions): void;
|
package/dist/index.js
CHANGED
|
@@ -29,7 +29,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
29
29
|
const fs_1 = __importDefault(require("fs"));
|
|
30
30
|
const json2mongoose_1 = __importDefault(require("json2mongoose"));
|
|
31
31
|
const openapiGen = __importStar(require("./generators/openapi.generator"));
|
|
32
|
-
const
|
|
32
|
+
const log_1 = __importDefault(require("./utils/log"));
|
|
33
|
+
const common_1 = __importDefault(require("./utils/common"));
|
|
33
34
|
const controllerGen = __importStar(require("./generators/controllers.generator"));
|
|
34
35
|
const routeGen = __importStar(require("./generators/routes.generator"));
|
|
35
36
|
const serverGen = __importStar(require("./generators/server.generator"));
|
|
@@ -50,38 +51,53 @@ function generate(schemaDir, openapiDir, outDir, options) {
|
|
|
50
51
|
if (!fs_1.default.existsSync(openapiDir)) {
|
|
51
52
|
fs_1.default.mkdirSync(openapiDir);
|
|
52
53
|
}
|
|
54
|
+
;
|
|
53
55
|
Object.values(dir).forEach((path) => {
|
|
54
56
|
if (!fs_1.default.existsSync(path)) {
|
|
55
57
|
fs_1.default.mkdirSync(path);
|
|
56
58
|
}
|
|
59
|
+
;
|
|
57
60
|
});
|
|
58
|
-
openapiGen.compile(schemaDir, openapiDir + '/openapi.gen.yaml');
|
|
59
|
-
utils_1.default.copyDir(`${openapiDir}`, outDir + '/openapi');
|
|
60
|
-
utils_1.default.copyDir(`${__dirname}/templates/utils`, dir.utilsDir);
|
|
61
61
|
let routeData = [];
|
|
62
62
|
const files = fs_1.default.readdirSync(schemaDir);
|
|
63
63
|
files.forEach((schemaFileName) => {
|
|
64
64
|
try {
|
|
65
65
|
if (!schemaFileName.endsWith('.json'))
|
|
66
66
|
return;
|
|
67
|
-
const
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
67
|
+
const file = fs_1.default.readFileSync(`${schemaDir}/${schemaFileName}`);
|
|
68
|
+
const jsonSchema = JSON.parse(file.toString());
|
|
69
|
+
const documentConfig = jsonSchema["x-documentConfig"];
|
|
70
|
+
if (!documentConfig)
|
|
71
|
+
log_1.default.error(`x-documentConfig not found in ${schemaDir}/${schemaFileName}`);
|
|
72
|
+
if (!documentConfig.documentName)
|
|
73
|
+
log_1.default.error(`x-documentConfig.documentName not found in ${schemaDir}/${schemaFileName}`);
|
|
74
|
+
if (!documentConfig.interfaceName)
|
|
75
|
+
log_1.default.error(`x-documentConfig.interfaceName not found in ${schemaDir}/${schemaFileName}`);
|
|
76
|
+
if (!documentConfig.methods)
|
|
77
|
+
log_1.default.error(`x-documentConfig.methods is not found in ${schemaDir}/${schemaFileName}`);
|
|
78
|
+
if (!Array.isArray(documentConfig.methods))
|
|
79
|
+
log_1.default.error(`x-documentConfig.methods is invalid in ${schemaDir}/${schemaFileName}`);
|
|
80
|
+
if (typeof jsonSchema.properties !== 'object')
|
|
81
|
+
log_1.default.error(`properties is invalid in ${schemaDir}/${schemaFileName}`);
|
|
82
|
+
json2mongoose_1.default.typesGen.compileFromFile(`${schemaDir}/${schemaFileName}`, `${dir.typeDir}/${documentConfig.interfaceName}.gen.ts`, options || common_1.default.defaultCompilerOptions);
|
|
83
|
+
json2mongoose_1.default.modelsGen.compileFromFile(`${schemaDir}/${schemaFileName}`, `${common_1.default.relativePath(dir.modelDir, dir.typeDir)}/${documentConfig.interfaceName}.gen`, `${dir.modelDir}/${documentConfig.interfaceName}Model.gen.ts`, options || common_1.default.defaultCompilerOptions);
|
|
71
84
|
routeData.push({
|
|
72
|
-
route: `/${
|
|
73
|
-
controllerClassName:
|
|
74
|
-
controllerPath:
|
|
85
|
+
route: `/${documentConfig.documentName}`,
|
|
86
|
+
controllerClassName: documentConfig.interfaceName,
|
|
87
|
+
controllerPath: common_1.default.relativePath(dir.routeDir, dir.controllerDir) + '/' + documentConfig.interfaceName + 'Controller.gen',
|
|
75
88
|
});
|
|
76
89
|
}
|
|
77
90
|
catch (err) {
|
|
78
|
-
|
|
91
|
+
log_1.default.error(`Processing File : ${schemaDir}/${schemaFileName}\n`, err);
|
|
79
92
|
}
|
|
80
93
|
;
|
|
81
94
|
});
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
95
|
+
openapiGen.compile(schemaDir, openapiDir + '/openapi.gen.yaml');
|
|
96
|
+
common_1.default.copyDir(`${openapiDir}`, outDir + '/openapi');
|
|
97
|
+
common_1.default.copyDir(`${__dirname}/templates/utils`, dir.utilsDir);
|
|
98
|
+
controllerGen.compile(openapiDir + '/openapi.gen.yaml', `${common_1.default.relativePath(dir.controllerDir, dir.modelDir)}`, dir.controllerDir, options || common_1.default.defaultCompilerOptions);
|
|
99
|
+
routeGen.compile(routeData, openapiDir + '/openapi.gen.yaml', `${dir.routeDir}/routes.gen.ts`, options || common_1.default.defaultCompilerOptions);
|
|
100
|
+
serverGen.compile(outDir, options || { headerComment: common_1.default.getSimpleHeaderComment() });
|
|
85
101
|
}
|
|
86
102
|
exports.default = generate;
|
|
87
103
|
;
|
|
@@ -21,52 +21,19 @@ class {{interfaceName}}Controller {
|
|
|
21
21
|
}
|
|
22
22
|
|
|
23
23
|
public routes() {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
this.get{{interfaceName}}.bind(this)
|
|
38
|
-
);
|
|
39
|
-
|
|
40
|
-
this.router.post(
|
|
41
|
-
'/',
|
|
42
|
-
[
|
|
43
|
-
{{postValidator}}
|
|
44
|
-
],
|
|
45
|
-
this.create{{interfaceName}}.bind(this)
|
|
46
|
-
);
|
|
47
|
-
|
|
48
|
-
this.router.put(
|
|
49
|
-
'/:id',
|
|
50
|
-
[
|
|
51
|
-
{{putValidator}}
|
|
52
|
-
],
|
|
53
|
-
this.update{{interfaceName}}.bind(this)
|
|
54
|
-
);
|
|
55
|
-
this.router.patch(
|
|
56
|
-
'/:id',
|
|
57
|
-
[
|
|
58
|
-
{{patchValidator}}
|
|
59
|
-
],
|
|
60
|
-
this.update{{interfaceName}}.bind(this)
|
|
61
|
-
);
|
|
62
|
-
|
|
63
|
-
this.router.delete(
|
|
64
|
-
'/:id',
|
|
65
|
-
[
|
|
66
|
-
{{deleteValidator}}
|
|
67
|
-
],
|
|
68
|
-
this.delete{{interfaceName}}.bind(this)
|
|
69
|
-
);
|
|
24
|
+
|
|
25
|
+
{{getListRoute}}
|
|
26
|
+
|
|
27
|
+
{{getOneRoute}}
|
|
28
|
+
|
|
29
|
+
{{postRoute}}
|
|
30
|
+
|
|
31
|
+
{{putRoute}}
|
|
32
|
+
|
|
33
|
+
{{patchRoute}}
|
|
34
|
+
|
|
35
|
+
{{deleteRoute}}
|
|
36
|
+
|
|
70
37
|
};
|
|
71
38
|
|
|
72
39
|
public async get{{interfaceName}}(req: Request, res: Response): Promise<Response> {
|
|
@@ -88,7 +55,7 @@ class {{interfaceName}}Controller {
|
|
|
88
55
|
}
|
|
89
56
|
}
|
|
90
57
|
|
|
91
|
-
public async
|
|
58
|
+
public async getList{{interfaceName}}(req: Request, res: Response): Promise<Response> {
|
|
92
59
|
try {
|
|
93
60
|
const user = await {{interfaceName}}Model.find();
|
|
94
61
|
if (!user) {
|
|
@@ -150,12 +117,33 @@ export default new {{interfaceName}}Controller().router;
|
|
|
150
117
|
template = template.replace(/{{headerComment}}/g, templateOptions.headerComment);
|
|
151
118
|
template = template.replace(/{{interfaceName}}/g, templateOptions.interfaceName);
|
|
152
119
|
template = template.replace(/{{modelPath}}/g, templateOptions.modelPath);
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
120
|
+
const indent2 = '\n ';
|
|
121
|
+
const indent3 = '\n ';
|
|
122
|
+
const indent4 = '\n ';
|
|
123
|
+
if (templateOptions.validator[templateOptions.endpoint].get) {
|
|
124
|
+
template = template.replace(/{{getListRoute}}/g, `this.router.get('/', ${indent3}[${indent4 + templateOptions.validator[templateOptions.endpoint].get.join(',' + indent4) + indent3}],${indent3}this.getList${templateOptions.interfaceName}.bind(this)${indent2});`);
|
|
125
|
+
}
|
|
126
|
+
;
|
|
127
|
+
if (templateOptions.validator[templateOptions.endpoint + '/{id}'].get) {
|
|
128
|
+
template = template.replace(/{{getOneRoute}}/g, `this.router.get('/:id', ${indent3}[${indent4 + templateOptions.validator[templateOptions.endpoint + '/{id}'].get.join(',' + indent4) + indent3}],${indent3}this.get${templateOptions.interfaceName}.bind(this)${indent2});`);
|
|
129
|
+
}
|
|
130
|
+
;
|
|
131
|
+
if (templateOptions.validator[templateOptions.endpoint].post) {
|
|
132
|
+
template = template.replace(/{{postRoute}}/g, `this.router.post('/', ${indent3}[${indent4 + templateOptions.validator[templateOptions.endpoint].post.join(',' + indent4) + indent3}],${indent3}this.create${templateOptions.interfaceName}.bind(this)${indent2});`);
|
|
133
|
+
}
|
|
134
|
+
;
|
|
135
|
+
if (templateOptions.validator[templateOptions.endpoint + '/{id}'].put) {
|
|
136
|
+
template = template.replace(/{{putRoute}}/g, `this.router.put('/:id', ${indent3}[${indent4 + templateOptions.validator[templateOptions.endpoint + '/{id}'].put.join(',' + indent4) + indent3}],${indent3}this.update${templateOptions.interfaceName}.bind(this)${indent2});`);
|
|
137
|
+
}
|
|
138
|
+
;
|
|
139
|
+
if (templateOptions.validator[templateOptions.endpoint + '/{id}'].patch) {
|
|
140
|
+
template = template.replace(/{{patchRoute}}/g, `this.router.patch('/:id', ${indent3}[${indent4 + templateOptions.validator[templateOptions.endpoint + '/{id}'].patch.join(',' + indent4) + indent3}],${indent3}this.update${templateOptions.interfaceName}.bind(this)${indent2});`);
|
|
141
|
+
}
|
|
142
|
+
;
|
|
143
|
+
if (templateOptions.validator[templateOptions.endpoint + '/{id}'].delete) {
|
|
144
|
+
template = template.replace(/{{deleteRoute}}/g, `this.router.delete('/:id', ${indent3}[${indent4 + templateOptions.validator[templateOptions.endpoint + '/{id}'].delete.join(',' + indent4) + indent3}],${indent3}this.delete${templateOptions.interfaceName}.bind(this)${indent2});`);
|
|
145
|
+
}
|
|
146
|
+
;
|
|
159
147
|
return template;
|
|
160
148
|
}
|
|
161
149
|
exports.default = controllerTemplate;
|
|
@@ -6,12 +6,13 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
6
6
|
exports.loadYaml = void 0;
|
|
7
7
|
const js_yaml_1 = __importDefault(require("js-yaml"));
|
|
8
8
|
const fs_1 = __importDefault(require("fs"));
|
|
9
|
+
const log_1 = __importDefault(require("../../utils/log"));
|
|
9
10
|
function loadYaml(yamlFilePath) {
|
|
10
11
|
try {
|
|
11
12
|
return js_yaml_1.default.load(fs_1.default.readFileSync(yamlFilePath, 'utf8'));
|
|
12
13
|
}
|
|
13
14
|
catch (e) {
|
|
14
|
-
|
|
15
|
+
log_1.default.error('Error Load OpenApi File :\n', e.message || e);
|
|
15
16
|
}
|
|
16
17
|
}
|
|
17
18
|
exports.loadYaml = loadYaml;
|
package/dist/types/types.d.ts
CHANGED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { compilerOptions } from "../types/types";
|
|
2
|
+
export declare const relativePath: (fromPath: string, toPath: string) => string;
|
|
3
|
+
export declare function loadYaml(yamlFilePath: string): void;
|
|
4
|
+
export declare function getGenaratorHeaderComment(comment?: string): string;
|
|
5
|
+
export declare function getSimpleHeaderComment(): string;
|
|
6
|
+
export declare function cleanXcustomValue(schemaObj: {
|
|
7
|
+
[key: string]: any;
|
|
8
|
+
}, additionalKeyArr?: string[]): {
|
|
9
|
+
[key: string]: any;
|
|
10
|
+
};
|
|
11
|
+
export declare function copyDir(source: string, destination: string): void;
|
|
12
|
+
export declare const defaultCompilerOptions: compilerOptions;
|
|
13
|
+
declare const _default: {
|
|
14
|
+
getGenaratorHeaderComment: typeof getGenaratorHeaderComment;
|
|
15
|
+
getSimpleHeaderComment: typeof getSimpleHeaderComment;
|
|
16
|
+
defaultCompilerOptions: compilerOptions;
|
|
17
|
+
relativePath: (fromPath: string, toPath: string) => string;
|
|
18
|
+
cleanXcustomValue: typeof cleanXcustomValue;
|
|
19
|
+
loadYaml: typeof loadYaml;
|
|
20
|
+
copyDir: typeof copyDir;
|
|
21
|
+
};
|
|
22
|
+
export default _default;
|
|
@@ -0,0 +1,105 @@
|
|
|
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
|
+
exports.defaultCompilerOptions = exports.copyDir = exports.cleanXcustomValue = exports.getSimpleHeaderComment = exports.getGenaratorHeaderComment = exports.loadYaml = exports.relativePath = void 0;
|
|
7
|
+
const fs_1 = __importDefault(require("fs"));
|
|
8
|
+
const path_1 = __importDefault(require("path"));
|
|
9
|
+
const js_yaml_1 = __importDefault(require("js-yaml"));
|
|
10
|
+
const log_1 = __importDefault(require("./log"));
|
|
11
|
+
function getPackageInfo() {
|
|
12
|
+
var _a;
|
|
13
|
+
const packageJson = fs_1.default.readFileSync("./package.json", "utf8");
|
|
14
|
+
const packageJsonObject = JSON.parse(packageJson);
|
|
15
|
+
return {
|
|
16
|
+
version: ((_a = packageJsonObject.dependencies['very-express']) === null || _a === void 0 ? void 0 : _a.replace('^', '')) || "[unknown version]",
|
|
17
|
+
author: packageJsonObject.author || "justin26l",
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
const relativePath = (fromPath, toPath) => {
|
|
21
|
+
return path_1.default.relative(fromPath, toPath).replace(/\\/g, '/');
|
|
22
|
+
};
|
|
23
|
+
exports.relativePath = relativePath;
|
|
24
|
+
function loadYaml(yamlFilePath) {
|
|
25
|
+
try {
|
|
26
|
+
const doc = js_yaml_1.default.load(fs_1.default.readFileSync(yamlFilePath, 'utf8'));
|
|
27
|
+
}
|
|
28
|
+
catch (e) {
|
|
29
|
+
log_1.default.error('Error Load OpenApi File :\n', e.message || e);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
exports.loadYaml = loadYaml;
|
|
33
|
+
;
|
|
34
|
+
function getGenaratorHeaderComment(comment) {
|
|
35
|
+
const packageInfo = getPackageInfo();
|
|
36
|
+
return `/* eslint-disable */
|
|
37
|
+
/**
|
|
38
|
+
* Generated by veryExpress@${packageInfo.version}
|
|
39
|
+
* DO NOT MODIFY MANUALLY. Instead, modify the JsonSchema file,
|
|
40
|
+
* then run veryExpress to regenerate this file.
|
|
41
|
+
*
|
|
42
|
+
* author: ${packageInfo.author}
|
|
43
|
+
* version: ${packageInfo.version}${comment ? `\n * comment: ${comment}` : ''}
|
|
44
|
+
*/
|
|
45
|
+
`;
|
|
46
|
+
}
|
|
47
|
+
exports.getGenaratorHeaderComment = getGenaratorHeaderComment;
|
|
48
|
+
;
|
|
49
|
+
function getSimpleHeaderComment() {
|
|
50
|
+
const packageInfo = getPackageInfo();
|
|
51
|
+
return `// Generated by veryExpress@${packageInfo.version}`;
|
|
52
|
+
}
|
|
53
|
+
exports.getSimpleHeaderComment = getSimpleHeaderComment;
|
|
54
|
+
;
|
|
55
|
+
function cleanXcustomValue(schemaObj, additionalKeyArr) {
|
|
56
|
+
let obj = Object.assign(schemaObj);
|
|
57
|
+
for (const key in obj) {
|
|
58
|
+
if (key.startsWith("x-")) {
|
|
59
|
+
delete obj[key];
|
|
60
|
+
}
|
|
61
|
+
else if (additionalKeyArr && additionalKeyArr.includes(key)) {
|
|
62
|
+
delete obj[key];
|
|
63
|
+
}
|
|
64
|
+
else if (typeof obj[key] === "object") {
|
|
65
|
+
cleanXcustomValue(obj[key], additionalKeyArr);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
return obj;
|
|
69
|
+
}
|
|
70
|
+
exports.cleanXcustomValue = cleanXcustomValue;
|
|
71
|
+
;
|
|
72
|
+
function copyDir(source, destination) {
|
|
73
|
+
if (!fs_1.default.existsSync(destination)) {
|
|
74
|
+
fs_1.default.mkdirSync(destination);
|
|
75
|
+
}
|
|
76
|
+
const files = fs_1.default.readdirSync(source);
|
|
77
|
+
for (let i = 0; i < files.length; i++) {
|
|
78
|
+
const current = fs_1.default.lstatSync(source + '/' + files[i]);
|
|
79
|
+
if (current.isDirectory()) {
|
|
80
|
+
copyDir(source + '/' + files[i], destination + '/' + files[i]);
|
|
81
|
+
}
|
|
82
|
+
else if (current.isSymbolicLink()) {
|
|
83
|
+
const symlink = fs_1.default.readlinkSync(source + '/' + files[i]);
|
|
84
|
+
fs_1.default.symlinkSync(symlink, destination + '/' + files[i]);
|
|
85
|
+
}
|
|
86
|
+
else {
|
|
87
|
+
const outPath = destination + '/' + files[i];
|
|
88
|
+
log_1.default.writing(`Utils : ${outPath}`);
|
|
89
|
+
fs_1.default.copyFileSync(source + '/' + files[i], outPath);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
exports.copyDir = copyDir;
|
|
94
|
+
exports.defaultCompilerOptions = {
|
|
95
|
+
headerComment: getGenaratorHeaderComment(),
|
|
96
|
+
};
|
|
97
|
+
exports.default = {
|
|
98
|
+
getGenaratorHeaderComment,
|
|
99
|
+
getSimpleHeaderComment,
|
|
100
|
+
defaultCompilerOptions: exports.defaultCompilerOptions,
|
|
101
|
+
relativePath: exports.relativePath,
|
|
102
|
+
cleanXcustomValue,
|
|
103
|
+
loadYaml,
|
|
104
|
+
copyDir,
|
|
105
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export declare function processing(msg: any, ...optionalParams: any[]): void;
|
|
2
|
+
export declare function writing(msg: any, ...optionalParams: any[]): void;
|
|
3
|
+
export declare function error(msg: any, ...optionalParams: any[]): void;
|
|
4
|
+
declare const _default: {
|
|
5
|
+
process: typeof processing;
|
|
6
|
+
writing: typeof writing;
|
|
7
|
+
error: typeof error;
|
|
8
|
+
};
|
|
9
|
+
export default _default;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.error = exports.writing = exports.processing = void 0;
|
|
4
|
+
function processing(msg, ...optionalParams) {
|
|
5
|
+
console.log('\x1b[36m%s\x1b[0m', '[Processing]', msg, optionalParams.length > 0 ? optionalParams : '');
|
|
6
|
+
}
|
|
7
|
+
exports.processing = processing;
|
|
8
|
+
function writing(msg, ...optionalParams) {
|
|
9
|
+
console.log('\x1b[32m%s\x1b[0m', '[Writing]', msg, optionalParams.length > 0 ? optionalParams : '');
|
|
10
|
+
}
|
|
11
|
+
exports.writing = writing;
|
|
12
|
+
function error(msg, ...optionalParams) {
|
|
13
|
+
console.log('\x1b[31m%s\x1b[0m', '[Error]', msg, optionalParams.length > 0 ? optionalParams : '');
|
|
14
|
+
process.exit(1);
|
|
15
|
+
}
|
|
16
|
+
exports.error = error;
|
|
17
|
+
exports.default = {
|
|
18
|
+
process: processing,
|
|
19
|
+
writing,
|
|
20
|
+
error,
|
|
21
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "very-express",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.5",
|
|
4
4
|
"description": "based on json schema and openapi, generate Express RestApi server with mongoDb ",
|
|
5
5
|
"author": "justin26l",
|
|
6
6
|
"license": "MIT",
|
|
@@ -24,16 +24,18 @@
|
|
|
24
24
|
],
|
|
25
25
|
"repository": {
|
|
26
26
|
"type": "git",
|
|
27
|
-
"url": "git+https://github.com/Justin26l/
|
|
27
|
+
"url": "git+https://github.com/Justin26l/VeryExpress.git"
|
|
28
28
|
},
|
|
29
29
|
"dependencies": {
|
|
30
30
|
"js-yaml": "4.1.0",
|
|
31
31
|
"json-schema-to-openapi-schema": "^0.4.0",
|
|
32
|
-
"json2mongoose": "^0.1.5"
|
|
32
|
+
"json2mongoose": "^0.1.5",
|
|
33
|
+
"minimist": "^1.2.8"
|
|
33
34
|
},
|
|
34
35
|
"devDependencies": {
|
|
35
36
|
"@types/express": "^4.17.21",
|
|
36
37
|
"@types/js-yaml": "^4.0.9",
|
|
38
|
+
"@types/minimist": "^1.2.5",
|
|
37
39
|
"@types/node": "^20.10.4",
|
|
38
40
|
"@types/swagger-ui-express": "^4.1.6",
|
|
39
41
|
"dotenv": "^16.3.1",
|