@nocobase/cli 2.1.0-beta.9 → 2.1.0-rc.2

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 (250) hide show
  1. package/bin/run.cmd +3 -0
  2. package/bin/run.js +145 -0
  3. package/bin/session-env.js +39 -0
  4. package/dist/commands/api/resource/create.js +15 -0
  5. package/dist/commands/api/resource/destroy.js +15 -0
  6. package/dist/commands/api/resource/get.js +15 -0
  7. package/dist/commands/api/resource/index.js +20 -0
  8. package/dist/commands/api/resource/list.js +16 -0
  9. package/dist/commands/api/resource/query.js +15 -0
  10. package/dist/commands/api/resource/update.js +15 -0
  11. package/dist/commands/app/autostart/disable.js +55 -0
  12. package/dist/commands/app/autostart/enable.js +55 -0
  13. package/dist/commands/app/autostart/list.js +37 -0
  14. package/dist/commands/app/autostart/run.js +84 -0
  15. package/dist/commands/app/autostart/shared.js +49 -0
  16. package/dist/commands/app/destroy.js +234 -0
  17. package/dist/commands/app/down.js +71 -0
  18. package/dist/commands/app/logs.js +115 -0
  19. package/dist/commands/app/restart.js +229 -0
  20. package/dist/commands/app/shared.js +123 -0
  21. package/dist/commands/app/start.js +416 -0
  22. package/dist/commands/app/stop.js +183 -0
  23. package/dist/commands/app/upgrade.js +523 -0
  24. package/dist/commands/backup/create.js +147 -0
  25. package/dist/commands/backup/index.js +20 -0
  26. package/dist/commands/backup/restore.js +105 -0
  27. package/{src/cli.js → dist/commands/build.js} +4 -11
  28. package/dist/commands/config/delete.js +42 -0
  29. package/dist/commands/config/get.js +39 -0
  30. package/dist/commands/config/index.js +20 -0
  31. package/dist/commands/config/list.js +29 -0
  32. package/dist/commands/config/set.js +49 -0
  33. package/dist/commands/db/check.js +240 -0
  34. package/dist/commands/db/logs.js +85 -0
  35. package/dist/commands/db/ps.js +47 -0
  36. package/dist/commands/db/shared.js +96 -0
  37. package/dist/commands/db/start.js +86 -0
  38. package/dist/commands/db/stop.js +71 -0
  39. package/{templates/plugin/src/client/models/index.ts → dist/commands/dev.js} +4 -4
  40. package/{src/commands/locale/react-js-cron/index.js → dist/commands/down.js} +3 -8
  41. package/dist/commands/download.js +13 -0
  42. package/dist/commands/env/add.js +406 -0
  43. package/dist/commands/env/auth.js +189 -0
  44. package/dist/commands/env/current.js +21 -0
  45. package/dist/commands/env/info.js +202 -0
  46. package/dist/commands/env/list.js +43 -0
  47. package/dist/commands/env/remove.js +174 -0
  48. package/dist/commands/env/shared.js +204 -0
  49. package/dist/commands/env/status.js +93 -0
  50. package/dist/commands/env/update.js +448 -0
  51. package/dist/commands/env/use.js +38 -0
  52. package/dist/commands/examples/prompts-stages.js +150 -0
  53. package/dist/commands/examples/prompts-test.js +181 -0
  54. package/dist/commands/init.js +1390 -0
  55. package/dist/commands/install.js +2609 -0
  56. package/dist/commands/license/activate.js +179 -0
  57. package/dist/commands/license/env.js +94 -0
  58. package/dist/commands/license/generate-id.js +108 -0
  59. package/dist/commands/license/id.js +70 -0
  60. package/dist/commands/license/index.js +20 -0
  61. package/dist/commands/license/plugins/clean.js +115 -0
  62. package/dist/commands/license/plugins/index.js +20 -0
  63. package/dist/commands/license/plugins/list.js +64 -0
  64. package/dist/commands/license/plugins/shared.js +382 -0
  65. package/dist/commands/license/plugins/sync.js +314 -0
  66. package/dist/commands/license/shared.js +423 -0
  67. package/dist/commands/license/status.js +64 -0
  68. package/dist/commands/logs.js +12 -0
  69. package/dist/commands/plugin/disable.js +86 -0
  70. package/dist/commands/plugin/enable.js +86 -0
  71. package/dist/commands/plugin/import.js +108 -0
  72. package/dist/commands/plugin/list.js +82 -0
  73. package/dist/commands/pm/disable.js +12 -0
  74. package/dist/commands/pm/enable.js +12 -0
  75. package/dist/commands/pm/list.js +12 -0
  76. package/dist/commands/proxy/caddy/current.js +17 -0
  77. package/dist/commands/proxy/caddy/generate.js +69 -0
  78. package/dist/commands/proxy/caddy/index.js +28 -0
  79. package/dist/commands/proxy/caddy/info.js +31 -0
  80. package/dist/commands/proxy/caddy/reload.js +30 -0
  81. package/dist/commands/proxy/caddy/restart.js +28 -0
  82. package/dist/commands/proxy/caddy/start.js +30 -0
  83. package/dist/commands/proxy/caddy/status.js +19 -0
  84. package/dist/commands/proxy/caddy/stop.js +30 -0
  85. package/dist/commands/proxy/caddy/use.js +26 -0
  86. package/dist/commands/proxy/index.js +28 -0
  87. package/dist/commands/proxy/nginx/current.js +18 -0
  88. package/dist/commands/proxy/nginx/generate.js +68 -0
  89. package/dist/commands/proxy/nginx/index.js +28 -0
  90. package/dist/commands/proxy/nginx/info.js +34 -0
  91. package/dist/commands/proxy/nginx/reload.js +30 -0
  92. package/dist/commands/proxy/nginx/restart.js +28 -0
  93. package/dist/commands/proxy/nginx/start.js +30 -0
  94. package/dist/commands/proxy/nginx/status.js +19 -0
  95. package/dist/commands/proxy/nginx/stop.js +30 -0
  96. package/dist/commands/proxy/nginx/use.js +31 -0
  97. package/dist/commands/restart.js +12 -0
  98. package/dist/commands/revision/create.js +118 -0
  99. package/dist/commands/scaffold/migration.js +38 -0
  100. package/dist/commands/scaffold/plugin.js +37 -0
  101. package/dist/commands/self/check.js +71 -0
  102. package/dist/commands/self/index.js +20 -0
  103. package/dist/commands/self/update.js +152 -0
  104. package/dist/commands/session/id.js +24 -0
  105. package/dist/commands/session/remove.js +57 -0
  106. package/dist/commands/session/setup.js +62 -0
  107. package/dist/commands/skills/check.js +69 -0
  108. package/dist/commands/skills/index.js +20 -0
  109. package/dist/commands/skills/install.js +80 -0
  110. package/dist/commands/skills/remove.js +80 -0
  111. package/dist/commands/skills/update.js +87 -0
  112. package/dist/commands/source/build.js +58 -0
  113. package/dist/commands/source/dev.js +182 -0
  114. package/dist/commands/source/download.js +883 -0
  115. package/dist/commands/source/publish.js +109 -0
  116. package/dist/commands/source/registry/logs.js +70 -0
  117. package/dist/commands/source/registry/start.js +57 -0
  118. package/dist/commands/source/registry/status.js +33 -0
  119. package/dist/commands/source/registry/stop.js +48 -0
  120. package/dist/commands/source/test.js +476 -0
  121. package/dist/commands/start.js +12 -0
  122. package/dist/commands/stop.js +12 -0
  123. package/dist/commands/test.js +12 -0
  124. package/dist/commands/upgrade.js +12 -0
  125. package/dist/commands/v1.js +210 -0
  126. package/dist/generated/command-registry.js +134 -0
  127. package/dist/help/runtime-help.js +23 -0
  128. package/dist/lib/api-client.js +335 -0
  129. package/dist/lib/api-command-compat.js +641 -0
  130. package/dist/lib/app-health.js +139 -0
  131. package/dist/lib/app-managed-resources.js +337 -0
  132. package/dist/lib/app-public-path.js +80 -0
  133. package/dist/lib/app-runtime.js +189 -0
  134. package/dist/lib/auth-store.js +528 -0
  135. package/dist/lib/backup.js +171 -0
  136. package/dist/lib/bootstrap.js +409 -0
  137. package/dist/lib/build-config.js +18 -0
  138. package/dist/lib/builtin-db.js +86 -0
  139. package/dist/lib/cli-config.js +569 -0
  140. package/dist/lib/cli-entry-error.js +52 -0
  141. package/dist/lib/cli-home.js +47 -0
  142. package/dist/lib/cli-locale.js +141 -0
  143. package/dist/lib/command-discovery.js +39 -0
  144. package/dist/lib/command-log.js +284 -0
  145. package/dist/lib/db-connection-check.js +219 -0
  146. package/dist/lib/docker-env-file.js +60 -0
  147. package/dist/lib/docker-image.js +37 -0
  148. package/dist/lib/docker-log-stream.js +45 -0
  149. package/dist/lib/env-auth.js +963 -0
  150. package/dist/lib/env-command-config.js +45 -0
  151. package/dist/lib/env-config.js +108 -0
  152. package/dist/lib/env-guard.js +61 -0
  153. package/dist/lib/env-paths.js +101 -0
  154. package/dist/lib/env-proxy.js +1325 -0
  155. package/dist/lib/generated-command.js +203 -0
  156. package/dist/lib/http-request.js +49 -0
  157. package/dist/lib/inquirer-theme.js +17 -0
  158. package/dist/lib/inquirer.js +243 -0
  159. package/dist/lib/managed-env-file.js +101 -0
  160. package/dist/lib/managed-init-env.js +32 -0
  161. package/dist/lib/naming.js +70 -0
  162. package/dist/lib/object-utils.js +76 -0
  163. package/dist/lib/openapi.js +62 -0
  164. package/dist/lib/plugin-import.js +279 -0
  165. package/dist/lib/plugin-storage.js +64 -0
  166. package/dist/lib/post-processors.js +23 -0
  167. package/dist/lib/prompt-catalog-core.js +186 -0
  168. package/dist/lib/prompt-catalog-terminal.js +374 -0
  169. package/{src/index.js → dist/lib/prompt-catalog.js} +2 -6
  170. package/dist/lib/prompt-validators.js +278 -0
  171. package/dist/lib/prompt-web-ui.js +2234 -0
  172. package/dist/lib/proxy-caddy.js +274 -0
  173. package/dist/lib/proxy-nginx.js +330 -0
  174. package/dist/lib/resource-command.js +357 -0
  175. package/dist/lib/resource-request.js +104 -0
  176. package/dist/lib/run-npm.js +429 -0
  177. package/dist/lib/runtime-env-vars.js +32 -0
  178. package/dist/lib/runtime-generator.js +498 -0
  179. package/dist/lib/runtime-store.js +56 -0
  180. package/dist/lib/self-manager.js +301 -0
  181. package/dist/lib/session-id.js +17 -0
  182. package/dist/lib/session-integration.js +703 -0
  183. package/dist/lib/session-store.js +118 -0
  184. package/dist/lib/skills-manager.js +438 -0
  185. package/dist/lib/source-publish.js +326 -0
  186. package/dist/lib/source-registry.js +188 -0
  187. package/dist/lib/startup-update.js +309 -0
  188. package/dist/lib/ui.js +159 -0
  189. package/dist/locale/en-US.json +526 -0
  190. package/dist/locale/zh-CN.json +526 -0
  191. package/dist/post-processors/data-modeling.js +84 -0
  192. package/dist/post-processors/data-source-manager.js +138 -0
  193. package/dist/post-processors/index.js +19 -0
  194. package/nocobase-ctl.config.json +388 -0
  195. package/package.json +133 -25
  196. package/bin/index.js +0 -39
  197. package/nocobase.conf.tpl +0 -95
  198. package/src/commands/benchmark.js +0 -73
  199. package/src/commands/build.js +0 -49
  200. package/src/commands/clean.js +0 -30
  201. package/src/commands/client.js +0 -166
  202. package/src/commands/create-nginx-conf.js +0 -37
  203. package/src/commands/create-plugin.js +0 -33
  204. package/src/commands/dev.js +0 -200
  205. package/src/commands/doc.js +0 -76
  206. package/src/commands/e2e.js +0 -265
  207. package/src/commands/global.js +0 -43
  208. package/src/commands/index.js +0 -45
  209. package/src/commands/instance-id.js +0 -47
  210. package/src/commands/locale/cronstrue.js +0 -122
  211. package/src/commands/locale/react-js-cron/en-US.json +0 -75
  212. package/src/commands/locale/react-js-cron/zh-CN.json +0 -33
  213. package/src/commands/locale/react-js-cron/zh-TW.json +0 -33
  214. package/src/commands/locale.js +0 -81
  215. package/src/commands/p-test.js +0 -88
  216. package/src/commands/perf.js +0 -63
  217. package/src/commands/pkg.js +0 -321
  218. package/src/commands/pm2.js +0 -37
  219. package/src/commands/postinstall.js +0 -88
  220. package/src/commands/start.js +0 -148
  221. package/src/commands/tar.js +0 -36
  222. package/src/commands/test-coverage.js +0 -55
  223. package/src/commands/test.js +0 -107
  224. package/src/commands/umi.js +0 -33
  225. package/src/commands/update-deps.js +0 -72
  226. package/src/commands/upgrade.js +0 -47
  227. package/src/commands/view-license-key.js +0 -44
  228. package/src/license.js +0 -76
  229. package/src/logger.js +0 -75
  230. package/src/plugin-generator.js +0 -80
  231. package/src/util.js +0 -517
  232. package/templates/bundle-status.html +0 -338
  233. package/templates/create-app-package.json +0 -39
  234. package/templates/plugin/.npmignore.tpl +0 -2
  235. package/templates/plugin/README.md.tpl +0 -1
  236. package/templates/plugin/client.d.ts +0 -2
  237. package/templates/plugin/client.js +0 -1
  238. package/templates/plugin/package.json.tpl +0 -11
  239. package/templates/plugin/server.d.ts +0 -2
  240. package/templates/plugin/server.js +0 -1
  241. package/templates/plugin/src/client/client.d.ts +0 -249
  242. package/templates/plugin/src/client/index.tsx.tpl +0 -1
  243. package/templates/plugin/src/client/locale.ts +0 -21
  244. package/templates/plugin/src/client/plugin.tsx.tpl +0 -10
  245. package/templates/plugin/src/index.ts +0 -2
  246. package/templates/plugin/src/locale/en-US.json +0 -1
  247. package/templates/plugin/src/locale/zh-CN.json +0 -1
  248. package/templates/plugin/src/server/collections/.gitkeep +0 -0
  249. package/templates/plugin/src/server/index.ts.tpl +0 -1
  250. package/templates/plugin/src/server/plugin.ts.tpl +0 -19
