@jayfong/x-server 1.10.0 → 1.10.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/CHANGELOG.md +2 -0
- package/lib/cli/cli.js +5 -2
- package/lib/cli/template_util.d.ts +8 -1
- package/lib/cli/template_util.js +39 -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,8 @@
|
|
|
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.1](https://github.com/jfWorks/x-server/compare/v1.10.0...v1.10.1) (2022-04-19)
|
|
6
|
+
|
|
5
7
|
## [1.10.0](https://github.com/jfWorks/x-server/compare/v1.8.0...v1.10.0) (2022-04-19)
|
|
6
8
|
|
|
7
9
|
|
package/lib/cli/cli.js
CHANGED
|
@@ -48,7 +48,10 @@ yargs_1.default
|
|
|
48
48
|
stdio: 'inherit',
|
|
49
49
|
});
|
|
50
50
|
}
|
|
51
|
-
await
|
|
51
|
+
await Promise.all([
|
|
52
|
+
template_util_1.TemplateUtil.initHelperPackage(process.cwd()),
|
|
53
|
+
template_util_1.TemplateUtil.initModels(process.cwd()),
|
|
54
|
+
]);
|
|
52
55
|
const watcher = chokidar_1.default.watch(['src', '.env'], {
|
|
53
56
|
cwd: process.cwd(),
|
|
54
57
|
});
|
|
@@ -101,7 +104,7 @@ yargs_1.default
|
|
|
101
104
|
value: 'production',
|
|
102
105
|
comment: '',
|
|
103
106
|
});
|
|
104
|
-
await template_util_1.TemplateUtil.
|
|
107
|
+
await template_util_1.TemplateUtil.initHelperPackage(process.cwd());
|
|
105
108
|
await (0, vscode_generate_index_standalone_1.generateManyIndex)({
|
|
106
109
|
cwd: process.cwd(),
|
|
107
110
|
patterns: ['src/.x/*.ts'],
|
package/lib/cli/template_util.js
CHANGED
|
@@ -6,13 +6,51 @@ 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");
|
|
9
10
|
class TemplateUtil {
|
|
10
|
-
|
|
11
|
+
/**
|
|
12
|
+
* 初始化辅助包。
|
|
13
|
+
*/
|
|
14
|
+
static async initHelperPackage(cwd) {
|
|
11
15
|
const fromDir = path_1.default.join(__dirname, 'templates');
|
|
12
16
|
const toDir = path_1.default.join(cwd, 'node_modules/.x');
|
|
13
17
|
await fs_extra_1.default.copy(fromDir, toDir, {
|
|
14
18
|
overwrite: true,
|
|
15
19
|
});
|
|
16
20
|
}
|
|
21
|
+
/**
|
|
22
|
+
* 初始化模型。
|
|
23
|
+
*/
|
|
24
|
+
static async initModels(cwd) {
|
|
25
|
+
const modelsDir = path_1.default.join(cwd, 'src/models');
|
|
26
|
+
const indexFile = path_1.default.join(modelsDir, 'index.ts');
|
|
27
|
+
if (!(await fs_extra_1.default.pathExists(indexFile))) {
|
|
28
|
+
await fs_extra_1.default.outputFile(indexFile, (0, vtils_1.dedent) `
|
|
29
|
+
// @index(['./**/*.ts', '!**/*.test.ts', '!**/_*'], f => \`export * from '\${f.path}'\`)
|
|
30
|
+
// @endindex
|
|
31
|
+
`);
|
|
32
|
+
}
|
|
33
|
+
const prismaClientFile = path_1.default.join(cwd, 'node_modules/.prisma/client/index.d.ts');
|
|
34
|
+
const prismaClientFileContent = await fs_extra_1.default.readFile(prismaClientFile, 'utf8');
|
|
35
|
+
const modelNames = [
|
|
36
|
+
...prismaClientFileContent
|
|
37
|
+
.match(/(?<=const ModelName:).+?(?=\})/s)[0]
|
|
38
|
+
.matchAll(/(\S+?):/g),
|
|
39
|
+
].map(match => (0, vtils_1.camelCase)(match[1]));
|
|
40
|
+
await Promise.all(modelNames.map(async (modelName) => {
|
|
41
|
+
const model_name = (0, vtils_1.snakeCase)(modelName);
|
|
42
|
+
const ModelName = (0, vtils_1.upperFirst)(modelName);
|
|
43
|
+
const modelFile = path_1.default.join(modelsDir, `${model_name}.ts`);
|
|
44
|
+
if (!(await fs_extra_1.default.pathExists(modelFile))) {
|
|
45
|
+
await fs_extra_1.default.outputFile(modelFile, (0, vtils_1.dedent) `
|
|
46
|
+
import { ${ModelName}BaseModel } from '@jayfong/x-server'
|
|
47
|
+
|
|
48
|
+
export class ${ModelName}Model extends ${ModelName}BaseModel {}
|
|
49
|
+
|
|
50
|
+
export const ${modelName}Model = new ${ModelName}Model()
|
|
51
|
+
`);
|
|
52
|
+
}
|
|
53
|
+
}));
|
|
54
|
+
}
|
|
17
55
|
}
|
|
18
56
|
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