@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 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
 
@@ -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.getSourceFileOrThrow(node_path_1.default.join(this.cwd, 'src/generated/handlers.ts'));
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.init(process.cwd());
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.init(process.cwd());
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
- const modelsDir = node_path_1.default.join(process.cwd(), 'src/models');
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', {
@@ -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,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
- static async init(cwd) {
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;
@@ -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.3",
4
4
  "license": "ISC",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",