@@ -0,0 +1,569 @@
1
+ /**
2
+ * This file is part of the NocoBase (R) project.
3
+ * Copyright (c) 2020-2024 NocoBase Co., Ltd.
4
+ * Authors: NocoBase Team.
5
+ *
6
+ * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
7
+ * For more information, please refer to: https://www.nocobase.com/agreement.
8
+ */
9
+ import { loadExactAuthConfig, saveAuthConfig } from './auth-store.js';
10
+ import { resolveCliHomeRoot, resolveDefaultConfigScope } from './cli-home.js';
11
+ import { CLI_LOCALE_FLAG_OPTIONS, normalizeCliLocale, resolveCliLocale } from './cli-locale.js';
12
+ export const DEFAULT_LICENSE_PKG_URL = 'https://pkg.nocobase.com/';
13
+ export const DEFAULT_DOCKER_NETWORK = 'nocobase';
14
+ export const DEFAULT_DOCKER_CONTAINER_PREFIX = 'nb';
15
+ export const DEFAULT_DOCKER_BIN = 'docker';
16
+ export const DEFAULT_CADDY_BIN = 'caddy';
17
+ export const DEFAULT_GIT_BIN = 'git';
18
+ export const DEFAULT_NGINX_BIN = 'nginx';
19
+ export const PROXY_PROVIDER_OPTIONS = ['nginx', 'caddy'];
20
+ export const DEFAULT_PROXY_PROVIDER = 'nginx';
21
+ export const NGINX_PROXY_DRIVER_OPTIONS = ['local', 'docker'];
22
+ export const DEFAULT_NGINX_PROXY_DRIVER = 'local';
23
+ export const CADDY_PROXY_DRIVER_OPTIONS = ['local', 'docker'];
24
+ export const DEFAULT_CADDY_PROXY_DRIVER = 'local';
25
+ export const DEFAULT_PROXY_HOST = '127.0.0.1';
26
+ export const DEFAULT_YARN_BIN = 'yarn';
27
+ export const DEFAULT_LOG_RETENTION_DAYS = 14;
28
+ export const DEFAULT_LOG_ENABLED = true;
29
+ export const CLI_UPDATE_POLICY_OPTIONS = ['prompt', 'auto', 'off'];
30
+ export const DEFAULT_UPDATE_POLICY = 'prompt';
31
+ export const SUPPORTED_CLI_CONFIG_KEYS = [
32
+ 'locale',
33
+ 'default-ui-host',
34
+ 'default-api-host',
35
+ 'update.policy',
36
+ 'license.pkg-url',
37
+ 'docker.network',
38
+ 'docker.container-prefix',
39
+ 'bin.docker',
40
+ 'bin.caddy',
41
+ 'bin.git',
42
+ 'bin.nginx',
43
+ 'proxy.nb-cli-root',
44
+ 'proxy.caddy-driver',
45
+ 'proxy.nginx-driver',
46
+ 'proxy.upstream-host',
47
+ 'bin.yarn',
48
+ 'log.enabled',
49
+ 'log.retention-days',
50
+ ];
51
+ function trimValue(value) {
52
+ const text = String(value ?? '').trim();
53
+ return text || undefined;
54
+ }
55
+ function resolveScope(options = {}) {
56
+ return {
57
+ scope: options.scope ?? resolveDefaultConfigScope(),
58
+ };
59
+ }
60
+ export function isSupportedCliConfigKey(value) {
61
+ return SUPPORTED_CLI_CONFIG_KEYS.includes(value);
62
+ }
63
+ export function assertSupportedCliConfigKey(value) {
64
+ if (!isSupportedCliConfigKey(value)) {
65
+ throw new Error(`Unsupported config key "${value}". Supported keys: ${SUPPORTED_CLI_CONFIG_KEYS.join(', ')}`);
66
+ }
67
+ return value;
68
+ }
69
+ export function normalizeCliUpdatePolicy(value) {
70
+ const normalized = trimValue(value);
71
+ if (!normalized) {
72
+ return undefined;
73
+ }
74
+ return CLI_UPDATE_POLICY_OPTIONS.includes(normalized) ? normalized : undefined;
75
+ }
76
+ export function normalizeProxyProvider(value) {
77
+ const normalized = trimValue(value);
78
+ if (!normalized) {
79
+ return undefined;
80
+ }
81
+ return PROXY_PROVIDER_OPTIONS.includes(normalized) ? normalized : undefined;
82
+ }
83
+ export function normalizeNginxProxyDriver(value) {
84
+ const normalized = trimValue(value);
85
+ if (!normalized) {
86
+ return undefined;
87
+ }
88
+ return NGINX_PROXY_DRIVER_OPTIONS.includes(normalized)
89
+ ? normalized
90
+ : undefined;
91
+ }
92
+ export function normalizeCaddyProxyDriver(value) {
93
+ const normalized = trimValue(value);
94
+ if (!normalized) {
95
+ return undefined;
96
+ }
97
+ return CADDY_PROXY_DRIVER_OPTIONS.includes(normalized)
98
+ ? normalized
99
+ : undefined;
100
+ }
101
+ function cloneSettings(config) {
102
+ return {
103
+ ...(config.settings?.locale ? { locale: trimValue(config.settings.locale) } : {}),
104
+ init: config.settings?.init ? { ...config.settings.init } : undefined,
105
+ update: config.settings?.update ? { ...config.settings.update } : undefined,
106
+ license: config.settings?.license ? { ...config.settings.license } : undefined,
107
+ docker: config.settings?.docker ? { ...config.settings.docker } : undefined,
108
+ bin: config.settings?.bin ? { ...config.settings.bin } : undefined,
109
+ proxy: config.settings?.proxy ? { ...config.settings.proxy } : undefined,
110
+ log: config.settings?.log ? { ...config.settings.log } : undefined,
111
+ };
112
+ }
113
+ function pruneSettings(config) {
114
+ if (config.settings && !trimValue(config.settings.locale)) {
115
+ delete config.settings.locale;
116
+ }
117
+ const init = config.settings?.init;
118
+ if (init && !trimValue(init.defaultUiHost) && !trimValue(init.defaultApiHost)) {
119
+ delete config.settings?.init;
120
+ }
121
+ const update = config.settings?.update;
122
+ if (update && !normalizeCliUpdatePolicy(update.policy)) {
123
+ delete config.settings?.update;
124
+ }
125
+ const license = config.settings?.license;
126
+ if (license && !trimValue(license.pkgUrl)) {
127
+ delete config.settings?.license;
128
+ }
129
+ const docker = config.settings?.docker;
130
+ if (docker && !trimValue(docker.network) && !trimValue(docker.containerPrefix)) {
131
+ delete config.settings?.docker;
132
+ }
133
+ const bin = config.settings?.bin;
134
+ if (bin &&
135
+ !trimValue(bin.docker) &&
136
+ !trimValue(bin.caddy) &&
137
+ !trimValue(bin.git) &&
138
+ !trimValue(bin.nginx) &&
139
+ !trimValue(bin.yarn)) {
140
+ delete config.settings?.bin;
141
+ }
142
+ const proxy = config.settings?.proxy;
143
+ if (proxy &&
144
+ !trimValue(proxy.nbCliRoot) &&
145
+ !trimValue(proxy.caddyDriver) &&
146
+ !trimValue(proxy.nginxDriver) &&
147
+ !trimValue(proxy.upstreamHost)) {
148
+ delete config.settings?.proxy;
149
+ }
150
+ const log = config.settings?.log;
151
+ if (log && typeof log.enabled !== 'boolean' && (!Number.isInteger(log.retentionDays) || log.retentionDays < 0)) {
152
+ delete config.settings?.log;
153
+ }
154
+ if (config.settings &&
155
+ !config.settings.locale &&
156
+ !config.settings.init &&
157
+ !config.settings.update &&
158
+ !config.settings.license &&
159
+ !config.settings.docker &&
160
+ !config.settings.bin &&
161
+ !config.settings.proxy &&
162
+ !config.settings.log) {
163
+ delete config.settings;
164
+ }
165
+ }
166
+ export function getExplicitCliConfigValue(config, key) {
167
+ switch (key) {
168
+ case 'locale':
169
+ return trimValue(config.settings?.locale);
170
+ case 'default-ui-host':
171
+ return trimValue(config.settings?.init?.defaultUiHost);
172
+ case 'default-api-host':
173
+ return trimValue(config.settings?.init?.defaultApiHost);
174
+ case 'update.policy':
175
+ return normalizeCliUpdatePolicy(config.settings?.update?.policy);
176
+ case 'license.pkg-url':
177
+ return trimValue(config.settings?.license?.pkgUrl);
178
+ case 'docker.network':
179
+ return trimValue(config.settings?.docker?.network);
180
+ case 'docker.container-prefix':
181
+ return trimValue(config.settings?.docker?.containerPrefix);
182
+ case 'bin.docker':
183
+ return trimValue(config.settings?.bin?.docker);
184
+ case 'bin.caddy':
185
+ return trimValue(config.settings?.bin?.caddy);
186
+ case 'bin.git':
187
+ return trimValue(config.settings?.bin?.git);
188
+ case 'bin.nginx':
189
+ return trimValue(config.settings?.bin?.nginx);
190
+ case 'proxy.nb-cli-root':
191
+ return trimValue(config.settings?.proxy?.nbCliRoot);
192
+ case 'proxy.caddy-driver':
193
+ return normalizeCaddyProxyDriver(config.settings?.proxy?.caddyDriver);
194
+ case 'proxy.nginx-driver':
195
+ return normalizeNginxProxyDriver(config.settings?.proxy?.nginxDriver);
196
+ case 'proxy.upstream-host':
197
+ return trimValue(config.settings?.proxy?.upstreamHost);
198
+ case 'bin.yarn':
199
+ return trimValue(config.settings?.bin?.yarn);
200
+ case 'log.enabled':
201
+ return typeof config.settings?.log?.enabled === 'boolean' ? String(config.settings?.log?.enabled) : undefined;
202
+ case 'log.retention-days':
203
+ return Number.isInteger(config.settings?.log?.retentionDays)
204
+ ? String(config.settings?.log?.retentionDays)
205
+ : undefined;
206
+ }
207
+ }
208
+ export function getEffectiveCliConfigValue(config, key) {
209
+ const explicit = getExplicitCliConfigValue(config, key);
210
+ if (explicit && key !== 'locale') {
211
+ return explicit;
212
+ }
213
+ switch (key) {
214
+ case 'locale':
215
+ return resolveCliLocale(undefined, { configuredLocale: trimValue(config.settings?.locale) });
216
+ case 'default-ui-host':
217
+ return '127.0.0.1';
218
+ case 'default-api-host':
219
+ return '127.0.0.1';
220
+ case 'update.policy':
221
+ return explicit ?? DEFAULT_UPDATE_POLICY;
222
+ case 'license.pkg-url':
223
+ return DEFAULT_LICENSE_PKG_URL;
224
+ case 'docker.network':
225
+ return trimValue(config.name) || DEFAULT_DOCKER_NETWORK;
226
+ case 'docker.container-prefix':
227
+ return trimValue(config.name) || DEFAULT_DOCKER_CONTAINER_PREFIX;
228
+ case 'bin.docker':
229
+ return DEFAULT_DOCKER_BIN;
230
+ case 'bin.caddy':
231
+ return DEFAULT_CADDY_BIN;
232
+ case 'bin.git':
233
+ return DEFAULT_GIT_BIN;
234
+ case 'bin.nginx':
235
+ return DEFAULT_NGINX_BIN;
236
+ case 'proxy.nb-cli-root':
237
+ return explicit ?? resolveCliHomeRoot();
238
+ case 'proxy.caddy-driver':
239
+ return explicit ?? DEFAULT_CADDY_PROXY_DRIVER;
240
+ case 'proxy.nginx-driver':
241
+ return explicit ?? DEFAULT_NGINX_PROXY_DRIVER;
242
+ case 'proxy.upstream-host':
243
+ return explicit ?? DEFAULT_PROXY_HOST;
244
+ case 'bin.yarn':
245
+ return DEFAULT_YARN_BIN;
246
+ case 'log.enabled':
247
+ return explicit ?? String(DEFAULT_LOG_ENABLED);
248
+ case 'log.retention-days':
249
+ return explicit ?? String(DEFAULT_LOG_RETENTION_DAYS);
250
+ }
251
+ }
252
+ export function normalizeCliConfigValue(key, value) {
253
+ const normalized = value.trim();
254
+ if (!normalized) {
255
+ throw new Error(`Config key "${key}" requires a non-empty value.`);
256
+ }
257
+ if (key === 'license.pkg-url') {
258
+ return normalized.replace(/\/+$/, '') + '/';
259
+ }
260
+ if (key === 'locale') {
261
+ const locale = normalizeCliLocale(normalized);
262
+ if (!locale) {
263
+ throw new Error(`Config key "${key}" must be one of: ${CLI_LOCALE_FLAG_OPTIONS.join(', ')}`);
264
+ }
265
+ return locale;
266
+ }
267
+ if (key === 'update.policy') {
268
+ const policy = normalizeCliUpdatePolicy(normalized);
269
+ if (!policy) {
270
+ throw new Error(`Config key "${key}" must be one of: ${CLI_UPDATE_POLICY_OPTIONS.join(', ')}`);
271
+ }
272
+ return policy;
273
+ }
274
+ if (key === 'log.retention-days') {
275
+ const retentionDays = Number.parseInt(normalized, 10);
276
+ if (!Number.isInteger(retentionDays) || retentionDays < 0) {
277
+ throw new Error(`Config key "${key}" must be a non-negative integer.`);
278
+ }
279
+ return String(retentionDays);
280
+ }
281
+ if (key === 'log.enabled') {
282
+ if (normalized !== 'true' && normalized !== 'false') {
283
+ throw new Error(`Config key "${key}" must be either "true" or "false".`);
284
+ }
285
+ return normalized;
286
+ }
287
+ if (key === 'proxy.nginx-driver') {
288
+ const driver = normalizeNginxProxyDriver(normalized);
289
+ if (!driver) {
290
+ throw new Error(`Config key "${key}" must be one of: ${NGINX_PROXY_DRIVER_OPTIONS.join(', ')}`);
291
+ }
292
+ return driver;
293
+ }
294
+ if (key === 'proxy.caddy-driver') {
295
+ const driver = normalizeCaddyProxyDriver(normalized);
296
+ if (!driver) {
297
+ throw new Error(`Config key "${key}" must be one of: ${CADDY_PROXY_DRIVER_OPTIONS.join(', ')}`);
298
+ }
299
+ return driver;
300
+ }
301
+ return normalized;
302
+ }
303
+ export async function loadCliConfig(options = {}) {
304
+ return await loadExactAuthConfig(resolveScope(options));
305
+ }
306
+ export async function getCliConfigValue(key, options = {}) {
307
+ const config = await loadCliConfig(options);
308
+ return getEffectiveCliConfigValue(config, key);
309
+ }
310
+ export async function listExplicitCliConfigValues(options = {}) {
311
+ const config = await loadCliConfig(options);
312
+ const out = {};
313
+ for (const key of SUPPORTED_CLI_CONFIG_KEYS) {
314
+ const value = getExplicitCliConfigValue(config, key);
315
+ if (value) {
316
+ out[key] = value;
317
+ }
318
+ }
319
+ return out;
320
+ }
321
+ export async function setCliConfigValue(key, value, options = {}) {
322
+ const scope = resolveScope(options);
323
+ const config = await loadExactAuthConfig(scope);
324
+ const normalized = normalizeCliConfigValue(key, value);
325
+ config.settings = cloneSettings(config);
326
+ switch (key) {
327
+ case 'locale':
328
+ config.settings.locale = normalized;
329
+ break;
330
+ case 'default-ui-host':
331
+ config.settings.init = {
332
+ ...(config.settings.init ?? {}),
333
+ defaultUiHost: normalized,
334
+ };
335
+ break;
336
+ case 'default-api-host':
337
+ config.settings.init = {
338
+ ...(config.settings.init ?? {}),
339
+ defaultApiHost: normalized,
340
+ };
341
+ break;
342
+ case 'update.policy':
343
+ config.settings.update = {
344
+ ...(config.settings.update ?? {}),
345
+ policy: normalized,
346
+ };
347
+ break;
348
+ case 'license.pkg-url':
349
+ config.settings.license = {
350
+ ...(config.settings.license ?? {}),
351
+ pkgUrl: normalized,
352
+ };
353
+ break;
354
+ case 'docker.network':
355
+ config.settings.docker = {
356
+ ...(config.settings.docker ?? {}),
357
+ network: normalized,
358
+ };
359
+ break;
360
+ case 'docker.container-prefix':
361
+ config.settings.docker = {
362
+ ...(config.settings.docker ?? {}),
363
+ containerPrefix: normalized,
364
+ };
365
+ break;
366
+ case 'bin.docker':
367
+ config.settings.bin = {
368
+ ...(config.settings.bin ?? {}),
369
+ docker: normalized,
370
+ };
371
+ break;
372
+ case 'bin.caddy':
373
+ config.settings.bin = {
374
+ ...(config.settings.bin ?? {}),
375
+ caddy: normalized,
376
+ };
377
+ break;
378
+ case 'bin.git':
379
+ config.settings.bin = {
380
+ ...(config.settings.bin ?? {}),
381
+ git: normalized,
382
+ };
383
+ break;
384
+ case 'bin.nginx':
385
+ config.settings.bin = {
386
+ ...(config.settings.bin ?? {}),
387
+ nginx: normalized,
388
+ };
389
+ break;
390
+ case 'proxy.nb-cli-root':
391
+ config.settings.proxy = {
392
+ ...(config.settings.proxy ?? {}),
393
+ nbCliRoot: normalized,
394
+ };
395
+ break;
396
+ case 'proxy.caddy-driver':
397
+ config.settings.proxy = {
398
+ ...(config.settings.proxy ?? {}),
399
+ caddyDriver: normalized,
400
+ };
401
+ break;
402
+ case 'proxy.nginx-driver':
403
+ config.settings.proxy = {
404
+ ...(config.settings.proxy ?? {}),
405
+ nginxDriver: normalized,
406
+ };
407
+ break;
408
+ case 'proxy.upstream-host':
409
+ config.settings.proxy = {
410
+ ...(config.settings.proxy ?? {}),
411
+ upstreamHost: normalized,
412
+ };
413
+ break;
414
+ case 'bin.yarn':
415
+ config.settings.bin = {
416
+ ...(config.settings.bin ?? {}),
417
+ yarn: normalized,
418
+ };
419
+ break;
420
+ case 'log.enabled':
421
+ config.settings.log = {
422
+ ...(config.settings.log ?? {}),
423
+ enabled: normalized === 'true',
424
+ };
425
+ break;
426
+ case 'log.retention-days':
427
+ config.settings.log = {
428
+ ...(config.settings.log ?? {}),
429
+ retentionDays: Number.parseInt(normalized, 10),
430
+ };
431
+ break;
432
+ }
433
+ pruneSettings(config);
434
+ await saveAuthConfig(config, scope);
435
+ return normalized;
436
+ }
437
+ export async function deleteCliConfigValue(key, options = {}) {
438
+ const scope = resolveScope(options);
439
+ const config = await loadExactAuthConfig(scope);
440
+ const hadValue = Boolean(getExplicitCliConfigValue(config, key));
441
+ if (!hadValue) {
442
+ return false;
443
+ }
444
+ config.settings = cloneSettings(config);
445
+ switch (key) {
446
+ case 'locale':
447
+ delete config.settings.locale;
448
+ break;
449
+ case 'default-ui-host':
450
+ if (config.settings.init) {
451
+ delete config.settings.init.defaultUiHost;
452
+ }
453
+ break;
454
+ case 'default-api-host':
455
+ if (config.settings.init) {
456
+ delete config.settings.init.defaultApiHost;
457
+ }
458
+ break;
459
+ case 'update.policy':
460
+ if (config.settings.update) {
461
+ delete config.settings.update.policy;
462
+ }
463
+ break;
464
+ case 'license.pkg-url':
465
+ if (config.settings.license) {
466
+ delete config.settings.license.pkgUrl;
467
+ }
468
+ break;
469
+ case 'docker.network':
470
+ if (config.settings.docker) {
471
+ delete config.settings.docker.network;
472
+ }
473
+ break;
474
+ case 'docker.container-prefix':
475
+ if (config.settings.docker) {
476
+ delete config.settings.docker.containerPrefix;
477
+ }
478
+ break;
479
+ case 'bin.docker':
480
+ if (config.settings.bin) {
481
+ delete config.settings.bin.docker;
482
+ }
483
+ break;
484
+ case 'bin.caddy':
485
+ if (config.settings.bin) {
486
+ delete config.settings.bin.caddy;
487
+ }
488
+ break;
489
+ case 'bin.git':
490
+ if (config.settings.bin) {
491
+ delete config.settings.bin.git;
492
+ }
493
+ break;
494
+ case 'bin.nginx':
495
+ if (config.settings.bin) {
496
+ delete config.settings.bin.nginx;
497
+ }
498
+ break;
499
+ case 'proxy.nb-cli-root':
500
+ if (config.settings.proxy) {
501
+ delete config.settings.proxy.nbCliRoot;
502
+ }
503
+ break;
504
+ case 'proxy.caddy-driver':
505
+ if (config.settings.proxy) {
506
+ delete config.settings.proxy.caddyDriver;
507
+ }
508
+ break;
509
+ case 'proxy.nginx-driver':
510
+ if (config.settings.proxy) {
511
+ delete config.settings.proxy.nginxDriver;
512
+ }
513
+ break;
514
+ case 'proxy.upstream-host':
515
+ if (config.settings.proxy) {
516
+ delete config.settings.proxy.upstreamHost;
517
+ }
518
+ break;
519
+ case 'bin.yarn':
520
+ if (config.settings.bin) {
521
+ delete config.settings.bin.yarn;
522
+ }
523
+ break;
524
+ case 'log.enabled':
525
+ if (config.settings.log) {
526
+ delete config.settings.log.enabled;
527
+ }
528
+ break;
529
+ case 'log.retention-days':
530
+ if (config.settings.log) {
531
+ delete config.settings.log.retentionDays;
532
+ }
533
+ break;
534
+ }
535
+ pruneSettings(config);
536
+ await saveAuthConfig(config, scope);
537
+ return true;
538
+ }
539
+ export async function resolveDockerNetworkName(options = {}) {
540
+ return await getCliConfigValue('docker.network', options);
541
+ }
542
+ export async function resolveDefaultUiHost(options = {}) {
543
+ return await getCliConfigValue('default-ui-host', options);
544
+ }
545
+ export async function resolveDefaultApiHost(options = {}) {
546
+ return await getCliConfigValue('default-api-host', options);
547
+ }
548
+ export async function resolveDockerContainerPrefix(options = {}) {
549
+ return await getCliConfigValue('docker.container-prefix', options);
550
+ }
551
+ export async function resolveLicensePkgUrlFromConfig(options = {}) {
552
+ return await getCliConfigValue('license.pkg-url', options);
553
+ }
554
+ const CONFIGURABLE_COMMAND_KEYS = {
555
+ docker: 'bin.docker',
556
+ caddy: 'bin.caddy',
557
+ git: 'bin.git',
558
+ nginx: 'bin.nginx',
559
+ yarn: 'bin.yarn',
560
+ };
561
+ export function isConfigurableCommandName(value) {
562
+ return Object.prototype.hasOwnProperty.call(CONFIGURABLE_COMMAND_KEYS, value);
563
+ }
564
+ export async function resolveConfiguredCommandName(commandName, options = {}) {
565
+ if (!isConfigurableCommandName(commandName)) {
566
+ return commandName;
567
+ }
568
+ return await getCliConfigValue(CONFIGURABLE_COMMAND_KEYS[commandName], options);
569
+ }
@@ -0,0 +1,52 @@
1
+ /**
2
+ * This file is part of the NocoBase (R) project.
3
+ * Copyright (c) 2020-2024 NocoBase Co., Ltd.
4
+ * Authors: NocoBase Team.
5
+ *
6
+ * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
7
+ * For more information, please refer to: https://www.nocobase.com/agreement.
8
+ */
9
+ export function getCommandPathTokens(argv) {
10
+ const tokens = [];
11
+ for (const token of argv) {
12
+ if (!token) {
13
+ continue;
14
+ }
15
+ if (token.startsWith('-')) {
16
+ break;
17
+ }
18
+ tokens.push(token);
19
+ }
20
+ return tokens;
21
+ }
22
+ export function formatCliEntryError(error, argv) {
23
+ const message = error instanceof Error ? error.message : String(error);
24
+ const missingCommandMatch = message.match(/^Command (.+) not found\.$/);
25
+ if (!missingCommandMatch) {
26
+ return message;
27
+ }
28
+ const commandPathTokens = getCommandPathTokens(argv);
29
+ const attemptedCommand = commandPathTokens.join(' ') || missingCommandMatch[1];
30
+ const isApiCommand = commandPathTokens[0] === 'api';
31
+ if (isApiCommand) {
32
+ const helpCommandTokens = commandPathTokens.length > 2 ? commandPathTokens.slice(0, -1) : ['api'];
33
+ const helpCommand = `nb ${helpCommandTokens.join(' ')} --help`;
34
+ return [
35
+ `Unknown command: \`${attemptedCommand}\`.`,
36
+ `If this is a built-in command or a typo, run \`${helpCommand}\` to inspect the commands available under that API group.`,
37
+ ].join('\n');
38
+ }
39
+ return [
40
+ `Unknown command: \`${attemptedCommand}\`.`,
41
+ 'If this is a built-in command or a typo, run `nb --help` to inspect available commands.',
42
+ `If \`${attemptedCommand}\` should be a runtime command from your NocoBase app, check whether the connected app exposes it, then retry the command.`,
43
+ ].join('\n');
44
+ }
45
+ export function appendDiagnosticLogPath(message, logFile) {
46
+ const normalizedMessage = String(message ?? '').trim();
47
+ const normalizedLogFile = String(logFile ?? '').trim();
48
+ if (!normalizedLogFile) {
49
+ return normalizedMessage;
50
+ }
51
+ return [normalizedMessage, `Diagnostic log: ${normalizedLogFile}`].filter(Boolean).join('\n\n');
52
+ }
@@ -0,0 +1,47 @@
1
+ /**
2
+ * This file is part of the NocoBase (R) project.
3
+ * Copyright (c) 2020-2024 NocoBase Co., Ltd.
4
+ * Authors: NocoBase Team.
5
+ *
6
+ * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
7
+ * For more information, please refer to: https://www.nocobase.com/agreement.
8
+ */
9
+ import os from 'node:os';
10
+ import path from 'node:path';
11
+ export const CLI_HOME_DIRNAME = '.nocobase';
12
+ export const NB_CLI_ROOT_ENV = 'NB_CLI_ROOT';
13
+ export function resolveDefaultConfigScope() {
14
+ return 'global';
15
+ }
16
+ function readConfiguredPath(name) {
17
+ const value = String(process.env[name] ?? '').trim();
18
+ return value || undefined;
19
+ }
20
+ function resolveGlobalCliHomeRoot() {
21
+ return readConfiguredPath(NB_CLI_ROOT_ENV) ?? os.homedir();
22
+ }
23
+ export function resolveCliHomeRoot(scope = resolveDefaultConfigScope()) {
24
+ void scope;
25
+ return resolveGlobalCliHomeRoot();
26
+ }
27
+ export function resolveCliHomeDir(scope = resolveDefaultConfigScope()) {
28
+ return path.join(resolveCliHomeRoot(scope), CLI_HOME_DIRNAME);
29
+ }
30
+ export function resolveEnvRoot(scope = resolveDefaultConfigScope()) {
31
+ void scope;
32
+ return resolveCliHomeRoot();
33
+ }
34
+ export function resolveEnvRelativePath(relativePath, scope = resolveDefaultConfigScope()) {
35
+ return path.resolve(resolveEnvRoot(scope), relativePath);
36
+ }
37
+ export function resolveConfiguredEnvPath(value, scope = resolveDefaultConfigScope()) {
38
+ const text = String(value ?? '').trim();
39
+ if (!text) {
40
+ return undefined;
41
+ }
42
+ return path.isAbsolute(text) ? text : resolveEnvRelativePath(text, scope);
43
+ }
44
+ export function formatCliHomeScope(scope) {
45
+ void scope;
46
+ return 'global';
47
+ }