@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 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 template_util_1.TemplateUtil.init(process.cwd());
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.init(process.cwd());
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'],
@@ -1,3 +1,10 @@
1
1
  export declare class TemplateUtil {
2
- static init(cwd: string): Promise<void>;
2
+ /**
3
+ * 初始化辅助包。
4
+ */
5
+ static initHelperPackage(cwd: string): Promise<void>;
6
+ /**
7
+ * 初始化模型。
8
+ */
9
+ static initModels(cwd: string): Promise<void>;
3
10
  }
@@ -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
- static async init(cwd) {
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;
@@ -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: `${server_1.Server.options.name}_task`,
12
+ prefix: `${x_1.x.appName}_task`,
14
13
  });
15
14
  queue.process(async (job) => {
16
15
  return options.handle(job.data);
@@ -21,10 +21,6 @@ export declare namespace XServer {
21
21
  handler: Handler;
22
22
  }
23
23
  interface Options {
24
- /**
25
- * 应用名称
26
- */
27
- name: string;
28
24
  /**
29
25
  * 监听主机
30
26
  */
@@ -4,9 +4,7 @@ import { MsValue } from 'vtils/date';
4
4
  export declare type CacheTTL = MsValue;
5
5
  export interface CacheOptions {
6
6
  /** 默认过期时间 */
7
- defaultTTL: CacheTTL;
8
- /** 存储键前缀 */
9
- prefix: string;
7
+ ttl: CacheTTL;
10
8
  }
11
9
  declare type Data = {
12
10
  [K in string]: (...args: any[]) => any;
@@ -11,7 +11,7 @@ class CacheService {
11
11
  constructor(options) {
12
12
  this.options = options;
13
13
  this.serviceName = 'cache';
14
- this.prefix = `${options.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
- .defaultTTL) {
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.defaultTTL) : ttl;
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.defaultTTL) {
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
- .defaultTTL) {
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
@@ -1,6 +1,7 @@
1
1
  import { BaseService } from './services/base';
2
2
  import './services/dispose';
3
3
  export interface X {
4
+ readonly appName: string;
4
5
  readonly register: (...services: BaseService[]) => void;
5
6
  }
6
7
  export declare const x: X;
package/lib/x.js CHANGED
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.x = void 0;
4
4
  const dispose_1 = require("./services/dispose");
5
5
  exports.x = {
6
+ appName: process.env.APP_NAME,
6
7
  register: (...services) => {
7
8
  for (const service of services) {
8
9
  // @ts-ignore
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jayfong/x-server",
3
- "version": "1.10.0",
3
+ "version": "1.10.1",
4
4
  "license": "ISC",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",