@jayfong/x-server 2.12.14 → 2.12.16

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.
Files changed (79) hide show
  1. package/lib/_cjs/cli/api_generator.js +20 -54
  2. package/lib/_cjs/cli/build_util.js +16 -45
  3. package/lib/_cjs/cli/cli.js +17 -29
  4. package/lib/_cjs/cli/deploy_util.js +0 -12
  5. package/lib/_cjs/cli/dev_util.js +18 -0
  6. package/lib/_cjs/cli/env_util.js +16 -49
  7. package/lib/_cjs/cli/{register.js → esbuild_register.js} +0 -1
  8. package/lib/_cjs/cli/template_util.js +26 -29
  9. package/lib/_cjs/core/define_bus.js +9 -6
  10. package/lib/_cjs/core/define_cron.js +0 -2
  11. package/lib/_cjs/core/define_handler.js +10 -12
  12. package/lib/_cjs/core/define_hook.js +0 -2
  13. package/lib/_cjs/core/define_server.js +0 -2
  14. package/lib/_cjs/core/define_task.js +7 -13
  15. package/lib/_cjs/core/get_handler_url.js +1 -2
  16. package/lib/_cjs/core/handler.js +8 -29
  17. package/lib/_cjs/core/http_error.js +0 -3
  18. package/lib/_cjs/core/http_header.js +8 -12
  19. package/lib/_cjs/core/server.js +8 -41
  20. package/lib/_cjs/core/types.js +0 -7
  21. package/lib/_cjs/index.js +0 -72
  22. package/lib/_cjs/plugins/cors.js +0 -7
  23. package/lib/_cjs/plugins/file_parser.js +2 -9
  24. package/lib/_cjs/plugins/form_body_parser.js +0 -6
  25. package/lib/_cjs/plugins/ws_parser.js +0 -6
  26. package/lib/_cjs/plugins/xml_parser.js +0 -12
  27. package/lib/_cjs/services/cache.js +18 -58
  28. package/lib/_cjs/services/captcha.js +0 -14
  29. package/lib/_cjs/services/dingtalk.js +0 -14
  30. package/lib/_cjs/services/dispose.js +0 -9
  31. package/lib/_cjs/services/emoji.js +2 -5
  32. package/lib/_cjs/services/jwt.js +0 -21
  33. package/lib/_cjs/services/log.js +0 -17
  34. package/lib/_cjs/services/mail.js +0 -9
  35. package/lib/_cjs/services/pay.js +9 -40
  36. package/lib/_cjs/services/rate_limit.js +0 -12
  37. package/lib/_cjs/services/redis.js +0 -4
  38. package/lib/_cjs/services/request.js +0 -5
  39. package/lib/_cjs/services/sensitive_words.js +13 -19
  40. package/lib/_cjs/x.js +0 -11
  41. package/lib/cli/api_generator.js +20 -42
  42. package/lib/cli/build_util.js +16 -19
  43. package/lib/cli/cli.js +17 -14
  44. package/lib/cli/deploy_util.js +0 -4
  45. package/lib/cli/dev_util.d.ts +8 -0
  46. package/lib/cli/dev_util.js +12 -0
  47. package/lib/cli/env_util.js +16 -37
  48. package/lib/cli/template_util.d.ts +1 -0
  49. package/lib/cli/template_util.js +26 -14
  50. package/lib/core/define_bus.js +7 -5
  51. package/lib/core/define_handler.js +10 -9
  52. package/lib/core/define_task.js +7 -6
  53. package/lib/core/get_handler_url.js +2 -1
  54. package/lib/core/handler.js +9 -22
  55. package/lib/core/http_header.js +6 -9
  56. package/lib/core/server.js +8 -31
  57. package/lib/core/types.js +0 -7
  58. package/lib/index.js +4 -3
  59. package/lib/plugins/cors.js +0 -3
  60. package/lib/plugins/file_parser.js +2 -5
  61. package/lib/plugins/form_body_parser.js +0 -3
  62. package/lib/plugins/ws_parser.js +0 -3
  63. package/lib/plugins/xml_parser.js +0 -10
  64. package/lib/services/cache.js +18 -52
  65. package/lib/services/captcha.js +0 -7
  66. package/lib/services/dingtalk.js +0 -7
  67. package/lib/services/dispose.js +0 -5
  68. package/lib/services/emoji.js +2 -3
  69. package/lib/services/jwt.js +0 -13
  70. package/lib/services/log.js +0 -8
  71. package/lib/services/mail.js +0 -3
  72. package/lib/services/pay.js +9 -30
  73. package/lib/services/rate_limit.js +0 -8
  74. package/lib/services/redis.js +0 -1
  75. package/lib/services/request.js +0 -1
  76. package/lib/services/sensitive_words.js +13 -15
  77. package/package.json +3 -3
  78. /package/lib/cli/{register.d.ts → esbuild_register.d.ts} +0 -0
  79. /package/lib/cli/{register.js → esbuild_register.js} +0 -0
