@nocobase/cli 2.1.0-alpha.4 → 2.1.0-alpha.45

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 (214) hide show
  1. package/LICENSE.txt +107 -0
  2. package/README.md +405 -19
  3. package/README.zh-CN.md +355 -0
  4. package/bin/run.cmd +3 -0
  5. package/bin/run.js +136 -0
  6. package/bin/session-env.js +39 -0
  7. package/dist/commands/api/resource/create.js +15 -0
  8. package/dist/commands/api/resource/destroy.js +15 -0
  9. package/dist/commands/api/resource/get.js +15 -0
  10. package/dist/commands/api/resource/index.js +20 -0
  11. package/dist/commands/api/resource/list.js +16 -0
  12. package/dist/commands/api/resource/query.js +15 -0
  13. package/dist/commands/api/resource/update.js +15 -0
  14. package/dist/commands/app/destroy.js +225 -0
  15. package/dist/commands/app/down.js +71 -0
  16. package/dist/commands/app/logs.js +114 -0
  17. package/dist/commands/app/restart.js +158 -0
  18. package/dist/commands/app/shared.js +122 -0
  19. package/dist/commands/app/start.js +286 -0
  20. package/dist/commands/app/stop.js +161 -0
  21. package/dist/commands/app/upgrade.js +519 -0
  22. package/dist/commands/backup/create.js +147 -0
  23. package/dist/commands/backup/index.js +20 -0
  24. package/dist/commands/backup/restore.js +105 -0
  25. package/{src/cli.js → dist/commands/build.js} +4 -11
  26. package/dist/commands/config/delete.js +34 -0
  27. package/dist/commands/config/get.js +33 -0
  28. package/dist/commands/config/index.js +20 -0
  29. package/dist/commands/config/list.js +29 -0
  30. package/dist/commands/config/set.js +39 -0
  31. package/dist/commands/db/check.js +240 -0
  32. package/dist/commands/db/logs.js +85 -0
  33. package/dist/commands/db/ps.js +60 -0
  34. package/dist/commands/db/shared.js +96 -0
  35. package/dist/commands/db/start.js +86 -0
  36. package/dist/commands/db/stop.js +71 -0
  37. package/{templates/plugin/src/client/models/index.ts → dist/commands/dev.js} +4 -4
  38. package/{src/commands/locale/react-js-cron/index.js → dist/commands/down.js} +3 -8
  39. package/dist/commands/download.js +13 -0
  40. package/dist/commands/env/add.js +426 -0
  41. package/dist/commands/env/auth.js +189 -0
  42. package/dist/commands/env/current.js +21 -0
  43. package/dist/commands/env/info.js +201 -0
  44. package/dist/commands/env/list.js +44 -0
  45. package/dist/commands/env/remove.js +84 -0
  46. package/dist/commands/env/shared.js +196 -0
  47. package/dist/commands/env/status.js +90 -0
  48. package/dist/commands/env/update.js +74 -0
  49. package/dist/commands/env/use.js +38 -0
  50. package/dist/commands/examples/prompts-stages.js +150 -0
  51. package/dist/commands/examples/prompts-test.js +181 -0
  52. package/dist/commands/init.js +1152 -0
  53. package/dist/commands/install.js +2424 -0
  54. package/dist/commands/license/activate.js +360 -0
  55. package/dist/commands/license/env.js +94 -0
  56. package/dist/commands/license/generate-id.js +108 -0
  57. package/dist/commands/license/id.js +70 -0
  58. package/dist/commands/license/index.js +20 -0
  59. package/dist/commands/license/plugins/clean.js +115 -0
  60. package/dist/commands/license/plugins/index.js +20 -0
  61. package/dist/commands/license/plugins/list.js +64 -0
  62. package/dist/commands/license/plugins/shared.js +331 -0
  63. package/dist/commands/license/plugins/sync.js +314 -0
  64. package/dist/commands/license/shared.js +423 -0
  65. package/dist/commands/license/status.js +64 -0
  66. package/dist/commands/logs.js +12 -0
  67. package/dist/commands/plugin/disable.js +86 -0
  68. package/dist/commands/plugin/enable.js +86 -0
  69. package/dist/commands/plugin/list.js +82 -0
  70. package/dist/commands/pm/disable.js +12 -0
  71. package/dist/commands/pm/enable.js +12 -0
  72. package/dist/commands/pm/list.js +12 -0
  73. package/dist/commands/restart.js +12 -0
  74. package/dist/commands/scaffold/migration.js +38 -0
  75. package/dist/commands/scaffold/plugin.js +37 -0
  76. package/dist/commands/self/check.js +71 -0
  77. package/dist/commands/self/index.js +20 -0
  78. package/dist/commands/self/update.js +95 -0
  79. package/dist/commands/session/id.js +24 -0
  80. package/dist/commands/session/remove.js +57 -0
  81. package/dist/commands/session/setup.js +62 -0
  82. package/dist/commands/skills/check.js +69 -0
  83. package/dist/commands/skills/index.js +20 -0
  84. package/dist/commands/skills/install.js +80 -0
  85. package/dist/commands/skills/remove.js +80 -0
  86. package/dist/commands/skills/update.js +87 -0
  87. package/dist/commands/source/build.js +58 -0
  88. package/dist/commands/source/dev.js +182 -0
  89. package/dist/commands/source/download.js +884 -0
  90. package/dist/commands/source/publish.js +109 -0
  91. package/dist/commands/source/registry/logs.js +70 -0
  92. package/dist/commands/source/registry/start.js +57 -0
  93. package/dist/commands/source/registry/status.js +33 -0
  94. package/dist/commands/source/registry/stop.js +48 -0
  95. package/dist/commands/source/test.js +477 -0
  96. package/dist/commands/start.js +12 -0
  97. package/dist/commands/stop.js +12 -0
  98. package/dist/commands/test.js +12 -0
  99. package/dist/commands/upgrade.js +12 -0
  100. package/dist/commands/v1.js +210 -0
  101. package/dist/generated/command-registry.js +134 -0
  102. package/dist/help/runtime-help.js +23 -0
  103. package/dist/lib/api-client.js +335 -0
  104. package/dist/lib/api-command-compat.js +641 -0
  105. package/dist/lib/app-health.js +132 -0
  106. package/dist/lib/app-managed-resources.js +316 -0
  107. package/dist/lib/app-runtime.js +180 -0
  108. package/dist/lib/auth-store.js +396 -0
  109. package/dist/lib/backup.js +171 -0
  110. package/dist/lib/bootstrap.js +403 -0
  111. package/dist/lib/build-config.js +18 -0
  112. package/dist/lib/builtin-db.js +86 -0
  113. package/dist/lib/cli-config.js +271 -0
  114. package/dist/lib/cli-home.js +47 -0
  115. package/dist/lib/cli-locale.js +141 -0
  116. package/dist/lib/command-discovery.js +39 -0
  117. package/dist/lib/db-connection-check.js +219 -0
  118. package/dist/lib/docker-env-file.js +52 -0
  119. package/dist/lib/docker-image.js +37 -0
  120. package/dist/lib/env-auth.js +952 -0
  121. package/dist/lib/env-config.js +95 -0
  122. package/dist/lib/env-guard.js +62 -0
  123. package/dist/lib/generated-command.js +203 -0
  124. package/dist/lib/http-request.js +49 -0
  125. package/dist/lib/inquirer-theme.js +17 -0
  126. package/dist/lib/inquirer.js +244 -0
  127. package/dist/lib/naming.js +70 -0
  128. package/dist/lib/object-utils.js +76 -0
  129. package/dist/lib/openapi.js +62 -0
  130. package/dist/lib/plugin-storage.js +64 -0
  131. package/dist/lib/post-processors.js +23 -0
  132. package/dist/lib/prompt-catalog-core.js +185 -0
  133. package/dist/lib/prompt-catalog-terminal.js +375 -0
  134. package/{src/index.js → dist/lib/prompt-catalog.js} +2 -6
  135. package/dist/lib/prompt-validators.js +258 -0
  136. package/dist/lib/prompt-web-ui.js +2227 -0
  137. package/dist/lib/resource-command.js +357 -0
  138. package/dist/lib/resource-request.js +104 -0
  139. package/dist/lib/run-npm.js +385 -0
  140. package/dist/lib/runtime-env-vars.js +32 -0
  141. package/dist/lib/runtime-generator.js +498 -0
  142. package/dist/lib/runtime-store.js +56 -0
  143. package/dist/lib/self-manager.js +301 -0
  144. package/dist/lib/session-id.js +17 -0
  145. package/dist/lib/session-integration.js +703 -0
  146. package/dist/lib/session-store.js +118 -0
  147. package/dist/lib/skills-manager.js +436 -0
  148. package/dist/lib/source-publish.js +306 -0
  149. package/dist/lib/source-registry.js +188 -0
  150. package/dist/lib/startup-update.js +285 -0
  151. package/dist/lib/ui.js +158 -0
  152. package/dist/locale/en-US.json +425 -0
  153. package/dist/locale/zh-CN.json +425 -0
  154. package/dist/post-processors/data-modeling.js +84 -0
  155. package/dist/post-processors/data-source-manager.js +138 -0
  156. package/dist/post-processors/index.js +19 -0
  157. package/nocobase-ctl.config.json +388 -0
  158. package/package.json +132 -26
  159. package/LICENSE +0 -661
  160. package/bin/index.js +0 -39
  161. package/nocobase.conf.tpl +0 -95
  162. package/src/commands/benchmark.js +0 -73
  163. package/src/commands/build.js +0 -49
  164. package/src/commands/clean.js +0 -30
  165. package/src/commands/client.js +0 -166
  166. package/src/commands/create-nginx-conf.js +0 -37
  167. package/src/commands/create-plugin.js +0 -33
  168. package/src/commands/dev.js +0 -200
  169. package/src/commands/doc.js +0 -76
  170. package/src/commands/e2e.js +0 -265
  171. package/src/commands/global.js +0 -43
  172. package/src/commands/index.js +0 -45
  173. package/src/commands/instance-id.js +0 -47
  174. package/src/commands/locale/cronstrue.js +0 -122
  175. package/src/commands/locale/react-js-cron/en-US.json +0 -75
  176. package/src/commands/locale/react-js-cron/zh-CN.json +0 -33
  177. package/src/commands/locale/react-js-cron/zh-TW.json +0 -33
  178. package/src/commands/locale.js +0 -81
  179. package/src/commands/p-test.js +0 -88
  180. package/src/commands/perf.js +0 -63
  181. package/src/commands/pkg.js +0 -321
  182. package/src/commands/pm2.js +0 -37
  183. package/src/commands/postinstall.js +0 -88
  184. package/src/commands/start.js +0 -148
  185. package/src/commands/tar.js +0 -36
  186. package/src/commands/test-coverage.js +0 -55
  187. package/src/commands/test.js +0 -107
  188. package/src/commands/umi.js +0 -33
  189. package/src/commands/update-deps.js +0 -72
  190. package/src/commands/upgrade.js +0 -47
  191. package/src/commands/view-license-key.js +0 -44
  192. package/src/license.js +0 -76
  193. package/src/logger.js +0 -75
  194. package/src/plugin-generator.js +0 -80
  195. package/src/util.js +0 -517
  196. package/templates/bundle-status.html +0 -338
  197. package/templates/create-app-package.json +0 -39
  198. package/templates/plugin/.npmignore.tpl +0 -2
  199. package/templates/plugin/README.md.tpl +0 -1
  200. package/templates/plugin/client.d.ts +0 -2
  201. package/templates/plugin/client.js +0 -1
  202. package/templates/plugin/package.json.tpl +0 -11
  203. package/templates/plugin/server.d.ts +0 -2
  204. package/templates/plugin/server.js +0 -1
  205. package/templates/plugin/src/client/client.d.ts +0 -249
  206. package/templates/plugin/src/client/index.tsx.tpl +0 -1
  207. package/templates/plugin/src/client/locale.ts +0 -21
  208. package/templates/plugin/src/client/plugin.tsx.tpl +0 -10
  209. package/templates/plugin/src/index.ts +0 -2
  210. package/templates/plugin/src/locale/en-US.json +0 -1
  211. package/templates/plugin/src/locale/zh-CN.json +0 -1
  212. package/templates/plugin/src/server/collections/.gitkeep +0 -0
  213. package/templates/plugin/src/server/index.ts.tpl +0 -1
  214. package/templates/plugin/src/server/plugin.ts.tpl +0 -19
