@jayfong/x-server 2.118.1 → 3.0.0

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 (290) hide show
  1. package/lib/_virtual/_rolldown/runtime.mjs +4 -0
  2. package/lib/cli/api_generator.d.mts +74 -0
  3. package/lib/cli/api_generator.mjs +310 -0
  4. package/lib/cli/build_util.d.mts +17 -0
  5. package/lib/cli/build_util.mjs +156 -0
  6. package/lib/cli/cli.d.mts +1 -0
  7. package/lib/cli/cli.mjs +355 -0
  8. package/lib/cli/css_register.d.mts +1 -0
  9. package/lib/cli/css_register.mjs +7 -0
  10. package/lib/cli/deploy_util.d.mts +23 -0
  11. package/lib/cli/deploy_util.mjs +70 -0
  12. package/lib/cli/dev_util.d.mts +7 -0
  13. package/lib/cli/dev_util.mjs +25 -0
  14. package/lib/cli/env_util.d.mts +45 -0
  15. package/lib/cli/env_util.mjs +207 -0
  16. package/lib/cli/esbuild_register.d.mts +1 -0
  17. package/lib/cli/esbuild_register.mjs +7 -0
  18. package/lib/cli/template_util.d.mts +18 -0
  19. package/lib/cli/template_util.mjs +85 -0
  20. package/lib/cli/templates/crons.ts +1 -1
  21. package/lib/cli/templates/handlers.ts +1 -1
  22. package/lib/cli/templates/hooks.ts +1 -1
  23. package/lib/cli/templates/models.ts +7 -7
  24. package/lib/cli/templates/package.json +11 -1
  25. package/lib/cli/templates/routes.ts +2 -1
  26. package/lib/core/ctx_storage.d.mts +3 -0
  27. package/lib/core/ctx_storage.mjs +4 -0
  28. package/lib/core/define_bus.d.mts +37 -0
  29. package/lib/core/define_bus.mjs +12 -0
  30. package/lib/core/{define_cron.d.ts → define_cron.d.mts} +2 -2
  31. package/lib/core/define_cron.mjs +11 -0
  32. package/lib/core/{define_handler.d.ts → define_handler.d.mts} +19 -19
  33. package/lib/core/define_handler.mjs +44 -0
  34. package/lib/core/define_hook.d.mts +2 -0
  35. package/lib/core/define_hook.mjs +6 -0
  36. package/lib/core/define_server.d.mts +3 -0
  37. package/lib/core/define_server.mjs +8 -0
  38. package/lib/core/{define_task.d.ts → define_task.d.mts} +2 -2
  39. package/lib/core/define_task.mjs +102 -0
  40. package/lib/core/get_handler_url.d.mts +2 -0
  41. package/lib/core/get_handler_url.mjs +7 -0
  42. package/lib/core/handler.d.mts +16 -0
  43. package/lib/core/handler.mjs +132 -0
  44. package/lib/core/http_error.d.mts +2 -0
  45. package/lib/core/http_error.mjs +1 -0
  46. package/lib/core/http_header.d.mts +856 -0
  47. package/lib/core/http_header.mjs +860 -0
  48. package/lib/core/http_method.d.mts +3 -0
  49. package/lib/core/http_method.mjs +10 -0
  50. package/lib/core/http_redirect.d.mts +5 -0
  51. package/lib/core/http_redirect.mjs +10 -0
  52. package/lib/core/server.d.mts +20 -0
  53. package/lib/core/server.mjs +186 -0
  54. package/lib/core/types.d.mts +256 -0
  55. package/lib/exports.d.mts +46 -0
  56. package/lib/exports.mjs +46 -0
  57. package/lib/index.d.mts +48 -0
  58. package/lib/index.mjs +45 -0
  59. package/lib/plugins/base.d.mts +4 -0
  60. package/lib/plugins/compress.d.mts +12 -0
  61. package/lib/plugins/compress.mjs +15 -0
  62. package/lib/plugins/cookie.d.mts +12 -0
  63. package/lib/plugins/cookie.mjs +15 -0
  64. package/lib/plugins/cors.d.mts +22 -0
  65. package/lib/plugins/cors.mjs +34 -0
  66. package/lib/plugins/file_parser.d.mts +15 -0
  67. package/lib/plugins/file_parser.mjs +24 -0
  68. package/lib/plugins/form_body_parser.d.mts +12 -0
  69. package/lib/plugins/form_body_parser.mjs +15 -0
  70. package/lib/plugins/ws_parser.d.mts +12 -0
  71. package/lib/plugins/ws_parser.mjs +15 -0
  72. package/lib/plugins/xml_parser.d.mts +19 -0
  73. package/lib/plugins/xml_parser.mjs +40 -0
  74. package/lib/services/ai.d.mts +31 -0
  75. package/lib/services/ai.mjs +44 -0
  76. package/lib/services/base.d.mts +4 -0
  77. package/lib/services/base.mjs +1 -0
  78. package/lib/services/cache.d.mts +165 -0
  79. package/lib/services/cache.mjs +323 -0
  80. package/lib/services/captcha.d.mts +65 -0
  81. package/lib/services/captcha.mjs +61 -0
  82. package/lib/services/{crypto.js → crypto.d.mts} +19 -78
  83. package/lib/services/crypto.mjs +141 -0
  84. package/lib/services/db_value.d.mts +60 -0
  85. package/lib/services/db_value.mjs +64 -0
  86. package/lib/services/dingtalk.d.mts +39 -0
  87. package/lib/services/dingtalk.mjs +44 -0
  88. package/lib/services/dispose.d.mts +17 -0
  89. package/lib/services/dispose.mjs +18 -0
  90. package/lib/services/emoji.d.mts +10 -0
  91. package/lib/services/emoji.mjs +2025 -0
  92. package/lib/services/file_store.d.mts +49 -0
  93. package/lib/services/file_store.mjs +58 -0
  94. package/lib/services/html.d.mts +11 -0
  95. package/lib/services/html.mjs +12 -0
  96. package/lib/services/jwt.d.mts +33 -0
  97. package/lib/services/jwt.mjs +42 -0
  98. package/lib/services/log.d.mts +37 -0
  99. package/lib/services/log.mjs +99 -0
  100. package/lib/services/mail.d.mts +22 -0
  101. package/lib/services/mail.mjs +28 -0
  102. package/lib/services/pay.d.mts +123 -0
  103. package/lib/services/pay.mjs +157 -0
  104. package/lib/services/random.d.mts +27 -0
  105. package/lib/services/random.mjs +42 -0
  106. package/lib/services/rate_limit.d.mts +37 -0
  107. package/lib/services/rate_limit.mjs +40 -0
  108. package/lib/services/redis.d.mts +11 -0
  109. package/lib/services/redis.mjs +6 -0
  110. package/lib/services/sensitive_words.d.mts +29 -0
  111. package/lib/services/sensitive_words.mjs +37 -0
  112. package/lib/services/sms.d.mts +40 -0
  113. package/lib/services/sms.mjs +30238 -0
  114. package/lib/types/dto.d.mts +30 -0
  115. package/lib/types/dto.mjs +1 -0
  116. package/lib/x.d.mts +11 -0
  117. package/lib/x.mjs +21 -0
  118. package/lib/x_init.d.mts +1 -0
  119. package/lib/x_init.mjs +10 -0
  120. package/package.json +99 -43
  121. package/lib/_cjs/cli/api_generator.js +0 -438
  122. package/lib/_cjs/cli/build_util.js +0 -243
  123. package/lib/_cjs/cli/cli.js +0 -378
  124. package/lib/_cjs/cli/css_register.js +0 -7
  125. package/lib/_cjs/cli/deploy_util.js +0 -76
  126. package/lib/_cjs/cli/dev_util.js +0 -25
  127. package/lib/_cjs/cli/env_util.js +0 -252
  128. package/lib/_cjs/cli/esbuild_register.js +0 -7
  129. package/lib/_cjs/cli/template_util.js +0 -101
  130. package/lib/_cjs/cli/templates/crons.ts +0 -2
  131. package/lib/_cjs/cli/templates/handlers.ts +0 -3
  132. package/lib/_cjs/cli/templates/hooks.ts +0 -2
  133. package/lib/_cjs/cli/templates/models.ts +0 -123
  134. package/lib/_cjs/cli/templates/package.json +0 -3
  135. package/lib/_cjs/cli/templates/routes.ts +0 -68
  136. package/lib/_cjs/core/ctx_storage.js +0 -6
  137. package/lib/_cjs/core/define_bus.js +0 -31
  138. package/lib/_cjs/core/define_cron.js +0 -13
  139. package/lib/_cjs/core/define_handler.js +0 -56
  140. package/lib/_cjs/core/define_hook.js +0 -8
  141. package/lib/_cjs/core/define_server.js +0 -10
  142. package/lib/_cjs/core/define_task.js +0 -113
  143. package/lib/_cjs/core/get_handler_url.js +0 -11
  144. package/lib/_cjs/core/handler.js +0 -216
  145. package/lib/_cjs/core/http_error.js +0 -6
  146. package/lib/_cjs/core/http_header.js +0 -142
  147. package/lib/_cjs/core/http_method.js +0 -12
  148. package/lib/_cjs/core/http_redirect.js +0 -11
  149. package/lib/_cjs/core/server.js +0 -221
  150. package/lib/_cjs/core/types.js +0 -8
  151. package/lib/_cjs/exports.js +0 -273
  152. package/lib/_cjs/index.js +0 -15
  153. package/lib/_cjs/plugins/base.js +0 -3
  154. package/lib/_cjs/plugins/compress.js +0 -18
  155. package/lib/_cjs/plugins/cookie.js +0 -18
  156. package/lib/_cjs/plugins/cors.js +0 -38
  157. package/lib/_cjs/plugins/file_parser.js +0 -28
  158. package/lib/_cjs/plugins/form_body_parser.js +0 -18
  159. package/lib/_cjs/plugins/ws_parser.js +0 -18
  160. package/lib/_cjs/plugins/xml_parser.js +0 -48
  161. package/lib/_cjs/services/ai.js +0 -48
  162. package/lib/_cjs/services/base.js +0 -3
  163. package/lib/_cjs/services/cache.js +0 -400
  164. package/lib/_cjs/services/captcha.js +0 -78
  165. package/lib/_cjs/services/crypto.js +0 -175
  166. package/lib/_cjs/services/db_value.js +0 -77
  167. package/lib/_cjs/services/dingtalk.js +0 -51
  168. package/lib/_cjs/services/dispose.js +0 -23
  169. package/lib/_cjs/services/emoji.js +0 -2034
  170. package/lib/_cjs/services/file_store.js +0 -67
  171. package/lib/_cjs/services/html.js +0 -17
  172. package/lib/_cjs/services/jwt.js +0 -65
  173. package/lib/_cjs/services/log.js +0 -108
  174. package/lib/_cjs/services/mail.js +0 -31
  175. package/lib/_cjs/services/pay.js +0 -191
  176. package/lib/_cjs/services/random.js +0 -51
  177. package/lib/_cjs/services/rate_limit.js +0 -54
  178. package/lib/_cjs/services/redis.js +0 -12
  179. package/lib/_cjs/services/sensitive_words.js +0 -62
  180. package/lib/_cjs/services/sms.js +0 -84
  181. package/lib/_cjs/types/dto.js +0 -3
  182. package/lib/_cjs/x.js +0 -35
  183. package/lib/cli/api_generator.d.ts +0 -74
  184. package/lib/cli/api_generator.js +0 -432
  185. package/lib/cli/build_util.d.ts +0 -17
  186. package/lib/cli/build_util.js +0 -236
  187. package/lib/cli/cli.d.ts +0 -2
  188. package/lib/cli/cli.js +0 -374
  189. package/lib/cli/css_register.js +0 -5
  190. package/lib/cli/deploy_util.d.ts +0 -23
  191. package/lib/cli/deploy_util.js +0 -70
  192. package/lib/cli/dev_util.d.ts +0 -8
  193. package/lib/cli/dev_util.js +0 -19
  194. package/lib/cli/env_util.d.ts +0 -45
  195. package/lib/cli/env_util.js +0 -246
  196. package/lib/cli/esbuild_register.js +0 -5
  197. package/lib/cli/template_util.d.ts +0 -18
  198. package/lib/cli/template_util.js +0 -95
  199. package/lib/core/ctx_storage.d.ts +0 -3
  200. package/lib/core/ctx_storage.js +0 -2
  201. package/lib/core/define_bus.d.ts +0 -37
  202. package/lib/core/define_bus.js +0 -27
  203. package/lib/core/define_cron.js +0 -9
  204. package/lib/core/define_handler.js +0 -48
  205. package/lib/core/define_hook.d.ts +0 -2
  206. package/lib/core/define_hook.js +0 -4
  207. package/lib/core/define_server.d.ts +0 -3
  208. package/lib/core/define_server.js +0 -6
  209. package/lib/core/define_task.js +0 -107
  210. package/lib/core/get_handler_url.d.ts +0 -2
  211. package/lib/core/get_handler_url.js +0 -7
  212. package/lib/core/handler.d.ts +0 -16
  213. package/lib/core/handler.js +0 -209
  214. package/lib/core/http_error.d.ts +0 -2
  215. package/lib/core/http_error.js +0 -2
  216. package/lib/core/http_header.d.ts +0 -856
  217. package/lib/core/http_header.js +0 -139
  218. package/lib/core/http_method.d.ts +0 -3
  219. package/lib/core/http_method.js +0 -8
  220. package/lib/core/http_redirect.d.ts +0 -5
  221. package/lib/core/http_redirect.js +0 -6
  222. package/lib/core/server.d.ts +0 -20
  223. package/lib/core/server.js +0 -215
  224. package/lib/core/types.d.ts +0 -265
  225. package/lib/core/types.js +0 -4
  226. package/lib/exports.d.ts +0 -45
  227. package/lib/exports.js +0 -47
  228. package/lib/index.d.ts +0 -2
  229. package/lib/index.js +0 -3
  230. package/lib/plugins/base.d.ts +0 -4
  231. package/lib/plugins/base.js +0 -1
  232. package/lib/plugins/compress.d.ts +0 -13
  233. package/lib/plugins/compress.js +0 -12
  234. package/lib/plugins/cookie.d.ts +0 -13
  235. package/lib/plugins/cookie.js +0 -12
  236. package/lib/plugins/cors.d.ts +0 -22
  237. package/lib/plugins/cors.js +0 -33
  238. package/lib/plugins/file_parser.d.ts +0 -16
  239. package/lib/plugins/file_parser.js +0 -22
  240. package/lib/plugins/form_body_parser.d.ts +0 -13
  241. package/lib/plugins/form_body_parser.js +0 -12
  242. package/lib/plugins/ws_parser.d.ts +0 -13
  243. package/lib/plugins/ws_parser.js +0 -12
  244. package/lib/plugins/xml_parser.d.ts +0 -19
  245. package/lib/plugins/xml_parser.js +0 -43
  246. package/lib/services/ai.d.ts +0 -31
  247. package/lib/services/ai.js +0 -43
  248. package/lib/services/base.d.ts +0 -4
  249. package/lib/services/base.js +0 -1
  250. package/lib/services/cache.d.ts +0 -173
  251. package/lib/services/cache.js +0 -394
  252. package/lib/services/captcha.d.ts +0 -65
  253. package/lib/services/captcha.js +0 -72
  254. package/lib/services/crypto.d.ts +0 -110
  255. package/lib/services/db_value.d.ts +0 -60
  256. package/lib/services/db_value.js +0 -72
  257. package/lib/services/dingtalk.d.ts +0 -39
  258. package/lib/services/dingtalk.js +0 -45
  259. package/lib/services/dispose.d.ts +0 -17
  260. package/lib/services/dispose.js +0 -17
  261. package/lib/services/emoji.d.ts +0 -10
  262. package/lib/services/emoji.js +0 -2029
  263. package/lib/services/file_store.d.ts +0 -49
  264. package/lib/services/file_store.js +0 -61
  265. package/lib/services/html.d.ts +0 -11
  266. package/lib/services/html.js +0 -12
  267. package/lib/services/jwt.d.ts +0 -34
  268. package/lib/services/jwt.js +0 -59
  269. package/lib/services/log.d.ts +0 -37
  270. package/lib/services/log.js +0 -102
  271. package/lib/services/mail.d.ts +0 -23
  272. package/lib/services/mail.js +0 -25
  273. package/lib/services/pay.d.ts +0 -123
  274. package/lib/services/pay.js +0 -185
  275. package/lib/services/random.d.ts +0 -27
  276. package/lib/services/random.js +0 -45
  277. package/lib/services/rate_limit.d.ts +0 -37
  278. package/lib/services/rate_limit.js +0 -49
  279. package/lib/services/redis.d.ts +0 -12
  280. package/lib/services/redis.js +0 -7
  281. package/lib/services/sensitive_words.d.ts +0 -30
  282. package/lib/services/sensitive_words.js +0 -57
  283. package/lib/services/sms.d.ts +0 -40
  284. package/lib/services/sms.js +0 -78
  285. package/lib/types/dto.d.ts +0 -86
  286. package/lib/types/dto.js +0 -1
  287. package/lib/x.d.ts +0 -17
  288. package/lib/x.js +0 -30
  289. /package/lib/{cli/css_register.d.ts → core/types.mjs} +0 -0
  290. /package/lib/{cli/esbuild_register.d.ts → plugins/base.mjs} +0 -0
