@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
@@ -0,0 +1,355 @@
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 exec from "execa";
13
+ import chokidar from "chokidar";
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
+ };
80
+ await TemplateUtil.init(process.cwd());
81
+ const xsignoreFile = path.join(process.cwd(), ".xsignore");
82
+ const xsignoreContent = await fs.pathExists(xsignoreFile) ? (await fs.readFile(xsignoreFile, "utf-8")).trim().split(/[\r\n]+/g).filter((v) => v && !v.startsWith("#")) : [];
83
+ chokidar.watch([
84
+ "src",
85
+ ".env",
86
+ ".env*"
87
+ ], {
88
+ cwd: process.cwd(),
89
+ ignored: ["**/*.test.*", ...xsignoreContent]
90
+ }).on("all", debounce(async () => {
91
+ const envFiles = EnvUtil.getFile(process.env.NODE_ENV, channel);
92
+ await EnvUtil.importFile({
93
+ cwd: process.cwd(),
94
+ file: envFiles,
95
+ channel
96
+ });
97
+ EnvUtil.outputTypes({
98
+ cwd: process.cwd(),
99
+ file: envFiles,
100
+ outFile: "env.d.ts",
101
+ channel
102
+ });
103
+ generateManyIndex({
104
+ cwd: process.cwd(),
105
+ patterns: [
106
+ "src/**/index.ts",
107
+ "node_modules/.x/*.ts",
108
+ "!src/handlers/**/*",
109
+ ...argv.index || []
110
+ ],
111
+ replaceFile: true,
112
+ onSuccess: (filePath) => {
113
+ console.log(`✔️ 索引文件已更新: ${filePath}`);
114
+ }
115
+ });
116
+ if (lastRun) {
117
+ console.log("==== 重启服务中 ====");
118
+ lastRun.kill();
119
+ setTimeout(run, 0);
120
+ } else {
121
+ console.log("==== 启动服务中 ====");
122
+ run();
123
+ }
124
+ }, 30));
125
+ }
126
+ });
127
+ }).command("build", "构建", (_) => _.positional("external", {
128
+ alias: "e",
129
+ describe: "不应该被打的包",
130
+ type: "string",
131
+ array: true
132
+ }).positional("minify", {
133
+ alias: "m",
134
+ describe: "是否压缩",
135
+ type: "boolean",
136
+ default: true
137
+ }).positional("local-install", {
138
+ describe: "是否本地安装外部依赖",
139
+ type: "boolean",
140
+ default: true
141
+ }).positional("channel", {
142
+ alias: "h",
143
+ describe: "渠道",
144
+ type: "string"
145
+ }).positional("deploy", {
146
+ alias: "d",
147
+ describe: "构建完是否部署",
148
+ type: "boolean",
149
+ default: false
150
+ }).positional("concurrency", {
151
+ alias: "c",
152
+ describe: "同时构建的数量",
153
+ type: "number",
154
+ default: 1
155
+ }).positional("exclude", {
156
+ alias: "x",
157
+ describe: "构建时排除的文件",
158
+ type: "string",
159
+ array: true
160
+ }).positional("empty-dist-dir", {
161
+ describe: "是否清空构建目录",
162
+ type: "boolean",
163
+ default: true
164
+ }), async (argv) => {
165
+ await EnvUtil.withChannel({
166
+ cwd: process.cwd(),
167
+ channel: argv.channel,
168
+ concurrency: argv.concurrency,
169
+ cb: async (channel) => {
170
+ process.env.NODE_ENV = "production";
171
+ const externals = castArray(argv.external || []).flatMap((item) => item.split(",")).filter(Boolean);
172
+ const envFiles = EnvUtil.getFile(process.env.NODE_ENV, channel);
173
+ const envMap = await EnvUtil.parseFileAsMap({
174
+ cwd: process.cwd(),
175
+ file: envFiles,
176
+ channel
177
+ });
178
+ envMap.NODE_ENV = "production";
179
+ const deployEnvFiles = EnvUtil.getFile("deploy", channel, true);
180
+ const deployEnv = await EnvUtil.parseFileAsMap({
181
+ cwd: process.cwd(),
182
+ file: deployEnvFiles,
183
+ channel
184
+ });
185
+ await TemplateUtil.init(process.cwd());
186
+ await generateManyIndex({
187
+ cwd: process.cwd(),
188
+ patterns: ["src/.x/*.ts"],
189
+ replaceFile: true,
190
+ onSuccess: (filePath) => {
191
+ console.log(`✔️ 索引文件已更新: ${filePath}`);
192
+ }
193
+ });
194
+ await BuildUtil.build({
195
+ appId: envMap.APP_ID,
196
+ cwd: process.cwd(),
197
+ external: externals,
198
+ inlineEnvs: envMap,
199
+ minify: argv.minify,
200
+ noInstall: argv["local-install"] === false,
201
+ channel,
202
+ excludeFiles: (argv.exclude ? castArray(argv.exclude) : []).map((item) => {
203
+ return StringTemplate.render(item, {
204
+ ...envMap,
205
+ _: vtils,
206
+ __: vtils.escapeRegExp
207
+ }, {
208
+ code: true,
209
+ onlyCode: true
210
+ });
211
+ }),
212
+ noEmptyDistDir: argv["empty-dist-dir"] === false,
213
+ pm2MaxMemoryRestart: deployEnv.MEMORY,
214
+ pm2CronRestart: deployEnv.CRON,
215
+ pm2NodeArgs: deployEnv.NODE_ARGS
216
+ });
217
+ console.log("构建成功");
218
+ if (argv.deploy) await deploy({
219
+ ...argv,
220
+ channel
221
+ });
222
+ }
223
+ });
224
+ }).command("api", "生成 API", (_) => _.positional("channel", {
225
+ alias: "h",
226
+ describe: "渠道",
227
+ type: "string"
228
+ }).positional("include", {
229
+ alias: "i",
230
+ describe: "包括",
231
+ type: "string",
232
+ array: true
233
+ }).positional("exclude", {
234
+ alias: "e",
235
+ describe: "排除",
236
+ type: "string",
237
+ array: true
238
+ }).positional("name", {
239
+ alias: "n",
240
+ describe: "名称",
241
+ type: "string"
242
+ }), async (argv) => {
243
+ await EnvUtil.withChannel({
244
+ cwd: process.cwd(),
245
+ channel: argv.channel,
246
+ cb: async (channel) => {
247
+ process.env.NODE_ENV = "production";
248
+ const envFiles = EnvUtil.getFile(process.env.NODE_ENV, channel);
249
+ await EnvUtil.importFile({
250
+ cwd: process.cwd(),
251
+ file: envFiles,
252
+ channel
253
+ });
254
+ await new ApiGenerator({
255
+ cwd: process.cwd(),
256
+ include: argv.include && castArray(argv.include).flatMap((v) => v.split(",")),
257
+ exclude: argv.exclude && castArray(argv.exclude).flatMap((v) => v.split(",")),
258
+ name: argv.name
259
+ }).start();
260
+ }
261
+ });
262
+ }).command("prisma", "prisma 代理,主要为了注入环境变量", (_) => _.positional("production", {
263
+ alias: "p",
264
+ describe: "是否生产模式",
265
+ type: "boolean",
266
+ default: false
267
+ }).positional("channel", {
268
+ alias: "h",
269
+ describe: "渠道",
270
+ type: "string"
271
+ }), async (argv) => {
272
+ await EnvUtil.withChannel({
273
+ cwd: process.cwd(),
274
+ channel: argv.channel,
275
+ cb: async (channel) => {
276
+ process.env.NODE_ENV = argv.production ? "production" : "development";
277
+ const envFiles = EnvUtil.getFile(process.env.NODE_ENV, channel);
278
+ await EnvUtil.importFile({
279
+ cwd: process.cwd(),
280
+ file: envFiles,
281
+ channel
282
+ });
283
+ process.env.DATABASE_URL = process.env.DATABASE_ACTION_URL || process.env.DATABASE_URL;
284
+ const schemaDir = path.join(process.cwd(), "src/db/schema");
285
+ const schemaArg = await fs.pathExists(schemaDir) ? "src/db" : "src/db/schema.prisma";
286
+ const prismaArgs = argv._.slice(1);
287
+ await exec("tnpx", [
288
+ "prisma",
289
+ ...prismaArgs,
290
+ "--schema",
291
+ schemaArg
292
+ ], {
293
+ cwd: process.cwd(),
294
+ stdio: "inherit"
295
+ });
296
+ if (!argv.production && prismaArgs[0] === "generate") await TemplateUtil.initModels(process.cwd());
297
+ }
298
+ });
299
+ }).command("run [script]", "执行脚本", (_) => _.positional("script", {
300
+ describe: "脚本",
301
+ type: "string"
302
+ }).positional("production", {
303
+ alias: "p",
304
+ describe: "是否生产模式",
305
+ type: "boolean",
306
+ default: false
307
+ }).positional("channel", {
308
+ alias: "h",
309
+ describe: "渠道",
310
+ type: "string"
311
+ }), async (argv) => {
312
+ await EnvUtil.withChannel({
313
+ cwd: process.cwd(),
314
+ channel: argv.channel,
315
+ cb: async (channel) => {
316
+ process.env.NODE_ENV = argv.production ? "production" : "development";
317
+ const envFiles = EnvUtil.getFile(process.env.NODE_ENV, channel);
318
+ await EnvUtil.importFile({
319
+ cwd: process.cwd(),
320
+ file: envFiles,
321
+ channel
322
+ });
323
+ process.env.DATABASE_URL = process.env.DATABASE_ACTION_URL || process.env.DATABASE_URL;
324
+ if (!argv.script) await exec("tnpx", ["select-run"], {
325
+ cwd: process.cwd(),
326
+ stdio: "inherit"
327
+ });
328
+ else await DevUtil.runFile({
329
+ file: argv.script,
330
+ cwd: process.cwd()
331
+ });
332
+ }
333
+ });
334
+ }).command("deploy [type]", "部署", (_) => _.positional("type", {
335
+ describe: "类型",
336
+ type: "string",
337
+ choices: ["env"]
338
+ }).positional("channel", {
339
+ alias: "h",
340
+ describe: "渠道",
341
+ type: "string"
342
+ }), deploy).command("get-channel [file]", "获取渠道", (_) => _.positional("file", {
343
+ describe: "写入文件",
344
+ type: "string"
345
+ }), async (argv) => {
346
+ await EnvUtil.withChannel({
347
+ cwd: process.cwd(),
348
+ channel: "_",
349
+ cb: async (channel) => {
350
+ if (channel && argv.file) await fs.writeFile(path.join(process.cwd(), argv.file), channel);
351
+ console.log(channel || "");
352
+ }
353
+ });
354
+ }).parse();
355
+ 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
+ [ -f no_install.lock ] && yarn_disable_self_update_check=true tyn
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,7 @@
1
+ import exec from "execa";
2
+ export declare class DevUtil {
3
+ static runFile(options: {
4
+ file: string;
5
+ cwd: string;
6
+ }): exec.ExecaChildProcess<string>;
7
+ }
@@ -0,0 +1,25 @@
1
+ import { __require } from "../_virtual/_rolldown/runtime.mjs";
2
+ import exec 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 exec("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
+ }