@jayfong/x-server 1.10.0 → 1.10.3
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/CHANGELOG.md +16 -0
- package/lib/cli/api_generator.js +1 -1
- package/lib/cli/cli.js +3 -27
- package/lib/cli/template_util.d.ts +8 -1
- package/lib/cli/template_util.js +45 -1
- package/lib/core/define_task.js +1 -2
- package/lib/core/types.d.ts +0 -4
- package/lib/services/cache.d.ts +1 -3
- package/lib/services/cache.js +5 -5
- package/lib/x.d.ts +1 -0
- package/lib/x.js +1 -0
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,22 @@
|
|
|
2
2
|
|
|
3
3
|
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
|
|
4
4
|
|
|
5
|
+
### [1.10.3](https://github.com/jfWorks/x-server/compare/v1.10.2...v1.10.3) (2022-04-19)
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
### Bug Fixes
|
|
9
|
+
|
|
10
|
+
* initModels ([85c6aa7](https://github.com/jfWorks/x-server/commit/85c6aa77d28ded5aeb336e57b2c62de3817a18f3))
|
|
11
|
+
|
|
12
|
+
### [1.10.2](https://github.com/jfWorks/x-server/compare/v1.10.1...v1.10.2) (2022-04-19)
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
### Bug Fixes
|
|
16
|
+
|
|
17
|
+
* api gen ([cc33df1](https://github.com/jfWorks/x-server/commit/cc33df1bf15c2b367b1a62cf920a1a1184c20339))
|
|
18
|
+
|
|
19
|
+
### [1.10.1](https://github.com/jfWorks/x-server/compare/v1.10.0...v1.10.1) (2022-04-19)
|
|
20
|
+
|
|
5
21
|
## [1.10.0](https://github.com/jfWorks/x-server/compare/v1.8.0...v1.10.0) (2022-04-19)
|
|
6
22
|
|
|
7
23
|
|
package/lib/cli/api_generator.js
CHANGED
|
@@ -273,7 +273,7 @@ class ApiGenerator {
|
|
|
273
273
|
tsConfigFilePath: node_path_1.default.join(this.cwd, 'tsconfig.json'),
|
|
274
274
|
});
|
|
275
275
|
this.debug('加载文件...');
|
|
276
|
-
const sourceFile = this.project.
|
|
276
|
+
const sourceFile = this.project.createSourceFile(node_path_1.default.join(this.cwd, 'src/generated/handlers.ts'), await fs_extra_1.default.readFile(node_path_1.default.join(this.cwd, 'node_modules/.x/handlers.ts'), 'utf-8'));
|
|
277
277
|
this.debug('导出处理器...');
|
|
278
278
|
const handlerGroup = sourceFile.getExportSymbols();
|
|
279
279
|
const handles = [];
|
package/lib/cli/cli.js
CHANGED
|
@@ -6,8 +6,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
6
6
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
7
|
const chokidar_1 = __importDefault(require("chokidar"));
|
|
8
8
|
const execa_1 = __importDefault(require("execa"));
|
|
9
|
-
const fs_extra_1 = __importDefault(require("fs-extra"));
|
|
10
|
-
const node_path_1 = __importDefault(require("node:path"));
|
|
11
9
|
const yargs_1 = __importDefault(require("yargs"));
|
|
12
10
|
const api_generator_1 = require("./api_generator");
|
|
13
11
|
const build_util_1 = require("./build_util");
|
|
@@ -48,7 +46,7 @@ yargs_1.default
|
|
|
48
46
|
stdio: 'inherit',
|
|
49
47
|
});
|
|
50
48
|
}
|
|
51
|
-
await template_util_1.TemplateUtil.
|
|
49
|
+
await template_util_1.TemplateUtil.initHelperPackage(process.cwd());
|
|
52
50
|
const watcher = chokidar_1.default.watch(['src', '.env'], {
|
|
53
51
|
cwd: process.cwd(),
|
|
54
52
|
});
|
|
@@ -101,7 +99,7 @@ yargs_1.default
|
|
|
101
99
|
value: 'production',
|
|
102
100
|
comment: '',
|
|
103
101
|
});
|
|
104
|
-
await template_util_1.TemplateUtil.
|
|
102
|
+
await template_util_1.TemplateUtil.initHelperPackage(process.cwd());
|
|
105
103
|
await (0, vscode_generate_index_standalone_1.generateManyIndex)({
|
|
106
104
|
cwd: process.cwd(),
|
|
107
105
|
patterns: ['src/.x/*.ts'],
|
|
@@ -146,29 +144,7 @@ yargs_1.default
|
|
|
146
144
|
stdio: 'inherit',
|
|
147
145
|
});
|
|
148
146
|
if (!argv.production) {
|
|
149
|
-
|
|
150
|
-
const baseModelFile = node_path_1.default.join(modelsDir, '_.ts');
|
|
151
|
-
await (0, vscode_generate_index_standalone_1.generateIndex)({
|
|
152
|
-
filePath: baseModelFile,
|
|
153
|
-
replaceFile: true,
|
|
154
|
-
});
|
|
155
|
-
const baseModelContent = await fs_extra_1.default.readFile(baseModelFile, 'utf-8');
|
|
156
|
-
const modelNames = [
|
|
157
|
-
...baseModelContent.matchAll(/makeBaseModel\('(\w+?)'\)/g),
|
|
158
|
-
].map(match => (0, vtils_1.snakeCase)(match[1]));
|
|
159
|
-
await Promise.all(modelNames.map(async (modelName) => {
|
|
160
|
-
const modelFile = node_path_1.default.join(modelsDir, `${modelName}.ts`);
|
|
161
|
-
if (!(await fs_extra_1.default.pathExists(modelFile))) {
|
|
162
|
-
const ModelName = (0, vtils_1.pascalCase)(modelName);
|
|
163
|
-
await fs_extra_1.default.writeFile(modelFile, (0, vtils_1.dedent) `
|
|
164
|
-
import { ${ModelName}BaseModel } from './_'
|
|
165
|
-
|
|
166
|
-
export class ${ModelName}Model extends ${ModelName}BaseModel {}
|
|
167
|
-
|
|
168
|
-
export const ${(0, vtils_1.camelCase)(modelName)}Model = new ${ModelName}Model()
|
|
169
|
-
`);
|
|
170
|
-
}
|
|
171
|
-
}));
|
|
147
|
+
await template_util_1.TemplateUtil.initModels(process.cwd());
|
|
172
148
|
}
|
|
173
149
|
})
|
|
174
150
|
.command('run [script]', '执行脚本', _ => _.positional('script', {
|
package/lib/cli/template_util.js
CHANGED
|
@@ -6,13 +6,57 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
6
6
|
exports.TemplateUtil = void 0;
|
|
7
7
|
const fs_extra_1 = __importDefault(require("fs-extra"));
|
|
8
8
|
const path_1 = __importDefault(require("path"));
|
|
9
|
+
const vtils_1 = require("vtils");
|
|
10
|
+
const vscode_generate_index_standalone_1 = require("vscode-generate-index-standalone");
|
|
9
11
|
class TemplateUtil {
|
|
10
|
-
|
|
12
|
+
/**
|
|
13
|
+
* 初始化辅助包。
|
|
14
|
+
*/
|
|
15
|
+
static async initHelperPackage(cwd) {
|
|
11
16
|
const fromDir = path_1.default.join(__dirname, 'templates');
|
|
12
17
|
const toDir = path_1.default.join(cwd, 'node_modules/.x');
|
|
13
18
|
await fs_extra_1.default.copy(fromDir, toDir, {
|
|
14
19
|
overwrite: true,
|
|
15
20
|
});
|
|
16
21
|
}
|
|
22
|
+
/**
|
|
23
|
+
* 初始化模型。
|
|
24
|
+
*/
|
|
25
|
+
static async initModels(cwd) {
|
|
26
|
+
const modelsDir = path_1.default.join(cwd, 'src/models');
|
|
27
|
+
const indexFile = path_1.default.join(modelsDir, 'index.ts');
|
|
28
|
+
const indexFile2 = path_1.default.join(cwd, 'node_modules/.x/models.ts');
|
|
29
|
+
const prismaClientFile = path_1.default.join(cwd, 'node_modules/.prisma/client/index.d.ts');
|
|
30
|
+
const prismaClientFileContent = await fs_extra_1.default.readFile(prismaClientFile, 'utf8');
|
|
31
|
+
const modelNames = [
|
|
32
|
+
...prismaClientFileContent
|
|
33
|
+
.match(/(?<=const ModelName:).+?(?=\})/s)[0]
|
|
34
|
+
.matchAll(/(\S+?):/g),
|
|
35
|
+
].map(match => (0, vtils_1.camelCase)(match[1]));
|
|
36
|
+
await Promise.all(modelNames.map(async (modelName) => {
|
|
37
|
+
const model_name = (0, vtils_1.snakeCase)(modelName);
|
|
38
|
+
const ModelName = (0, vtils_1.upperFirst)(modelName);
|
|
39
|
+
const modelFile = path_1.default.join(modelsDir, `${model_name}.ts`);
|
|
40
|
+
if (!(await fs_extra_1.default.pathExists(modelFile))) {
|
|
41
|
+
await fs_extra_1.default.outputFile(modelFile, (0, vtils_1.dedent) `
|
|
42
|
+
import { ${ModelName}BaseModel } from '@jayfong/x-server'
|
|
43
|
+
|
|
44
|
+
export class ${ModelName}Model extends ${ModelName}BaseModel {}
|
|
45
|
+
|
|
46
|
+
export const ${modelName}Model = new ${ModelName}Model()
|
|
47
|
+
`);
|
|
48
|
+
}
|
|
49
|
+
}));
|
|
50
|
+
if (!(await fs_extra_1.default.pathExists(indexFile))) {
|
|
51
|
+
await fs_extra_1.default.outputFile(indexFile, (0, vtils_1.dedent) `
|
|
52
|
+
// @index(['./**/*.ts', '!**/*.test.ts', '!**/_*'], f => \`export * from '\${f.path}'\`)
|
|
53
|
+
// @endindex
|
|
54
|
+
`);
|
|
55
|
+
}
|
|
56
|
+
await (0, vscode_generate_index_standalone_1.generateManyIndex)({
|
|
57
|
+
patterns: [indexFile, indexFile2],
|
|
58
|
+
replaceFile: true,
|
|
59
|
+
});
|
|
60
|
+
}
|
|
17
61
|
}
|
|
18
62
|
exports.TemplateUtil = TemplateUtil;
|
package/lib/core/define_task.js
CHANGED
|
@@ -5,12 +5,11 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.defineTask = void 0;
|
|
7
7
|
const bull_1 = __importDefault(require("bull"));
|
|
8
|
-
const server_1 = require("./server");
|
|
9
8
|
const x_1 = require("../x");
|
|
10
9
|
async function defineTask(options) {
|
|
11
10
|
const queue = new bull_1.default(options.name, {
|
|
12
11
|
redis: x_1.x.redis.options,
|
|
13
|
-
prefix: `${
|
|
12
|
+
prefix: `${x_1.x.appName}_task`,
|
|
14
13
|
});
|
|
15
14
|
queue.process(async (job) => {
|
|
16
15
|
return options.handle(job.data);
|
package/lib/core/types.d.ts
CHANGED
package/lib/services/cache.d.ts
CHANGED
|
@@ -4,9 +4,7 @@ import { MsValue } from 'vtils/date';
|
|
|
4
4
|
export declare type CacheTTL = MsValue;
|
|
5
5
|
export interface CacheOptions {
|
|
6
6
|
/** 默认过期时间 */
|
|
7
|
-
|
|
8
|
-
/** 存储键前缀 */
|
|
9
|
-
prefix: string;
|
|
7
|
+
ttl: CacheTTL;
|
|
10
8
|
}
|
|
11
9
|
declare type Data = {
|
|
12
10
|
[K in string]: (...args: any[]) => any;
|
package/lib/services/cache.js
CHANGED
|
@@ -11,7 +11,7 @@ class CacheService {
|
|
|
11
11
|
constructor(options) {
|
|
12
12
|
this.options = options;
|
|
13
13
|
this.serviceName = 'cache';
|
|
14
|
-
this.prefix = `${
|
|
14
|
+
this.prefix = `${x_1.x.appName}:`;
|
|
15
15
|
}
|
|
16
16
|
toRedisKey(key) {
|
|
17
17
|
return `${this.prefix}${Array.isArray(key) ? key.join('_') : String(key)}`;
|
|
@@ -25,10 +25,10 @@ class CacheService {
|
|
|
25
25
|
* @returns 返回设置的缓存内容
|
|
26
26
|
*/
|
|
27
27
|
async set(key, value, ttl = this.options
|
|
28
|
-
.
|
|
28
|
+
.ttl) {
|
|
29
29
|
const redisKey = this.toRedisKey(key);
|
|
30
30
|
const redisValue = JSON.stringify(value);
|
|
31
|
-
const redisTtl = typeof ttl === 'function' ? ttl(value, this.options.
|
|
31
|
+
const redisTtl = typeof ttl === 'function' ? ttl(value, this.options.ttl) : ttl;
|
|
32
32
|
await x_1.x.redis.set(redisKey, redisValue,
|
|
33
33
|
// 毫秒
|
|
34
34
|
'PX', (0, date_1.ms)(redisTtl));
|
|
@@ -40,7 +40,7 @@ class CacheService {
|
|
|
40
40
|
* @param value 值
|
|
41
41
|
* @param ttl 存活时间
|
|
42
42
|
*/
|
|
43
|
-
async save(value, ttl = this.options.
|
|
43
|
+
async save(value, ttl = this.options.ttl) {
|
|
44
44
|
const key = `X__${(0, cuid_1.default)()}`;
|
|
45
45
|
await this.set(key, typeof value === 'function' ? await value(key) : value, ttl);
|
|
46
46
|
return key;
|
|
@@ -93,7 +93,7 @@ class CacheService {
|
|
|
93
93
|
* @returns 返回获取到的内容
|
|
94
94
|
*/
|
|
95
95
|
async remember(key, action, ttl = this.options
|
|
96
|
-
.
|
|
96
|
+
.ttl) {
|
|
97
97
|
let value = await this.get(key);
|
|
98
98
|
if (value === null) {
|
|
99
99
|
value = await action();
|
package/lib/x.d.ts
CHANGED
package/lib/x.js
CHANGED