@@ -1,41 +1,29 @@
1
1
  "use strict";
2
2
 
3
3
  var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
4
-
5
4
  var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;
6
-
7
5
  exports.__esModule = true;
8
6
  exports.ApiGenerator = void 0;
9
-
10
7
  var parseComment = _interopRequireWildcard(require("comment-parser"));
11
-
12
8
  var ts = _interopRequireWildcard(require("ts-morph"));
13
-
14
9
  var _debug = _interopRequireDefault(require("debug"));
15
-
16
10
  var _fsExtra = _interopRequireDefault(require("fs-extra"));
17
-
18
11
  var _nodePath = _interopRequireDefault(require("node:path"));
19
-
20
12
  var _http_method = require("../core/http_method");
21
-
22
13
  var _vtils = require("vtils");
23
-
24
14
  class ApiGenerator {
25
15
  constructor() {
26
16
  this.debug = (0, _debug.default)('api');
27
17
  this.cwd = process.cwd();
18
+ // cwd = '/Users/admin/Documents/jfWorks/x-server-test'
28
19
  this.project = void 0;
29
20
  }
30
-
31
21
  getTypeBySymbol(symbol) {
32
22
  return symbol.getTypeAtLocation(symbol.getDeclarations()[0] || symbol.getValueDeclarationOrThrow());
33
23
  }
34
-
35
24
  getComment(declaration) {
36
25
  var _declaration$getLeadi;
37
-
38
- const text = ((declaration == null ? void 0 : (_declaration$getLeadi = declaration.getLeadingCommentRanges()[0]) == null ? void 0 : _declaration$getLeadi.getText()) || '').trim();
26
+ const text = ((declaration == null || (_declaration$getLeadi = declaration.getLeadingCommentRanges()[0]) == null ? void 0 : _declaration$getLeadi.getText()) || '').trim();
39
27
  const comment = parseComment.parse(text)[0];
40
28
  const description = (comment == null ? void 0 : comment.description) || '';
41
29
  const tags = new Map();
@@ -48,16 +36,12 @@ class ApiGenerator {
48
36
  tags: tags
49
37
  };
50
38
  }
51
-
52
39
  getCommentBySymbol(symbol) {
53
40
  var _this$getComment;
54
-
55
41
  return ((_this$getComment = this.getComment(symbol.getDeclarations()[0])) == null ? void 0 : _this$getComment.description) || '';
56
42
  }
57
-
58
43
  typeToApiData(type, _symbol) {
59
44
  var _type$getSymbol, _type$getSymbol2, _type$getSymbol3;
60
-
61
45
  // ws
62
46
  if (((_type$getSymbol = type.getSymbol()) == null ? void 0 : _type$getSymbol.getName()) === 'SocketStream') {
63
47
  return {
@@ -68,12 +52,11 @@ class ApiGenerator {
68
52
  children: [],
69
53
  enum: []
70
54
  };
71
- } // XFile
72
-
55
+ }
73
56
 
57
+ // XFile
74
58
  if (((_type$getSymbol2 = type.getSymbol()) == null ? void 0 : _type$getSymbol2.getName()) === 'MultipartFile') {
75
59
  const symbol = _symbol || type.getSymbol();
76
-
77
60
  return {
78
61
  name: 'file',
79
62
  desc: symbol && this.getCommentBySymbol(symbol) || '',
@@ -83,25 +66,22 @@ class ApiGenerator {
83
66
  enum: []
84
67
  };
85
68
  }
86
-
87
69
  let isRequired = true;
88
70
  let isUnion = type.isUnion();
89
71
  const unionTypes = isUnion ? type.getUnionTypes().filter(item => !item.isBooleanLiteral() && !item.isNull() && !item.isUndefined()) : [];
90
72
  isUnion = !!unionTypes.length;
91
-
92
73
  if (isUnion) {
93
74
  if (unionTypes.length === 1 && !unionTypes[0].isLiteral()) {
94
75
  isUnion = false;
95
- } // 兼容 prisma 生成的类型用 null 表示可选
96
-
97
-
98
- isRequired = unionTypes.length === type.getUnionTypes().length; // 必须用 getBaseTypeOfLiteralType 获取枚举字面量的原始类型
99
-
76
+ }
77
+ // 兼容 prisma 生成的类型用 null 表示可选
78
+ isRequired = unionTypes.length === type.getUnionTypes().length;
79
+ // 必须用 getBaseTypeOfLiteralType 获取枚举字面量的原始类型
100
80
  type = unionTypes[0].getBaseTypeOfLiteralType();
101
81
  }
102
-
103
82
  const isEnum = type.isEnum();
104
- const enumData = isEnum ? // @ts-ignore
83
+ const enumData = isEnum ?
84
+ // @ts-ignore
105
85
  type.compilerType.types.reduce((res, item) => {
106
86
  res[item.getSymbol().getName()] = item.value;
107
87
  return res;
@@ -114,11 +94,10 @@ class ApiGenerator {
114
94
  const isString = isEnum ? typeof enumValues[0] === 'string' : type.isString() || ['Date'].includes(((_type$getSymbol3 = type.getSymbol()) == null ? void 0 : _type$getSymbol3.getName()) || '');
115
95
  const isNumber = isEnum ? typeof enumValues[0] === 'number' : type.isNumber();
116
96
  const isBoolean = type.isBoolean() || type.isUnion() && type.getUnionTypes().some(item => item.isBooleanLiteral()) && type.getUnionTypes().every(item => item.isBooleanLiteral() || item.isNull() || item.isUndefined());
117
- const isObject = !isArray && !isString && !isNumber && !isBoolean && type.isObject() || // 将交集类型视为对象
97
+ const isObject = !isArray && !isString && !isNumber && !isBoolean && type.isObject() ||
98
+ // 将交集类型视为对象
118
99
  isIntersection;
119
-
120
100
  const symbol = _symbol || type.getSymbol();
121
-
122
101
  const apiName = (symbol == null ? void 0 : symbol.getName()) || '__type';
123
102
  const apiDesc = [symbol && this.getCommentBySymbol(symbol), isEnum && `枚举:${enumKeys.map((key, index) => `${key}->${enumValues[index]}`).join('; ')}`].filter(Boolean).join('\n');
124
103
  const apiEnum = isUnion ? unionTypes.map(t => t.getLiteralValue()) : isEnum ? enumValues : [];
@@ -129,20 +108,21 @@ class ApiGenerator {
129
108
  const compilerFactory = context.compilerFactory;
130
109
  const rawChecker = type.compilerType.checker;
131
110
  let symbols = [];
132
-
133
111
  if (intersectionTypes) {
134
112
  // https://github.com/microsoft/TypeScript/issues/38184
135
- symbols = rawChecker.getAllPossiblePropertiesOfTypes(intersectionTypes.map(item => item.compilerType)) // https://github.com/dsherret/ts-morph/blob/a7072fcf6f9babb784b40f0326c80dea4563a4aa/packages/ts-morph/src/compiler/types/Type.ts#L296
113
+ symbols = rawChecker.getAllPossiblePropertiesOfTypes(intersectionTypes.map(item => item.compilerType))
114
+ // https://github.com/dsherret/ts-morph/blob/a7072fcf6f9babb784b40f0326c80dea4563a4aa/packages/ts-morph/src/compiler/types/Type.ts#L296
136
115
  .map(symbol => compilerFactory.getSymbol(symbol));
137
116
  } else {
138
117
  // symbols = type.getApparentProperties()
139
118
  // https://github.com/microsoft/TypeScript/issues/38184
140
- symbols = rawChecker.getAllPossiblePropertiesOfTypes([type.compilerType]) // https://github.com/dsherret/ts-morph/blob/a7072fcf6f9babb784b40f0326c80dea4563a4aa/packages/ts-morph/src/compiler/types/Type.ts#L296
119
+ symbols = rawChecker.getAllPossiblePropertiesOfTypes([type.compilerType])
120
+ // https://github.com/dsherret/ts-morph/blob/a7072fcf6f9babb784b40f0326c80dea4563a4aa/packages/ts-morph/src/compiler/types/Type.ts#L296
141
121
  .map(symbol => compilerFactory.getSymbol(symbol));
142
122
  }
143
-
144
123
  return symbols.map(symbol => {
145
- return this.typeToApiData(!symbol.compilerSymbol.declarations ? // 对于复杂对象,没有定义的,通过 type 直接获取(在前面通过 getText 预处理得到)
124
+ return this.typeToApiData(!symbol.compilerSymbol.declarations ?
125
+ // 对于复杂对象,没有定义的,通过 type 直接获取(在前面通过 getText 预处理得到)
146
126
  compilerFactory.getType(symbol.compilerSymbol.type) : this.getTypeBySymbol(symbol), symbol);
147
127
  });
148
128
  }) : [];
@@ -155,10 +135,8 @@ class ApiGenerator {
155
135
  children: apiChildren
156
136
  };
157
137
  }
158
-
159
138
  apiDataToJsonSchema(apiData, jsonSchema = {}) {
160
139
  jsonSchema.description = apiData.desc;
161
-
162
140
  if (apiData.type === 'object') {
163
141
  jsonSchema.type = 'object';
164
142
  jsonSchema.properties = apiData.children.reduce((res, item) => {
@@ -172,18 +150,14 @@ class ApiGenerator {
172
150
  jsonSchema.items = apiData.children.map(item => this.apiDataToJsonSchema(item))[0];
173
151
  } else {
174
152
  jsonSchema.type = apiData.type;
175
-
176
153
  if (apiData.enum.length) {
177
154
  jsonSchema.enum = apiData.enum;
178
155
  }
179
156
  }
180
-
181
157
  return jsonSchema;
182
158
  }
183
-
184
159
  genYApiData(handles) {
185
160
  const data = {};
186
-
187
161
  for (const handle of handles) {
188
162
  data[handle.category] = data[handle.category] || [];
189
163
  data[handle.category].push(handle.handlerMethod === 'GET' ? {
@@ -237,14 +211,12 @@ class ApiGenerator {
237
211
  res_body: JSON.stringify(handle.responseDataJsonSchema)
238
212
  });
239
213
  }
240
-
241
214
  return Object.keys(data).map(cat => ({
242
215
  name: cat,
243
216
  desc: cat,
244
217
  list: data[cat]
245
218
  }));
246
219
  }
247
-
248
220
  async generate() {
249
221
  this.debug('启动项目...');
250
222
  this.project = new ts.Project({
@@ -256,11 +228,9 @@ class ApiGenerator {
256
228
  const handlerGroup = sourceFile.getExportSymbols();
257
229
  const handles = [];
258
230
  this.debug('生成API文档...');
259
-
260
231
  for (const handlerList of handlerGroup) {
261
232
  const handlerListSourceFile = this.getTypeBySymbol(handlerList).getProperties()[0].getDeclarations()[0].getSourceFile();
262
233
  const basePath = `/${handlerListSourceFile.getFilePath().replace('.ts', '').split('/src/handlers/')[1].replace(/(^|\/)index$/, '/').split('/').map(v => (0, _vtils.snakeCase)(v)).join('/')}/`.replace(/\/{2,}/g, '/');
263
-
264
234
  for (const handler of handlerListSourceFile.getVariableStatements().filter(item => item.isExported())) {
265
235
  // 重要:这一步必须,先调一遍 getText 获取看到的对象,后续对于复杂定义才不会报错
266
236
  handler.getDeclarations().forEach(exp => {
@@ -271,12 +241,10 @@ class ApiGenerator {
271
241
  this.debug('生成接口: %s ...', handlerPath);
272
242
  const [requestType, responseType, methodType] = handlerExp.getType().getTypeArguments();
273
243
  const handlerComment = this.getComment(handlerExp.getParent().getParent());
274
-
275
244
  if (handlerComment.tags.has('private')) {
276
245
  this.debug('跳过生成接口: %s', `${handlerPath}`);
277
246
  continue;
278
247
  }
279
-
280
248
  const handlerCategory = (0, _vtils.pascalCase)(basePath) || 'Index';
281
249
  const handlerName = handlerComment.description || handlerPath;
282
250
  const handlerMethod = methodType.getLiteralValueOrThrow();
@@ -298,7 +266,6 @@ class ApiGenerator {
298
266
  });
299
267
  }
300
268
  }
301
-
302
269
  this.debug('写入文件...');
303
270
  await Promise.all([_fsExtra.default.outputJSON(_nodePath.default.join(this.cwd, 'temp/api.json'), handles, {
304
271
  spaces: 2
@@ -306,8 +273,7 @@ class ApiGenerator {
306
273
  spaces: 2
307
274
  })]);
308
275
  }
276
+ }
309
277
 
310
- } // new ApiGenerator().generate()
311
-
312
-
278
+ // new ApiGenerator().generate()
313
279
  exports.ApiGenerator = ApiGenerator;
@@ -1,60 +1,39 @@
1
1
  "use strict";
2
2
 
3
3
  var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
4
-
5
4
  var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;
6
-
7
5
  exports.__esModule = true;
8
6
  exports.BuildUtil = void 0;
9
-
10
7
  var esbuild = _interopRequireWildcard(require("esbuild"));
11
-
12
8
  var _compressing = _interopRequireDefault(require("compressing"));
13
-
14
9
  var _execa = _interopRequireDefault(require("execa"));
15
-
16
10
  var _fsExtra = _interopRequireDefault(require("fs-extra"));
17
-
18
11
  var _globby = _interopRequireDefault(require("globby"));
19
-
20
12
  var _nodePath = _interopRequireDefault(require("node:path"));
21
-
22
13
  var _vtils = require("vtils");
23
-
24
14
  class BuildUtil {
25
15
  static async build(options) {
26
16
  var _options$minify, _options$inlineEnvs;
27
-
28
17
  const mainFile = _nodePath.default.join(options.cwd, 'src/main.ts');
29
-
30
18
  const pkgFile = _nodePath.default.join(options.cwd, 'package.json');
31
-
32
19
  const pkgContent = await _fsExtra.default.readJson(pkgFile);
33
-
34
20
  const distDir = _nodePath.default.join(options.cwd, 'dist');
35
-
36
21
  const tempDir = _nodePath.default.join(options.cwd, 'temp');
37
-
38
22
  const distMainFile = _nodePath.default.join(distDir, 'main.js');
39
-
40
23
  const distPkgFile = _nodePath.default.join(distDir, 'package.json');
41
-
42
24
  const distPm2File = _nodePath.default.join(distDir, 'pm2.config.js');
43
-
44
25
  const distNoInstallFile = _nodePath.default.join(distDir, 'no_install.lock');
45
-
46
26
  const distBundleFile = _nodePath.default.join(tempDir, `app.${pkgContent.version}.tgz`);
47
-
48
27
  await _fsExtra.default.ensureDir(distDir);
49
- await _fsExtra.default.ensureDir(tempDir); // 处理 external
28
+ await _fsExtra.default.ensureDir(tempDir);
50
29
 
30
+ // 处理 external
51
31
  const external = (0, _vtils.uniq)(options.external || []);
52
32
  const externalWithVersions = (await Promise.all(external.map(async item => {
53
33
  try {
54
34
  // 为何不用 require.resolve:
55
35
  // Package subpath './package.json' is not defined by "exports"
56
36
  const itemPkgFile = _nodePath.default.join(process.cwd(), `node_modules/${item}/package.json`);
57
-
58
37
  const itemPkgContent = await _fsExtra.default.readJson(itemPkgFile);
59
38
  return {
60
39
  name: item,
@@ -66,17 +45,17 @@ class BuildUtil {
66
45
  version: ''
67
46
  };
68
47
  }
69
- }))).filter(item => !!item.version); // fix: hexoid 构建问题
48
+ }))).filter(item => !!item.version);
70
49
 
50
+ // fix: hexoid 构建问题
71
51
  try {
72
52
  const hexoidPackageJsonFile = require.resolve('hexoid/package.json');
73
-
74
53
  const pkg = await _fsExtra.default.readJson(hexoidPackageJsonFile);
75
54
  delete pkg.module;
76
55
  await _fsExtra.default.writeJson(hexoidPackageJsonFile, pkg);
77
- } catch {} // 构建
78
-
56
+ } catch {}
79
57
 
58
+ // 构建
80
59
  await _fsExtra.default.emptyDir(distDir);
81
60
  await esbuild.build({
82
61
  entryPoints: [mainFile],
@@ -102,7 +81,6 @@ class BuildUtil {
102
81
  },
103
82
  plugins: [{
104
83
  name: 'extract-assets',
105
-
106
84
  setup(build) {
107
85
  build.onLoad({
108
86
  filter: /\/svg-captcha\/lib\/option-manager\.js$/
@@ -130,11 +108,8 @@ class BuildUtil {
130
108
  filter: /\/vm2\/lib\/vm\.js$/
131
109
  }, async args => {
132
110
  let js = await _fsExtra.default.readFile(args.path, 'utf-8');
133
-
134
111
  const jsDir = _nodePath.default.dirname(args.path);
135
-
136
112
  const assetsDir = _nodePath.default.join(distDir, 'assets/vm2');
137
-
138
113
  await _fsExtra.default.ensureDir(assetsDir);
139
114
  js = js.replace(/__dirname/g, '"./assets/vm2"');
140
115
  await Promise.all([_fsExtra.default.copyFile(_nodePath.default.join(jsDir, 'bridge.js'), _nodePath.default.join(assetsDir, 'bridge.js')), _fsExtra.default.copyFile(_nodePath.default.join(jsDir, 'setup-sandbox.js'), _nodePath.default.join(assetsDir, 'setup-sandbox.js'))]);
@@ -144,31 +119,29 @@ class BuildUtil {
144
119
  };
145
120
  });
146
121
  }
147
-
148
122
  }]
149
- }); // 兼容 prisma
123
+ });
150
124
 
125
+ // 兼容 prisma
151
126
  const prismaSchemaFile = _nodePath.default.join(options.cwd, 'src/db/schema.prisma');
152
-
153
127
  if (await _fsExtra.default.pathExists(prismaSchemaFile)) {
154
128
  // 复制 schema.prisma
155
129
  const distPrismaSchemaFile = _nodePath.default.join(distDir, _nodePath.default.basename(prismaSchemaFile));
130
+ await _fsExtra.default.copyFile(prismaSchemaFile, distPrismaSchemaFile);
156
131
 
157
- await _fsExtra.default.copyFile(prismaSchemaFile, distPrismaSchemaFile); // 复制查询引擎
158
-
132
+ // 复制查询引擎
159
133
  const libqueryEngineFiles = await (0, _globby.default)('libquery_engine-*', {
160
134
  cwd: _nodePath.default.join(options.cwd, 'node_modules/.prisma/client'),
161
135
  ignore: ['libquery_engine-{darwin,windows}*'],
162
136
  onlyFiles: true,
163
137
  absolute: true
164
138
  });
165
-
166
139
  for (const libqueryEngineFile of libqueryEngineFiles) {
167
140
  await _fsExtra.default.copyFile(libqueryEngineFile, _nodePath.default.join(distDir, _nodePath.default.basename(libqueryEngineFile)));
168
141
  }
169
- } // 写入 pkg
170
-
142
+ }
171
143
 
144
+ // 写入 pkg
172
145
  const deps = externalWithVersions.reduce((res, item) => {
173
146
  res[item.name] = item.version;
174
147
  return res;
@@ -178,8 +151,9 @@ class BuildUtil {
178
151
  version: pkgContent.version,
179
152
  dependencies: deps
180
153
  };
181
- await _fsExtra.default.writeJSON(distPkgFile, distPkgContent); // 写入 pm2 配置
154
+ await _fsExtra.default.writeJSON(distPkgFile, distPkgContent);
182
155
 
156
+ // 写入 pm2 配置
183
157
  await _fsExtra.default.writeFile(distPm2File, (0, _vtils.dedent)`
184
158
  module.exports = {
185
159
  apps: [
@@ -191,7 +165,6 @@ class BuildUtil {
191
165
  ],
192
166
  }
193
167
  `);
194
-
195
168
  if (externalWithVersions.length) {
196
169
  if (options.noInstall) {
197
170
  await _fsExtra.default.writeFile(distNoInstallFile, '');
@@ -208,12 +181,10 @@ class BuildUtil {
208
181
  }
209
182
  });
210
183
  }
211
- } // 打包为 tgz
212
-
184
+ }
213
185
 
186
+ // 打包为 tgz
214
187
  await _compressing.default.tgz.compressDir(distDir, distBundleFile);
215
188
  }
216
-
217
189
  }
218
-
219
190
  exports.BuildUtil = BuildUtil;
@@ -2,27 +2,17 @@
2
2
  "use strict";
3
3
 
4
4
  var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
5
-
6
5
  var _chokidar = _interopRequireDefault(require("chokidar"));
7
-
8
6
  var _execa = _interopRequireDefault(require("execa"));
9
-
10
7
  var _yargs = _interopRequireDefault(require("yargs"));
11
-
12
8
  var _api_generator = require("./api_generator");
13
-
14
9
  var _build_util = require("./build_util");
15
-
16
10
  var _vtils = require("vtils");
17
-
18
11
  var _deploy_util = require("./deploy_util");
19
-
12
+ var _dev_util = require("./dev_util");
20
13
  var _env_util = require("./env_util");
21
-
22
14
  var _vscodeGenerateIndexStandalone = require("vscode-generate-index-standalone");
23
-
24
15
  var _template_util = require("./template_util");
25
-
26
16
  _yargs.default.command('dev', '开始开发', _ => _.positional('index', {
27
17
  alias: 'i',
28
18
  describe: '额外需要生成的索引文件',
@@ -31,17 +21,19 @@ _yargs.default.command('dev', '开始开发', _ => _.positional('index', {
31
21
  }).positional('npc', {
32
22
  describe: 'npc 地址及密钥,格式:地址@密钥',
33
23
  type: 'string'
24
+ }).positional('tsx', {
25
+ describe: '使用 tsx 运行',
26
+ type: 'boolean'
34
27
  }), async argv => {
35
28
  process.env.NODE_ENV = 'development';
36
29
  let lastRun;
37
-
38
30
  const run = () => {
39
- lastRun = (0, _execa.default)('node', ['-r', require.resolve("./register"), 'src/main.ts'], {
31
+ lastRun = _dev_util.DevUtil.runFile({
32
+ file: 'src/main.ts',
40
33
  cwd: process.cwd(),
41
- stdio: 'inherit'
34
+ runner: argv.tsx ? 'tsx' : 'esbuild-register'
42
35
  });
43
36
  };
44
-
45
37
  if (argv.npc) {
46
38
  const [server, vkey] = argv.npc.split('@');
47
39
  (0, _execa.default)('docker', ['run', '--rm', 'ffdfgdfg/npc:v0.26.9', `-server=${server}`, `-vkey=${vkey}`, '-type=tcp'], {
@@ -49,25 +41,21 @@ _yargs.default.command('dev', '开始开发', _ => _.positional('index', {
49
41
  stdio: 'inherit'
50
42
  });
51
43
  }
52
-
53
44
  await _template_util.TemplateUtil.init(process.cwd());
54
-
55
45
  const watcher = _chokidar.default.watch(['src', '.env', '.env.*'], {
56
- cwd: process.cwd()
46
+ cwd: process.cwd(),
47
+ ignored: '**/*.test.*'
57
48
  });
58
-
59
49
  watcher.on('all', (0, _vtils.debounce)(async () => {
60
50
  await _env_util.EnvUtil.importFile({
61
51
  cwd: process.cwd(),
62
52
  file: '.env'
63
53
  });
64
-
65
54
  _env_util.EnvUtil.outputTypes({
66
55
  cwd: process.cwd(),
67
56
  file: '.env',
68
57
  outFile: 'env.d.ts'
69
58
  });
70
-
71
59
  (0, _vscodeGenerateIndexStandalone.generateManyIndex)({
72
60
  cwd: process.cwd(),
73
61
  patterns: ['src/**/index.ts', 'node_modules/.x/*.ts', '!src/handlers/**/*', ...(argv.index || [])],
@@ -76,7 +64,6 @@ _yargs.default.command('dev', '开始开发', _ => _.positional('index', {
76
64
  console.log(`✔️ 索引文件已更新: ${filePath}`);
77
65
  }
78
66
  });
79
-
80
67
  if (lastRun) {
81
68
  console.log('==== 重启服务中 ====');
82
69
  lastRun.kill();
@@ -142,15 +129,14 @@ _yargs.default.command('dev', '开始开发', _ => _.positional('index', {
142
129
  await _env_util.EnvUtil.importFile({
143
130
  cwd: process.cwd(),
144
131
  file: '.env'
145
- }); // 之所以能成功是因为 Prisma 用的 dotenv 默认不会覆盖已经设置的环境变量
132
+ });
133
+ // 之所以能成功是因为 Prisma 用的 dotenv 默认不会覆盖已经设置的环境变量
146
134
  // https://github.com/motdotla/dotenv#override
147
-
148
135
  process.env.DATABASE_URL = process.env.DATABASE_ACTION_URL || process.env.DATABASE_URL;
149
136
  await (0, _execa.default)('tnpx', ['prisma', ...argv._.slice(1), '--schema', 'src/db/schema.prisma'], {
150
137
  cwd: process.cwd(),
151
138
  stdio: 'inherit'
152
139
  });
153
-
154
140
  if (!argv.production) {
155
141
  await _template_util.TemplateUtil.initModels(process.cwd());
156
142
  }
@@ -162,6 +148,9 @@ _yargs.default.command('dev', '开始开发', _ => _.positional('index', {
162
148
  describe: '是否生产模式',
163
149
  type: 'boolean',
164
150
  default: false
151
+ }).positional('tsx', {
152
+ describe: '使用 tsx 运行',
153
+ type: 'boolean'
165
154
  }), async argv => {
166
155
  process.env.NODE_ENV = argv.production ? 'production' : 'development';
167
156
  await _env_util.EnvUtil.importFile({
@@ -169,16 +158,16 @@ _yargs.default.command('dev', '开始开发', _ => _.positional('index', {
169
158
  file: '.env'
170
159
  });
171
160
  process.env.DATABASE_URL = process.env.DATABASE_ACTION_URL || process.env.DATABASE_URL;
172
-
173
161
  if (!argv.script) {
174
162
  await (0, _execa.default)('tnpx', ['select-run'], {
175
163
  cwd: process.cwd(),
176
164
  stdio: 'inherit'
177
165
  });
178
166
  } else {
179
- await (0, _execa.default)('node', ['-r', require.resolve("./register"), argv.script], {
167
+ await _dev_util.DevUtil.runFile({
168
+ file: argv.script,
180
169
  cwd: process.cwd(),
181
- stdio: 'inherit'
170
+ runner: argv.tsx ? 'tsx' : 'esbuild-register'
182
171
  });
183
172
  }
184
173
  }).command('deploy [type]', '部署', _ => _.positional('type', {
@@ -190,7 +179,6 @@ _yargs.default.command('dev', '开始开发', _ => _.positional('index', {
190
179
  cwd: process.cwd(),
191
180
  file: '.env.deploy'
192
181
  });
193
-
194
182
  if (argv.type === 'env') {
195
183
  const appEnv = await _env_util.EnvUtil.parseFileAsMap({
196
184
  cwd: process.cwd(),
@@ -1,28 +1,19 @@
1
1
  "use strict";
2
2
 
3
3
  var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
4
-
5
4
  exports.__esModule = true;
6
5
  exports.DeployUtil = void 0;
7
-
8
6
  var _fsExtra = _interopRequireDefault(require("fs-extra"));
9
-
10
7
  var _path = _interopRequireDefault(require("path"));
11
-
12
8
  var _vtils = require("vtils");
13
-
14
9
  var _nodeSsh = require("node-ssh");
15
-
16
10
  class DeployUtil {
17
11
  static async deploy(options) {
18
12
  const pkgContent = await _fsExtra.default.readJson(_path.default.join(options.cwd, 'package.json'));
19
-
20
13
  const appFile = _path.default.join(options.cwd, `temp/app.${pkgContent.version}.tgz`);
21
-
22
14
  if (!(await _fsExtra.default.pathExists(appFile))) {
23
15
  throw new Error('请先构建');
24
16
  }
25
-
26
17
  const ssh = new _nodeSsh.NodeSSH();
27
18
  await ssh.connect({
28
19
  host: options.host,
@@ -56,7 +47,6 @@ class DeployUtil {
56
47
  });
57
48
  ssh.dispose();
58
49
  }
59
-
60
50
  static async deployEnv(options) {
61
51
  const ssh = new _nodeSsh.NodeSSH();
62
52
  await ssh.connect({
@@ -80,7 +70,5 @@ class DeployUtil {
80
70
  });
81
71
  ssh.dispose();
82
72
  }
83
-
84
73
  }
85
-
86
74
  exports.DeployUtil = DeployUtil;
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
4
+ exports.__esModule = true;
5
+ exports.DevUtil = void 0;
6
+ var _execa = _interopRequireDefault(require("execa"));
7
+ class DevUtil {
8
+ static runFile(options) {
9
+ return options.runner === 'tsx' ? (0, _execa.default)('tsx', [options.file], {
10
+ cwd: options.cwd,
11
+ stdio: 'inherit'
12
+ }) : (0, _execa.default)('node', ['-r', require.resolve("./esbuild_register"), options.file], {
13
+ cwd: options.cwd,
14
+ stdio: 'inherit'
15
+ });
16
+ }
17
+ }
18
+ exports.DevUtil = DevUtil;