@@ -1,236 +0,0 @@
1
- import path from 'node:path';
2
- import compressing from 'compressing';
3
- import * as esbuild from 'esbuild';
4
- import exec from 'execa';
5
- import fs from 'fs-extra';
6
- import { dedent, uniq } from 'vtils';
7
- export class BuildUtil {
8
- static async build(options) {
9
- const mainFile = path.join(options.cwd, 'src/main.ts');
10
- const pkgFile = path.join(options.cwd, 'package.json');
11
- const pkgContent = await fs.readJson(pkgFile);
12
- const distDir = path.join(options.cwd, options.channel ? `dist/${options.channel}` : 'dist');
13
- const tempDir = path.join(options.cwd, options.channel ? `temp/${options.channel}` : 'temp');
14
- const distMainFile = path.join(distDir, 'main.js');
15
- const distPkgFile = path.join(distDir, 'package.json');
16
- const distNvmrcFile = path.join(distDir, '.nvmrc');
17
- const distPm2File = path.join(distDir, 'pm2.config.js');
18
- const distNoInstallFile = path.join(distDir, 'no_install.lock');
19
- const distBundleFile = path.join(tempDir, `app.${pkgContent.version}.tgz`);
20
- await fs.ensureDir(distDir);
21
- await fs.ensureDir(tempDir);
22
-
23
- // 处理 external
24
- const external = uniq([...(options.external || []),
25
- // bull 始终作为外部依赖
26
- 'bull']);
27
- const externalWithVersions = (await Promise.all(external.map(async item => {
28
- try {
29
- // 为何不用 require.resolve:
30
- // Package subpath './package.json' is not defined by "exports"
31
- const itemPkgFile = path.join(process.cwd(), `node_modules/${item}/package.json`);
32
- const itemPkgContent = await fs.readJson(itemPkgFile);
33
- return {
34
- name: item,
35
- version: itemPkgContent.version
36
- };
37
- } catch {
38
- return {
39
- name: item,
40
- version: ''
41
- };
42
- }
43
- }))).filter(item => !!item.version);
44
-
45
- // fix: hexoid 构建问题
46
- try {
47
- const hexoidPackageJsonFile = require.resolve('hexoid/package.json');
48
- const pkg = await fs.readJson(hexoidPackageJsonFile);
49
- delete pkg.module;
50
- await fs.writeJson(hexoidPackageJsonFile, pkg);
51
- } catch {}
52
-
53
- // 构建
54
- if (!options.noEmptyDistDir) {
55
- await fs.emptyDir(distDir);
56
- }
57
- // @ts-ignore
58
- const {
59
- default: macrosPlugin
60
- } = await import('unplugin-macros/esbuild');
61
- await esbuild.build({
62
- entryPoints: [mainFile],
63
- bundle: true,
64
- outfile: distMainFile,
65
- platform: 'node',
66
- // https://esbuild.github.io/api/#main-fields
67
- // 当 platform 是 node 时,
68
- // esbuild 默认的 mainFields 是 main,module,
69
- // 这会导致 treeShaking 失效
70
- mainFields: ['module', 'main'],
71
- target: `node${process.version.slice(1)}`,
72
- external: external,
73
- minify: options.minify ?? true,
74
- format: 'cjs',
75
- sourcemap: false,
76
- treeShaking: true,
77
- banner: {
78
- js: [...Object.keys(options.inlineEnvs || {}).map(key => `process.env[${JSON.stringify(key)}]=${JSON.stringify(options.inlineEnvs[key])};`), `process.env["X_SERVER_ENVS"]=${JSON.stringify(JSON.stringify(options.inlineEnvs || {}))};`].join('\n')
79
- },
80
- plugins: [{
81
- name: 'extract-assets',
82
- async setup(build) {
83
- build.onLoad({
84
- filter: /\/svg-captcha\/lib\/option-manager\.js$/
85
- }, async args => {
86
- let js = await fs.readFile(args.path, 'utf-8');
87
- js = js.replace('../fonts/Comismsh.ttf', './assets/svg-captcha/fonts/Comismsh.ttf');
88
- await fs.copy(path.dirname(require.resolve('svg-captcha/fonts/LICENSE.md')), path.join(distDir, 'assets/svg-captcha/fonts'));
89
- return {
90
- contents: js,
91
- loader: 'js'
92
- };
93
- });
94
-
95
- // build.onLoad(
96
- // { filter: /\/bull\/lib\/commands\/index\.js$/ },
97
- // async args => {
98
- // let js = await fs.readFile(args.path, 'utf-8');
99
- // js = js.replace(/__dirname/g, '"./assets/bull/commands"');
100
- // await fs.copy(
101
- // path.dirname(require.resolve('bull/lib/commands/index.js')),
102
- // path.join(distDir, 'assets/bull/commands'),
103
- // );
104
- // return {
105
- // contents: js,
106
- // loader: 'js',
107
- // };
108
- // },
109
- // );
110
-
111
- build.onLoad({
112
- filter: /\/vm2\/lib\/vm\.js$/
113
- }, async args => {
114
- let js = await fs.readFile(args.path, 'utf-8');
115
- const jsDir = path.dirname(args.path);
116
- const assetsDir = path.join(distDir, 'assets/vm2');
117
- await fs.ensureDir(assetsDir);
118
- js = js.replace(/__dirname/g, '"./assets/vm2"');
119
- await Promise.all([fs.copyFile(path.join(jsDir, 'bridge.js'), path.join(assetsDir, 'bridge.js')), fs.copyFile(path.join(jsDir, 'setup-sandbox.js'), path.join(assetsDir, 'setup-sandbox.js'))]);
120
- return {
121
- contents: js,
122
- loader: 'js'
123
- };
124
- });
125
-
126
- // 支持仅开发时生效的路由
127
- build.onLoad({
128
- filter: /@dev[/.]/
129
- }, async () => {
130
- return {
131
- contents: 'module.exports = {}',
132
- loader: 'js'
133
- };
134
- });
135
-
136
- // 支持构建时排除文件
137
- if (options.excludeFiles?.length) {
138
- const excludeRegExps = options.excludeFiles.map(item => new RegExp(item));
139
- build.onLoad({
140
- filter: /\.ts$/
141
- }, async args => {
142
- if (excludeRegExps.some(re => re.test(args.path))) {
143
- return {
144
- // 不用 export {} 避免 esbuild 检测报错
145
- contents: 'module.exports = {}',
146
- loader: 'js'
147
- };
148
- }
149
- });
150
- }
151
- }
152
- }, macrosPlugin()],
153
- loader: {
154
- // 兼容 x-text-render 在服务端使用
155
- // dev 时用 css_register 兼容
156
- // https://esbuild.github.io/content-types/#empty-file
157
- '.css': 'empty'
158
- }
159
- });
160
-
161
- // 兼容 prisma
162
- const prismaSchemaFile = path.join(options.cwd, 'src/db/schema.prisma');
163
- if (await fs.pathExists(prismaSchemaFile)) {
164
- // 复制 schema.prisma
165
- const distPrismaSchemaFile = path.join(distDir, path.basename(prismaSchemaFile));
166
- await fs.copyFile(prismaSchemaFile, distPrismaSchemaFile);
167
-
168
- // 多 schema 文件兼容
169
- const multipleSchemaDir = path.join(options.cwd, 'src/db/schema');
170
- if (await fs.pathExists(multipleSchemaDir)) {
171
- const distMultipleSchemaDir = path.join(distDir, path.basename(multipleSchemaDir));
172
- await fs.copy(multipleSchemaDir, distMultipleSchemaDir);
173
- }
174
- }
175
-
176
- // 写入 pkg
177
- const deps = externalWithVersions.reduce((res, item) => {
178
- res[item.name] = item.version;
179
- return res;
180
- }, {});
181
- const distPkgContent = {
182
- name: options.appId,
183
- version: pkgContent.version,
184
- dependencies: deps
185
- };
186
- await fs.writeJSON(distPkgFile, distPkgContent);
187
-
188
- // .nvmrc
189
- const nvmrcFile = path.join(options.cwd, '.nvmrc');
190
- const hasNvmrc = await fs.pathExists(nvmrcFile);
191
- if (hasNvmrc) {
192
- await fs.writeFile(distNvmrcFile, await fs.readFile(nvmrcFile));
193
- }
194
-
195
- // 写入 pm2 配置
196
- await fs.writeFile(distPm2File, dedent`
197
- module.exports = {
198
- apps: [
199
- {
200
- name: ${JSON.stringify(distPkgContent.name)},
201
- script: './main.js',
202
- interpreter: 'node',
203
- interpreter_args: ${JSON.stringify(options.pm2NodeArgs || [])},
204
- ${hasNvmrc ? `
205
- env: {
206
- PATH: \`\${require('child_process').execSync('fnm exec which node').toString().trim().replace(/\\/node$/, '')}:\${process.env.PATH}\`,
207
- },
208
- ` : ''}
209
- ${options.pm2MaxMemoryRestart ? `max_memory_restart: '${options.pm2MaxMemoryRestart}',` : ''}
210
- ${options.pm2CronRestart ? `cron_restart: '${options.pm2CronRestart}',` : ''}
211
- },
212
- ],
213
- }
214
- `);
215
- if (externalWithVersions.length) {
216
- if (options.noInstall) {
217
- await fs.writeFile(distNoInstallFile, '');
218
- } else {
219
- // 安装 external 依赖
220
- await exec('tyn', {
221
- cwd: distDir,
222
- stdio: 'inherit',
223
- // @ts-ignore
224
- env: {
225
- // 禁止 yarn 更新检测,因网络原因容易超时长时间卡住
226
- // https://classic.yarnpkg.com/lang/en/docs/yarnrc/#toc-disable-self-update-check
227
- yarn_disable_self_update_check: 'true'
228
- }
229
- });
230
- }
231
- }
232
-
233
- // 打包为 tgz
234
- await compressing.tgz.compressDir(distDir, distBundleFile);
235
- }
236
- }
package/lib/cli/cli.d.ts DELETED
@@ -1,2 +0,0 @@
1
- #!/usr/bin/env node
2
- export {};
package/lib/cli/cli.js DELETED
@@ -1,374 +0,0 @@
1
- #!/usr/bin/env node
2
- import path from 'node:path';
3
- import chokidar from 'chokidar';
4
- import execa from 'execa';
5
- import fs from 'fs-extra';
6
- import { generateManyIndex } from 'vscode-generate-index-standalone';
7
- import * as vtils from 'vtils';
8
- import { castArray, debounce, StringTemplate } from 'vtils';
9
- import yargs from 'yargs';
10
- import { ApiGenerator } from "./api_generator";
11
- import { BuildUtil } from "./build_util";
12
- import { DeployUtil } from "./deploy_util";
13
- import { DevUtil } from "./dev_util";
14
- import { EnvUtil } from "./env_util";
15
- import { TemplateUtil } from "./template_util";
16
- async function deploy(argv) {
17
- await EnvUtil.withChannel({
18
- cwd: process.cwd(),
19
- channel: argv.channel,
20
- cb: async channel => {
21
- const deployEnvFiles = EnvUtil.getFile('deploy', channel, true);
22
- const deployEnv = await EnvUtil.parseFileAsMap({
23
- cwd: process.cwd(),
24
- file: deployEnvFiles,
25
- channel: channel
26
- });
27
- if (argv.type === 'env') {
28
- const appEnvFiles = EnvUtil.getFile('production', channel);
29
- const appEnv = await EnvUtil.parseFileAsMap({
30
- cwd: process.cwd(),
31
- file: appEnvFiles,
32
- channel: channel
33
- });
34
- await DeployUtil.deployEnv({
35
- host: deployEnv.HOST,
36
- user: deployEnv.USER,
37
- key: deployEnv.KEY,
38
- pass: deployEnv.PASSWORD,
39
- token: appEnv.APP_TOKEN,
40
- port: appEnv.APP_PORT,
41
- env: appEnv
42
- });
43
- } else {
44
- await DeployUtil.deploy({
45
- cwd: process.cwd(),
46
- host: deployEnv.HOST,
47
- user: deployEnv.USER,
48
- key: deployEnv.KEY,
49
- pass: deployEnv.PASSWORD,
50
- dir: deployEnv.DIR,
51
- cmd: deployEnv.CMD,
52
- channel: channel
53
- });
54
- }
55
- }
56
- });
57
- }
58
- yargs.command('dev', '开始开发', _ => _.positional('index', {
59
- alias: 'i',
60
- describe: '额外需要生成的索引文件',
61
- type: 'string',
62
- array: true
63
- }).positional('npc', {
64
- describe: 'npc 地址及密钥,格式:地址@密钥',
65
- type: 'string'
66
- }).positional('tsx', {
67
- describe: '使用 tsx 运行',
68
- type: 'boolean'
69
- }).positional('channel', {
70
- alias: 'h',
71
- describe: '渠道',
72
- type: 'string'
73
- }), async argv => {
74
- await EnvUtil.withChannel({
75
- cwd: process.cwd(),
76
- channel: argv.channel,
77
- cb: async channel => {
78
- process.env.NODE_ENV = 'development';
79
- let lastRun;
80
- const run = () => {
81
- lastRun = DevUtil.runFile({
82
- file: 'src/main.ts',
83
- cwd: process.cwd(),
84
- runner: argv.tsx ? 'tsx' : 'esbuild-register'
85
- });
86
- };
87
- if (argv.npc) {
88
- const [server, vkey] = argv.npc.split('@');
89
- execa('docker', ['run', '--rm', 'ffdfgdfg/npc:v0.26.9', `-server=${server}`, `-vkey=${vkey}`, '-type=tcp'], {
90
- cwd: process.cwd(),
91
- stdio: 'inherit'
92
- });
93
- }
94
- await TemplateUtil.init(process.cwd());
95
- const xsignoreFile = path.join(process.cwd(), '.xsignore');
96
- const xsignoreContent = (await fs.pathExists(xsignoreFile)) ? (await fs.readFile(xsignoreFile, 'utf-8')).trim().split(/[\r\n]+/g).filter(v => v && !v.startsWith('#')) : [];
97
- const watcher = chokidar.watch(['src', '.env', '.env*'], {
98
- cwd: process.cwd(),
99
- ignored: ['**/*.test.*', ...xsignoreContent]
100
- });
101
- watcher.on('all', debounce(async () => {
102
- const envFiles = EnvUtil.getFile(process.env.NODE_ENV, channel);
103
- await EnvUtil.importFile({
104
- cwd: process.cwd(),
105
- file: envFiles,
106
- channel: channel
107
- });
108
- EnvUtil.outputTypes({
109
- cwd: process.cwd(),
110
- file: envFiles,
111
- outFile: 'env.d.ts',
112
- channel: channel
113
- });
114
- generateManyIndex({
115
- cwd: process.cwd(),
116
- patterns: ['src/**/index.ts', 'node_modules/.x/*.ts', '!src/handlers/**/*', ...(argv.index || [])],
117
- replaceFile: true,
118
- onSuccess: filePath => {
119
- console.log(`✔️ 索引文件已更新: ${filePath}`);
120
- }
121
- });
122
- if (lastRun) {
123
- console.log('==== 重启服务中 ====');
124
- lastRun.kill();
125
- setTimeout(run, 0);
126
- } else {
127
- console.log('==== 启动服务中 ====');
128
- run();
129
- }
130
- }, 30));
131
- }
132
- });
133
- }).command('build', '构建', _ => _.positional('external', {
134
- alias: 'e',
135
- describe: '不应该被打的包',
136
- type: 'string',
137
- array: true
138
- }).positional('minify', {
139
- alias: 'm',
140
- describe: '是否压缩',
141
- type: 'boolean',
142
- default: true
143
- }).positional('local-install', {
144
- describe: '是否本地安装外部依赖',
145
- type: 'boolean',
146
- default: true
147
- }).positional('channel', {
148
- alias: 'h',
149
- describe: '渠道',
150
- type: 'string'
151
- }).positional('deploy', {
152
- alias: 'd',
153
- describe: '构建完是否部署',
154
- type: 'boolean',
155
- default: false
156
- }).positional('concurrency', {
157
- alias: 'c',
158
- describe: '同时构建的数量',
159
- type: 'number',
160
- default: 1
161
- }).positional('exclude', {
162
- alias: 'x',
163
- describe: '构建时排除的文件',
164
- type: 'string',
165
- array: true
166
- }).positional('empty-dist-dir', {
167
- describe: '是否清空构建目录',
168
- type: 'boolean',
169
- default: true
170
- }), async argv => {
171
- await EnvUtil.withChannel({
172
- cwd: process.cwd(),
173
- channel: argv.channel,
174
- concurrency: argv.concurrency,
175
- cb: async channel => {
176
- process.env.NODE_ENV = 'production';
177
- const externals = castArray(argv.external || []).flatMap(item => item.split(',')).filter(Boolean);
178
- const envFiles = EnvUtil.getFile(process.env.NODE_ENV, channel);
179
- const envMap = await EnvUtil.parseFileAsMap({
180
- cwd: process.cwd(),
181
- file: envFiles,
182
- channel: channel
183
- });
184
- envMap.NODE_ENV = 'production';
185
- const deployEnvFiles = EnvUtil.getFile('deploy', channel, true);
186
- const deployEnv = await EnvUtil.parseFileAsMap({
187
- cwd: process.cwd(),
188
- file: deployEnvFiles,
189
- channel: channel
190
- });
191
- await TemplateUtil.init(process.cwd());
192
- await generateManyIndex({
193
- cwd: process.cwd(),
194
- patterns: ['src/.x/*.ts'],
195
- replaceFile: true,
196
- onSuccess: filePath => {
197
- console.log(`✔️ 索引文件已更新: ${filePath}`);
198
- }
199
- });
200
- await BuildUtil.build({
201
- appId: envMap.APP_ID,
202
- cwd: process.cwd(),
203
- external: externals,
204
- inlineEnvs: envMap,
205
- minify: argv.minify,
206
- noInstall: argv['local-install'] === false,
207
- channel: channel,
208
- excludeFiles: (argv.exclude ? castArray(argv.exclude) : []).map(item => {
209
- return StringTemplate.render(item, {
210
- ...envMap,
211
- _: vtils,
212
- __: vtils.escapeRegExp
213
- }, {
214
- code: true,
215
- onlyCode: true
216
- });
217
- }),
218
- noEmptyDistDir: argv['empty-dist-dir'] === false,
219
- pm2MaxMemoryRestart: deployEnv.MEMORY,
220
- pm2CronRestart: deployEnv.CRON,
221
- pm2NodeArgs: deployEnv.NODE_ARGS
222
- });
223
- console.log('构建成功');
224
- if (argv.deploy) {
225
- await deploy({
226
- ...argv,
227
- channel
228
- });
229
- }
230
- }
231
- });
232
- }).command('api', '生成 API', _ => _.positional('channel', {
233
- alias: 'h',
234
- describe: '渠道',
235
- type: 'string'
236
- }).positional('include', {
237
- alias: 'i',
238
- describe: '包括',
239
- type: 'string',
240
- array: true
241
- }).positional('exclude', {
242
- alias: 'e',
243
- describe: '排除',
244
- type: 'string',
245
- array: true
246
- }).positional('name', {
247
- alias: 'n',
248
- describe: '名称',
249
- type: 'string'
250
- }), async argv => {
251
- await EnvUtil.withChannel({
252
- cwd: process.cwd(),
253
- channel: argv.channel,
254
- cb: async channel => {
255
- process.env.NODE_ENV = 'production';
256
- const envFiles = EnvUtil.getFile(process.env.NODE_ENV, channel);
257
- await EnvUtil.importFile({
258
- cwd: process.cwd(),
259
- file: envFiles,
260
- channel: channel
261
- });
262
- await new ApiGenerator({
263
- cwd: process.cwd(),
264
- include: argv.include && castArray(argv.include).flatMap(v => v.split(',')),
265
- exclude: argv.exclude && castArray(argv.exclude).flatMap(v => v.split(',')),
266
- name: argv.name
267
- }).start();
268
- }
269
- });
270
- }).command('prisma', 'prisma 代理,主要为了注入环境变量', _ => _.positional('production', {
271
- alias: 'p',
272
- describe: '是否生产模式',
273
- type: 'boolean',
274
- default: false
275
- }).positional('channel', {
276
- alias: 'h',
277
- describe: '渠道',
278
- type: 'string'
279
- }), async argv => {
280
- await EnvUtil.withChannel({
281
- cwd: process.cwd(),
282
- channel: argv.channel,
283
- cb: async channel => {
284
- process.env.NODE_ENV = argv.production ? 'production' : 'development';
285
- const envFiles = EnvUtil.getFile(process.env.NODE_ENV, channel);
286
- await EnvUtil.importFile({
287
- cwd: process.cwd(),
288
- file: envFiles,
289
- channel: channel
290
- });
291
- // 之所以能成功是因为 Prisma 用的 dotenv 默认不会覆盖已经设置的环境变量
292
- // https://github.com/motdotla/dotenv#override
293
- process.env.DATABASE_URL = process.env.DATABASE_ACTION_URL || process.env.DATABASE_URL;
294
-
295
- // 多 schema 文件兼容
296
- // https://www.prisma.io/docs/orm/prisma-schema/overview/location#how-to-use-existing-prisma-cli-commands-with-multiple-prisma-schema-files
297
- const schemaDir = path.join(process.cwd(), 'src/db/schema');
298
- const isMultipleFiles = await fs.pathExists(schemaDir);
299
- const schemaArg = isMultipleFiles ? 'src/db' : 'src/db/schema.prisma';
300
- const prismaArgs = argv._.slice(1);
301
- await execa('tnpx', ['prisma', ...prismaArgs, '--schema', schemaArg], {
302
- cwd: process.cwd(),
303
- stdio: 'inherit'
304
- });
305
- if (!argv.production && prismaArgs[0] === 'generate') {
306
- await TemplateUtil.initModels(process.cwd());
307
- }
308
- }
309
- });
310
- }).command('run [script]', '执行脚本', _ => _.positional('script', {
311
- describe: '脚本',
312
- type: 'string'
313
- }).positional('production', {
314
- alias: 'p',
315
- describe: '是否生产模式',
316
- type: 'boolean',
317
- default: false
318
- }).positional('channel', {
319
- alias: 'h',
320
- describe: '渠道',
321
- type: 'string'
322
- }).positional('tsx', {
323
- describe: '使用 tsx 运行',
324
- type: 'boolean'
325
- }), async argv => {
326
- await EnvUtil.withChannel({
327
- cwd: process.cwd(),
328
- channel: argv.channel,
329
- cb: async channel => {
330
- process.env.NODE_ENV = argv.production ? 'production' : 'development';
331
- const envFiles = EnvUtil.getFile(process.env.NODE_ENV, channel);
332
- await EnvUtil.importFile({
333
- cwd: process.cwd(),
334
- file: envFiles,
335
- channel: channel
336
- });
337
- process.env.DATABASE_URL = process.env.DATABASE_ACTION_URL || process.env.DATABASE_URL;
338
- if (!argv.script) {
339
- await execa('tnpx', ['select-run'], {
340
- cwd: process.cwd(),
341
- stdio: 'inherit'
342
- });
343
- } else {
344
- await DevUtil.runFile({
345
- file: argv.script,
346
- cwd: process.cwd(),
347
- runner: argv.tsx ? 'tsx' : 'esbuild-register'
348
- });
349
- }
350
- }
351
- });
352
- }).command('deploy [type]', '部署', _ => _.positional('type', {
353
- describe: '类型',
354
- type: 'string',
355
- choices: ['env']
356
- }).positional('channel', {
357
- alias: 'h',
358
- describe: '渠道',
359
- type: 'string'
360
- }), deploy).command('get-channel [file]', '获取渠道', _ => _.positional('file', {
361
- describe: '写入文件',
362
- type: 'string'
363
- }), async argv => {
364
- await EnvUtil.withChannel({
365
- cwd: process.cwd(),
366
- channel: '_',
367
- cb: async channel => {
368
- if (channel && argv.file) {
369
- await fs.writeFile(path.join(process.cwd(), argv.file), channel);
370
- }
371
- console.log(channel || '');
372
- }
373
- });
374
- }).parse();
@@ -1,5 +0,0 @@
1
- import { addHook } from 'pirates';
2
- addHook(() => 'module.exports = {};', {
3
- exts: ['.css'],
4
- ignoreNodeModules: false
5
- });
@@ -1,23 +0,0 @@
1
- export interface DeployOptions {
2
- cwd: string;
3
- host: string;
4
- user: string;
5
- key?: string;
6
- pass?: string;
7
- dir: string;
8
- cmd?: string;
9
- channel?: string;
10
- }
11
- export interface DeployEnvOptions {
12
- host: string;
13
- user: string;
14
- key?: string;
15
- pass?: string;
16
- port: number;
17
- token: string;
18
- env: Record<string, any>;
19
- }
20
- export declare class DeployUtil {
21
- static deploy(options: DeployOptions): Promise<void>;
22
- static deployEnv(options: DeployEnvOptions): Promise<void>;
23
- }