@@ -0,0 +1,122 @@
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 fsp from 'node:fs/promises';
10
+ import os from 'node:os';
11
+ import path from 'node:path';
12
+ import { buildDockerDbContainerName } from '../../lib/app-runtime.js';
13
+ import { resolveConfiguredEnvPath } from '../../lib/cli-home.js';
14
+ import { commandOutput, run } from '../../lib/run-npm.js';
15
+ export function resolveConfiguredPath(value) {
16
+ return resolveConfiguredEnvPath(value);
17
+ }
18
+ function assertSafeRemovalPath(target, label) {
19
+ const resolved = path.resolve(target);
20
+ const cwd = path.resolve(process.cwd());
21
+ const home = path.resolve(os.homedir());
22
+ const root = path.parse(resolved).root;
23
+ if (resolved === root || resolved === cwd || resolved === home) {
24
+ throw new Error(`Refusing to remove ${label} at "${resolved}" because it is too broad.`);
25
+ }
26
+ }
27
+ export async function removePathIfExists(target, label) {
28
+ const resolved = path.resolve(target);
29
+ assertSafeRemovalPath(resolved, label);
30
+ await fsp.rm(resolved, { recursive: true, force: true });
31
+ }
32
+ function isMissingDockerContainerError(error) {
33
+ const message = error instanceof Error ? error.message : String(error);
34
+ return /No such (container|object)/i.test(message);
35
+ }
36
+ function isMissingDockerNetworkError(error) {
37
+ const message = error instanceof Error ? error.message : String(error);
38
+ return /No such network|network .* not found/i.test(message);
39
+ }
40
+ export async function removeDockerContainerIfExists(containerName, options) {
41
+ try {
42
+ await commandOutput('docker', ['container', 'inspect', containerName], {
43
+ errorName: 'docker container inspect',
44
+ });
45
+ }
46
+ catch (error) {
47
+ if (isMissingDockerContainerError(error)) {
48
+ return 'missing';
49
+ }
50
+ throw error;
51
+ }
52
+ await run('docker', ['rm', '-f', containerName], {
53
+ errorName: 'docker rm',
54
+ stdio: options?.stdio ?? 'ignore',
55
+ });
56
+ return 'removed';
57
+ }
58
+ async function dockerNetworkExists(networkName) {
59
+ try {
60
+ await commandOutput('docker', ['network', 'inspect', networkName], {
61
+ errorName: 'docker network inspect',
62
+ });
63
+ return true;
64
+ }
65
+ catch (error) {
66
+ if (isMissingDockerNetworkError(error)) {
67
+ return false;
68
+ }
69
+ throw error;
70
+ }
71
+ }
72
+ async function dockerNetworkHasActiveEndpoints(networkName) {
73
+ try {
74
+ const output = await commandOutput('docker', ['network', 'inspect', networkName, '--format', '{{json .Containers}}'], {
75
+ errorName: 'docker network inspect',
76
+ });
77
+ const containers = JSON.parse(output || '{}');
78
+ return Boolean(containers && typeof containers === 'object' && Object.keys(containers).length > 0);
79
+ }
80
+ catch {
81
+ return false;
82
+ }
83
+ }
84
+ export async function removeDockerNetworkIfUnused(networkName) {
85
+ if (!(await dockerNetworkExists(networkName))) {
86
+ return 'missing';
87
+ }
88
+ try {
89
+ await run('docker', ['network', 'rm', networkName], {
90
+ errorName: 'docker network rm',
91
+ stdio: 'ignore',
92
+ });
93
+ return 'removed';
94
+ }
95
+ catch (error) {
96
+ const message = error instanceof Error ? error.message : String(error);
97
+ if (/has active endpoints|is in use|active endpoints/i.test(message) ||
98
+ ((await dockerNetworkExists(networkName)) && (await dockerNetworkHasActiveEndpoints(networkName)))) {
99
+ return 'in-use';
100
+ }
101
+ if (isMissingDockerNetworkError(error)) {
102
+ return 'missing';
103
+ }
104
+ throw error;
105
+ }
106
+ }
107
+ export function builtinDbContainerName(runtime) {
108
+ if (!runtime.env.config.builtinDb) {
109
+ return undefined;
110
+ }
111
+ const dbDialect = String(runtime.env.config.dbDialect ?? 'postgres').trim() || 'postgres';
112
+ return buildDockerDbContainerName(runtime.envName, dbDialect, runtime.dockerContainerPrefix || runtime.workspaceName);
113
+ }
114
+ export function managedDockerNetworkName(runtime) {
115
+ return runtime.dockerNetworkName?.trim() || runtime.workspaceName?.trim() || undefined;
116
+ }
117
+ export function resolveManagedLocalAppPath(runtime) {
118
+ return resolveConfiguredPath(runtime.env.config.appRootPath) || runtime.projectRoot;
119
+ }
120
+ export function shouldRemoveManagedLocalAppFiles(runtime) {
121
+ return runtime.source === 'npm' || runtime.source === 'git';
122
+ }
@@ -0,0 +1,286 @@
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 { Command, Flags } from '@oclif/core';
10
+ import { ensureCrossEnvConfirmed, hasExplicitEnvSelection } from '../../lib/env-guard.js';
11
+ import { formatMissingManagedAppEnvMessage, resolveManagedAppRuntime, runLocalNocoBaseCommand, } from '../../lib/app-runtime.js';
12
+ import { AppHealthCheckError, formatAppUrl, isAppReady, resolveManagedAppApiBaseUrl, waitForAppReady, } from '../../lib/app-health.js';
13
+ import { ensureBuiltinDbReady, ensureLocalPostinstall, ensureSavedLocalSource, recreateSavedDockerApp, } from '../../lib/app-managed-resources.js';
14
+ import { run } from '../../lib/run-npm.js';
15
+ import { announceTargetEnv, failTask, printInfo, startTask, succeedTask } from '../../lib/ui.js';
16
+ function shouldPrintStartSuccess() {
17
+ return process.env.NB_SKIP_APP_START_SUCCESS_LOG !== '1';
18
+ }
19
+ function argvHasToken(argv, tokens) {
20
+ return tokens.some((token) => argv.includes(token));
21
+ }
22
+ function formatDockerStartFailure(envName, message) {
23
+ return [
24
+ `Couldn't start NocoBase for "${envName}".`,
25
+ 'Check that the Docker runtime for this env is still available, then try again.',
26
+ `Details: ${message}`,
27
+ ].join('\n');
28
+ }
29
+ function formatLocalStartFailure(envName, options) {
30
+ const sourceLabel = options?.source === 'git'
31
+ ? 'the local Git checkout'
32
+ : options?.source === 'npm'
33
+ ? 'the local npm app'
34
+ : 'the local app';
35
+ const portHint = options?.port ? ` Expected app port: ${options.port}.` : '';
36
+ return [
37
+ `Couldn't start NocoBase for "${envName}".`,
38
+ `The CLI was not able to start ${sourceLabel} successfully.`,
39
+ `Check that the app dependencies, database connection, and local env settings are ready, then try again.${portHint}`,
40
+ ].join('\n');
41
+ }
42
+ function formatLocalReadyFailure(envName, message, options) {
43
+ const sourceLabel = options?.source === 'git'
44
+ ? 'the local Git checkout'
45
+ : options?.source === 'npm'
46
+ ? 'the local npm app'
47
+ : 'the local app';
48
+ const portHint = options?.port ? ` Expected app port: ${options.port}.` : '';
49
+ return [
50
+ `NocoBase did not become ready for "${envName}".`,
51
+ `The CLI started ${sourceLabel}, but the app did not pass its health check in time.`,
52
+ `Check the startup logs, database connection, and local env settings, then try again.${portHint}`,
53
+ `Details: ${message}`,
54
+ ].join('\n');
55
+ }
56
+ export default class AppStart extends Command {
57
+ static hidden = false;
58
+ static description = 'Start NocoBase for the selected env. Local npm/git installs run the app command, and Docker installs recreate the saved app container.';
59
+ static examples = [
60
+ '<%= config.bin %> <%= command.id %>',
61
+ '<%= config.bin %> <%= command.id %> --env local',
62
+ '<%= config.bin %> <%= command.id %> --env local --quickstart',
63
+ '<%= config.bin %> <%= command.id %> --env local --port 12000',
64
+ '<%= config.bin %> <%= command.id %> --env local --daemon',
65
+ '<%= config.bin %> <%= command.id %> --env local --no-daemon',
66
+ '<%= config.bin %> <%= command.id %> --env local --instances 2',
67
+ '<%= config.bin %> <%= command.id %> --env local --launch-mode pm2',
68
+ '<%= config.bin %> <%= command.id %> --env local --verbose',
69
+ '<%= config.bin %> <%= command.id %> --env local-docker',
70
+ ];
71
+ static flags = {
72
+ env: Flags.string({
73
+ char: 'e',
74
+ description: 'CLI env name to start. Defaults to the current env when omitted',
75
+ }),
76
+ yes: Flags.boolean({
77
+ char: 'y',
78
+ description: 'Confirm using --env when it targets a different env than the current env',
79
+ default: false,
80
+ }),
81
+ quickstart: Flags.boolean({ description: 'Quickstart the application', required: false }),
82
+ port: Flags.string({
83
+ description: 'Port (overrides appPort from env config when set)',
84
+ char: 'p',
85
+ required: false,
86
+ }),
87
+ daemon: Flags.boolean({
88
+ description: 'Run the application as a daemon (default: true; use --no-daemon to stay in the foreground)',
89
+ char: 'd',
90
+ required: false,
91
+ default: true,
92
+ allowNo: true,
93
+ }),
94
+ instances: Flags.integer({ description: 'Number of instances to run', char: 'i', required: false }),
95
+ 'launch-mode': Flags.string({ description: 'Launch Mode', required: false, options: ['pm2', 'node'] }),
96
+ verbose: Flags.boolean({
97
+ description: 'Show raw startup output from the underlying local or Docker command',
98
+ default: false,
99
+ }),
100
+ };
101
+ async run() {
102
+ const { flags } = await this.parse(AppStart);
103
+ const requestedEnv = flags.env?.trim() || undefined;
104
+ if (requestedEnv && hasExplicitEnvSelection(this.argv)) {
105
+ const confirmed = await ensureCrossEnvConfirmed({
106
+ command: this,
107
+ requestedEnv,
108
+ yes: flags.yes,
109
+ });
110
+ if (!confirmed) {
111
+ return;
112
+ }
113
+ }
114
+ const daemonFlagWasProvided = argvHasToken(this.argv, ['--daemon', '--no-daemon']);
115
+ const runtime = await resolveManagedAppRuntime(requestedEnv);
116
+ const commandStdio = flags.verbose ? 'inherit' : 'ignore';
117
+ if (!runtime) {
118
+ this.error(formatMissingManagedAppEnvMessage(requestedEnv));
119
+ }
120
+ if (runtime.kind === 'http') {
121
+ this.error([
122
+ `Can't start "${runtime.envName}" from this machine.`,
123
+ 'This env only has an API connection, so there is no saved local app or Docker runtime to launch here.',
124
+ 'Connect it to a local checkout or reinstall it with npm, git, or Docker if you want CLI-managed start and stop.',
125
+ ].join('\n'));
126
+ }
127
+ if (runtime.kind === 'ssh') {
128
+ this.error([
129
+ `Can't start "${runtime.envName}" yet.`,
130
+ 'SSH env support is reserved but not implemented yet.',
131
+ 'Use a local or Docker env if you need CLI-managed start and stop right now.',
132
+ ].join('\n'));
133
+ }
134
+ announceTargetEnv(runtime.envName);
135
+ if (runtime.kind === 'docker') {
136
+ const unsupportedFlags = [
137
+ flags.port ? '--port' : undefined,
138
+ daemonFlagWasProvided ? (flags.daemon ? '--daemon' : '--no-daemon') : undefined,
139
+ flags.instances !== undefined ? '--instances' : undefined,
140
+ flags['launch-mode'] ? '--launch-mode' : undefined,
141
+ ].filter(Boolean);
142
+ if (unsupportedFlags.length > 0) {
143
+ this.error([
144
+ `Can't apply ${unsupportedFlags.join(', ')} to "${runtime.envName}".`,
145
+ 'This env is managed by Docker, so those options are only available for local npm/git installs.',
146
+ `Run \`nb app start --env ${runtime.envName}\` to recreate the saved container, or recreate the env if you need different runtime settings.`,
147
+ ].join('\n'));
148
+ }
149
+ await ensureBuiltinDbReady(runtime, {
150
+ verbose: flags.verbose,
151
+ onStartTask: startTask,
152
+ onSucceedTask: succeedTask,
153
+ onFailTask: failTask,
154
+ });
155
+ const appUrl = formatAppUrl(runtime.env.appPort === undefined || runtime.env.appPort === null ? undefined : String(runtime.env.appPort));
156
+ const apiBaseUrl = resolveManagedAppApiBaseUrl(runtime);
157
+ startTask(`Recreating the Docker app container for "${runtime.envName}"...`);
158
+ try {
159
+ await run('docker', ['rm', '-f', runtime.containerName], {
160
+ errorName: 'docker rm',
161
+ stdio: commandStdio,
162
+ }).catch(() => undefined);
163
+ await recreateSavedDockerApp(runtime, {
164
+ verbose: flags.verbose,
165
+ });
166
+ succeedTask(`Docker app container is ready for "${runtime.envName}".`);
167
+ }
168
+ catch (error) {
169
+ const message = error instanceof Error ? error.message : String(error);
170
+ failTask(`Failed to recreate NocoBase for "${runtime.envName}".`);
171
+ this.error(formatDockerStartFailure(runtime.envName, message));
172
+ }
173
+ await waitForAppReady({
174
+ envName: runtime.envName,
175
+ apiBaseUrl,
176
+ containerName: runtime.containerName,
177
+ logHint: `You can inspect startup logs with \`nb app logs --env ${runtime.envName}\`.`,
178
+ });
179
+ if (shouldPrintStartSuccess()) {
180
+ succeedTask(`NocoBase is running for "${runtime.envName}"${appUrl ? ` at ${appUrl}` : ''}.`);
181
+ }
182
+ return;
183
+ }
184
+ await ensureBuiltinDbReady(runtime, {
185
+ verbose: flags.verbose,
186
+ onStartTask: startTask,
187
+ onSucceedTask: succeedTask,
188
+ onFailTask: failTask,
189
+ });
190
+ if (runtime.source === 'npm' || runtime.source === 'git') {
191
+ const runCommand = this.config.runCommand.bind(this.config);
192
+ const downloadableRuntime = runtime;
193
+ await ensureSavedLocalSource(downloadableRuntime, runCommand, {
194
+ verbose: flags.verbose,
195
+ onStartTask: startTask,
196
+ onSucceedTask: succeedTask,
197
+ onFailTask: failTask,
198
+ });
199
+ }
200
+ const npmArgs = ['start'];
201
+ if (flags.quickstart) {
202
+ npmArgs.push('--quickstart');
203
+ }
204
+ if (flags.port) {
205
+ npmArgs.push('--port', flags.port);
206
+ }
207
+ else if (runtime.env.appPort !== undefined &&
208
+ runtime.env.appPort !== null &&
209
+ String(runtime.env.appPort).trim() !== '') {
210
+ npmArgs.push('--port', String(runtime.env.appPort));
211
+ }
212
+ if (flags.daemon !== false) {
213
+ npmArgs.push('--daemon');
214
+ }
215
+ if (flags.instances !== undefined) {
216
+ npmArgs.push('--instances', flags.instances.toString());
217
+ }
218
+ if (flags['launch-mode']) {
219
+ npmArgs.push('--launch-mode', flags['launch-mode']);
220
+ }
221
+ const effectivePort = flags.port ||
222
+ (runtime.env.appPort !== undefined && runtime.env.appPort !== null
223
+ ? String(runtime.env.appPort).trim()
224
+ : undefined);
225
+ const appUrl = formatAppUrl(effectivePort);
226
+ const apiBaseUrl = resolveManagedAppApiBaseUrl(runtime, {
227
+ portOverride: effectivePort,
228
+ });
229
+ if (await isAppReady(apiBaseUrl, { requestTimeoutMs: 1_500 })) {
230
+ if (flags.daemon === false) {
231
+ printInfo(`NocoBase is already running for "${runtime.envName}"${appUrl ? ` at ${appUrl}` : ''}. Use \`nb app stop --env ${runtime.envName}\` before starting it again in the foreground.`);
232
+ }
233
+ else {
234
+ if (shouldPrintStartSuccess()) {
235
+ succeedTask(`NocoBase is already running for "${runtime.envName}"${appUrl ? ` at ${appUrl}` : ''}.`);
236
+ }
237
+ }
238
+ return;
239
+ }
240
+ try {
241
+ await ensureLocalPostinstall(runtime, {
242
+ verbose: flags.verbose,
243
+ onStartTask: startTask,
244
+ onSucceedTask: succeedTask,
245
+ onFailTask: failTask,
246
+ });
247
+ }
248
+ catch (error) {
249
+ this.error(error instanceof Error ? error.message : String(error));
250
+ }
251
+ if (flags.daemon === false) {
252
+ printInfo(`Starting NocoBase for "${runtime.envName}" in the foreground${appUrl ? ` at ${appUrl}` : ''}. Press Ctrl+C to stop.`);
253
+ }
254
+ else {
255
+ startTask(`Starting NocoBase for "${runtime.envName}" in the background...`);
256
+ }
257
+ try {
258
+ await runLocalNocoBaseCommand(runtime, npmArgs, {
259
+ stdio: commandStdio,
260
+ });
261
+ if (flags.daemon !== false) {
262
+ await waitForAppReady({
263
+ envName: runtime.envName,
264
+ apiBaseUrl,
265
+ logHint: `You can inspect startup logs with \`nb app logs --env ${runtime.envName}\`.`,
266
+ });
267
+ if (shouldPrintStartSuccess()) {
268
+ succeedTask(`NocoBase is running for "${runtime.envName}"${appUrl ? ` at ${appUrl}` : ''}.`);
269
+ }
270
+ }
271
+ }
272
+ catch (error) {
273
+ failTask(`Failed to start NocoBase for "${runtime.envName}".`);
274
+ if (error instanceof AppHealthCheckError) {
275
+ this.error(formatLocalReadyFailure(runtime.envName, error.message, {
276
+ port: effectivePort,
277
+ source: runtime.source,
278
+ }));
279
+ }
280
+ this.error(formatLocalStartFailure(runtime.envName, {
281
+ port: effectivePort,
282
+ source: runtime.source,
283
+ }));
284
+ }
285
+ }
286
+ }
@@ -0,0 +1,161 @@
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 { Command, Flags } from '@oclif/core';
10
+ import { getCurrentEnvName } from '../../lib/auth-store.js';
11
+ import { ensureCrossEnvConfirmed, hasExplicitEnvSelection } from '../../lib/env-guard.js';
12
+ import { formatMissingManagedAppEnvMessage, resolveManagedAppRuntime, runLocalNocoBaseCommand, } from '../../lib/app-runtime.js';
13
+ import { announceTargetEnv, failTask, isInteractiveTerminal, printInfo, startTask, succeedTask } from '../../lib/ui.js';
14
+ import { builtinDbContainerName, removeDockerContainerIfExists } from './shared.js';
15
+ function formatStopFailure(envName, message) {
16
+ return [
17
+ `Couldn't stop NocoBase for "${envName}".`,
18
+ 'Check that the local app or Docker runtime is still available, then try again.',
19
+ `Details: ${message}`,
20
+ ].join('\n');
21
+ }
22
+ function formatStopCrossEnvConfirmationRequiredMessage(currentEnv, requestedEnv) {
23
+ return [
24
+ `Refusing to stop env "${requestedEnv}" because the current env is "${currentEnv}" and interactive confirmation is unavailable in the current session.`,
25
+ '',
26
+ 'Re-run the command with `--yes` to confirm this one-off cross-env stop, or switch the current env first.',
27
+ ].join('\n');
28
+ }
29
+ export default class AppStop extends Command {
30
+ static hidden = false;
31
+ static description = 'Stop NocoBase for the selected env. Local npm/git installs stop the app process, and Docker installs remove the saved app container.';
32
+ static examples = [
33
+ '<%= config.bin %> <%= command.id %>',
34
+ '<%= config.bin %> <%= command.id %> --env local',
35
+ '<%= config.bin %> <%= command.id %> --env local --verbose',
36
+ '<%= config.bin %> <%= command.id %> --env local-docker',
37
+ ];
38
+ static flags = {
39
+ env: Flags.string({
40
+ char: 'e',
41
+ description: 'CLI env name to stop. Defaults to the current env when omitted',
42
+ }),
43
+ yes: Flags.boolean({
44
+ char: 'y',
45
+ description: 'Confirm using --env when it targets a different env than the current env',
46
+ default: false,
47
+ }),
48
+ force: Flags.boolean({
49
+ char: 'f',
50
+ hidden: true,
51
+ default: false,
52
+ }),
53
+ 'with-db': Flags.boolean({
54
+ description: 'Also remove the CLI-managed built-in database container when present',
55
+ default: false,
56
+ }),
57
+ verbose: Flags.boolean({
58
+ description: 'Show raw shutdown output from the underlying local or Docker command',
59
+ default: false,
60
+ }),
61
+ };
62
+ async run() {
63
+ const { flags } = await this.parse(AppStop);
64
+ const requestedEnv = flags.env?.trim() || undefined;
65
+ if (requestedEnv && hasExplicitEnvSelection(this.argv)) {
66
+ const skipCrossEnvConfirmation = flags.yes || flags.force;
67
+ if (!isInteractiveTerminal()) {
68
+ const currentEnv = await getCurrentEnvName();
69
+ const normalizedCurrentEnv = String(currentEnv ?? '').trim() || undefined;
70
+ if (normalizedCurrentEnv && normalizedCurrentEnv !== requestedEnv && !skipCrossEnvConfirmation) {
71
+ this.error(formatStopCrossEnvConfirmationRequiredMessage(normalizedCurrentEnv, requestedEnv));
72
+ }
73
+ }
74
+ else {
75
+ const confirmed = await ensureCrossEnvConfirmed({
76
+ command: this,
77
+ requestedEnv,
78
+ yes: skipCrossEnvConfirmation,
79
+ });
80
+ if (!confirmed) {
81
+ return;
82
+ }
83
+ }
84
+ }
85
+ const runtime = await resolveManagedAppRuntime(requestedEnv);
86
+ const commandStdio = flags.verbose ? 'inherit' : 'ignore';
87
+ if (!runtime) {
88
+ this.error(formatMissingManagedAppEnvMessage(requestedEnv));
89
+ }
90
+ if (runtime.kind === 'http') {
91
+ this.error([
92
+ `Can't stop "${runtime.envName}" from this machine.`,
93
+ 'This env only has an API connection, so there is no saved local app or Docker runtime to stop here.',
94
+ 'If the app is running on a server, stop it there or reconnect this env to a local runtime first.',
95
+ ].join('\n'));
96
+ }
97
+ if (runtime.kind === 'ssh') {
98
+ this.error([
99
+ `Can't stop "${runtime.envName}" yet.`,
100
+ 'SSH env support is reserved but not implemented yet.',
101
+ 'Use a local or Docker env if you need CLI-managed stop right now.',
102
+ ].join('\n'));
103
+ }
104
+ announceTargetEnv(runtime.envName);
105
+ if (runtime.kind === 'docker') {
106
+ startTask(`Removing the saved Docker app container for "${runtime.envName}"...`);
107
+ try {
108
+ const state = await removeDockerContainerIfExists(runtime.containerName, {
109
+ stdio: commandStdio,
110
+ });
111
+ succeedTask(state === 'removed'
112
+ ? `Docker app container removed for "${runtime.envName}".`
113
+ : `No Docker app container found for "${runtime.envName}".`);
114
+ }
115
+ catch (error) {
116
+ const message = error instanceof Error ? error.message : String(error);
117
+ failTask(`Failed to stop NocoBase for "${runtime.envName}".`);
118
+ this.error(formatStopFailure(runtime.envName, message));
119
+ }
120
+ }
121
+ else {
122
+ startTask(`Stopping NocoBase for "${runtime.envName}"...`);
123
+ try {
124
+ await runLocalNocoBaseCommand(runtime, ['pm2', 'kill'], {
125
+ stdio: commandStdio,
126
+ });
127
+ succeedTask(`NocoBase has stopped for "${runtime.envName}".`);
128
+ }
129
+ catch (error) {
130
+ const message = error instanceof Error ? error.message : String(error);
131
+ failTask(`Failed to stop NocoBase for "${runtime.envName}".`);
132
+ this.error(formatStopFailure(runtime.envName, message));
133
+ }
134
+ }
135
+ if (!flags['with-db']) {
136
+ return;
137
+ }
138
+ if (runtime.kind !== 'local' && runtime.kind !== 'docker') {
139
+ return;
140
+ }
141
+ const dbContainerName = builtinDbContainerName(runtime);
142
+ if (!dbContainerName) {
143
+ printInfo(`Env "${runtime.envName}" does not use a CLI-managed built-in database. Only the app runtime was stopped.`);
144
+ return;
145
+ }
146
+ startTask(`Removing the built-in database container for "${runtime.envName}"...`);
147
+ try {
148
+ const state = await removeDockerContainerIfExists(dbContainerName, {
149
+ stdio: commandStdio,
150
+ });
151
+ succeedTask(state === 'removed'
152
+ ? `Built-in database container removed for "${runtime.envName}".`
153
+ : `No built-in database container found for "${runtime.envName}".`);
154
+ }
155
+ catch (error) {
156
+ const message = error instanceof Error ? error.message : String(error);
157
+ failTask(`Failed to stop the built-in database for "${runtime.envName}".`);
158
+ this.error(formatStopFailure(runtime.envName, message));
159
+ }
160
+ }
161
+ }