bpmax-cli 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +33 -0
- package/dist/bin.js +1258 -0
- package/dist/bin.js.map +1 -0
- package/package.json +39 -0
- package/scripts/install-skills.mjs +128 -0
- package/skills/bpmax-auth/SKILL.md +53 -0
- package/skills/bpmax-cli/SKILL.md +27 -0
- package/skills/bpmax-flow/SKILL.md +39 -0
- package/skills/bpmax-project/SKILL.md +51 -0
- package/skills/bpmax-schema/SKILL.md +32 -0
- package/skills/bpmax-shared/SKILL.md +103 -0
- package/skills/bpmax-user/SKILL.md +31 -0
package/dist/bin.js
ADDED
|
@@ -0,0 +1,1258 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
// src/bin.ts
|
|
4
|
+
import { Command } from "commander";
|
|
5
|
+
|
|
6
|
+
// src/core/config.ts
|
|
7
|
+
import { homedir } from "os";
|
|
8
|
+
import { mkdir, readFile, writeFile } from "fs/promises";
|
|
9
|
+
import path from "path";
|
|
10
|
+
var DEFAULT_WORKSPACE = "default";
|
|
11
|
+
var DEFAULT_LANGUAGE = "zhCN";
|
|
12
|
+
var CONFIG_DIR = path.join(homedir(), ".bpmax");
|
|
13
|
+
var CONFIG_PATH = path.join(CONFIG_DIR, "config.json");
|
|
14
|
+
var defaultConfig = () => ({
|
|
15
|
+
version: 2,
|
|
16
|
+
current_profile: "default",
|
|
17
|
+
profiles: {
|
|
18
|
+
default: {
|
|
19
|
+
origin: "",
|
|
20
|
+
workspace: DEFAULT_WORKSPACE,
|
|
21
|
+
language: DEFAULT_LANGUAGE
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
var normalizeOrigin = (origin) => origin.trim().replace(/\/+$/, "");
|
|
26
|
+
var isV2Config = (input) => typeof input === "object" && input !== null && "version" in input && input.version === 2 && "profiles" in input;
|
|
27
|
+
var toV2Config = (input) => {
|
|
28
|
+
if (isV2Config(input)) {
|
|
29
|
+
return {
|
|
30
|
+
version: 2,
|
|
31
|
+
current_profile: input.current_profile || "default",
|
|
32
|
+
profiles: Object.fromEntries(
|
|
33
|
+
Object.entries(input.profiles || {}).map(([name, profile]) => [
|
|
34
|
+
name,
|
|
35
|
+
{
|
|
36
|
+
...profile,
|
|
37
|
+
origin: normalizeOrigin(profile.origin || ""),
|
|
38
|
+
workspace: profile.workspace || DEFAULT_WORKSPACE,
|
|
39
|
+
language: profile.language || DEFAULT_LANGUAGE
|
|
40
|
+
}
|
|
41
|
+
])
|
|
42
|
+
)
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
const legacy = input;
|
|
46
|
+
const migrated = defaultConfig();
|
|
47
|
+
migrated.profiles.default = {
|
|
48
|
+
origin: normalizeOrigin(legacy.host || ""),
|
|
49
|
+
api_key: legacy.api_key || "",
|
|
50
|
+
workspace: DEFAULT_WORKSPACE,
|
|
51
|
+
language: DEFAULT_LANGUAGE
|
|
52
|
+
};
|
|
53
|
+
return migrated;
|
|
54
|
+
};
|
|
55
|
+
async function loadConfig() {
|
|
56
|
+
try {
|
|
57
|
+
const raw = await readFile(CONFIG_PATH, "utf8");
|
|
58
|
+
const parsed = JSON.parse(raw);
|
|
59
|
+
return toV2Config(parsed);
|
|
60
|
+
} catch (error) {
|
|
61
|
+
if (error.code === "ENOENT") {
|
|
62
|
+
return defaultConfig();
|
|
63
|
+
}
|
|
64
|
+
throw error;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
async function saveConfig(config) {
|
|
68
|
+
await mkdir(CONFIG_DIR, { recursive: true });
|
|
69
|
+
await writeFile(CONFIG_PATH, `${JSON.stringify(config, null, 2)}
|
|
70
|
+
`, "utf8");
|
|
71
|
+
}
|
|
72
|
+
function getConfigPath() {
|
|
73
|
+
return CONFIG_PATH;
|
|
74
|
+
}
|
|
75
|
+
function getConfigDir() {
|
|
76
|
+
return CONFIG_DIR;
|
|
77
|
+
}
|
|
78
|
+
function maskApiKey(apiKey) {
|
|
79
|
+
if (!apiKey) {
|
|
80
|
+
return "(unset)";
|
|
81
|
+
}
|
|
82
|
+
if (apiKey.length <= 10) {
|
|
83
|
+
return apiKey;
|
|
84
|
+
}
|
|
85
|
+
return `${apiKey.slice(0, 8)}****${apiKey.slice(-4)}`;
|
|
86
|
+
}
|
|
87
|
+
function ensureProfile(config, profileName) {
|
|
88
|
+
const profile = config.profiles[profileName];
|
|
89
|
+
if (!profile) {
|
|
90
|
+
throw new Error(`Profile "${profileName}" not found.`);
|
|
91
|
+
}
|
|
92
|
+
return profile;
|
|
93
|
+
}
|
|
94
|
+
function resolveProfile(config, profileName) {
|
|
95
|
+
const name = profileName || config.current_profile || "default";
|
|
96
|
+
const profile = ensureProfile(config, name);
|
|
97
|
+
if (!profile.origin) {
|
|
98
|
+
throw new Error(`Profile "${name}" has no origin configured.`);
|
|
99
|
+
}
|
|
100
|
+
return {
|
|
101
|
+
name,
|
|
102
|
+
config: {
|
|
103
|
+
origin: normalizeOrigin(profile.origin),
|
|
104
|
+
api_key: profile.api_key,
|
|
105
|
+
workspace: profile.workspace || DEFAULT_WORKSPACE,
|
|
106
|
+
language: profile.language || DEFAULT_LANGUAGE,
|
|
107
|
+
user: profile.user
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
async function upsertProfile(name, nextProfile) {
|
|
112
|
+
const config = await loadConfig();
|
|
113
|
+
config.profiles[name] = {
|
|
114
|
+
...config.profiles[name],
|
|
115
|
+
...nextProfile,
|
|
116
|
+
origin: normalizeOrigin(nextProfile.origin || config.profiles[name]?.origin || ""),
|
|
117
|
+
workspace: nextProfile.workspace || config.profiles[name]?.workspace || DEFAULT_WORKSPACE,
|
|
118
|
+
language: nextProfile.language || config.profiles[name]?.language || DEFAULT_LANGUAGE
|
|
119
|
+
};
|
|
120
|
+
if (!config.current_profile) {
|
|
121
|
+
config.current_profile = name;
|
|
122
|
+
}
|
|
123
|
+
await saveConfig(config);
|
|
124
|
+
return config;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// src/core/http.ts
|
|
128
|
+
import axios, { AxiosHeaders } from "axios";
|
|
129
|
+
var ensureApiPath = (path4) => {
|
|
130
|
+
if (!path4.startsWith("/")) {
|
|
131
|
+
path4 = `/${path4}`;
|
|
132
|
+
}
|
|
133
|
+
if (path4.startsWith("/api/")) {
|
|
134
|
+
return path4;
|
|
135
|
+
}
|
|
136
|
+
return `/api${path4}`;
|
|
137
|
+
};
|
|
138
|
+
function createApiClient(profile) {
|
|
139
|
+
const instance = axios.create({
|
|
140
|
+
baseURL: profile.config.origin,
|
|
141
|
+
withCredentials: false
|
|
142
|
+
});
|
|
143
|
+
instance.interceptors.request.use((config) => {
|
|
144
|
+
config.params = {
|
|
145
|
+
...config.params || {},
|
|
146
|
+
$workspace: config.params?.$workspace ?? profile.config.workspace,
|
|
147
|
+
$language: config.params?.$language ?? profile.config.language
|
|
148
|
+
};
|
|
149
|
+
if (profile.config.api_key) {
|
|
150
|
+
const headers = AxiosHeaders.from(config.headers || {});
|
|
151
|
+
headers.set("Authorization", `Bearer ${profile.config.api_key}`);
|
|
152
|
+
config.headers = headers;
|
|
153
|
+
}
|
|
154
|
+
return config;
|
|
155
|
+
});
|
|
156
|
+
return instance;
|
|
157
|
+
}
|
|
158
|
+
async function apiRequest(client, method, path4, options = {}) {
|
|
159
|
+
const response = await client.request({
|
|
160
|
+
method,
|
|
161
|
+
url: ensureApiPath(path4),
|
|
162
|
+
params: options.params,
|
|
163
|
+
data: options.data
|
|
164
|
+
});
|
|
165
|
+
const body = response.data;
|
|
166
|
+
if (body.code !== 0) {
|
|
167
|
+
throw new Error(body.msg || `Request failed with code ${body.code}`);
|
|
168
|
+
}
|
|
169
|
+
return body.data ?? body;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
// src/core/output.ts
|
|
173
|
+
var noticeProvider = null;
|
|
174
|
+
var isRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
175
|
+
function setNoticeProvider(provider) {
|
|
176
|
+
noticeProvider = provider;
|
|
177
|
+
}
|
|
178
|
+
function currentNotice() {
|
|
179
|
+
return noticeProvider?.() || null;
|
|
180
|
+
}
|
|
181
|
+
function withJsonNotice(value) {
|
|
182
|
+
const notice = currentNotice();
|
|
183
|
+
if (!notice || Object.keys(notice).length === 0) {
|
|
184
|
+
return value;
|
|
185
|
+
}
|
|
186
|
+
if (isRecord(value)) {
|
|
187
|
+
return { ...value, _notice: notice };
|
|
188
|
+
}
|
|
189
|
+
return value;
|
|
190
|
+
}
|
|
191
|
+
function emitTextNotice() {
|
|
192
|
+
const notice = currentNotice();
|
|
193
|
+
if (!notice) {
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
Object.values(notice).forEach((entry) => {
|
|
197
|
+
if (typeof entry.message === "string") {
|
|
198
|
+
console.error(entry.message);
|
|
199
|
+
}
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
function printResult(format, value) {
|
|
203
|
+
switch (format) {
|
|
204
|
+
case "json":
|
|
205
|
+
console.log(JSON.stringify(withJsonNotice(value), null, 2));
|
|
206
|
+
return;
|
|
207
|
+
case "ndjson":
|
|
208
|
+
if (Array.isArray(value)) {
|
|
209
|
+
value.forEach((item) => {
|
|
210
|
+
console.log(JSON.stringify(item));
|
|
211
|
+
});
|
|
212
|
+
} else {
|
|
213
|
+
console.log(JSON.stringify(value));
|
|
214
|
+
}
|
|
215
|
+
return;
|
|
216
|
+
case "table":
|
|
217
|
+
if (Array.isArray(value)) {
|
|
218
|
+
console.table(value);
|
|
219
|
+
} else if (isRecord(value)) {
|
|
220
|
+
console.table([value]);
|
|
221
|
+
} else {
|
|
222
|
+
console.log(value);
|
|
223
|
+
}
|
|
224
|
+
return;
|
|
225
|
+
case "pretty":
|
|
226
|
+
default:
|
|
227
|
+
emitTextNotice();
|
|
228
|
+
if (typeof value === "string") {
|
|
229
|
+
console.log(value);
|
|
230
|
+
return;
|
|
231
|
+
}
|
|
232
|
+
console.log(JSON.stringify(value, null, 2));
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
// src/commands/api.ts
|
|
237
|
+
function parseJson(input) {
|
|
238
|
+
if (!input) {
|
|
239
|
+
return void 0;
|
|
240
|
+
}
|
|
241
|
+
return JSON.parse(input);
|
|
242
|
+
}
|
|
243
|
+
function registerApiCommands(program2) {
|
|
244
|
+
program2.command("api <method> <path>").description("Call an arbitrary BPMAX /api endpoint").option("--profile <profile>", "Profile name").option("--params <json>", "Query params JSON").option("--data <json>", "Request body JSON").option("--format <format>", "Output format", "json").action(async (method, path4, options) => {
|
|
245
|
+
const config = await loadConfig();
|
|
246
|
+
const profile = resolveProfile(config, options.profile);
|
|
247
|
+
const result = await apiRequest(createApiClient(profile), method, path4, {
|
|
248
|
+
params: parseJson(options.params),
|
|
249
|
+
data: parseJson(options.data)
|
|
250
|
+
});
|
|
251
|
+
printResult(options.format, result);
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
// src/core/auth.ts
|
|
256
|
+
import { createServer } from "http";
|
|
257
|
+
import { randomBytes } from "crypto";
|
|
258
|
+
import open from "open";
|
|
259
|
+
function buildBridgeUrl(origin, workspace, params) {
|
|
260
|
+
const query = new URLSearchParams(params);
|
|
261
|
+
return `${origin}/${encodeURIComponent(workspace)}/cli-login?${query.toString()}`;
|
|
262
|
+
}
|
|
263
|
+
async function runBrowserLogin(options) {
|
|
264
|
+
const state = randomBytes(16).toString("hex");
|
|
265
|
+
const timeoutMs = options.timeoutMs ?? 5 * 60 * 1e3;
|
|
266
|
+
return await new Promise((resolve, reject) => {
|
|
267
|
+
const server = createServer(async (req, res) => {
|
|
268
|
+
res.setHeader("Access-Control-Allow-Origin", "*");
|
|
269
|
+
res.setHeader("Access-Control-Allow-Methods", "POST, OPTIONS");
|
|
270
|
+
res.setHeader("Access-Control-Allow-Headers", "Content-Type");
|
|
271
|
+
if (req.method === "OPTIONS") {
|
|
272
|
+
res.statusCode = 204;
|
|
273
|
+
res.end();
|
|
274
|
+
return;
|
|
275
|
+
}
|
|
276
|
+
if (req.method !== "POST" || req.url !== "/callback") {
|
|
277
|
+
res.statusCode = 404;
|
|
278
|
+
res.end(JSON.stringify({ ok: false, message: "Not found" }));
|
|
279
|
+
return;
|
|
280
|
+
}
|
|
281
|
+
let raw = "";
|
|
282
|
+
req.on("data", (chunk) => {
|
|
283
|
+
raw += chunk.toString();
|
|
284
|
+
});
|
|
285
|
+
req.on("end", () => {
|
|
286
|
+
try {
|
|
287
|
+
const payload = JSON.parse(raw);
|
|
288
|
+
if (payload.state !== state) {
|
|
289
|
+
res.statusCode = 400;
|
|
290
|
+
res.end(JSON.stringify({ ok: false, message: "Invalid state" }));
|
|
291
|
+
return;
|
|
292
|
+
}
|
|
293
|
+
if (!payload.api_key) {
|
|
294
|
+
res.statusCode = 400;
|
|
295
|
+
res.end(JSON.stringify({ ok: false, message: "Missing api_key" }));
|
|
296
|
+
return;
|
|
297
|
+
}
|
|
298
|
+
res.statusCode = 200;
|
|
299
|
+
res.end(JSON.stringify({ ok: true }));
|
|
300
|
+
clearTimeout(timer);
|
|
301
|
+
server.close();
|
|
302
|
+
resolve({ apiKey: payload.api_key, user: payload.user, state });
|
|
303
|
+
} catch (error) {
|
|
304
|
+
res.statusCode = 400;
|
|
305
|
+
res.end(
|
|
306
|
+
JSON.stringify({
|
|
307
|
+
ok: false,
|
|
308
|
+
message: error instanceof Error ? error.message : "Invalid callback payload"
|
|
309
|
+
})
|
|
310
|
+
);
|
|
311
|
+
}
|
|
312
|
+
});
|
|
313
|
+
req.on("error", reject);
|
|
314
|
+
});
|
|
315
|
+
server.listen(0, "127.0.0.1", async () => {
|
|
316
|
+
const address = server.address();
|
|
317
|
+
if (!address || typeof address === "string") {
|
|
318
|
+
reject(new Error("Failed to allocate local callback port."));
|
|
319
|
+
return;
|
|
320
|
+
}
|
|
321
|
+
const callbackUrl = `http://127.0.0.1:${address.port}/callback`;
|
|
322
|
+
const bridgeUrl = buildBridgeUrl(options.origin, options.workspace, {
|
|
323
|
+
callback: callbackUrl,
|
|
324
|
+
state,
|
|
325
|
+
profile: options.profileName,
|
|
326
|
+
key_name: options.apiKeyName
|
|
327
|
+
});
|
|
328
|
+
await open(bridgeUrl);
|
|
329
|
+
});
|
|
330
|
+
server.on("error", reject);
|
|
331
|
+
const timer = setTimeout(() => {
|
|
332
|
+
server.close();
|
|
333
|
+
reject(new Error("Timed out waiting for browser login callback."));
|
|
334
|
+
}, timeoutMs);
|
|
335
|
+
});
|
|
336
|
+
}
|
|
337
|
+
function updateProfileAuth(profile, result) {
|
|
338
|
+
return {
|
|
339
|
+
...profile,
|
|
340
|
+
api_key: result.apiKey,
|
|
341
|
+
user: result.user
|
|
342
|
+
};
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
// src/commands/auth.ts
|
|
346
|
+
function registerAuthCommands(program2) {
|
|
347
|
+
const auth = program2.command("auth").description("Authenticate and manage API key state");
|
|
348
|
+
auth.command("login").description("Open the hpt-frontend bridge page and persist the returned API key").option("--profile <profile>", "Profile name").option("--key-name <keyName>", "Target API key name", "bpmax-cli").action(async (options) => {
|
|
349
|
+
const config = await loadConfig();
|
|
350
|
+
const profile = resolveProfile(config, options.profile);
|
|
351
|
+
const result = await runBrowserLogin({
|
|
352
|
+
origin: profile.config.origin,
|
|
353
|
+
workspace: profile.config.workspace,
|
|
354
|
+
profileName: profile.name,
|
|
355
|
+
apiKeyName: options.keyName
|
|
356
|
+
});
|
|
357
|
+
config.profiles[profile.name] = updateProfileAuth(config.profiles[profile.name], result);
|
|
358
|
+
await saveConfig(config);
|
|
359
|
+
console.log(`Saved API key for profile "${profile.name}".`);
|
|
360
|
+
});
|
|
361
|
+
auth.command("status").description("Show saved auth state and validate /api/user/current").option("--profile <profile>", "Profile name").option("--format <format>", "Output format", "pretty").action(async (options) => {
|
|
362
|
+
const config = await loadConfig();
|
|
363
|
+
const profile = resolveProfile(config, options.profile);
|
|
364
|
+
const output = {
|
|
365
|
+
profile: profile.name,
|
|
366
|
+
origin: profile.config.origin,
|
|
367
|
+
workspace: profile.config.workspace,
|
|
368
|
+
language: profile.config.language,
|
|
369
|
+
api_key: maskApiKey(profile.config.api_key)
|
|
370
|
+
};
|
|
371
|
+
if (profile.config.api_key) {
|
|
372
|
+
try {
|
|
373
|
+
const currentUser = await apiRequest(
|
|
374
|
+
createApiClient(profile),
|
|
375
|
+
"GET",
|
|
376
|
+
"/user/current"
|
|
377
|
+
);
|
|
378
|
+
output.user = currentUser;
|
|
379
|
+
output.authenticated = true;
|
|
380
|
+
} catch (error) {
|
|
381
|
+
output.authenticated = false;
|
|
382
|
+
output.error = error instanceof Error ? error.message : String(error);
|
|
383
|
+
}
|
|
384
|
+
} else {
|
|
385
|
+
output.authenticated = false;
|
|
386
|
+
}
|
|
387
|
+
printResult(options.format, output);
|
|
388
|
+
});
|
|
389
|
+
auth.command("logout").description("Clear the stored API key for the selected profile").option("--profile <profile>", "Profile name").action(async (options) => {
|
|
390
|
+
const config = await loadConfig();
|
|
391
|
+
const profile = resolveProfile(config, options.profile);
|
|
392
|
+
config.profiles[profile.name] = {
|
|
393
|
+
...config.profiles[profile.name],
|
|
394
|
+
api_key: "",
|
|
395
|
+
user: void 0
|
|
396
|
+
};
|
|
397
|
+
await saveConfig(config);
|
|
398
|
+
console.log(`Cleared API key for profile "${profile.name}".`);
|
|
399
|
+
});
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
// src/commands/config.ts
|
|
403
|
+
import { text } from "@clack/prompts";
|
|
404
|
+
async function ensureValue(current, label, placeholder) {
|
|
405
|
+
if (current) {
|
|
406
|
+
return current;
|
|
407
|
+
}
|
|
408
|
+
const result = await text({
|
|
409
|
+
message: label,
|
|
410
|
+
placeholder
|
|
411
|
+
});
|
|
412
|
+
if (typeof result !== "string" || !result.trim()) {
|
|
413
|
+
throw new Error(`${label} is required.`);
|
|
414
|
+
}
|
|
415
|
+
return result.trim();
|
|
416
|
+
}
|
|
417
|
+
function registerConfigCommands(program2) {
|
|
418
|
+
const config = program2.command("config").description("Manage local bpmax-cli profiles");
|
|
419
|
+
const profile = config.command("profile").description("Manage named environment profiles");
|
|
420
|
+
profile.command("add").description("Add or update a profile").option("--name <name>", "Profile name").option("--origin <origin>", "Profile origin").option("--api-key <apiKey>", "API key").option("--workspace <workspace>", "Default workspace", DEFAULT_WORKSPACE).option("--language <language>", "Default language", DEFAULT_LANGUAGE).action(async (options) => {
|
|
421
|
+
const name = await ensureValue(options.name, "Profile name", "default");
|
|
422
|
+
const origin = await ensureValue(options.origin, "Origin", "https://example.com");
|
|
423
|
+
await upsertProfile(name, {
|
|
424
|
+
origin,
|
|
425
|
+
api_key: options.apiKey,
|
|
426
|
+
workspace: options.workspace,
|
|
427
|
+
language: options.language
|
|
428
|
+
});
|
|
429
|
+
console.log(`Saved profile "${name}" to ${getConfigPath()}.`);
|
|
430
|
+
});
|
|
431
|
+
profile.command("list").description("List profiles").option("--format <format>", "Output format", "table").action(async (options) => {
|
|
432
|
+
const configData = await loadConfig();
|
|
433
|
+
const rows = Object.entries(configData.profiles).map(([name, profileConfig]) => ({
|
|
434
|
+
name,
|
|
435
|
+
current: configData.current_profile === name,
|
|
436
|
+
origin: profileConfig.origin,
|
|
437
|
+
workspace: profileConfig.workspace || DEFAULT_WORKSPACE,
|
|
438
|
+
language: profileConfig.language || DEFAULT_LANGUAGE,
|
|
439
|
+
api_key: maskApiKey(profileConfig.api_key)
|
|
440
|
+
}));
|
|
441
|
+
printResult(options.format, rows);
|
|
442
|
+
});
|
|
443
|
+
profile.command("use <name>").description("Set the active profile").action(async (name) => {
|
|
444
|
+
const configData = await loadConfig();
|
|
445
|
+
if (!configData.profiles[name]) {
|
|
446
|
+
throw new Error(`Profile "${name}" not found.`);
|
|
447
|
+
}
|
|
448
|
+
configData.current_profile = name;
|
|
449
|
+
await saveConfig(configData);
|
|
450
|
+
console.log(`Current profile set to "${name}".`);
|
|
451
|
+
});
|
|
452
|
+
profile.command("remove <name>").description("Remove a profile").action(async (name) => {
|
|
453
|
+
const configData = await loadConfig();
|
|
454
|
+
if (!configData.profiles[name]) {
|
|
455
|
+
throw new Error(`Profile "${name}" not found.`);
|
|
456
|
+
}
|
|
457
|
+
delete configData.profiles[name];
|
|
458
|
+
if (configData.current_profile === name) {
|
|
459
|
+
const [nextProfile] = Object.keys(configData.profiles);
|
|
460
|
+
configData.current_profile = nextProfile || "default";
|
|
461
|
+
}
|
|
462
|
+
await saveConfig(configData);
|
|
463
|
+
console.log(`Removed profile "${name}".`);
|
|
464
|
+
});
|
|
465
|
+
profile.command("show").description("Show a profile").option("--profile <profile>", "Profile name").option("--format <format>", "Output format", "pretty").action(async (options) => {
|
|
466
|
+
const configData = await loadConfig();
|
|
467
|
+
const name = options.profile || configData.current_profile;
|
|
468
|
+
const target = configData.profiles[name];
|
|
469
|
+
if (!target) {
|
|
470
|
+
throw new Error(`Profile "${name}" not found.`);
|
|
471
|
+
}
|
|
472
|
+
printResult(options.format, {
|
|
473
|
+
name,
|
|
474
|
+
config_path: getConfigPath(),
|
|
475
|
+
origin: target.origin,
|
|
476
|
+
workspace: target.workspace || DEFAULT_WORKSPACE,
|
|
477
|
+
language: target.language || DEFAULT_LANGUAGE,
|
|
478
|
+
api_key: maskApiKey(target.api_key),
|
|
479
|
+
user: target.user || null
|
|
480
|
+
});
|
|
481
|
+
});
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
// src/commands/flow.ts
|
|
485
|
+
function registerFlowCommands(program2) {
|
|
486
|
+
const flow = program2.command("flow").description("Flow commands");
|
|
487
|
+
flow.command("list").description("List flows").option("--profile <profile>", "Profile name").option("--draft <draft>", "Draft flag", "1").option("--format <format>", "Output format", "table").action(async (options) => {
|
|
488
|
+
const config = await loadConfig();
|
|
489
|
+
const profile = resolveProfile(config, options.profile);
|
|
490
|
+
const result = await apiRequest(
|
|
491
|
+
createApiClient(profile),
|
|
492
|
+
"GET",
|
|
493
|
+
"/flow/flow_type",
|
|
494
|
+
{
|
|
495
|
+
params: {
|
|
496
|
+
draft: options.draft,
|
|
497
|
+
skip_i18n: "true"
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
);
|
|
501
|
+
printResult(options.format, result);
|
|
502
|
+
});
|
|
503
|
+
flow.command("options").description("List flow options").option("--profile <profile>", "Profile name").option("--name <name>", "Flow name filter").option("--format <format>", "Output format", "table").action(async (options) => {
|
|
504
|
+
const config = await loadConfig();
|
|
505
|
+
const profile = resolveProfile(config, options.profile);
|
|
506
|
+
const result = await apiRequest(
|
|
507
|
+
createApiClient(profile),
|
|
508
|
+
"GET",
|
|
509
|
+
"/flow/options",
|
|
510
|
+
{
|
|
511
|
+
params: options.name ? { name: options.name } : void 0
|
|
512
|
+
}
|
|
513
|
+
);
|
|
514
|
+
printResult(options.format, result);
|
|
515
|
+
});
|
|
516
|
+
flow.command("get").description("Get flow detail by id").requiredOption("--id <id>", "Flow id").option("--profile <profile>", "Profile name").option("--format <format>", "Output format", "pretty").action(async (options) => {
|
|
517
|
+
const config = await loadConfig();
|
|
518
|
+
const profile = resolveProfile(config, options.profile);
|
|
519
|
+
const result = await apiRequest(
|
|
520
|
+
createApiClient(profile),
|
|
521
|
+
"POST",
|
|
522
|
+
"/flow/batch_get",
|
|
523
|
+
{
|
|
524
|
+
data: {
|
|
525
|
+
ids: [Number(options.id)]
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
);
|
|
529
|
+
printResult(options.format, result[0] ?? null);
|
|
530
|
+
});
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
// src/commands/project.ts
|
|
534
|
+
function parseJson2(input) {
|
|
535
|
+
if (!input) {
|
|
536
|
+
return {};
|
|
537
|
+
}
|
|
538
|
+
return JSON.parse(input);
|
|
539
|
+
}
|
|
540
|
+
async function resolveFlowIdentifiers(client, options) {
|
|
541
|
+
if (options.flowType && options.flowKey !== void 0) {
|
|
542
|
+
return {
|
|
543
|
+
flow_type: options.flowType,
|
|
544
|
+
flow_key: options.flowKey
|
|
545
|
+
};
|
|
546
|
+
}
|
|
547
|
+
if (!options.flowId) {
|
|
548
|
+
throw new Error("Either --flow-id or both --flow-type and --flow-key are required.");
|
|
549
|
+
}
|
|
550
|
+
const flows = await apiRequest(
|
|
551
|
+
client,
|
|
552
|
+
"POST",
|
|
553
|
+
"/flow/batch_get",
|
|
554
|
+
{
|
|
555
|
+
data: { ids: [Number(options.flowId)] }
|
|
556
|
+
}
|
|
557
|
+
);
|
|
558
|
+
const flow = flows[0];
|
|
559
|
+
if (!flow) {
|
|
560
|
+
throw new Error(`Flow ${options.flowId} not found.`);
|
|
561
|
+
}
|
|
562
|
+
return {
|
|
563
|
+
flow_type: String(flow.flow_type || "default"),
|
|
564
|
+
flow_key: String(flow.key || "")
|
|
565
|
+
};
|
|
566
|
+
}
|
|
567
|
+
function registerProjectCommands(program2) {
|
|
568
|
+
const project = program2.command("project").description("Project commands");
|
|
569
|
+
project.command("list").description("List projects").option("--profile <profile>", "Profile name").option("--page <page>", "Page number", "1").option("--page-size <pageSize>", "Page size", "20").option("--body <json>", "Project list body JSON", "{}").option("--format <format>", "Output format", "table").action(async (options) => {
|
|
570
|
+
const config = await loadConfig();
|
|
571
|
+
const profile = resolveProfile(config, options.profile);
|
|
572
|
+
const result = await apiRequest(
|
|
573
|
+
createApiClient(profile),
|
|
574
|
+
"POST",
|
|
575
|
+
"/project/list",
|
|
576
|
+
{
|
|
577
|
+
params: {
|
|
578
|
+
page: Number(options.page),
|
|
579
|
+
page_size: Number(options.pageSize)
|
|
580
|
+
},
|
|
581
|
+
data: parseJson2(options.body)
|
|
582
|
+
}
|
|
583
|
+
);
|
|
584
|
+
printResult(options.format, result);
|
|
585
|
+
});
|
|
586
|
+
project.command("get <id>").description("Get project detail").option("--profile <profile>", "Profile name").option("--format <format>", "Output format", "pretty").action(async (id, options) => {
|
|
587
|
+
const config = await loadConfig();
|
|
588
|
+
const profile = resolveProfile(config, options.profile);
|
|
589
|
+
const result = await apiRequest(
|
|
590
|
+
createApiClient(profile),
|
|
591
|
+
"GET",
|
|
592
|
+
`/project/${id}`
|
|
593
|
+
);
|
|
594
|
+
printResult(options.format, result);
|
|
595
|
+
});
|
|
596
|
+
project.command("my-step").description("List my pending project steps").option("--profile <profile>", "Profile name").option("--finished <finished>", "Finished flag", "0").option("--page <page>", "Page number", "1").option("--page-size <pageSize>", "Page size", "20").option("--format <format>", "Output format", "table").action(async (options) => {
|
|
597
|
+
const config = await loadConfig();
|
|
598
|
+
const profile = resolveProfile(config, options.profile);
|
|
599
|
+
const result = await apiRequest(
|
|
600
|
+
createApiClient(profile),
|
|
601
|
+
"GET",
|
|
602
|
+
"/project/my_step",
|
|
603
|
+
{
|
|
604
|
+
params: {
|
|
605
|
+
finished: options.finished,
|
|
606
|
+
page: Number(options.page),
|
|
607
|
+
page_size: Number(options.pageSize)
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
);
|
|
611
|
+
printResult(options.format, result);
|
|
612
|
+
});
|
|
613
|
+
project.command("create").description("Minimal project create shortcut").option("--profile <profile>", "Profile name").option("--flow-id <flowId>", "Flow id").option("--flow-type <flowType>", "Flow type").option("--flow-key <flowKey>", "Flow key").option("--name <name>", "Project name").option("--json <json>", "Additional request JSON").option("--body <body>", "Full request body JSON override").option("--format <format>", "Output format", "pretty").action(async (options) => {
|
|
614
|
+
const config = await loadConfig();
|
|
615
|
+
const profile = resolveProfile(config, options.profile);
|
|
616
|
+
const client = createApiClient(profile);
|
|
617
|
+
let payload = parseJson2(options.body);
|
|
618
|
+
if (!options.body) {
|
|
619
|
+
if (!options.name) {
|
|
620
|
+
throw new Error("--name is required unless --body is provided.");
|
|
621
|
+
}
|
|
622
|
+
const flow = await resolveFlowIdentifiers(client, options);
|
|
623
|
+
const extra = parseJson2(options.json);
|
|
624
|
+
const form = typeof extra.form === "object" && extra.form !== null && !Array.isArray(extra.form) ? extra.form : {};
|
|
625
|
+
payload = {
|
|
626
|
+
...extra,
|
|
627
|
+
...flow,
|
|
628
|
+
name: options.name,
|
|
629
|
+
workspace: profile.config.workspace,
|
|
630
|
+
form
|
|
631
|
+
};
|
|
632
|
+
}
|
|
633
|
+
const result = await apiRequest(
|
|
634
|
+
client,
|
|
635
|
+
"POST",
|
|
636
|
+
"/project/open-api/create",
|
|
637
|
+
{
|
|
638
|
+
data: payload
|
|
639
|
+
}
|
|
640
|
+
);
|
|
641
|
+
printResult(options.format, result);
|
|
642
|
+
});
|
|
643
|
+
}
|
|
644
|
+
|
|
645
|
+
// src/core/manifest.ts
|
|
646
|
+
var schemaEntries = [
|
|
647
|
+
{ name: "auth.login", description: "Open hpt-frontend bridge page and persist API key locally." },
|
|
648
|
+
{ name: "auth.status", description: "Show current profile status and validate /api/user/current." },
|
|
649
|
+
{ name: "auth.logout", description: "Clear the saved API key for the selected profile." },
|
|
650
|
+
{ name: "user.current", description: "Get current user.", method: "GET", path: "/api/user/current" },
|
|
651
|
+
{ name: "flow.list", description: "List flows.", method: "GET", path: "/api/flow/flow_type" },
|
|
652
|
+
{ name: "flow.options", description: "List flow options.", method: "GET", path: "/api/flow/options" },
|
|
653
|
+
{ name: "flow.get", description: "Fetch flow details by id.", method: "POST", path: "/api/flow/batch_get" },
|
|
654
|
+
{ name: "project.list", description: "List projects.", method: "POST", path: "/api/project/list" },
|
|
655
|
+
{ name: "project.get", description: "Get project detail.", method: "GET", path: "/api/project/:id" },
|
|
656
|
+
{ name: "project.my-step", description: "List my pending steps.", method: "GET", path: "/api/project/my_step" },
|
|
657
|
+
{ name: "project.create", description: "Minimal project create shortcut.", method: "POST", path: "/api/project/open-api/create" }
|
|
658
|
+
];
|
|
659
|
+
|
|
660
|
+
// src/commands/schema.ts
|
|
661
|
+
function registerSchemaCommands(program2) {
|
|
662
|
+
const schema = program2.command("schema").description("Inspect built-in bpmax-cli schema metadata");
|
|
663
|
+
schema.command("list").description("List known command and endpoint mappings").option("--format <format>", "Output format", "table").action((options) => {
|
|
664
|
+
printResult(options.format, schemaEntries);
|
|
665
|
+
});
|
|
666
|
+
schema.command("show <name>").description("Show a single schema entry").option("--format <format>", "Output format", "pretty").action((name, options) => {
|
|
667
|
+
const entry = schemaEntries.find((item) => item.name === name);
|
|
668
|
+
if (!entry) {
|
|
669
|
+
throw new Error(`Schema entry "${name}" not found.`);
|
|
670
|
+
}
|
|
671
|
+
printResult(options.format, entry);
|
|
672
|
+
});
|
|
673
|
+
}
|
|
674
|
+
|
|
675
|
+
// package.json
|
|
676
|
+
var package_default = {
|
|
677
|
+
name: "bpmax-cli",
|
|
678
|
+
version: "0.1.0",
|
|
679
|
+
description: "CLI for BPMAX environments with multi-profile config and local login callback flow",
|
|
680
|
+
type: "module",
|
|
681
|
+
bin: {
|
|
682
|
+
"bpmax-cli": "dist/bin.js"
|
|
683
|
+
},
|
|
684
|
+
files: [
|
|
685
|
+
"dist",
|
|
686
|
+
"scripts/install-skills.mjs",
|
|
687
|
+
"skills",
|
|
688
|
+
"README.md"
|
|
689
|
+
],
|
|
690
|
+
scripts: {
|
|
691
|
+
postinstall: "node scripts/install-skills.mjs",
|
|
692
|
+
build: "tsup",
|
|
693
|
+
dev: "tsx src/bin.ts",
|
|
694
|
+
"install:skills": "node scripts/install-skills.mjs",
|
|
695
|
+
test: "vitest run",
|
|
696
|
+
typecheck: "tsc --noEmit"
|
|
697
|
+
},
|
|
698
|
+
engines: {
|
|
699
|
+
node: ">=18"
|
|
700
|
+
},
|
|
701
|
+
dependencies: {
|
|
702
|
+
"@clack/prompts": "^1.2.0",
|
|
703
|
+
axios: "^1.9.0",
|
|
704
|
+
commander: "^14.0.0",
|
|
705
|
+
open: "^10.1.2"
|
|
706
|
+
},
|
|
707
|
+
devDependencies: {
|
|
708
|
+
"@types/node": "^22.15.17",
|
|
709
|
+
tsup: "^8.5.0",
|
|
710
|
+
tsx: "^4.19.4",
|
|
711
|
+
typescript: "^5.8.3",
|
|
712
|
+
vitest: "^3.1.3"
|
|
713
|
+
}
|
|
714
|
+
};
|
|
715
|
+
|
|
716
|
+
// src/core/build.ts
|
|
717
|
+
var CLI_NAME = "bpmax-cli";
|
|
718
|
+
var VERSION = package_default.version;
|
|
719
|
+
var NPM_PACKAGE = "bpmax-cli";
|
|
720
|
+
|
|
721
|
+
// src/core/skills.ts
|
|
722
|
+
import { mkdir as mkdir2, readFile as readFile2, writeFile as writeFile2 } from "fs/promises";
|
|
723
|
+
import path2 from "path";
|
|
724
|
+
import { fileURLToPath } from "url";
|
|
725
|
+
|
|
726
|
+
// src/core/process.ts
|
|
727
|
+
import { spawn } from "child_process";
|
|
728
|
+
var runCommand = (command, args, options = {}) => new Promise((resolve) => {
|
|
729
|
+
const child = spawn(command, args, {
|
|
730
|
+
cwd: options.cwd,
|
|
731
|
+
env: options.env,
|
|
732
|
+
stdio: ["ignore", "pipe", "pipe"]
|
|
733
|
+
});
|
|
734
|
+
let stdout = "";
|
|
735
|
+
let stderr = "";
|
|
736
|
+
let settled = false;
|
|
737
|
+
const timer = options.timeoutMs && options.timeoutMs > 0 ? setTimeout(() => {
|
|
738
|
+
if (settled) {
|
|
739
|
+
return;
|
|
740
|
+
}
|
|
741
|
+
settled = true;
|
|
742
|
+
child.kill("SIGTERM");
|
|
743
|
+
resolve({
|
|
744
|
+
stdout,
|
|
745
|
+
stderr: `${stderr}${stderr ? "\n" : ""}Timed out after ${options.timeoutMs}ms`,
|
|
746
|
+
exitCode: 124
|
|
747
|
+
});
|
|
748
|
+
}, options.timeoutMs) : void 0;
|
|
749
|
+
child.stdout?.on("data", (chunk) => {
|
|
750
|
+
stdout += String(chunk);
|
|
751
|
+
});
|
|
752
|
+
child.stderr?.on("data", (chunk) => {
|
|
753
|
+
stderr += String(chunk);
|
|
754
|
+
});
|
|
755
|
+
child.on("error", (error) => {
|
|
756
|
+
if (settled) {
|
|
757
|
+
return;
|
|
758
|
+
}
|
|
759
|
+
settled = true;
|
|
760
|
+
if (timer) {
|
|
761
|
+
clearTimeout(timer);
|
|
762
|
+
}
|
|
763
|
+
resolve({ stdout, stderr: `${stderr}${stderr ? "\n" : ""}${error.message}`, exitCode: 1 });
|
|
764
|
+
});
|
|
765
|
+
child.on("close", (code) => {
|
|
766
|
+
if (settled) {
|
|
767
|
+
return;
|
|
768
|
+
}
|
|
769
|
+
settled = true;
|
|
770
|
+
if (timer) {
|
|
771
|
+
clearTimeout(timer);
|
|
772
|
+
}
|
|
773
|
+
resolve({ stdout, stderr, exitCode: code ?? 1 });
|
|
774
|
+
});
|
|
775
|
+
});
|
|
776
|
+
|
|
777
|
+
// src/core/skills.ts
|
|
778
|
+
var SKILLS_STAMP_FILE = "skills.stamp";
|
|
779
|
+
var SKILLS_INSTALL_TIMEOUT_MS = 2 * 60 * 1e3;
|
|
780
|
+
function stampPath(configDir = getConfigDir()) {
|
|
781
|
+
return path2.join(configDir, SKILLS_STAMP_FILE);
|
|
782
|
+
}
|
|
783
|
+
async function readSkillsStamp(configDir = getConfigDir()) {
|
|
784
|
+
try {
|
|
785
|
+
return (await readFile2(stampPath(configDir), "utf8")).trim();
|
|
786
|
+
} catch {
|
|
787
|
+
return "";
|
|
788
|
+
}
|
|
789
|
+
}
|
|
790
|
+
async function writeSkillsStamp(version, configDir = getConfigDir()) {
|
|
791
|
+
await mkdir2(configDir, { recursive: true });
|
|
792
|
+
await writeFile2(stampPath(configDir), version, "utf8");
|
|
793
|
+
}
|
|
794
|
+
async function checkSkillsSync(currentVersion, configDir = getConfigDir()) {
|
|
795
|
+
if (process.env.BPMAX_CLI_NO_SKILLS_NOTIFIER) {
|
|
796
|
+
return { current: currentVersion, target: currentVersion, inSync: true };
|
|
797
|
+
}
|
|
798
|
+
const current = await readSkillsStamp(configDir);
|
|
799
|
+
return { current, target: currentVersion, inSync: current === currentVersion };
|
|
800
|
+
}
|
|
801
|
+
function defaultPackageRoot() {
|
|
802
|
+
return path2.resolve(path2.dirname(fileURLToPath(import.meta.url)), "..", "..");
|
|
803
|
+
}
|
|
804
|
+
async function runSkillsInstall(options = {}) {
|
|
805
|
+
const runner = options.runner || runCommand;
|
|
806
|
+
return runner(process.execPath, [path2.join(options.packageRoot || defaultPackageRoot(), "scripts", "install-skills.mjs")], {
|
|
807
|
+
timeoutMs: SKILLS_INSTALL_TIMEOUT_MS
|
|
808
|
+
});
|
|
809
|
+
}
|
|
810
|
+
async function syncSkills(version, options = {}) {
|
|
811
|
+
if (!options.force) {
|
|
812
|
+
const status = await checkSkillsSync(version, options.configDir);
|
|
813
|
+
if (status.inSync) {
|
|
814
|
+
return { action: "in_sync" };
|
|
815
|
+
}
|
|
816
|
+
}
|
|
817
|
+
const result = await runSkillsInstall({ packageRoot: options.packageRoot, runner: options.runner });
|
|
818
|
+
if (result.exitCode === 0) {
|
|
819
|
+
await writeSkillsStamp(version, options.configDir);
|
|
820
|
+
return { action: "synced", result };
|
|
821
|
+
}
|
|
822
|
+
return { action: "failed", result };
|
|
823
|
+
}
|
|
824
|
+
|
|
825
|
+
// src/core/update.ts
|
|
826
|
+
import { access, mkdir as mkdir3, readFile as readFile3, realpath, writeFile as writeFile3 } from "fs/promises";
|
|
827
|
+
import path3 from "path";
|
|
828
|
+
var DEFAULT_REGISTRY_URL = "https://registry.npmjs.org/";
|
|
829
|
+
var UPDATE_STATE_FILE = "update-state.json";
|
|
830
|
+
var CACHE_TTL_MS = 24 * 60 * 60 * 1e3;
|
|
831
|
+
var FETCH_TIMEOUT_MS = 5e3;
|
|
832
|
+
var NPM_INSTALL_TIMEOUT_MS = 10 * 60 * 1e3;
|
|
833
|
+
function buildRegistryUrl(packageName = NPM_PACKAGE, env = process.env) {
|
|
834
|
+
const base = env.BPMAX_CLI_REGISTRY_URL || DEFAULT_REGISTRY_URL;
|
|
835
|
+
const normalizedBase = base.endsWith("/") ? base : `${base}/`;
|
|
836
|
+
return new URL(`${encodeURIComponent(packageName).replace("%40", "@")}/latest`, normalizedBase).toString();
|
|
837
|
+
}
|
|
838
|
+
function parseVersion(version) {
|
|
839
|
+
const clean = version.trim().replace(/^v/, "").split("+")[0];
|
|
840
|
+
const [coreText, prereleaseText = ""] = clean.split("-", 2);
|
|
841
|
+
const coreParts = coreText.split(".");
|
|
842
|
+
if (coreParts.length !== 3) {
|
|
843
|
+
return null;
|
|
844
|
+
}
|
|
845
|
+
const core = coreParts.map((part) => {
|
|
846
|
+
if (!/^(0|[1-9]\d*)$/.test(part)) {
|
|
847
|
+
return Number.NaN;
|
|
848
|
+
}
|
|
849
|
+
return Number(part);
|
|
850
|
+
});
|
|
851
|
+
if (core.some((part) => Number.isNaN(part))) {
|
|
852
|
+
return null;
|
|
853
|
+
}
|
|
854
|
+
if (prereleaseText && !/^(?:0|[1-9]\d*|[0-9A-Za-z-]*[A-Za-z-][0-9A-Za-z-]*)(?:\.(?:0|[1-9]\d*|[0-9A-Za-z-]*[A-Za-z-][0-9A-Za-z-]*))*$/.test(prereleaseText)) {
|
|
855
|
+
return null;
|
|
856
|
+
}
|
|
857
|
+
return { core, prerelease: prereleaseText ? prereleaseText.split(".") : [] };
|
|
858
|
+
}
|
|
859
|
+
function comparePrerelease(left, right) {
|
|
860
|
+
if (left.length === 0 && right.length === 0) {
|
|
861
|
+
return 0;
|
|
862
|
+
}
|
|
863
|
+
if (left.length === 0) {
|
|
864
|
+
return 1;
|
|
865
|
+
}
|
|
866
|
+
if (right.length === 0) {
|
|
867
|
+
return -1;
|
|
868
|
+
}
|
|
869
|
+
const max = Math.max(left.length, right.length);
|
|
870
|
+
for (let index = 0; index < max; index += 1) {
|
|
871
|
+
const a = left[index];
|
|
872
|
+
const b = right[index];
|
|
873
|
+
if (a === void 0) {
|
|
874
|
+
return -1;
|
|
875
|
+
}
|
|
876
|
+
if (b === void 0) {
|
|
877
|
+
return 1;
|
|
878
|
+
}
|
|
879
|
+
const aNumber = /^(0|[1-9]\d*)$/.test(a) ? Number(a) : null;
|
|
880
|
+
const bNumber = /^(0|[1-9]\d*)$/.test(b) ? Number(b) : null;
|
|
881
|
+
if (aNumber !== null && bNumber !== null) {
|
|
882
|
+
if (aNumber !== bNumber) {
|
|
883
|
+
return aNumber > bNumber ? 1 : -1;
|
|
884
|
+
}
|
|
885
|
+
continue;
|
|
886
|
+
}
|
|
887
|
+
if (aNumber !== null) {
|
|
888
|
+
return -1;
|
|
889
|
+
}
|
|
890
|
+
if (bNumber !== null) {
|
|
891
|
+
return 1;
|
|
892
|
+
}
|
|
893
|
+
if (a !== b) {
|
|
894
|
+
return a > b ? 1 : -1;
|
|
895
|
+
}
|
|
896
|
+
}
|
|
897
|
+
return 0;
|
|
898
|
+
}
|
|
899
|
+
function isNewerVersion(candidate, current) {
|
|
900
|
+
const next = parseVersion(candidate);
|
|
901
|
+
if (!next) {
|
|
902
|
+
return false;
|
|
903
|
+
}
|
|
904
|
+
const existing = parseVersion(current);
|
|
905
|
+
if (!existing) {
|
|
906
|
+
return true;
|
|
907
|
+
}
|
|
908
|
+
for (let index = 0; index < 3; index += 1) {
|
|
909
|
+
if (next.core[index] > existing.core[index]) {
|
|
910
|
+
return true;
|
|
911
|
+
}
|
|
912
|
+
if (next.core[index] < existing.core[index]) {
|
|
913
|
+
return false;
|
|
914
|
+
}
|
|
915
|
+
}
|
|
916
|
+
return comparePrerelease(next.prerelease, existing.prerelease) > 0;
|
|
917
|
+
}
|
|
918
|
+
async function fetchLatestVersion(options = {}) {
|
|
919
|
+
const controller = new AbortController();
|
|
920
|
+
const timer = setTimeout(() => controller.abort(), FETCH_TIMEOUT_MS);
|
|
921
|
+
try {
|
|
922
|
+
const fetchImpl = options.fetchImpl || fetch;
|
|
923
|
+
const response = await fetchImpl(buildRegistryUrl(options.packageName, options.env), {
|
|
924
|
+
signal: controller.signal
|
|
925
|
+
});
|
|
926
|
+
if (!response.ok) {
|
|
927
|
+
throw new Error(`npm registry returned HTTP ${response.status}`);
|
|
928
|
+
}
|
|
929
|
+
const body = await response.json();
|
|
930
|
+
if (typeof body.version !== "string" || !body.version.trim()) {
|
|
931
|
+
throw new Error("npm registry returned an empty version");
|
|
932
|
+
}
|
|
933
|
+
return body.version.trim();
|
|
934
|
+
} finally {
|
|
935
|
+
clearTimeout(timer);
|
|
936
|
+
}
|
|
937
|
+
}
|
|
938
|
+
function updateStatePath(configDir = getConfigDir()) {
|
|
939
|
+
return path3.join(configDir, UPDATE_STATE_FILE);
|
|
940
|
+
}
|
|
941
|
+
async function readUpdateState(configDir) {
|
|
942
|
+
try {
|
|
943
|
+
const raw = await readFile3(updateStatePath(configDir), "utf8");
|
|
944
|
+
const parsed = JSON.parse(raw);
|
|
945
|
+
if (typeof parsed.latest_version !== "string" || typeof parsed.checked_at !== "number") {
|
|
946
|
+
return null;
|
|
947
|
+
}
|
|
948
|
+
return { latest_version: parsed.latest_version, checked_at: parsed.checked_at };
|
|
949
|
+
} catch {
|
|
950
|
+
return null;
|
|
951
|
+
}
|
|
952
|
+
}
|
|
953
|
+
async function writeUpdateState(state, configDir = getConfigDir()) {
|
|
954
|
+
await mkdir3(configDir, { recursive: true });
|
|
955
|
+
await writeFile3(updateStatePath(configDir), JSON.stringify(state), "utf8");
|
|
956
|
+
}
|
|
957
|
+
async function checkCachedUpdate(currentVersion, configDir = getConfigDir()) {
|
|
958
|
+
if (process.env.BPMAX_CLI_NO_UPDATE_NOTIFIER) {
|
|
959
|
+
return null;
|
|
960
|
+
}
|
|
961
|
+
const state = await readUpdateState(configDir);
|
|
962
|
+
if (!state) {
|
|
963
|
+
return null;
|
|
964
|
+
}
|
|
965
|
+
const checkedAtMs = state.checked_at * 1e3;
|
|
966
|
+
if (Date.now() - checkedAtMs > CACHE_TTL_MS) {
|
|
967
|
+
return null;
|
|
968
|
+
}
|
|
969
|
+
if (!isNewerVersion(state.latest_version, currentVersion)) {
|
|
970
|
+
return null;
|
|
971
|
+
}
|
|
972
|
+
return { current: currentVersion, latest: state.latest_version };
|
|
973
|
+
}
|
|
974
|
+
async function refreshUpdateCache(currentVersion, configDir = getConfigDir(), options = {}) {
|
|
975
|
+
if (process.env.BPMAX_CLI_NO_UPDATE_NOTIFIER) {
|
|
976
|
+
return null;
|
|
977
|
+
}
|
|
978
|
+
const state = await readUpdateState(configDir);
|
|
979
|
+
if (state && Date.now() - state.checked_at * 1e3 < CACHE_TTL_MS) {
|
|
980
|
+
return isNewerVersion(state.latest_version, currentVersion) ? { current: currentVersion, latest: state.latest_version } : null;
|
|
981
|
+
}
|
|
982
|
+
const latest = await fetchLatestVersion(options);
|
|
983
|
+
await writeUpdateState({ latest_version: latest, checked_at: Math.floor(Date.now() / 1e3) }, configDir);
|
|
984
|
+
return isNewerVersion(latest, currentVersion) ? { current: currentVersion, latest } : null;
|
|
985
|
+
}
|
|
986
|
+
async function executablePath() {
|
|
987
|
+
const candidate = process.argv[1] || "";
|
|
988
|
+
if (!candidate) {
|
|
989
|
+
return "";
|
|
990
|
+
}
|
|
991
|
+
try {
|
|
992
|
+
return await realpath(candidate);
|
|
993
|
+
} catch {
|
|
994
|
+
return candidate;
|
|
995
|
+
}
|
|
996
|
+
}
|
|
997
|
+
async function findNpmPath() {
|
|
998
|
+
const command = process.platform === "win32" ? "where" : "which";
|
|
999
|
+
const result = await runCommand(command, ["npm"], { timeoutMs: 1e4 });
|
|
1000
|
+
if (result.exitCode !== 0) {
|
|
1001
|
+
return null;
|
|
1002
|
+
}
|
|
1003
|
+
return result.stdout.split(/\r?\n/).find(Boolean)?.trim() || null;
|
|
1004
|
+
}
|
|
1005
|
+
function detectInstallMethod(input) {
|
|
1006
|
+
const resolvedPath = input.executablePath || "";
|
|
1007
|
+
const method = resolvedPath.includes(`node_modules${path3.sep}`) || resolvedPath.includes("node_modules/") ? "npm" : "manual";
|
|
1008
|
+
return {
|
|
1009
|
+
method,
|
|
1010
|
+
resolvedPath,
|
|
1011
|
+
npmAvailable: method === "npm" && Boolean(input.npmPath)
|
|
1012
|
+
};
|
|
1013
|
+
}
|
|
1014
|
+
async function detectCurrentInstallMethod() {
|
|
1015
|
+
return detectInstallMethod({
|
|
1016
|
+
executablePath: await executablePath(),
|
|
1017
|
+
npmPath: await findNpmPath()
|
|
1018
|
+
});
|
|
1019
|
+
}
|
|
1020
|
+
async function runNpmInstall(version, options = {}) {
|
|
1021
|
+
const runner = options.runner || runCommand;
|
|
1022
|
+
return runner("npm", ["install", "-g", `${options.packageName || NPM_PACKAGE}@${version}`], {
|
|
1023
|
+
timeoutMs: NPM_INSTALL_TIMEOUT_MS
|
|
1024
|
+
});
|
|
1025
|
+
}
|
|
1026
|
+
|
|
1027
|
+
// src/commands/update.ts
|
|
1028
|
+
var MAX_DETAIL = 2e3;
|
|
1029
|
+
function printJson(value) {
|
|
1030
|
+
console.log(JSON.stringify(value, null, 2));
|
|
1031
|
+
}
|
|
1032
|
+
function truncate(value) {
|
|
1033
|
+
if (value.length <= MAX_DETAIL) {
|
|
1034
|
+
return value;
|
|
1035
|
+
}
|
|
1036
|
+
return value.slice(value.length - MAX_DETAIL);
|
|
1037
|
+
}
|
|
1038
|
+
function manualReason(detect) {
|
|
1039
|
+
if (detect.method === "npm" && !detect.npmAvailable) {
|
|
1040
|
+
return "installed via npm, but npm is not available in PATH";
|
|
1041
|
+
}
|
|
1042
|
+
return "not installed via npm";
|
|
1043
|
+
}
|
|
1044
|
+
function skillsAction(result) {
|
|
1045
|
+
if (result.action !== "failed") {
|
|
1046
|
+
return { skills_action: result.action };
|
|
1047
|
+
}
|
|
1048
|
+
return {
|
|
1049
|
+
skills_action: "failed",
|
|
1050
|
+
skills_warning: "skills update failed",
|
|
1051
|
+
...result.result?.stderr ? { skills_detail: truncate(result.result.stderr.trim()) } : {}
|
|
1052
|
+
};
|
|
1053
|
+
}
|
|
1054
|
+
function registerUpdateCommands(program2, deps = {}) {
|
|
1055
|
+
const fetchLatest = deps.fetchLatest || (() => fetchLatestVersion({ packageName: NPM_PACKAGE }));
|
|
1056
|
+
const detectInstall = deps.detectInstall || detectCurrentInstallMethod;
|
|
1057
|
+
const install = deps.install || ((version) => runNpmInstall(version, { packageName: NPM_PACKAGE }));
|
|
1058
|
+
const runSkills = deps.syncSkills || ((version, options) => syncSkills(version, options));
|
|
1059
|
+
const getSkillsStatus = deps.getSkillsStatus || (() => checkSkillsSync(VERSION));
|
|
1060
|
+
program2.command("update").description("Update bpmax-cli to the latest version").option("--check", "only check for updates, do not install").option("--force", "force reinstall even if already up to date").option("--json", "structured JSON output").action(async (options) => {
|
|
1061
|
+
let latest;
|
|
1062
|
+
try {
|
|
1063
|
+
latest = await fetchLatest();
|
|
1064
|
+
} catch (error) {
|
|
1065
|
+
const message = `failed to check latest version: ${error instanceof Error ? error.message : String(error)}`;
|
|
1066
|
+
if (options.json) {
|
|
1067
|
+
printJson({ ok: false, error: { type: "network", message } });
|
|
1068
|
+
process.exitCode = 1;
|
|
1069
|
+
return;
|
|
1070
|
+
}
|
|
1071
|
+
throw new Error(message);
|
|
1072
|
+
}
|
|
1073
|
+
const detect = await detectInstall();
|
|
1074
|
+
const updateAvailable = isNewerVersion(latest, VERSION);
|
|
1075
|
+
if (options.check) {
|
|
1076
|
+
const status = await getSkillsStatus();
|
|
1077
|
+
const body2 = {
|
|
1078
|
+
ok: true,
|
|
1079
|
+
previous_version: VERSION,
|
|
1080
|
+
current_version: VERSION,
|
|
1081
|
+
latest_version: latest,
|
|
1082
|
+
action: updateAvailable ? "update_available" : "already_up_to_date",
|
|
1083
|
+
auto_update: detect.method === "npm" && detect.npmAvailable,
|
|
1084
|
+
skills_status: {
|
|
1085
|
+
current: status.current,
|
|
1086
|
+
target: status.target,
|
|
1087
|
+
in_sync: status.inSync
|
|
1088
|
+
}
|
|
1089
|
+
};
|
|
1090
|
+
if (options.json) {
|
|
1091
|
+
printJson(body2);
|
|
1092
|
+
} else if (updateAvailable) {
|
|
1093
|
+
console.error(`Update available: ${VERSION} -> ${latest}`);
|
|
1094
|
+
console.error(`Run: bpmax-cli update`);
|
|
1095
|
+
} else {
|
|
1096
|
+
console.error(`bpmax-cli ${VERSION} is already up to date`);
|
|
1097
|
+
}
|
|
1098
|
+
return;
|
|
1099
|
+
}
|
|
1100
|
+
if (!options.force && !updateAvailable) {
|
|
1101
|
+
const synced2 = await runSkills(VERSION, { force: false });
|
|
1102
|
+
const body2 = {
|
|
1103
|
+
ok: true,
|
|
1104
|
+
previous_version: VERSION,
|
|
1105
|
+
current_version: VERSION,
|
|
1106
|
+
latest_version: latest,
|
|
1107
|
+
action: "already_up_to_date",
|
|
1108
|
+
...skillsAction(synced2)
|
|
1109
|
+
};
|
|
1110
|
+
if (options.json) {
|
|
1111
|
+
printJson(body2);
|
|
1112
|
+
} else {
|
|
1113
|
+
console.error(`bpmax-cli ${VERSION} is already up to date`);
|
|
1114
|
+
if (synced2.action === "failed") {
|
|
1115
|
+
console.error("Skills update failed. Run manually: npm run install:skills");
|
|
1116
|
+
} else if (synced2.action === "synced") {
|
|
1117
|
+
console.error("Skills updated");
|
|
1118
|
+
}
|
|
1119
|
+
}
|
|
1120
|
+
return;
|
|
1121
|
+
}
|
|
1122
|
+
if (detect.method !== "npm" || !detect.npmAvailable) {
|
|
1123
|
+
const synced2 = await runSkills(VERSION, { force: Boolean(options.force) });
|
|
1124
|
+
const body2 = {
|
|
1125
|
+
ok: true,
|
|
1126
|
+
previous_version: VERSION,
|
|
1127
|
+
current_version: VERSION,
|
|
1128
|
+
latest_version: latest,
|
|
1129
|
+
action: "manual_required",
|
|
1130
|
+
message: `Automatic update unavailable: ${manualReason(detect)} (path: ${detect.resolvedPath})`,
|
|
1131
|
+
manual_command: `npm install -g ${NPM_PACKAGE}@${latest}`,
|
|
1132
|
+
...skillsAction(synced2)
|
|
1133
|
+
};
|
|
1134
|
+
if (options.json) {
|
|
1135
|
+
printJson(body2);
|
|
1136
|
+
} else {
|
|
1137
|
+
console.error(body2.message);
|
|
1138
|
+
console.error(`Run manually: ${body2.manual_command}`);
|
|
1139
|
+
}
|
|
1140
|
+
return;
|
|
1141
|
+
}
|
|
1142
|
+
if (!options.json) {
|
|
1143
|
+
console.error(`Updating bpmax-cli ${VERSION} -> ${latest} via npm ...`);
|
|
1144
|
+
}
|
|
1145
|
+
const installResult = await install(latest);
|
|
1146
|
+
if (installResult.exitCode !== 0) {
|
|
1147
|
+
const detail = truncate(`${installResult.stdout}${installResult.stderr}`.trim());
|
|
1148
|
+
if (options.json) {
|
|
1149
|
+
printJson({
|
|
1150
|
+
ok: false,
|
|
1151
|
+
error: {
|
|
1152
|
+
type: "update_error",
|
|
1153
|
+
message: `npm install failed with exit code ${installResult.exitCode}`,
|
|
1154
|
+
detail
|
|
1155
|
+
}
|
|
1156
|
+
});
|
|
1157
|
+
process.exitCode = 1;
|
|
1158
|
+
return;
|
|
1159
|
+
}
|
|
1160
|
+
throw new Error(`npm install failed with exit code ${installResult.exitCode}${detail ? `
|
|
1161
|
+
${detail}` : ""}`);
|
|
1162
|
+
}
|
|
1163
|
+
const synced = await runSkills(latest, { force: Boolean(options.force) });
|
|
1164
|
+
const body = {
|
|
1165
|
+
ok: true,
|
|
1166
|
+
previous_version: VERSION,
|
|
1167
|
+
current_version: latest,
|
|
1168
|
+
latest_version: latest,
|
|
1169
|
+
action: "updated",
|
|
1170
|
+
...skillsAction(synced)
|
|
1171
|
+
};
|
|
1172
|
+
if (options.json) {
|
|
1173
|
+
printJson(body);
|
|
1174
|
+
} else {
|
|
1175
|
+
console.error(`Successfully updated bpmax-cli from ${VERSION} to ${latest}`);
|
|
1176
|
+
if (synced.action === "failed") {
|
|
1177
|
+
console.error("Skills update failed. Run manually: npm run install:skills");
|
|
1178
|
+
} else if (synced.action === "synced") {
|
|
1179
|
+
console.error("Skills updated");
|
|
1180
|
+
}
|
|
1181
|
+
}
|
|
1182
|
+
});
|
|
1183
|
+
}
|
|
1184
|
+
|
|
1185
|
+
// src/commands/user.ts
|
|
1186
|
+
function registerUserCommands(program2) {
|
|
1187
|
+
const user = program2.command("user").description("User commands");
|
|
1188
|
+
user.command("current").description("Get current user").option("--profile <profile>", "Profile name").option("--format <format>", "Output format", "pretty").action(async (options) => {
|
|
1189
|
+
const config = await loadConfig();
|
|
1190
|
+
const profile = resolveProfile(config, options.profile);
|
|
1191
|
+
const result = await apiRequest(
|
|
1192
|
+
createApiClient(profile),
|
|
1193
|
+
"GET",
|
|
1194
|
+
"/user/current"
|
|
1195
|
+
);
|
|
1196
|
+
printResult(options.format, result);
|
|
1197
|
+
});
|
|
1198
|
+
}
|
|
1199
|
+
|
|
1200
|
+
// src/core/notices.ts
|
|
1201
|
+
var pendingUpdate = null;
|
|
1202
|
+
var pendingSkills = null;
|
|
1203
|
+
function updateMessage(info) {
|
|
1204
|
+
return `bpmax-cli ${info.latest} available, current ${info.current}, run: bpmax-cli update`;
|
|
1205
|
+
}
|
|
1206
|
+
function skillsMessage(info) {
|
|
1207
|
+
if (!info.current) {
|
|
1208
|
+
return "bpmax-cli skills not installed, run: bpmax-cli update";
|
|
1209
|
+
}
|
|
1210
|
+
return `bpmax-cli skills ${info.current} out of sync with binary ${info.target}, run: bpmax-cli update`;
|
|
1211
|
+
}
|
|
1212
|
+
async function setupNotices() {
|
|
1213
|
+
pendingUpdate = await checkCachedUpdate(VERSION);
|
|
1214
|
+
const skills = await checkSkillsSync(VERSION);
|
|
1215
|
+
pendingSkills = skills.inSync ? null : { current: skills.current, target: skills.target };
|
|
1216
|
+
setNoticeProvider(() => {
|
|
1217
|
+
const notice = {};
|
|
1218
|
+
if (pendingUpdate) {
|
|
1219
|
+
notice.update = {
|
|
1220
|
+
current: pendingUpdate.current,
|
|
1221
|
+
latest: pendingUpdate.latest,
|
|
1222
|
+
message: updateMessage(pendingUpdate)
|
|
1223
|
+
};
|
|
1224
|
+
}
|
|
1225
|
+
if (pendingSkills) {
|
|
1226
|
+
notice.skills = {
|
|
1227
|
+
current: pendingSkills.current,
|
|
1228
|
+
target: pendingSkills.target,
|
|
1229
|
+
message: skillsMessage(pendingSkills)
|
|
1230
|
+
};
|
|
1231
|
+
}
|
|
1232
|
+
return Object.keys(notice).length > 0 ? notice : null;
|
|
1233
|
+
});
|
|
1234
|
+
void refreshUpdateCache(VERSION).then((info) => {
|
|
1235
|
+
if (info) {
|
|
1236
|
+
pendingUpdate = info;
|
|
1237
|
+
}
|
|
1238
|
+
}).catch(() => {
|
|
1239
|
+
});
|
|
1240
|
+
}
|
|
1241
|
+
|
|
1242
|
+
// src/bin.ts
|
|
1243
|
+
var program = new Command();
|
|
1244
|
+
program.name(CLI_NAME).description("BPMAX multi-environment CLI").version(VERSION);
|
|
1245
|
+
registerConfigCommands(program);
|
|
1246
|
+
registerAuthCommands(program);
|
|
1247
|
+
registerApiCommands(program);
|
|
1248
|
+
registerSchemaCommands(program);
|
|
1249
|
+
registerUserCommands(program);
|
|
1250
|
+
registerFlowCommands(program);
|
|
1251
|
+
registerProjectCommands(program);
|
|
1252
|
+
registerUpdateCommands(program);
|
|
1253
|
+
var isCompletion = process.argv.includes("completion") || process.argv.includes("__complete");
|
|
1254
|
+
(isCompletion ? Promise.resolve() : setupNotices()).then(() => program.parseAsync(process.argv)).catch((error) => {
|
|
1255
|
+
console.error(error instanceof Error ? error.message : String(error));
|
|
1256
|
+
process.exitCode = 1;
|
|
1257
|
+
});
|
|
1258
|
+
//# sourceMappingURL=bin.js.map
|