@nocobase/cli 2.1.0-alpha.3 → 2.1.0-alpha.30

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 (182) hide show
  1. package/LICENSE.txt +107 -0
  2. package/README.md +379 -19
  3. package/README.zh-CN.md +329 -0
  4. package/bin/run.cmd +3 -0
  5. package/bin/run.js +131 -0
  6. package/dist/commands/api/resource/create.js +15 -0
  7. package/dist/commands/api/resource/destroy.js +15 -0
  8. package/dist/commands/api/resource/get.js +15 -0
  9. package/dist/commands/api/resource/index.js +20 -0
  10. package/dist/commands/api/resource/list.js +16 -0
  11. package/dist/commands/api/resource/query.js +15 -0
  12. package/dist/commands/api/resource/update.js +15 -0
  13. package/dist/commands/app/down.js +266 -0
  14. package/dist/commands/app/logs.js +98 -0
  15. package/dist/commands/app/restart.js +75 -0
  16. package/dist/commands/app/start.js +253 -0
  17. package/dist/commands/app/stop.js +99 -0
  18. package/dist/commands/app/upgrade.js +582 -0
  19. package/{src/cli.js → dist/commands/build.js} +4 -11
  20. package/dist/commands/config/delete.js +30 -0
  21. package/dist/commands/config/get.js +29 -0
  22. package/dist/commands/config/index.js +20 -0
  23. package/dist/commands/config/list.js +29 -0
  24. package/dist/commands/config/set.js +35 -0
  25. package/dist/commands/db/check.js +238 -0
  26. package/dist/commands/db/logs.js +85 -0
  27. package/dist/commands/db/ps.js +60 -0
  28. package/dist/commands/db/shared.js +96 -0
  29. package/dist/commands/db/start.js +71 -0
  30. package/dist/commands/db/stop.js +71 -0
  31. package/{templates/plugin/src/client/models/index.ts → dist/commands/dev.js} +4 -4
  32. package/{src/index.js → dist/commands/down.js} +4 -6
  33. package/{src/commands/locale/react-js-cron/index.js → dist/commands/download.js} +4 -8
  34. package/dist/commands/env/add.js +312 -0
  35. package/dist/commands/env/auth.js +55 -0
  36. package/dist/commands/env/info.js +156 -0
  37. package/dist/commands/env/list.js +50 -0
  38. package/dist/commands/env/remove.js +59 -0
  39. package/dist/commands/env/shared.js +158 -0
  40. package/dist/commands/env/update.js +67 -0
  41. package/dist/commands/env/use.js +28 -0
  42. package/dist/commands/examples/prompts-stages.js +150 -0
  43. package/dist/commands/examples/prompts-test.js +181 -0
  44. package/dist/commands/init.js +1027 -0
  45. package/dist/commands/install.js +2206 -0
  46. package/dist/commands/license/activate.js +360 -0
  47. package/dist/commands/license/env.js +94 -0
  48. package/dist/commands/license/generate-id.js +108 -0
  49. package/dist/commands/license/id.js +56 -0
  50. package/dist/commands/license/index.js +20 -0
  51. package/dist/commands/license/plugins/clean.js +101 -0
  52. package/dist/commands/license/plugins/index.js +20 -0
  53. package/dist/commands/license/plugins/list.js +50 -0
  54. package/dist/commands/license/plugins/shared.js +325 -0
  55. package/dist/commands/license/plugins/sync.js +269 -0
  56. package/dist/commands/license/shared.js +414 -0
  57. package/dist/commands/license/status.js +50 -0
  58. package/dist/commands/logs.js +12 -0
  59. package/dist/commands/plugin/disable.js +66 -0
  60. package/dist/commands/plugin/enable.js +66 -0
  61. package/dist/commands/plugin/list.js +62 -0
  62. package/dist/commands/pm/disable.js +12 -0
  63. package/dist/commands/pm/enable.js +12 -0
  64. package/dist/commands/pm/list.js +12 -0
  65. package/dist/commands/restart.js +12 -0
  66. package/dist/commands/scaffold/migration.js +38 -0
  67. package/dist/commands/scaffold/plugin.js +37 -0
  68. package/dist/commands/self/check.js +71 -0
  69. package/dist/commands/self/index.js +20 -0
  70. package/dist/commands/self/update.js +86 -0
  71. package/dist/commands/skills/check.js +69 -0
  72. package/dist/commands/skills/index.js +20 -0
  73. package/dist/commands/skills/install.js +71 -0
  74. package/dist/commands/skills/remove.js +71 -0
  75. package/dist/commands/skills/update.js +78 -0
  76. package/dist/commands/source/build.js +58 -0
  77. package/dist/commands/source/dev.js +158 -0
  78. package/dist/commands/source/download.js +866 -0
  79. package/dist/commands/source/test.js +467 -0
  80. package/dist/commands/start.js +12 -0
  81. package/dist/commands/stop.js +12 -0
  82. package/dist/commands/test.js +12 -0
  83. package/dist/commands/upgrade.js +12 -0
  84. package/dist/generated/command-registry.js +133 -0
  85. package/dist/help/runtime-help.js +23 -0
  86. package/dist/lib/api-client.js +329 -0
  87. package/dist/lib/app-health.js +126 -0
  88. package/dist/lib/app-managed-resources.js +268 -0
  89. package/dist/lib/app-runtime.js +171 -0
  90. package/dist/lib/auth-store.js +328 -0
  91. package/dist/lib/bootstrap.js +384 -0
  92. package/dist/lib/build-config.js +18 -0
  93. package/dist/lib/builtin-db.js +86 -0
  94. package/dist/lib/cli-config.js +176 -0
  95. package/dist/lib/cli-home.js +47 -0
  96. package/dist/lib/cli-locale.js +129 -0
  97. package/dist/lib/command-discovery.js +39 -0
  98. package/dist/lib/db-connection-check.js +178 -0
  99. package/dist/lib/env-auth.js +872 -0
  100. package/dist/lib/env-config.js +87 -0
  101. package/dist/lib/generated-command.js +171 -0
  102. package/dist/lib/http-request.js +49 -0
  103. package/dist/lib/naming.js +70 -0
  104. package/dist/lib/openapi.js +62 -0
  105. package/dist/lib/plugin-storage.js +127 -0
  106. package/dist/lib/post-processors.js +23 -0
  107. package/dist/lib/prompt-catalog.js +581 -0
  108. package/dist/lib/prompt-validators.js +185 -0
  109. package/dist/lib/prompt-web-ui.js +2103 -0
  110. package/dist/lib/resource-command.js +343 -0
  111. package/dist/lib/resource-request.js +104 -0
  112. package/dist/lib/run-npm.js +250 -0
  113. package/dist/lib/runtime-env-vars.js +32 -0
  114. package/dist/lib/runtime-generator.js +498 -0
  115. package/dist/lib/runtime-store.js +56 -0
  116. package/dist/lib/self-manager.js +301 -0
  117. package/dist/lib/skills-manager.js +296 -0
  118. package/dist/lib/startup-update.js +281 -0
  119. package/dist/lib/ui.js +178 -0
  120. package/dist/locale/en-US.json +339 -0
  121. package/dist/locale/zh-CN.json +339 -0
  122. package/dist/post-processors/data-modeling.js +66 -0
  123. package/dist/post-processors/data-source-manager.js +114 -0
  124. package/dist/post-processors/index.js +19 -0
  125. package/nocobase-ctl.config.json +369 -0
  126. package/package.json +95 -26
  127. package/LICENSE +0 -661
  128. package/bin/index.js +0 -39
  129. package/nocobase.conf.tpl +0 -95
  130. package/src/commands/benchmark.js +0 -73
  131. package/src/commands/build.js +0 -49
  132. package/src/commands/clean.js +0 -30
  133. package/src/commands/client.js +0 -166
  134. package/src/commands/create-nginx-conf.js +0 -37
  135. package/src/commands/create-plugin.js +0 -33
  136. package/src/commands/dev.js +0 -200
  137. package/src/commands/doc.js +0 -76
  138. package/src/commands/e2e.js +0 -265
  139. package/src/commands/global.js +0 -43
  140. package/src/commands/index.js +0 -45
  141. package/src/commands/instance-id.js +0 -47
  142. package/src/commands/locale/cronstrue.js +0 -122
  143. package/src/commands/locale/react-js-cron/en-US.json +0 -75
  144. package/src/commands/locale/react-js-cron/zh-CN.json +0 -33
  145. package/src/commands/locale/react-js-cron/zh-TW.json +0 -33
  146. package/src/commands/locale.js +0 -81
  147. package/src/commands/p-test.js +0 -88
  148. package/src/commands/perf.js +0 -63
  149. package/src/commands/pkg.js +0 -321
  150. package/src/commands/pm2.js +0 -37
  151. package/src/commands/postinstall.js +0 -88
  152. package/src/commands/start.js +0 -148
  153. package/src/commands/tar.js +0 -36
  154. package/src/commands/test-coverage.js +0 -55
  155. package/src/commands/test.js +0 -107
  156. package/src/commands/umi.js +0 -33
  157. package/src/commands/update-deps.js +0 -72
  158. package/src/commands/upgrade.js +0 -47
  159. package/src/commands/view-license-key.js +0 -44
  160. package/src/license.js +0 -76
  161. package/src/logger.js +0 -75
  162. package/src/plugin-generator.js +0 -80
  163. package/src/util.js +0 -517
  164. package/templates/bundle-status.html +0 -338
  165. package/templates/create-app-package.json +0 -39
  166. package/templates/plugin/.npmignore.tpl +0 -2
  167. package/templates/plugin/README.md.tpl +0 -1
  168. package/templates/plugin/client.d.ts +0 -2
  169. package/templates/plugin/client.js +0 -1
  170. package/templates/plugin/package.json.tpl +0 -11
  171. package/templates/plugin/server.d.ts +0 -2
  172. package/templates/plugin/server.js +0 -1
  173. package/templates/plugin/src/client/client.d.ts +0 -249
  174. package/templates/plugin/src/client/index.tsx.tpl +0 -1
  175. package/templates/plugin/src/client/locale.ts +0 -21
  176. package/templates/plugin/src/client/plugin.tsx.tpl +0 -10
  177. package/templates/plugin/src/index.ts +0 -2
  178. package/templates/plugin/src/locale/en-US.json +0 -1
  179. package/templates/plugin/src/locale/zh-CN.json +0 -1
  180. package/templates/plugin/src/server/collections/.gitkeep +0 -0
  181. package/templates/plugin/src/server/index.ts.tpl +0 -1
  182. package/templates/plugin/src/server/plugin.ts.tpl +0 -19
