@jayfong/x-server 2.119.0 → 3.1.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.
Files changed (288) hide show
  1. package/lib/_virtual/_rolldown/runtime.mjs +4 -0
  2. package/lib/cli/api_generator.d.mts +75 -0
  3. package/lib/cli/api_generator.mjs +334 -0
  4. package/lib/cli/build_util.d.mts +15 -0
  5. package/lib/cli/build_util.mjs +101 -0
  6. package/lib/cli/cli.d.mts +1 -0
  7. package/lib/cli/cli.mjs +345 -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 +9 -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/template_util.d.mts +18 -0
  17. package/lib/cli/template_util.mjs +85 -0
  18. package/lib/cli/templates/crons.ts +1 -1
  19. package/lib/cli/templates/handlers.ts +1 -1
  20. package/lib/cli/templates/hooks.ts +1 -1
  21. package/lib/cli/templates/models.ts +7 -7
  22. package/lib/cli/templates/package.json +11 -1
  23. package/lib/cli/templates/routes.ts +2 -2
  24. package/lib/core/ctx_storage.d.mts +3 -0
  25. package/lib/core/ctx_storage.mjs +4 -0
  26. package/lib/core/define_bus.d.mts +37 -0
  27. package/lib/core/define_bus.mjs +12 -0
  28. package/lib/core/{define_cron.d.ts → define_cron.d.mts} +2 -2
  29. package/lib/core/define_cron.mjs +11 -0
  30. package/lib/core/{define_handler.d.ts → define_handler.d.mts} +19 -19
  31. package/lib/core/define_handler.mjs +44 -0
  32. package/lib/core/define_hook.d.mts +2 -0
  33. package/lib/core/define_hook.mjs +6 -0
  34. package/lib/core/define_server.d.mts +3 -0
  35. package/lib/core/define_server.mjs +8 -0
  36. package/lib/core/{define_task.d.ts → define_task.d.mts} +2 -2
  37. package/lib/core/define_task.mjs +106 -0
  38. package/lib/core/get_handler_url.d.mts +2 -0
  39. package/lib/core/get_handler_url.mjs +7 -0
  40. package/lib/core/handler.d.mts +16 -0
  41. package/lib/core/handler.mjs +132 -0
  42. package/lib/core/http_error.d.mts +2 -0
  43. package/lib/core/http_error.mjs +1 -0
  44. package/lib/core/http_header.d.mts +856 -0
  45. package/lib/core/http_header.mjs +860 -0
  46. package/lib/core/http_method.d.mts +3 -0
  47. package/lib/core/http_method.mjs +10 -0
  48. package/lib/core/http_redirect.d.mts +5 -0
  49. package/lib/core/http_redirect.mjs +10 -0
  50. package/lib/core/server.d.mts +20 -0
  51. package/lib/core/server.mjs +186 -0
  52. package/lib/core/types.d.mts +256 -0
  53. package/lib/exports.d.mts +46 -0
  54. package/lib/exports.mjs +46 -0
  55. package/lib/index.d.mts +48 -0
  56. package/lib/index.mjs +45 -0
  57. package/lib/plugins/base.d.mts +4 -0
  58. package/lib/plugins/compress.d.mts +12 -0
  59. package/lib/plugins/compress.mjs +15 -0
  60. package/lib/plugins/cookie.d.mts +12 -0
  61. package/lib/plugins/cookie.mjs +15 -0
  62. package/lib/plugins/cors.d.mts +22 -0
  63. package/lib/plugins/cors.mjs +34 -0
  64. package/lib/plugins/file_parser.d.mts +15 -0
  65. package/lib/plugins/file_parser.mjs +24 -0
  66. package/lib/plugins/form_body_parser.d.mts +12 -0
  67. package/lib/plugins/form_body_parser.mjs +15 -0
  68. package/lib/plugins/ws_parser.d.mts +12 -0
  69. package/lib/plugins/ws_parser.mjs +15 -0
  70. package/lib/plugins/xml_parser.d.mts +19 -0
  71. package/lib/plugins/xml_parser.mjs +40 -0
  72. package/lib/services/ai.d.mts +31 -0
  73. package/lib/services/ai.mjs +44 -0
  74. package/lib/services/base.d.mts +4 -0
  75. package/lib/services/base.mjs +1 -0
  76. package/lib/services/cache.d.mts +165 -0
  77. package/lib/services/cache.mjs +323 -0
  78. package/lib/services/captcha.d.mts +65 -0
  79. package/lib/services/captcha.mjs +61 -0
  80. package/lib/services/{crypto.js → crypto.d.mts} +19 -78
  81. package/lib/services/crypto.mjs +141 -0
  82. package/lib/services/db_value.d.mts +60 -0
  83. package/lib/services/db_value.mjs +64 -0
  84. package/lib/services/dingtalk.d.mts +39 -0
  85. package/lib/services/dingtalk.mjs +44 -0
  86. package/lib/services/dispose.d.mts +17 -0
  87. package/lib/services/dispose.mjs +18 -0
  88. package/lib/services/emoji.d.mts +10 -0
  89. package/lib/services/emoji.mjs +2025 -0
  90. package/lib/services/file_store.d.mts +49 -0
  91. package/lib/services/file_store.mjs +58 -0
  92. package/lib/services/html.d.mts +11 -0
  93. package/lib/services/html.mjs +12 -0
  94. package/lib/services/jwt.d.mts +33 -0
  95. package/lib/services/jwt.mjs +42 -0
  96. package/lib/services/log.d.mts +37 -0
  97. package/lib/services/log.mjs +99 -0
  98. package/lib/services/mail.d.mts +22 -0
  99. package/lib/services/mail.mjs +28 -0
  100. package/lib/services/pay.d.mts +123 -0
  101. package/lib/services/pay.mjs +157 -0
  102. package/lib/services/random.d.mts +27 -0
  103. package/lib/services/random.mjs +42 -0
  104. package/lib/services/rate_limit.d.mts +37 -0
  105. package/lib/services/rate_limit.mjs +40 -0
  106. package/lib/services/redis.d.mts +11 -0
  107. package/lib/services/redis.mjs +6 -0
  108. package/lib/services/sensitive_words.d.mts +29 -0
  109. package/lib/services/sensitive_words.mjs +37 -0
  110. package/lib/services/sms.d.mts +40 -0
  111. package/lib/services/sms.mjs +30238 -0
  112. package/lib/types/dto.d.mts +30 -0
  113. package/lib/types/dto.mjs +1 -0
  114. package/lib/x.d.mts +11 -0
  115. package/lib/x.mjs +21 -0
  116. package/lib/x_init.d.mts +1 -0
  117. package/lib/x_init.mjs +10 -0
  118. package/package.json +82 -32
  119. package/lib/_cjs/cli/api_generator.js +0 -458
  120. package/lib/_cjs/cli/build_util.js +0 -243
  121. package/lib/_cjs/cli/cli.js +0 -378
  122. package/lib/_cjs/cli/css_register.js +0 -7
  123. package/lib/_cjs/cli/deploy_util.js +0 -76
  124. package/lib/_cjs/cli/dev_util.js +0 -26
  125. package/lib/_cjs/cli/env_util.js +0 -252
  126. package/lib/_cjs/cli/esbuild_register.js +0 -7
  127. package/lib/_cjs/cli/template_util.js +0 -101
  128. package/lib/_cjs/cli/templates/crons.ts +0 -2
  129. package/lib/_cjs/cli/templates/handlers.ts +0 -3
  130. package/lib/_cjs/cli/templates/hooks.ts +0 -2
  131. package/lib/_cjs/cli/templates/models.ts +0 -123
  132. package/lib/_cjs/cli/templates/package.json +0 -3
  133. package/lib/_cjs/cli/templates/routes.ts +0 -68
  134. package/lib/_cjs/core/ctx_storage.js +0 -6
  135. package/lib/_cjs/core/define_bus.js +0 -31
  136. package/lib/_cjs/core/define_cron.js +0 -13
  137. package/lib/_cjs/core/define_handler.js +0 -56
  138. package/lib/_cjs/core/define_hook.js +0 -8
  139. package/lib/_cjs/core/define_server.js +0 -10
  140. package/lib/_cjs/core/define_task.js +0 -113
  141. package/lib/_cjs/core/get_handler_url.js +0 -11
  142. package/lib/_cjs/core/handler.js +0 -216
  143. package/lib/_cjs/core/http_error.js +0 -6
  144. package/lib/_cjs/core/http_header.js +0 -142
  145. package/lib/_cjs/core/http_method.js +0 -12
  146. package/lib/_cjs/core/http_redirect.js +0 -11
  147. package/lib/_cjs/core/server.js +0 -219
  148. package/lib/_cjs/core/types.js +0 -8
  149. package/lib/_cjs/exports.js +0 -273
  150. package/lib/_cjs/index.js +0 -15
  151. package/lib/_cjs/plugins/base.js +0 -3
  152. package/lib/_cjs/plugins/compress.js +0 -18
  153. package/lib/_cjs/plugins/cookie.js +0 -18
  154. package/lib/_cjs/plugins/cors.js +0 -38
  155. package/lib/_cjs/plugins/file_parser.js +0 -28
  156. package/lib/_cjs/plugins/form_body_parser.js +0 -18
  157. package/lib/_cjs/plugins/ws_parser.js +0 -18
  158. package/lib/_cjs/plugins/xml_parser.js +0 -48
  159. package/lib/_cjs/services/ai.js +0 -48
  160. package/lib/_cjs/services/base.js +0 -3
  161. package/lib/_cjs/services/cache.js +0 -400
  162. package/lib/_cjs/services/captcha.js +0 -78
  163. package/lib/_cjs/services/crypto.js +0 -175
  164. package/lib/_cjs/services/db_value.js +0 -77
  165. package/lib/_cjs/services/dingtalk.js +0 -51
  166. package/lib/_cjs/services/dispose.js +0 -23
  167. package/lib/_cjs/services/emoji.js +0 -2034
  168. package/lib/_cjs/services/file_store.js +0 -67
  169. package/lib/_cjs/services/html.js +0 -17
  170. package/lib/_cjs/services/jwt.js +0 -65
  171. package/lib/_cjs/services/log.js +0 -108
  172. package/lib/_cjs/services/mail.js +0 -31
  173. package/lib/_cjs/services/pay.js +0 -191
  174. package/lib/_cjs/services/random.js +0 -51
  175. package/lib/_cjs/services/rate_limit.js +0 -54
  176. package/lib/_cjs/services/redis.js +0 -12
  177. package/lib/_cjs/services/sensitive_words.js +0 -61
  178. package/lib/_cjs/services/sms.js +0 -84
  179. package/lib/_cjs/types/dto.js +0 -3
  180. package/lib/_cjs/x.js +0 -35
  181. package/lib/cli/api_generator.d.ts +0 -74
  182. package/lib/cli/api_generator.js +0 -452
  183. package/lib/cli/build_util.d.ts +0 -17
  184. package/lib/cli/build_util.js +0 -236
  185. package/lib/cli/cli.d.ts +0 -2
  186. package/lib/cli/cli.js +0 -374
  187. package/lib/cli/css_register.js +0 -5
  188. package/lib/cli/deploy_util.d.ts +0 -23
  189. package/lib/cli/deploy_util.js +0 -70
  190. package/lib/cli/dev_util.d.ts +0 -8
  191. package/lib/cli/dev_util.js +0 -20
  192. package/lib/cli/env_util.d.ts +0 -45
  193. package/lib/cli/env_util.js +0 -246
  194. package/lib/cli/esbuild_register.js +0 -5
  195. package/lib/cli/template_util.d.ts +0 -18
  196. package/lib/cli/template_util.js +0 -95
  197. package/lib/core/ctx_storage.d.ts +0 -3
  198. package/lib/core/ctx_storage.js +0 -2
  199. package/lib/core/define_bus.d.ts +0 -37
  200. package/lib/core/define_bus.js +0 -27
  201. package/lib/core/define_cron.js +0 -9
  202. package/lib/core/define_handler.js +0 -48
  203. package/lib/core/define_hook.d.ts +0 -2
  204. package/lib/core/define_hook.js +0 -4
  205. package/lib/core/define_server.d.ts +0 -3
  206. package/lib/core/define_server.js +0 -6
  207. package/lib/core/define_task.js +0 -107
  208. package/lib/core/get_handler_url.d.ts +0 -2
  209. package/lib/core/get_handler_url.js +0 -7
  210. package/lib/core/handler.d.ts +0 -16
  211. package/lib/core/handler.js +0 -209
  212. package/lib/core/http_error.d.ts +0 -2
  213. package/lib/core/http_error.js +0 -2
  214. package/lib/core/http_header.d.ts +0 -856
  215. package/lib/core/http_header.js +0 -139
  216. package/lib/core/http_method.d.ts +0 -3
  217. package/lib/core/http_method.js +0 -8
  218. package/lib/core/http_redirect.d.ts +0 -5
  219. package/lib/core/http_redirect.js +0 -6
  220. package/lib/core/server.d.ts +0 -20
  221. package/lib/core/server.js +0 -213
  222. package/lib/core/types.d.ts +0 -265
  223. package/lib/core/types.js +0 -4
  224. package/lib/exports.d.ts +0 -45
  225. package/lib/exports.js +0 -47
  226. package/lib/index.d.ts +0 -2
  227. package/lib/index.js +0 -3
  228. package/lib/plugins/base.d.ts +0 -4
  229. package/lib/plugins/base.js +0 -1
  230. package/lib/plugins/compress.d.ts +0 -13
  231. package/lib/plugins/compress.js +0 -12
  232. package/lib/plugins/cookie.d.ts +0 -13
  233. package/lib/plugins/cookie.js +0 -12
  234. package/lib/plugins/cors.d.ts +0 -22
  235. package/lib/plugins/cors.js +0 -32
  236. package/lib/plugins/file_parser.d.ts +0 -16
  237. package/lib/plugins/file_parser.js +0 -22
  238. package/lib/plugins/form_body_parser.d.ts +0 -13
  239. package/lib/plugins/form_body_parser.js +0 -12
  240. package/lib/plugins/ws_parser.d.ts +0 -13
  241. package/lib/plugins/ws_parser.js +0 -12
  242. package/lib/plugins/xml_parser.d.ts +0 -19
  243. package/lib/plugins/xml_parser.js +0 -43
  244. package/lib/services/ai.d.ts +0 -31
  245. package/lib/services/ai.js +0 -43
  246. package/lib/services/base.d.ts +0 -4
  247. package/lib/services/base.js +0 -1
  248. package/lib/services/cache.d.ts +0 -173
  249. package/lib/services/cache.js +0 -394
  250. package/lib/services/captcha.d.ts +0 -65
  251. package/lib/services/captcha.js +0 -72
  252. package/lib/services/crypto.d.ts +0 -110
  253. package/lib/services/db_value.d.ts +0 -60
  254. package/lib/services/db_value.js +0 -72
  255. package/lib/services/dingtalk.d.ts +0 -39
  256. package/lib/services/dingtalk.js +0 -45
  257. package/lib/services/dispose.d.ts +0 -17
  258. package/lib/services/dispose.js +0 -17
  259. package/lib/services/emoji.d.ts +0 -10
  260. package/lib/services/emoji.js +0 -2029
  261. package/lib/services/file_store.d.ts +0 -49
  262. package/lib/services/file_store.js +0 -61
  263. package/lib/services/html.d.ts +0 -11
  264. package/lib/services/html.js +0 -12
  265. package/lib/services/jwt.d.ts +0 -34
  266. package/lib/services/jwt.js +0 -59
  267. package/lib/services/log.d.ts +0 -37
  268. package/lib/services/log.js +0 -102
  269. package/lib/services/mail.d.ts +0 -23
  270. package/lib/services/mail.js +0 -25
  271. package/lib/services/pay.d.ts +0 -123
  272. package/lib/services/pay.js +0 -185
  273. package/lib/services/random.d.ts +0 -27
  274. package/lib/services/random.js +0 -45
  275. package/lib/services/rate_limit.d.ts +0 -37
  276. package/lib/services/rate_limit.js +0 -49
  277. package/lib/services/redis.d.ts +0 -12
  278. package/lib/services/redis.js +0 -7
  279. package/lib/services/sensitive_words.d.ts +0 -29
  280. package/lib/services/sensitive_words.js +0 -56
  281. package/lib/services/sms.d.ts +0 -40
  282. package/lib/services/sms.js +0 -78
  283. package/lib/types/dto.d.ts +0 -86
  284. package/lib/types/dto.js +0 -1
  285. package/lib/x.d.ts +0 -17
  286. package/lib/x.js +0 -30
  287. /package/lib/{cli/css_register.d.ts → core/types.mjs} +0 -0
  288. /package/lib/{cli/esbuild_register.d.ts → plugins/base.mjs} +0 -0
