@zenorm/generate 1.10.1 → 2.0.1
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/dist/bin/zenorm-generate.js +11 -10
- package/dist/generate.d.ts +1 -1
- package/dist/generate.js +22 -26
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -17
- package/dist/types.js +1 -2
- package/dist/utils.d.ts +1 -1
- package/dist/utils.js +10 -18
- package/package.json +9 -9
|
@@ -1,26 +1,27 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const generate_1 = require("../generate");
|
|
6
|
-
function getConfig(filename) {
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { generate } from '../generate.js';
|
|
4
|
+
async function getConfig(filename) {
|
|
7
5
|
const configFile = path.join(process.cwd(), filename);
|
|
8
|
-
const config =
|
|
6
|
+
const config = (await import(configFile)).default;
|
|
9
7
|
return Object.assign({
|
|
10
8
|
backend: '@zenorm/generate-mysql',
|
|
11
9
|
}, config);
|
|
12
10
|
}
|
|
13
11
|
async function main(configFilename) {
|
|
14
12
|
const config = await getConfig(configFilename);
|
|
15
|
-
const call =
|
|
16
|
-
await
|
|
13
|
+
const call = (await import(config.backend)).default;
|
|
14
|
+
await generate(call()(config), config);
|
|
17
15
|
}
|
|
18
16
|
if (!process.argv[2]) {
|
|
19
|
-
console.log('zenorm-generate config.
|
|
17
|
+
console.log('zenorm-generate config.js');
|
|
20
18
|
process.exit(1);
|
|
21
19
|
}
|
|
22
20
|
else {
|
|
23
|
-
main(process.argv[2]).then(() =>
|
|
21
|
+
await main(process.argv[2]).then(() => {
|
|
22
|
+
console.log('Done.');
|
|
23
|
+
process.exit();
|
|
24
|
+
}, e => {
|
|
24
25
|
console.error(e);
|
|
25
26
|
process.exit(1);
|
|
26
27
|
});
|
package/dist/generate.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { GenerateConfig, TabelDescribe } from './types';
|
|
1
|
+
import { GenerateConfig, TabelDescribe } from './types.js';
|
|
2
2
|
export declare function generate(tables: AsyncGenerator<TabelDescribe>, cfg?: GenerateConfig): Promise<void>;
|
package/dist/generate.js
CHANGED
|
@@ -1,13 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
const pascal_case_1 = require("pascal-case");
|
|
7
|
-
const snake_case_1 = require("snake-case");
|
|
8
|
-
const utils_1 = require("./utils");
|
|
1
|
+
import { promises as fs } from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { pascalCase } from 'pascal-case';
|
|
4
|
+
import { snakeCase } from 'snake-case';
|
|
5
|
+
import { checkFileDir, currentDatetime, cwdPath, notExistsPut } from './utils.js';
|
|
9
6
|
const zenormName = process.env.ZENORM_NAME || 'zenorm';
|
|
10
|
-
async function generate(tables, cfg) {
|
|
7
|
+
export async function generate(tables, cfg) {
|
|
11
8
|
console.log('generate models...');
|
|
12
9
|
const config = Object.assign({
|
|
13
10
|
outputDir: './src/model',
|
|
@@ -16,19 +13,19 @@ async function generate(tables, cfg) {
|
|
|
16
13
|
staticMethods: true,
|
|
17
14
|
instanceMethods: true,
|
|
18
15
|
}, cfg);
|
|
19
|
-
const outputDir =
|
|
20
|
-
await
|
|
16
|
+
const outputDir = cwdPath(config.outputDir);
|
|
17
|
+
await checkFileDir(outputDir);
|
|
21
18
|
console.log('database:', config.database);
|
|
22
19
|
const remark = [
|
|
23
20
|
'// zenorm 自动生成文件',
|
|
24
21
|
'// 请不要修改此文件,因为此文件在每次重新生成数据库结构时会被覆盖',
|
|
25
|
-
`// create at: ${
|
|
22
|
+
`// create at: ${currentDatetime()}`,
|
|
26
23
|
`// create by: ${process.env.USER || process.env.USERNAME || '-'}@${process.env.COMPUTERNAME || '-'}`,
|
|
27
24
|
`// database: ${config.database}`,
|
|
28
25
|
];
|
|
29
26
|
const structs = [
|
|
30
27
|
...remark,
|
|
31
|
-
config.globalFilename ? `import _Global from './${config.globalFilename}';` : null,
|
|
28
|
+
config.globalFilename ? `import _Global from './${config.globalFilename}.js';` : null,
|
|
32
29
|
'',
|
|
33
30
|
];
|
|
34
31
|
const models = [];
|
|
@@ -41,8 +38,8 @@ async function generate(tables, cfg) {
|
|
|
41
38
|
// console.log('table:', tableName, 'ignore');
|
|
42
39
|
continue;
|
|
43
40
|
}
|
|
44
|
-
const className =
|
|
45
|
-
const name =
|
|
41
|
+
const className = pascalCase(tableName);
|
|
42
|
+
const name = snakeCase(tableName);
|
|
46
43
|
const outputFilename = path.join(outputDir, name + '.ts');
|
|
47
44
|
console.log('table:', tableName);
|
|
48
45
|
let pk;
|
|
@@ -77,10 +74,10 @@ async function generate(tables, cfg) {
|
|
|
77
74
|
structs.push('}');
|
|
78
75
|
structs.push('');
|
|
79
76
|
// model class
|
|
80
|
-
await
|
|
77
|
+
await notExistsPut(outputFilename, () => {
|
|
81
78
|
return [
|
|
82
79
|
`import { model } from '${zenormName}';`,
|
|
83
|
-
`import { ${className}Table } from './${config.tablesFilename}';`,
|
|
80
|
+
`import { ${className}Table } from './${config.tablesFilename}.js';`,
|
|
84
81
|
'',
|
|
85
82
|
`@model({`,
|
|
86
83
|
pk ? ` pk: '${pk}',` : null,
|
|
@@ -96,7 +93,7 @@ async function generate(tables, cfg) {
|
|
|
96
93
|
}
|
|
97
94
|
const tablesFilename = path.join(outputDir, config.tablesFilename + '.ts');
|
|
98
95
|
console.log(`write tables file: ${tablesFilename}`);
|
|
99
|
-
await
|
|
96
|
+
await fs.writeFile(tablesFilename, structs.filter(i => i !== null).join('\n'));
|
|
100
97
|
const imports = ['createRepositoryQuery'];
|
|
101
98
|
if (config.generateRepositories || config.bindQuery) {
|
|
102
99
|
imports.push('QueryParam');
|
|
@@ -104,8 +101,8 @@ async function generate(tables, cfg) {
|
|
|
104
101
|
const repositories = [
|
|
105
102
|
...remark,
|
|
106
103
|
`import { ${imports.join(', ')} } from '${zenormName}';`,
|
|
107
|
-
`import * as _tables from '
|
|
108
|
-
...models.map(({ name, className }) => `import _${className} from './${name}';`),
|
|
104
|
+
`import * as _tables from './${config.tablesFilename}.js';`,
|
|
105
|
+
...models.map(({ name, className }) => `import _${className} from './${name}.js';`),
|
|
109
106
|
];
|
|
110
107
|
// 绑定静态 Query
|
|
111
108
|
if (config.bindQuery) {
|
|
@@ -164,23 +161,22 @@ async function generate(tables, cfg) {
|
|
|
164
161
|
}
|
|
165
162
|
const repositoriesFilename = path.join(outputDir, config.repositoriesFilename + '.ts');
|
|
166
163
|
console.log(`write repositories file: ${repositoriesFilename}`);
|
|
167
|
-
await
|
|
164
|
+
await fs.writeFile(repositoriesFilename, repositories.join('\n'));
|
|
168
165
|
// 生成 global.ts
|
|
169
166
|
if (config.globalFilename) {
|
|
170
167
|
const globalFilename = path.join(outputDir, config.globalFilename + '.ts');
|
|
171
|
-
await
|
|
168
|
+
await notExistsPut(globalFilename, () => {
|
|
172
169
|
console.log(`write file: ${globalFilename}`);
|
|
173
170
|
return 'export default class Global {}';
|
|
174
171
|
});
|
|
175
172
|
}
|
|
176
173
|
// 生成 index.ts
|
|
177
174
|
const indexFilename = path.join(outputDir, 'index.ts');
|
|
178
|
-
await
|
|
175
|
+
await notExistsPut(indexFilename, () => {
|
|
179
176
|
console.log(`write file: ${indexFilename}`);
|
|
180
177
|
return [
|
|
181
|
-
`export * from './${config.tablesFilename}';`,
|
|
182
|
-
`export * from './${config.repositoriesFilename}';`,
|
|
178
|
+
`export * from './${config.tablesFilename}.js';`,
|
|
179
|
+
`export * from './${config.repositoriesFilename}.js';`,
|
|
183
180
|
].join('\n');
|
|
184
181
|
});
|
|
185
182
|
}
|
|
186
|
-
exports.generate = generate;
|
package/dist/index.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export * from './types';
|
|
1
|
+
export * from './types.js';
|
package/dist/index.js
CHANGED
|
@@ -1,17 +1 @@
|
|
|
1
|
-
|
|
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 __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./types"), exports);
|
|
1
|
+
export * from './types.js';
|
package/dist/types.js
CHANGED
|
@@ -1,2 +1 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1
|
+
export {};
|
package/dist/utils.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export declare function cwdPath(p: string): string;
|
|
2
|
-
export declare function checkFileDir(dir: string): Promise<
|
|
2
|
+
export declare function checkFileDir(dir: string): Promise<string | undefined>;
|
|
3
3
|
export declare function fileExists(f: string): Promise<boolean>;
|
|
4
4
|
/**
|
|
5
5
|
* 文件不存在则创建并写入内容
|
package/dist/utils.js
CHANGED
|
@@ -1,37 +1,29 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const fs_1 = require("fs");
|
|
5
|
-
const path = require("path");
|
|
6
|
-
function cwdPath(p) {
|
|
1
|
+
import { promises as fs } from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
export function cwdPath(p) {
|
|
7
4
|
if (p.startsWith('./')) {
|
|
8
5
|
return path.join(process.cwd(), p.slice(2));
|
|
9
6
|
}
|
|
10
7
|
return p;
|
|
11
8
|
}
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
return fs_1.promises.mkdir(dir, { recursive: true });
|
|
9
|
+
export function checkFileDir(dir) {
|
|
10
|
+
return fs.mkdir(dir, { recursive: true });
|
|
15
11
|
}
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
return fs_1.promises.access(f).then(() => true, () => false);
|
|
12
|
+
export function fileExists(f) {
|
|
13
|
+
return fs.access(f).then(() => true, () => false);
|
|
19
14
|
}
|
|
20
|
-
exports.fileExists = fileExists;
|
|
21
15
|
/**
|
|
22
16
|
* 文件不存在则创建并写入内容
|
|
23
17
|
* @param filename
|
|
24
18
|
* @param getContent
|
|
25
19
|
*/
|
|
26
|
-
async function notExistsPut(filename, getContent) {
|
|
20
|
+
export async function notExistsPut(filename, getContent) {
|
|
27
21
|
if (!await fileExists(filename)) {
|
|
28
|
-
await
|
|
22
|
+
await fs.writeFile(filename, await getContent());
|
|
29
23
|
return true;
|
|
30
24
|
}
|
|
31
25
|
return false;
|
|
32
26
|
}
|
|
33
|
-
|
|
34
|
-
function currentDatetime() {
|
|
27
|
+
export function currentDatetime() {
|
|
35
28
|
return new Date().toLocaleString();
|
|
36
29
|
}
|
|
37
|
-
exports.currentDatetime = currentDatetime;
|
package/package.json
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zenorm/generate",
|
|
3
|
+
"type": "module",
|
|
3
4
|
"description": "Easy ORM, easy query. easy typing! Auto generate typescript declaration.",
|
|
4
|
-
"version": "
|
|
5
|
+
"version": "2.0.1",
|
|
5
6
|
"exports": "./dist/index.js",
|
|
6
7
|
"types": "./dist/index.d.ts",
|
|
7
8
|
"homepage": "https://zenorm.node.ltd",
|
|
@@ -16,8 +17,8 @@
|
|
|
16
17
|
"scripts": {
|
|
17
18
|
"build": "rimraf dist && tsc",
|
|
18
19
|
"prepack": "npm run build",
|
|
19
|
-
"gen": "cd test && ts-node ../src/bin/zenorm-generate.ts config.
|
|
20
|
-
"t1": "cd test && cross-env DEBUG=* ts-node t1"
|
|
20
|
+
"gen": "cd test && node --loader ts-node/esm ../src/bin/zenorm-generate.ts config.js",
|
|
21
|
+
"t1": "cd test && cross-env DEBUG=* node --loader ts-node/esm t1.ts"
|
|
21
22
|
},
|
|
22
23
|
"keywords": [
|
|
23
24
|
"mysql",
|
|
@@ -28,18 +29,17 @@
|
|
|
28
29
|
"database"
|
|
29
30
|
],
|
|
30
31
|
"devDependencies": {
|
|
31
|
-
"@types/node": "^
|
|
32
|
-
"@zenorm/generate-mysql": "^
|
|
32
|
+
"@types/node": "^16",
|
|
33
|
+
"@zenorm/generate-mysql": "^2.0.1",
|
|
33
34
|
"cross-env": "^7.0.3",
|
|
34
35
|
"mysql-easy-query": "^3.13.0",
|
|
35
36
|
"rimraf": "^4.4.1",
|
|
36
37
|
"ts-node": "^10.9.1",
|
|
37
|
-
"typescript": "^5.
|
|
38
|
-
"zenorm": "^
|
|
38
|
+
"typescript": "^5.6.3",
|
|
39
|
+
"zenorm": "^4.0.0"
|
|
39
40
|
},
|
|
40
41
|
"dependencies": {
|
|
41
42
|
"pascal-case": "^3.1.2",
|
|
42
43
|
"snake-case": "^3.0.4"
|
|
43
|
-
}
|
|
44
|
-
"packageManager": "yarn@1.22.21+sha1.1959a18351b811cdeedbd484a8f86c3cc3bbaf72"
|
|
44
|
+
}
|
|
45
45
|
}
|