@@ -0,0 +1,301 @@
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 fs from 'node:fs';
10
+ import fsp from 'node:fs/promises';
11
+ import path from 'node:path';
12
+ import { fileURLToPath } from 'node:url';
13
+ import { resolveCliHomeDir } from './cli-home.js';
14
+ import { commandOutput, run } from './run-npm.js';
15
+ const DEFAULT_PACKAGE_NAME = '@nocobase/cli';
16
+ const PACKAGE_ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..', '..');
17
+ const INSTALL_METHOD_CACHE_FILE = 'self-install-methods.json';
18
+ function normalizePath(value) {
19
+ return path.resolve(value);
20
+ }
21
+ function isSubPath(parent, child) {
22
+ const relative = path.relative(normalizePath(parent), normalizePath(child));
23
+ return relative === '' || (!relative.startsWith('..') && !path.isAbsolute(relative));
24
+ }
25
+ function parseVersion(version) {
26
+ const normalized = String(version ?? '').trim();
27
+ const match = normalized.match(/^(\d+)\.(\d+)\.(\d+)(?:-([0-9A-Za-z-.]+))?$/);
28
+ if (!match) {
29
+ return undefined;
30
+ }
31
+ return {
32
+ major: Number(match[1]),
33
+ minor: Number(match[2]),
34
+ patch: Number(match[3]),
35
+ prerelease: match[4] ? match[4].split('.').filter(Boolean) : [],
36
+ };
37
+ }
38
+ function compareIdentifier(left, right) {
39
+ const leftNumeric = /^\d+$/.test(left);
40
+ const rightNumeric = /^\d+$/.test(right);
41
+ if (leftNumeric && rightNumeric) {
42
+ return Number(left) - Number(right);
43
+ }
44
+ if (leftNumeric) {
45
+ return -1;
46
+ }
47
+ if (rightNumeric) {
48
+ return 1;
49
+ }
50
+ return left.localeCompare(right);
51
+ }
52
+ export function compareVersions(leftVersion, rightVersion) {
53
+ const left = parseVersion(leftVersion);
54
+ const right = parseVersion(rightVersion);
55
+ if (!left || !right) {
56
+ return String(leftVersion ?? '').localeCompare(String(rightVersion ?? ''));
57
+ }
58
+ if (left.major !== right.major) {
59
+ return left.major - right.major;
60
+ }
61
+ if (left.minor !== right.minor) {
62
+ return left.minor - right.minor;
63
+ }
64
+ if (left.patch !== right.patch) {
65
+ return left.patch - right.patch;
66
+ }
67
+ if (left.prerelease.length === 0 && right.prerelease.length === 0) {
68
+ return 0;
69
+ }
70
+ if (left.prerelease.length === 0) {
71
+ return 1;
72
+ }
73
+ if (right.prerelease.length === 0) {
74
+ return -1;
75
+ }
76
+ const maxLength = Math.max(left.prerelease.length, right.prerelease.length);
77
+ for (let index = 0; index < maxLength; index += 1) {
78
+ const leftIdentifier = left.prerelease[index];
79
+ const rightIdentifier = right.prerelease[index];
80
+ if (leftIdentifier === undefined) {
81
+ return -1;
82
+ }
83
+ if (rightIdentifier === undefined) {
84
+ return 1;
85
+ }
86
+ const compared = compareIdentifier(leftIdentifier, rightIdentifier);
87
+ if (compared !== 0) {
88
+ return compared;
89
+ }
90
+ }
91
+ return 0;
92
+ }
93
+ function detectChannel(currentVersion) {
94
+ if (/-alpha(?:[.-]|$)/i.test(currentVersion)) {
95
+ return 'alpha';
96
+ }
97
+ if (/-beta(?:[.-]|$)/i.test(currentVersion)) {
98
+ return 'beta';
99
+ }
100
+ return 'latest';
101
+ }
102
+ function readCurrentVersion(packageRoot) {
103
+ const packageJsonPath = path.join(packageRoot, 'package.json');
104
+ const content = fs.readFileSync(packageJsonPath, 'utf8');
105
+ const pkg = JSON.parse(content);
106
+ return String(pkg.version ?? '').trim();
107
+ }
108
+ function detectInstallMethod(packageRoot, globalPrefix) {
109
+ if (fs.existsSync(path.join(packageRoot, 'src'))
110
+ && fs.existsSync(path.join(packageRoot, 'tsconfig.json'))) {
111
+ return 'source';
112
+ }
113
+ if (globalPrefix && isSubPath(globalPrefix, packageRoot)) {
114
+ return 'npm-global';
115
+ }
116
+ if (packageRoot.includes(`${path.sep}node_modules${path.sep}`)) {
117
+ return 'package-local';
118
+ }
119
+ return 'unknown';
120
+ }
121
+ function getInstallMethodCacheFile() {
122
+ return path.join(resolveCliHomeDir('global'), INSTALL_METHOD_CACHE_FILE);
123
+ }
124
+ function getInstallMethodCacheKey(packageRoot) {
125
+ return normalizePath(path.join(packageRoot, 'bin', 'run.js'));
126
+ }
127
+ async function readInstallMethodCache() {
128
+ try {
129
+ const raw = await fsp.readFile(getInstallMethodCacheFile(), 'utf8');
130
+ return JSON.parse(raw);
131
+ }
132
+ catch {
133
+ return {};
134
+ }
135
+ }
136
+ async function writeInstallMethodCache(state) {
137
+ const filePath = getInstallMethodCacheFile();
138
+ await fsp.mkdir(path.dirname(filePath), { recursive: true });
139
+ await fsp.writeFile(filePath, JSON.stringify(state, null, 2));
140
+ }
141
+ async function readCachedInstallMethod(packageRoot) {
142
+ const state = await readInstallMethodCache();
143
+ return state.entries?.[getInstallMethodCacheKey(packageRoot)];
144
+ }
145
+ async function writeCachedInstallMethod(packageRoot, entry) {
146
+ const state = await readInstallMethodCache();
147
+ const entries = {
148
+ ...(state.entries ?? {}),
149
+ [getInstallMethodCacheKey(packageRoot)]: entry,
150
+ };
151
+ await writeInstallMethodCache({ entries });
152
+ }
153
+ async function readGlobalPrefix(commandOutputFn) {
154
+ try {
155
+ return (await commandOutputFn('npm', ['prefix', '-g'], {
156
+ errorName: 'npm prefix',
157
+ })).trim();
158
+ }
159
+ catch {
160
+ return undefined;
161
+ }
162
+ }
163
+ async function readDistTags(packageName, commandOutputFn) {
164
+ const output = await commandOutputFn('npm', ['view', packageName, 'dist-tags', '--json'], {
165
+ errorName: 'npm view',
166
+ });
167
+ const parsed = JSON.parse(output);
168
+ return parsed ?? {};
169
+ }
170
+ function getUnsupportedSelfUpdateReason(installMethod) {
171
+ if (installMethod === 'source') {
172
+ return [
173
+ 'This CLI is running from source in a repository checkout.',
174
+ 'Automatic self-update is only supported for standard global npm installs.',
175
+ 'Upgrade this checkout through your repo workflow instead.',
176
+ ].join(' ');
177
+ }
178
+ if (installMethod === 'package-local') {
179
+ return [
180
+ 'This CLI is installed from a local project dependency tree.',
181
+ 'Automatic self-update is only supported for standard global npm installs.',
182
+ 'Upgrade the parent project dependency that provides this CLI instead.',
183
+ ].join(' ');
184
+ }
185
+ if (installMethod === 'unknown') {
186
+ return [
187
+ 'This CLI install could not be recognized as a standard global npm install.',
188
+ 'Automatic self-update is only supported for standard global npm installs.',
189
+ ].join(' ');
190
+ }
191
+ return undefined;
192
+ }
193
+ export function getRecommendedSelfUpdateCommand(status) {
194
+ if (!status.updatable || !status.updateAvailable) {
195
+ return undefined;
196
+ }
197
+ return 'nb self update --yes';
198
+ }
199
+ export function formatSelfUpdateUnavailableMessage(status) {
200
+ if (status.registryError) {
201
+ return [
202
+ `Couldn't resolve the latest published version for ${status.packageName}.`,
203
+ 'Check your npm registry access and try again.',
204
+ `Details: ${status.registryError}`,
205
+ ].join('\n');
206
+ }
207
+ return [
208
+ `Couldn't resolve the latest published version for ${status.packageName}.`,
209
+ 'Check your npm registry access and try again.',
210
+ ].join('\n');
211
+ }
212
+ export function getSelfUpdatePackageSpec(status) {
213
+ return `${status.packageName}@${status.channel}`;
214
+ }
215
+ export async function inspectSelfInstall(options = {}) {
216
+ const packageRoot = options.packageRoot ? normalizePath(options.packageRoot) : PACKAGE_ROOT;
217
+ const commandOutputFn = options.commandOutputFn ?? commandOutput;
218
+ const cachedInstallMethod = await readCachedInstallMethod(packageRoot);
219
+ const globalPrefix = cachedInstallMethod?.globalPrefix ?? await readGlobalPrefix(commandOutputFn);
220
+ const installMethod = cachedInstallMethod?.installMethod ?? detectInstallMethod(packageRoot, globalPrefix);
221
+ if (!cachedInstallMethod) {
222
+ await writeCachedInstallMethod(packageRoot, {
223
+ installMethod,
224
+ globalPrefix,
225
+ });
226
+ }
227
+ return {
228
+ packageRoot,
229
+ installMethod,
230
+ globalPrefix,
231
+ };
232
+ }
233
+ export async function inspectSelfStatus(options = {}) {
234
+ const packageRoot = options.packageRoot ? normalizePath(options.packageRoot) : PACKAGE_ROOT;
235
+ const packageName = options.packageName ?? DEFAULT_PACKAGE_NAME;
236
+ const currentVersion = options.currentVersion ?? readCurrentVersion(packageRoot);
237
+ const channel = options.channel && options.channel !== 'auto' ? options.channel : detectChannel(currentVersion);
238
+ const commandOutputFn = options.commandOutputFn ?? commandOutput;
239
+ const { installMethod, globalPrefix } = await inspectSelfInstall({
240
+ packageRoot,
241
+ commandOutputFn,
242
+ });
243
+ let latestVersion;
244
+ let registryError;
245
+ try {
246
+ const distTags = await readDistTags(packageName, commandOutputFn);
247
+ latestVersion = distTags[channel] || distTags.latest;
248
+ }
249
+ catch (error) {
250
+ registryError = error instanceof Error ? error.message : String(error);
251
+ }
252
+ const updateAvailable = latestVersion ? compareVersions(latestVersion, currentVersion) > 0 : false;
253
+ return {
254
+ packageName,
255
+ packageRoot,
256
+ currentVersion,
257
+ channel,
258
+ latestVersion,
259
+ updateAvailable,
260
+ installMethod,
261
+ updatable: installMethod === 'npm-global',
262
+ updateBlockedReason: getUnsupportedSelfUpdateReason(installMethod),
263
+ globalPrefix,
264
+ registryError,
265
+ };
266
+ }
267
+ export function formatUnsupportedSelfUpdateMessage(status) {
268
+ return status.updateBlockedReason
269
+ ?? [
270
+ 'Automatic self-update is only supported for standard global npm installs.',
271
+ ].join('\n');
272
+ }
273
+ export async function updateSelf(options = {}) {
274
+ const status = await inspectSelfStatus(options);
275
+ if (!status.updatable) {
276
+ throw new Error(formatUnsupportedSelfUpdateMessage(status));
277
+ }
278
+ const targetVersion = options.targetVersion ?? status.latestVersion;
279
+ if (!targetVersion) {
280
+ throw new Error(formatSelfUpdateUnavailableMessage(status));
281
+ }
282
+ if (!targetVersion || compareVersions(targetVersion, status.currentVersion) <= 0) {
283
+ return {
284
+ action: 'noop',
285
+ status,
286
+ targetVersion,
287
+ packageSpec: getSelfUpdatePackageSpec(status),
288
+ };
289
+ }
290
+ const packageSpec = getSelfUpdatePackageSpec(status);
291
+ await (options.runFn ?? run)('npm', ['install', '-g', packageSpec], {
292
+ stdio: options.verbose ? 'inherit' : 'ignore',
293
+ errorName: 'npm install',
294
+ });
295
+ return {
296
+ action: 'updated',
297
+ status,
298
+ targetVersion,
299
+ packageSpec,
300
+ };
301
+ }
@@ -0,0 +1,296 @@
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 path from 'node:path';
11
+ import { resolveCliHomeDir } from './cli-home.js';
12
+ import { compareVersions } from './self-manager.js';
13
+ import { commandOutput, commandOutputViaFile, run } from './run-npm.js';
14
+ export const NOCOBASE_SKILLS_SOURCE = 'nocobase/skills';
15
+ export const NOCOBASE_SKILLS_PACKAGE_NAME = '@nocobase/skills';
16
+ const NOCOBASE_SKILLS_NAME_PREFIX = 'nocobase-';
17
+ function normalizePath(value) {
18
+ return path.resolve(value);
19
+ }
20
+ export function resolveGlobalSkillsRoot(_startCwd = process.cwd()) {
21
+ return normalizePath(resolveCliHomeDir('global'));
22
+ }
23
+ export function resolveSkillsWorkspaceRoot(startCwd = process.cwd()) {
24
+ return resolveGlobalSkillsRoot(startCwd);
25
+ }
26
+ function resolveSkillsRoot(options = {}) {
27
+ return options.globalRoot
28
+ ? normalizePath(options.globalRoot)
29
+ : options.workspaceRoot
30
+ ? normalizePath(options.workspaceRoot)
31
+ : resolveGlobalSkillsRoot();
32
+ }
33
+ function getSkillsCacheRoot(globalRoot) {
34
+ return path.join(globalRoot, 'cache', 'skills');
35
+ }
36
+ export function getManagedSkillsStateFile(workspaceRoot) {
37
+ return path.join(workspaceRoot, 'skills.json');
38
+ }
39
+ async function ensureSkillsWorkspaceRoot(workspaceRoot) {
40
+ await fsp.mkdir(workspaceRoot, { recursive: true });
41
+ }
42
+ async function readManagedSkillsState(workspaceRoot) {
43
+ const filePath = getManagedSkillsStateFile(workspaceRoot);
44
+ try {
45
+ const content = await fsp.readFile(filePath, 'utf8');
46
+ return JSON.parse(content);
47
+ }
48
+ catch {
49
+ return undefined;
50
+ }
51
+ }
52
+ async function writeManagedSkillsState(workspaceRoot, state) {
53
+ const filePath = getManagedSkillsStateFile(workspaceRoot);
54
+ await fsp.mkdir(path.dirname(filePath), { recursive: true });
55
+ await fsp.writeFile(filePath, JSON.stringify(state, null, 2));
56
+ }
57
+ export async function listGlobalSkills(options = {}) {
58
+ const globalRoot = resolveSkillsRoot(options);
59
+ await ensureSkillsWorkspaceRoot(globalRoot);
60
+ const output = await (options.commandOutputFn ?? commandOutputViaFile)('npx', ['-y', 'skills', 'list', '-g', '--json'], {
61
+ cwd: globalRoot,
62
+ errorName: 'skills list',
63
+ });
64
+ const parsed = JSON.parse(output);
65
+ return Array.isArray(parsed) ? parsed : [];
66
+ }
67
+ export async function listProjectSkills(options = {}) {
68
+ return await listGlobalSkills(options);
69
+ }
70
+ function pickInstalledNocoBaseSkillNames(installedSkills, state) {
71
+ const installedNames = new Set(installedSkills.map((skill) => String(skill.name ?? '').trim()).filter(Boolean));
72
+ if (state?.skillNames?.length) {
73
+ return state.skillNames.filter((name) => installedNames.has(name)).sort();
74
+ }
75
+ return Array.from(installedNames)
76
+ .filter((name) => name.startsWith(NOCOBASE_SKILLS_NAME_PREFIX))
77
+ .sort();
78
+ }
79
+ async function readPublishedSkillsVersion(options = {}) {
80
+ const globalRoot = resolveSkillsRoot(options);
81
+ await ensureSkillsWorkspaceRoot(globalRoot);
82
+ try {
83
+ const output = await (options.commandOutputFn ?? commandOutput)('npm', ['view', NOCOBASE_SKILLS_PACKAGE_NAME, 'version', '--json'], {
84
+ cwd: globalRoot,
85
+ errorName: 'npm view',
86
+ });
87
+ const parsed = JSON.parse(output);
88
+ const version = String(parsed ?? '').trim();
89
+ return { version: version || undefined };
90
+ }
91
+ catch (error) {
92
+ return {
93
+ error: error instanceof Error ? error.message : String(error),
94
+ };
95
+ }
96
+ }
97
+ async function readCachedSkillsVersion(cacheRoot) {
98
+ const packageJsonPath = path.join(cacheRoot, 'node_modules', '@nocobase', 'skills', 'package.json');
99
+ try {
100
+ const content = await fsp.readFile(packageJsonPath, 'utf8');
101
+ const parsed = JSON.parse(content);
102
+ const version = String(parsed.version ?? '').trim();
103
+ return version || undefined;
104
+ }
105
+ catch {
106
+ return undefined;
107
+ }
108
+ }
109
+ async function prepareLocalSkillsPackage(globalRoot, options = {}, targetVersion) {
110
+ const cacheRoot = getSkillsCacheRoot(globalRoot);
111
+ const packageDir = path.join(cacheRoot, 'node_modules', '@nocobase', 'skills');
112
+ const packageSpec = targetVersion ? `${NOCOBASE_SKILLS_PACKAGE_NAME}@${targetVersion}` : NOCOBASE_SKILLS_PACKAGE_NAME;
113
+ const cachedVersion = await readCachedSkillsVersion(cacheRoot);
114
+ await fsp.mkdir(cacheRoot, { recursive: true });
115
+ if (targetVersion && cachedVersion && compareVersions(cachedVersion, targetVersion) === 0) {
116
+ return {
117
+ packageDir,
118
+ cleanup: async () => undefined,
119
+ };
120
+ }
121
+ await fsp.rm(path.join(cacheRoot, 'node_modules'), { recursive: true, force: true });
122
+ await (options.runFn ?? run)('npm', ['install', '--no-save', '--ignore-scripts', '--no-package-lock', packageSpec], {
123
+ cwd: cacheRoot,
124
+ stdio: options.verbose ? 'inherit' : 'ignore',
125
+ errorName: 'npm install',
126
+ });
127
+ try {
128
+ await fsp.access(packageDir);
129
+ }
130
+ catch {
131
+ throw new Error(`npm install did not produce a local ${NOCOBASE_SKILLS_PACKAGE_NAME} package.`);
132
+ }
133
+ return {
134
+ packageDir,
135
+ cleanup: async () => undefined,
136
+ };
137
+ }
138
+ export async function inspectSkillsStatus(options = {}) {
139
+ const globalRoot = resolveSkillsRoot(options);
140
+ const stateFile = getManagedSkillsStateFile(globalRoot);
141
+ const [installedSkills, managedState] = await Promise.all([
142
+ listGlobalSkills({
143
+ globalRoot,
144
+ commandOutputFn: options.commandOutputFn,
145
+ }),
146
+ readManagedSkillsState(globalRoot),
147
+ ]);
148
+ const installedSkillNames = pickInstalledNocoBaseSkillNames(installedSkills, managedState);
149
+ const managedByNb = managedState?.packageName === NOCOBASE_SKILLS_PACKAGE_NAME;
150
+ let latestVersion;
151
+ let registryError;
152
+ let updateAvailable = installedSkillNames.length > 0 ? null : false;
153
+ if (installedSkillNames.length > 0 || managedByNb) {
154
+ const published = await readPublishedSkillsVersion({
155
+ globalRoot,
156
+ commandOutputFn: options.commandOutputFn,
157
+ });
158
+ latestVersion = published.version;
159
+ registryError = published.error;
160
+ const installedVersion = managedState?.installedVersion ?? managedState?.installedRef;
161
+ if (installedVersion && latestVersion) {
162
+ updateAvailable = compareVersions(latestVersion, installedVersion) > 0;
163
+ }
164
+ }
165
+ const installedVersion = managedState?.installedVersion ?? managedState?.installedRef;
166
+ return {
167
+ globalRoot,
168
+ workspaceRoot: globalRoot,
169
+ stateFile,
170
+ installed: installedSkillNames.length > 0,
171
+ managedByNb,
172
+ sourcePackage: managedState?.sourcePackage ?? NOCOBASE_SKILLS_SOURCE,
173
+ npmPackageName: managedState?.packageName ?? NOCOBASE_SKILLS_PACKAGE_NAME,
174
+ installedSkillNames,
175
+ latestVersion,
176
+ installedVersion,
177
+ latestRef: latestVersion,
178
+ installedRef: installedVersion,
179
+ updateAvailable,
180
+ registryError,
181
+ };
182
+ }
183
+ async function persistManagedSkillsState(globalRoot, options = {}) {
184
+ const installedSkills = await listGlobalSkills({
185
+ globalRoot,
186
+ commandOutputFn: options.commandOutputFn,
187
+ });
188
+ const managedState = await readManagedSkillsState(globalRoot);
189
+ const installedSkillNames = pickInstalledNocoBaseSkillNames(installedSkills, managedState);
190
+ const published = await readPublishedSkillsVersion({
191
+ globalRoot,
192
+ commandOutputFn: options.commandOutputFn,
193
+ });
194
+ const now = new Date().toISOString();
195
+ await writeManagedSkillsState(globalRoot, {
196
+ packageName: NOCOBASE_SKILLS_PACKAGE_NAME,
197
+ sourcePackage: NOCOBASE_SKILLS_SOURCE,
198
+ installedAt: managedState?.installedAt ?? now,
199
+ updatedAt: now,
200
+ installedVersion: published.version,
201
+ skillNames: installedSkillNames,
202
+ });
203
+ return await inspectSkillsStatus({
204
+ globalRoot,
205
+ commandOutputFn: options.commandOutputFn,
206
+ });
207
+ }
208
+ async function reinstallManagedSkills(globalRoot, options = {}, targetVersion) {
209
+ const prepared = await prepareLocalSkillsPackage(globalRoot, options, targetVersion);
210
+ try {
211
+ await (options.runFn ?? run)('npx', ['-y', 'skills', 'add', prepared.packageDir, '-g', '-y'], {
212
+ cwd: globalRoot,
213
+ stdio: options.verbose ? 'inherit' : 'ignore',
214
+ errorName: 'skills add',
215
+ });
216
+ }
217
+ finally {
218
+ await prepared.cleanup();
219
+ }
220
+ }
221
+ export async function installNocoBaseSkills(options = {}) {
222
+ const globalRoot = resolveSkillsRoot(options);
223
+ const status = await inspectSkillsStatus({
224
+ globalRoot,
225
+ commandOutputFn: options.commandOutputFn,
226
+ });
227
+ if (status.installed) {
228
+ return {
229
+ action: 'noop',
230
+ status,
231
+ };
232
+ }
233
+ await ensureSkillsWorkspaceRoot(globalRoot);
234
+ await reinstallManagedSkills(globalRoot, options, status.latestVersion);
235
+ return {
236
+ action: 'installed',
237
+ status: await persistManagedSkillsState(globalRoot, options),
238
+ };
239
+ }
240
+ export async function updateNocoBaseSkills(options = {}) {
241
+ const globalRoot = resolveSkillsRoot(options);
242
+ const status = await inspectSkillsStatus({
243
+ globalRoot,
244
+ commandOutputFn: options.commandOutputFn,
245
+ });
246
+ if (!status.installed) {
247
+ return {
248
+ action: 'noop',
249
+ reason: 'not-installed',
250
+ status,
251
+ };
252
+ }
253
+ if (status.managedByNb
254
+ && status.latestVersion
255
+ && status.installedVersion
256
+ && compareVersions(status.latestVersion, status.installedVersion) <= 0) {
257
+ return {
258
+ action: 'noop',
259
+ reason: 'up-to-date',
260
+ status,
261
+ };
262
+ }
263
+ await reinstallManagedSkills(globalRoot, options, status.latestVersion);
264
+ return {
265
+ action: 'updated',
266
+ status: await persistManagedSkillsState(globalRoot, options),
267
+ };
268
+ }
269
+ export async function removeNocoBaseSkills(options = {}) {
270
+ const globalRoot = resolveSkillsRoot(options);
271
+ const status = await inspectSkillsStatus({
272
+ globalRoot,
273
+ commandOutputFn: options.commandOutputFn,
274
+ });
275
+ if (!status.installed || status.installedSkillNames.length === 0) {
276
+ return {
277
+ action: 'noop',
278
+ status,
279
+ };
280
+ }
281
+ for (const skillName of status.installedSkillNames) {
282
+ await (options.runFn ?? run)('npx', ['-y', 'skills', 'remove', skillName, '-g', '-y'], {
283
+ cwd: globalRoot,
284
+ stdio: options.verbose ? 'inherit' : 'ignore',
285
+ errorName: 'skills remove',
286
+ });
287
+ }
288
+ await fsp.rm(getManagedSkillsStateFile(globalRoot), { force: true });
289
+ return {
290
+ action: 'removed',
291
+ status: await inspectSkillsStatus({
292
+ globalRoot,
293
+ commandOutputFn: options.commandOutputFn,
294
+ }),
295
+ };
296
+ }