@@ -0,0 +1,345 @@
1
+ #!/usr/bin/env node
2
+ import { ApiGenerator } from "./api_generator.mjs";
3
+ import { BuildUtil } from "./build_util.mjs";
4
+ import { DeployUtil } from "./deploy_util.mjs";
5
+ import { DevUtil } from "./dev_util.mjs";
6
+ import { EnvUtil } from "./env_util.mjs";
7
+ import { TemplateUtil } from "./template_util.mjs";
8
+ import * as vtils from "vtils";
9
+ import { StringTemplate, castArray, debounce } from "vtils";
10
+ import path from "node:path";
11
+ import fs from "fs-extra";
12
+ import { watch } from "chokidar";
13
+ import { execa } from "execa";
14
+ import { generateManyIndex } from "vscode-generate-index-standalone";
15
+ import yargs from "yargs";
16
+ import { hideBin } from "yargs/helpers";
17
+ // src/cli/cli.ts
18
+ async function deploy(argv) {
19
+ await EnvUtil.withChannel({
20
+ cwd: process.cwd(),
21
+ channel: argv.channel,
22
+ cb: async (channel) => {
23
+ const deployEnvFiles = EnvUtil.getFile("deploy", channel, true);
24
+ const deployEnv = await EnvUtil.parseFileAsMap({
25
+ cwd: process.cwd(),
26
+ file: deployEnvFiles,
27
+ channel
28
+ });
29
+ if (argv.type === "env") {
30
+ const appEnvFiles = EnvUtil.getFile("production", channel);
31
+ const appEnv = await EnvUtil.parseFileAsMap({
32
+ cwd: process.cwd(),
33
+ file: appEnvFiles,
34
+ channel
35
+ });
36
+ await DeployUtil.deployEnv({
37
+ host: deployEnv.HOST,
38
+ user: deployEnv.USER,
39
+ key: deployEnv.KEY,
40
+ pass: deployEnv.PASSWORD,
41
+ token: appEnv.APP_TOKEN,
42
+ port: appEnv.APP_PORT,
43
+ env: appEnv
44
+ });
45
+ } else await DeployUtil.deploy({
46
+ cwd: process.cwd(),
47
+ host: deployEnv.HOST,
48
+ user: deployEnv.USER,
49
+ key: deployEnv.KEY,
50
+ pass: deployEnv.PASSWORD,
51
+ dir: deployEnv.DIR,
52
+ cmd: deployEnv.CMD,
53
+ channel
54
+ });
55
+ }
56
+ });
57
+ }
58
+ yargs(hideBin(process.argv)).command("dev", "开始开发", (_) => _.positional("index", {
59
+ alias: "i",
60
+ describe: "额外需要生成的索引文件",
61
+ type: "string",
62
+ array: true
63
+ }).positional("channel", {
64
+ alias: "h",
65
+ describe: "渠道",
66
+ type: "string"
67
+ }), async (argv) => {
68
+ await EnvUtil.withChannel({
69
+ cwd: process.cwd(),
70
+ channel: argv.channel,
71
+ cb: async (channel) => {
72
+ process.env.NODE_ENV = "development";
73
+ let lastRun;
74
+ const run = () => {
75
+ lastRun = DevUtil.runFile({
76
+ file: "src/main.ts",
77
+ cwd: process.cwd()
78
+ });
79
+ lastRun.catch(vtils.noop);
80
+ };
81
+ await TemplateUtil.init(process.cwd());
82
+ const xsignoreFile = path.join(process.cwd(), ".xsignore");
83
+ const xsignoreContent = await fs.pathExists(xsignoreFile) ? (await fs.readFile(xsignoreFile, "utf-8")).trim().split(/[\r\n]+/g).filter((v) => v && !v.startsWith("#")) : [];
84
+ watch([
85
+ "src",
86
+ ".env",
87
+ ".env*"
88
+ ], {
89
+ cwd: process.cwd(),
90
+ ignored: ["**/*.test.*", ...xsignoreContent]
91
+ }).on("all", debounce(async () => {
92
+ const envFiles = EnvUtil.getFile(process.env.NODE_ENV, channel);
93
+ await EnvUtil.importFile({
94
+ cwd: process.cwd(),
95
+ file: envFiles,
96
+ channel
97
+ });
98
+ EnvUtil.outputTypes({
99
+ cwd: process.cwd(),
100
+ file: envFiles,
101
+ outFile: "env.d.ts",
102
+ channel
103
+ });
104
+ generateManyIndex({
105
+ cwd: process.cwd(),
106
+ patterns: [
107
+ "src/**/index.ts",
108
+ "node_modules/@a/x/*.ts",
109
+ "!src/handlers/**/*",
110
+ ...argv.index || []
111
+ ],
112
+ replaceFile: true,
113
+ onSuccess: (filePath) => {
114
+ console.log(`✔️ 索引文件已更新: ${filePath}`);
115
+ }
116
+ });
117
+ if (lastRun) {
118
+ console.log("==== 重启服务中 ====");
119
+ lastRun.kill();
120
+ setTimeout(run, 0);
121
+ } else {
122
+ console.log("==== 启动服务中 ====");
123
+ run();
124
+ }
125
+ }, 30));
126
+ }
127
+ });
128
+ }).command("build", "构建", (_) => _.positional("minify", {
129
+ alias: "m",
130
+ describe: "是否压缩",
131
+ type: "boolean",
132
+ default: false
133
+ }).positional("channel", {
134
+ alias: "h",
135
+ describe: "渠道",
136
+ type: "string"
137
+ }).positional("deploy", {
138
+ alias: "d",
139
+ describe: "构建完是否部署",
140
+ type: "boolean",
141
+ default: false
142
+ }).positional("concurrency", {
143
+ alias: "c",
144
+ describe: "同时构建的数量",
145
+ type: "number",
146
+ default: 1
147
+ }).positional("exclude", {
148
+ alias: "x",
149
+ describe: "构建时排除的文件",
150
+ type: "string",
151
+ array: true
152
+ }).positional("empty-dist-dir", {
153
+ describe: "是否清空构建目录",
154
+ type: "boolean",
155
+ default: true
156
+ }), async (argv) => {
157
+ await EnvUtil.withChannel({
158
+ cwd: process.cwd(),
159
+ channel: argv.channel,
160
+ concurrency: argv.concurrency,
161
+ cb: async (channel) => {
162
+ process.env.NODE_ENV = "production";
163
+ const envFiles = EnvUtil.getFile(process.env.NODE_ENV, channel);
164
+ const envMap = await EnvUtil.parseFileAsMap({
165
+ cwd: process.cwd(),
166
+ file: envFiles,
167
+ channel
168
+ });
169
+ envMap.NODE_ENV = "production";
170
+ const deployEnvFiles = EnvUtil.getFile("deploy", channel, true);
171
+ const deployEnv = await EnvUtil.parseFileAsMap({
172
+ cwd: process.cwd(),
173
+ file: deployEnvFiles,
174
+ channel
175
+ });
176
+ await TemplateUtil.init(process.cwd());
177
+ await generateManyIndex({
178
+ cwd: process.cwd(),
179
+ patterns: ["src/@a/x/*.ts"],
180
+ replaceFile: true,
181
+ onSuccess: (filePath) => {
182
+ console.log(`✔️ 索引文件已更新: ${filePath}`);
183
+ }
184
+ });
185
+ await BuildUtil.build({
186
+ appId: envMap.APP_ID,
187
+ cwd: process.cwd(),
188
+ inlineEnvs: envMap,
189
+ minify: argv.minify,
190
+ channel,
191
+ excludeFiles: (argv.exclude ? castArray(argv.exclude) : []).map((item) => {
192
+ return StringTemplate.render(item, {
193
+ ...envMap,
194
+ _: vtils,
195
+ __: vtils.escapeRegExp
196
+ }, {
197
+ code: true,
198
+ onlyCode: true
199
+ });
200
+ }),
201
+ noEmptyDistDir: argv["empty-dist-dir"] === false,
202
+ pm2MaxMemoryRestart: deployEnv.MEMORY,
203
+ pm2CronRestart: deployEnv.CRON,
204
+ pm2NodeArgs: deployEnv.NODE_ARGS
205
+ });
206
+ console.log("构建成功");
207
+ if (argv.deploy) await deploy({
208
+ ...argv,
209
+ channel
210
+ });
211
+ }
212
+ });
213
+ }).command("api", "生成 API", (_) => _.positional("channel", {
214
+ alias: "h",
215
+ describe: "渠道",
216
+ type: "string"
217
+ }).positional("include", {
218
+ alias: "i",
219
+ describe: "包括",
220
+ type: "string",
221
+ array: true
222
+ }).positional("exclude", {
223
+ alias: "e",
224
+ describe: "排除",
225
+ type: "string",
226
+ array: true
227
+ }).positional("name", {
228
+ alias: "n",
229
+ describe: "名称",
230
+ type: "string"
231
+ }), async (argv) => {
232
+ await EnvUtil.withChannel({
233
+ cwd: process.cwd(),
234
+ channel: argv.channel,
235
+ cb: async (channel) => {
236
+ process.env.NODE_ENV = "production";
237
+ const envFiles = EnvUtil.getFile(process.env.NODE_ENV, channel);
238
+ await EnvUtil.importFile({
239
+ cwd: process.cwd(),
240
+ file: envFiles,
241
+ channel
242
+ });
243
+ await new ApiGenerator({
244
+ cwd: process.cwd(),
245
+ include: argv.include && castArray(argv.include).flatMap((v) => v.split(",")),
246
+ exclude: argv.exclude && castArray(argv.exclude).flatMap((v) => v.split(",")),
247
+ name: argv.name
248
+ }).start();
249
+ }
250
+ });
251
+ }).command("prisma", "prisma 代理,主要为了注入环境变量", (_) => _.positional("production", {
252
+ alias: "p",
253
+ describe: "是否生产模式",
254
+ type: "boolean",
255
+ default: false
256
+ }).positional("channel", {
257
+ alias: "h",
258
+ describe: "渠道",
259
+ type: "string"
260
+ }), async (argv) => {
261
+ await EnvUtil.withChannel({
262
+ cwd: process.cwd(),
263
+ channel: argv.channel,
264
+ cb: async (channel) => {
265
+ process.env.NODE_ENV = argv.production ? "production" : "development";
266
+ const envFiles = EnvUtil.getFile(process.env.NODE_ENV, channel);
267
+ await EnvUtil.importFile({
268
+ cwd: process.cwd(),
269
+ file: envFiles,
270
+ channel
271
+ });
272
+ process.env.DATABASE_URL = process.env.DATABASE_ACTION_URL || process.env.DATABASE_URL;
273
+ const schemaDir = path.join(process.cwd(), "src/db/schema");
274
+ const schemaArg = await fs.pathExists(schemaDir) ? "src/db" : "src/db/schema.prisma";
275
+ const prismaArgs = argv._.slice(1);
276
+ await execa("pnpm", [
277
+ "exec",
278
+ "prisma",
279
+ ...prismaArgs,
280
+ "--schema",
281
+ schemaArg
282
+ ], {
283
+ cwd: process.cwd(),
284
+ stdio: "inherit"
285
+ });
286
+ if (!argv.production && prismaArgs[0] === "generate") await TemplateUtil.initModels(process.cwd());
287
+ }
288
+ });
289
+ }).command("run [script]", "执行脚本", (_) => _.positional("script", {
290
+ describe: "脚本",
291
+ type: "string"
292
+ }).positional("production", {
293
+ alias: "p",
294
+ describe: "是否生产模式",
295
+ type: "boolean",
296
+ default: false
297
+ }).positional("channel", {
298
+ alias: "h",
299
+ describe: "渠道",
300
+ type: "string"
301
+ }), async (argv) => {
302
+ await EnvUtil.withChannel({
303
+ cwd: process.cwd(),
304
+ channel: argv.channel,
305
+ cb: async (channel) => {
306
+ process.env.NODE_ENV = argv.production ? "production" : "development";
307
+ const envFiles = EnvUtil.getFile(process.env.NODE_ENV, channel);
308
+ await EnvUtil.importFile({
309
+ cwd: process.cwd(),
310
+ file: envFiles,
311
+ channel
312
+ });
313
+ process.env.DATABASE_URL = process.env.DATABASE_ACTION_URL || process.env.DATABASE_URL;
314
+ if (!argv.script) await execa("pnpm", ["exec", "select-run"], {
315
+ cwd: process.cwd(),
316
+ stdio: "inherit"
317
+ });
318
+ else await DevUtil.runFile({
319
+ file: argv.script,
320
+ cwd: process.cwd()
321
+ });
322
+ }
323
+ });
324
+ }).command("deploy [type]", "部署", (_) => _.positional("type", {
325
+ describe: "类型",
326
+ type: "string",
327
+ choices: ["env"]
328
+ }).positional("channel", {
329
+ alias: "h",
330
+ describe: "渠道",
331
+ type: "string"
332
+ }), deploy).command("get-channel [file]", "获取渠道", (_) => _.positional("file", {
333
+ describe: "写入文件",
334
+ type: "string"
335
+ }), async (argv) => {
336
+ await EnvUtil.withChannel({
337
+ cwd: process.cwd(),
338
+ channel: "_",
339
+ cb: async (channel) => {
340
+ if (channel && argv.file) await fs.writeFile(path.join(process.cwd(), argv.file), channel);
341
+ console.log(channel || "");
342
+ }
343
+ });
344
+ }).parse();
345
+ export {};
@@ -0,0 +1 @@
1
+ export {}
@@ -0,0 +1,7 @@
1
+ import { addHook } from "pirates";
2
+ // src/cli/css_register.ts
3
+ addHook(() => "module.exports = {};", {
4
+ exts: [".css"],
5
+ ignoreNodeModules: false
6
+ });
7
+ export {};
@@ -0,0 +1,23 @@
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
+ }
@@ -0,0 +1,70 @@
1
+ import { dedent } from "vtils";
2
+ import path from "node:path";
3
+ import fs from "fs-extra";
4
+ import { NodeSSH } from "node-ssh";
5
+ // src/cli/deploy_util.ts
6
+ var DeployUtil = class {
7
+ static async deploy(options) {
8
+ const pkgContent = await fs.readJson(path.join(options.cwd, "package.json"));
9
+ const appFile = path.join(options.cwd, `temp${options.channel ? `/${options.channel}` : ""}/app.${pkgContent.version}.tgz`);
10
+ if (!await fs.pathExists(appFile)) throw new Error("请先构建");
11
+ const ssh = new NodeSSH();
12
+ await ssh.connect({
13
+ host: options.host,
14
+ username: options.user,
15
+ privateKey: options.key,
16
+ password: options.pass
17
+ });
18
+ const appName = pkgContent.name;
19
+ const appDir = `./${appName}`;
20
+ await ssh.execCommand(dedent`
21
+ set -ex
22
+ mkdir -p ./app_files/${appName}/${options.channel || "dist"}
23
+ mkdir -p ${appDir}
24
+ `, {
25
+ cwd: options.dir,
26
+ onStdout: (buf) => console.log(buf.toString()),
27
+ onStderr: (buf) => console.log(buf.toString())
28
+ });
29
+ const remoteAppFile = `${options.dir}/app_files/${appName}/${options.channel || "dist"}/${path.basename(appFile)}`;
30
+ await ssh.putFile(appFile, remoteAppFile);
31
+ await ssh.execCommand(dedent`
32
+ set -ex
33
+ tar -zxf ${remoteAppFile} -C ${appDir}
34
+ cd "${appDir}/${options.channel || "dist"}"
35
+ pnpm install
36
+ ${options.cmd || ""}
37
+ pm2 startOrReload pm2.config.js
38
+ `, {
39
+ cwd: options.dir,
40
+ onStdout: (buf) => console.log(buf.toString()),
41
+ onStderr: (buf) => console.log(buf.toString())
42
+ });
43
+ ssh.dispose();
44
+ }
45
+ static async deployEnv(options) {
46
+ const ssh = new NodeSSH();
47
+ await ssh.connect({
48
+ host: options.host,
49
+ username: options.user,
50
+ privateKey: options.key,
51
+ password: options.pass
52
+ });
53
+ await ssh.execCommand(dedent`
54
+ set -ex
55
+ curl --silent --header "Content-Type: application/json" \\
56
+ --request "POST" \\
57
+ --data ${JSON.stringify(JSON.stringify({
58
+ token: options.token,
59
+ type: "updateEnv",
60
+ data: options.env
61
+ }))} \\
62
+ "http://localhost:${options.port}/$"
63
+ `, {
64
+ onStdout: (buf) => console.log(buf.toString()),
65
+ onStderr: (buf) => console.log(buf.toString())
66
+ });
67
+ ssh.dispose();
68
+ }
69
+ };
70
+ export { DeployUtil };
@@ -0,0 +1,9 @@
1
+ export declare class DevUtil {
2
+ static runFile(options: {
3
+ file: string;
4
+ cwd: string;
5
+ }): import("execa").ResultPromise<{
6
+ cwd: string;
7
+ stdio: "inherit";
8
+ }>;
9
+ }
@@ -0,0 +1,25 @@
1
+ import { __require } from "../_virtual/_rolldown/runtime.mjs";
2
+ import { execa } from "execa";
3
+ import fs from "node:fs";
4
+ import { fileURLToPath } from "node:url";
5
+ // src/cli/dev_util.ts
6
+ var DevUtil = class {
7
+ static runFile(options) {
8
+ const cssRegisterFile = fileURLToPath(import.meta.resolve("./css_register.mjs"));
9
+ const tsxPkgFile = __require.resolve("tsx/package.json");
10
+ const tsxJsFile = tsxPkgFile.replace("/package.json", "/dist/cli.js");
11
+ const tsxCjsFile = tsxPkgFile.replace("/package.json", "/dist/cli.cjs");
12
+ const tsxMjsFile = tsxPkgFile.replace("/package.json", "/dist/cli.mjs");
13
+ const tsxFile = fs.existsSync(tsxMjsFile) ? tsxMjsFile : fs.existsSync(tsxCjsFile) ? tsxCjsFile : tsxJsFile;
14
+ return execa("node", [
15
+ "-r",
16
+ cssRegisterFile,
17
+ tsxFile,
18
+ options.file
19
+ ], {
20
+ cwd: options.cwd,
21
+ stdio: "inherit"
22
+ });
23
+ }
24
+ };
25
+ export { DevUtil };
@@ -0,0 +1,45 @@
1
+ export interface ParsedEnv {
2
+ comment: string;
3
+ key: string;
4
+ value: any;
5
+ }
6
+ export declare class EnvUtil {
7
+ static NEWLINE: string;
8
+ static RE_INI_KEY_VAL: RegExp;
9
+ static RE_NEWLINES: RegExp;
10
+ static NEWLINES_MATCH: RegExp;
11
+ static COMMENT_MATCH: RegExp;
12
+ static getAllChannel(cwd: string, excludeChannels?: string[]): Promise<string[]>;
13
+ static chooseChannel(cwd: string, excludeChannels?: string[]): Promise<string | undefined>;
14
+ static withChannel(options: {
15
+ cwd: string;
16
+ channel?: string;
17
+ concurrency?: number;
18
+ cb: (channel: string | undefined) => any;
19
+ }): Promise<void>;
20
+ static getFile(env?: string, channel?: string, noBase?: boolean): string[];
21
+ static parseContent(src: string, _envItemMap?: Record<string, ParsedEnv>, _envValueMap?: Record<string, any>, channel?: string): ParsedEnv[];
22
+ static parseFile(options: {
23
+ cwd: string;
24
+ file: string[];
25
+ channel?: string;
26
+ }): Promise<ParsedEnv[]>;
27
+ static parseFileAsMap(options: {
28
+ cwd: string;
29
+ file: string[];
30
+ channel?: string;
31
+ }): Promise<Record<string, any>>;
32
+ static importFile(options: {
33
+ cwd: string;
34
+ file: string[];
35
+ channel?: string;
36
+ }): Promise<void>;
37
+ static makeTypes(envs: ParsedEnv[]): string;
38
+ static outputTypes(options: {
39
+ cwd: string;
40
+ file: string[];
41
+ outFile: string;
42
+ channel?: string;
43
+ }): Promise<void>;
44
+ private static getTypesFromValue;
45
+ }