bpmax-cli 0.1.16 → 0.1.18

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 CHANGED
@@ -132,6 +132,14 @@ bpmax-cli project my-step --key-of-flow-node 'flow802926525::1776572829122' --fo
132
132
 
133
133
  Use `--flow-key` when the target flow has a non-empty key. `--flow-step-id` maps to the backend node filter.
134
134
 
135
+ Create a project with an interactive form guide:
136
+
137
+ ```bash
138
+ bpmax-cli project create --profile yuanrong --flow-id 123 --interactive --format json
139
+ ```
140
+
141
+ During the guide, choose "打开 BPMAX 表单页继续填写" to hand off to the web form. The CLI prints a PC URL and mobile path with the fields already entered encoded in `init_value`.
142
+
135
143
  ## ES fast queries
136
144
 
137
145
  Use raw ES project search for high-volume reads, aggregations, budget summaries, trend analysis, and other reporting-style queries. Prefer ES aggregations over paging through `project list` and grouping locally.
package/dist/bin.js CHANGED
@@ -812,7 +812,7 @@ function registerFlowCommands(program2) {
812
812
  // package.json
813
813
  var package_default = {
814
814
  name: "bpmax-cli",
815
- version: "0.1.16",
815
+ version: "0.1.18",
816
816
  description: "CLI for BPMAX environments with multi-profile config and local login callback flow",
817
817
  type: "module",
818
818
  bin: {
@@ -1189,6 +1189,18 @@ function fieldFromRecord(source) {
1189
1189
  ...options ? { options } : {}
1190
1190
  };
1191
1191
  }
1192
+ function isSubFormField(field) {
1193
+ const type = field.type?.toLowerCase() ?? "";
1194
+ return /(sub[_-]?form|relation[_-]?form|associated[_-]?form|table)/.test(type);
1195
+ }
1196
+ function mergeField(existing, incoming) {
1197
+ const children = incoming.children ?? existing?.children;
1198
+ return {
1199
+ ...existing,
1200
+ ...incoming,
1201
+ ...children?.length ? { children } : {}
1202
+ };
1203
+ }
1192
1204
  function collectFieldCandidates(value, fields, depth = 0) {
1193
1205
  if (depth > 8) {
1194
1206
  return;
@@ -1204,7 +1216,21 @@ function collectFieldCandidates(value, fields, depth = 0) {
1204
1216
  }
1205
1217
  const field = fieldFromRecord(value);
1206
1218
  if (field) {
1207
- fields.set(field.key, { ...fields.get(field.key), ...field });
1219
+ if (isSubFormField(field)) {
1220
+ const childFields = /* @__PURE__ */ new Map();
1221
+ for (const child of Object.values(value)) {
1222
+ if (isRecord2(child) || Array.isArray(child)) {
1223
+ collectFieldCandidates(child, childFields, depth + 1);
1224
+ }
1225
+ }
1226
+ const children = [...childFields.values()].sort((left, right) => left.key.localeCompare(right.key));
1227
+ fields.set(
1228
+ field.key,
1229
+ mergeField(fields.get(field.key), { ...field, ...children.length ? { children } : {} })
1230
+ );
1231
+ return;
1232
+ }
1233
+ fields.set(field.key, mergeField(fields.get(field.key), field));
1208
1234
  }
1209
1235
  for (const child of Object.values(value)) {
1210
1236
  if (isRecord2(child) || Array.isArray(child)) {
@@ -1225,6 +1251,9 @@ function validateType(field, value) {
1225
1251
  if (!type || isEmpty(value)) {
1226
1252
  return null;
1227
1253
  }
1254
+ if (isSubFormField(field)) {
1255
+ return Array.isArray(value) ? null : `${field.key} must be a JSON array of row objects`;
1256
+ }
1228
1257
  if (field.options) {
1229
1258
  const values = Array.isArray(value) ? value.map(String) : [String(value)];
1230
1259
  const invalid = values.filter((item) => !field.options?.includes(item));
@@ -1246,28 +1275,246 @@ function validateType(field, value) {
1246
1275
  }
1247
1276
  return null;
1248
1277
  }
1249
- function validateFormAgainstFields(form, fields) {
1278
+ function validateFormEntries(form, fields, prefix = "") {
1250
1279
  const byKey = new Map(fields.map((field) => [field.key, field]));
1251
1280
  const errors = [];
1252
1281
  for (const field of fields) {
1253
1282
  if (field.required && isEmpty(form[field.key])) {
1254
- errors.push(`${field.key} is required`);
1283
+ errors.push(`${prefix}${field.key} is required`);
1255
1284
  }
1256
1285
  }
1257
1286
  for (const [key, value] of Object.entries(form)) {
1258
1287
  const field = byKey.get(key);
1259
1288
  if (!field) {
1260
- errors.push(`${key} is not defined in the flow create form`);
1289
+ errors.push(`${prefix}${key} is not defined in the flow create form`);
1261
1290
  continue;
1262
1291
  }
1263
1292
  const typeError = validateType(field, value);
1264
1293
  if (typeError) {
1265
- errors.push(typeError);
1294
+ errors.push(prefix ? `${prefix}${typeError}` : typeError);
1295
+ continue;
1296
+ }
1297
+ if (isSubFormField(field) && Array.isArray(value) && field.children?.length) {
1298
+ for (const [index, row] of value.entries()) {
1299
+ if (isRecord2(row)) {
1300
+ errors.push(...validateFormEntries(row, field.children, `${prefix}${field.key}[${index}].`));
1301
+ }
1302
+ }
1266
1303
  }
1267
1304
  }
1305
+ return errors;
1306
+ }
1307
+ function validateFormAgainstFields(form, fields) {
1308
+ const errors = validateFormEntries(form, fields);
1268
1309
  return { fields, errors };
1269
1310
  }
1270
1311
 
1312
+ // src/core/form-prompts.ts
1313
+ import { confirm, isCancel, multiselect, select, text as text2 } from "@clack/prompts";
1314
+ function isEmpty2(value) {
1315
+ return value === void 0 || value === null || value === "" || Array.isArray(value) && value.length === 0;
1316
+ }
1317
+ function cancelIfNeeded(value) {
1318
+ if (isCancel(value)) {
1319
+ throw new Error("Interactive project creation cancelled.");
1320
+ }
1321
+ }
1322
+ function fieldLabel(field) {
1323
+ return field.label ? `${field.label} (${field.key})` : field.key;
1324
+ }
1325
+ function isArrayField(field) {
1326
+ return /(array|multi|multiple)/.test(field.type?.toLowerCase() ?? "");
1327
+ }
1328
+ function isBooleanField(field) {
1329
+ return /(boolean|bool|switch|checkbox)/.test(field.type?.toLowerCase() ?? "");
1330
+ }
1331
+ function isNumberField(field) {
1332
+ return /(number|int|decimal|float|double|amount)/.test(field.type?.toLowerCase() ?? "");
1333
+ }
1334
+ function stringifyInitial(value) {
1335
+ if (value === void 0 || value === null) {
1336
+ return "";
1337
+ }
1338
+ return typeof value === "string" ? value : JSON.stringify(value);
1339
+ }
1340
+ function subFormHint(field) {
1341
+ if (!field.children?.length) {
1342
+ return 'JSON array, e.g. [{"field_key":"value"}]';
1343
+ }
1344
+ const children = field.children.map((child) => `${child.key}${child.required ? "*" : ""}`).join(", ");
1345
+ return `JSON array rows: ${children}`;
1346
+ }
1347
+ async function promptField(field, currentValue) {
1348
+ const message = field.required ? `${fieldLabel(field)} *` : fieldLabel(field);
1349
+ if (isSubFormField(field)) {
1350
+ const value2 = await text2({
1351
+ message,
1352
+ placeholder: subFormHint(field),
1353
+ initialValue: stringifyInitial(currentValue),
1354
+ validate(input) {
1355
+ const candidate = input ?? "";
1356
+ if (field.required && !candidate.trim()) {
1357
+ return `${field.key} is required`;
1358
+ }
1359
+ if (!candidate.trim()) {
1360
+ return;
1361
+ }
1362
+ try {
1363
+ const parsed = JSON.parse(candidate);
1364
+ if (!Array.isArray(parsed)) {
1365
+ return `${field.key} must be a JSON array`;
1366
+ }
1367
+ const invalidRowIndex = parsed.findIndex(
1368
+ (item) => typeof item !== "object" || item === null || Array.isArray(item)
1369
+ );
1370
+ if (invalidRowIndex >= 0) {
1371
+ return `${field.key} rows must be JSON objects`;
1372
+ }
1373
+ } catch {
1374
+ return `${field.key} must be valid JSON`;
1375
+ }
1376
+ }
1377
+ });
1378
+ cancelIfNeeded(value2);
1379
+ if (typeof value2 !== "string" || !value2.trim()) {
1380
+ return void 0;
1381
+ }
1382
+ return JSON.parse(value2);
1383
+ }
1384
+ if (field.options?.length && isArrayField(field)) {
1385
+ const value2 = await multiselect({
1386
+ message,
1387
+ options: field.options.map((option) => ({ value: option, label: option })),
1388
+ initialValues: Array.isArray(currentValue) ? currentValue.map(String) : [],
1389
+ required: field.required
1390
+ });
1391
+ cancelIfNeeded(value2);
1392
+ return value2;
1393
+ }
1394
+ if (field.options?.length) {
1395
+ const value2 = await select({
1396
+ message,
1397
+ options: [
1398
+ ...!field.required ? [{ value: "", label: "(skip)" }] : [],
1399
+ ...field.options.map((option) => ({ value: option, label: option }))
1400
+ ],
1401
+ initialValue: typeof currentValue === "string" ? currentValue : void 0
1402
+ });
1403
+ cancelIfNeeded(value2);
1404
+ return value2;
1405
+ }
1406
+ if (isBooleanField(field)) {
1407
+ const value2 = await confirm({
1408
+ message,
1409
+ initialValue: typeof currentValue === "boolean" ? currentValue : false
1410
+ });
1411
+ cancelIfNeeded(value2);
1412
+ return value2;
1413
+ }
1414
+ const value = await text2({
1415
+ message,
1416
+ placeholder: isArrayField(field) ? 'JSON array, e.g. ["A","B"]' : void 0,
1417
+ initialValue: stringifyInitial(currentValue),
1418
+ validate(input) {
1419
+ const candidate = input ?? "";
1420
+ if (field.required && !candidate.trim()) {
1421
+ return `${field.key} is required`;
1422
+ }
1423
+ if (!candidate.trim()) {
1424
+ return;
1425
+ }
1426
+ if (isNumberField(field) && Number.isNaN(Number(candidate))) {
1427
+ return `${field.key} must be a number`;
1428
+ }
1429
+ if (isArrayField(field)) {
1430
+ try {
1431
+ const parsed = JSON.parse(candidate);
1432
+ if (!Array.isArray(parsed)) {
1433
+ return `${field.key} must be a JSON array`;
1434
+ }
1435
+ } catch {
1436
+ return `${field.key} must be valid JSON`;
1437
+ }
1438
+ }
1439
+ }
1440
+ });
1441
+ cancelIfNeeded(value);
1442
+ if (typeof value !== "string" || !value.trim()) {
1443
+ return void 0;
1444
+ }
1445
+ if (isNumberField(field)) {
1446
+ return Number(value);
1447
+ }
1448
+ if (isArrayField(field)) {
1449
+ return JSON.parse(value);
1450
+ }
1451
+ return value;
1452
+ }
1453
+ async function promptAction(message) {
1454
+ const value = await select({
1455
+ message,
1456
+ options: [
1457
+ { value: "continue", label: "\u7EE7\u7EED\u586B\u5199" },
1458
+ { value: "open_form", label: "\u6253\u5F00 BPMAX \u8868\u5355\u9875\u7EE7\u7EED\u586B\u5199" },
1459
+ { value: "submit", label: "\u76F4\u63A5\u63D0\u4EA4" },
1460
+ { value: "cancel", label: "\u53D6\u6D88" }
1461
+ ],
1462
+ initialValue: "continue"
1463
+ });
1464
+ cancelIfNeeded(value);
1465
+ return value;
1466
+ }
1467
+ async function promptProjectCreateForm(input) {
1468
+ let name = input.name;
1469
+ if (!name?.trim()) {
1470
+ const value = await text2({
1471
+ message: "\u6D41\u7A0B\u5355\u6807\u9898 *",
1472
+ validate(candidate) {
1473
+ if (!(candidate ?? "").trim()) {
1474
+ return "\u6D41\u7A0B\u5355\u6807\u9898 is required";
1475
+ }
1476
+ }
1477
+ });
1478
+ cancelIfNeeded(value);
1479
+ name = String(value).trim();
1480
+ }
1481
+ const form = { ...input.form };
1482
+ const requiredFields = input.fields.filter((field) => field.required);
1483
+ const optionalFields = input.fields.filter((field) => !field.required);
1484
+ for (const field of requiredFields) {
1485
+ form[field.key] = await promptField(field, form[field.key]);
1486
+ const action2 = await promptAction("\u4E0B\u4E00\u6B65");
1487
+ if (action2 === "open_form" || action2 === "submit" || action2 === "cancel") {
1488
+ return { name, form, action: action2 };
1489
+ }
1490
+ }
1491
+ if (optionalFields.length > 0) {
1492
+ const fillOptional = await confirm({
1493
+ message: "\u662F\u5426\u7EE7\u7EED\u586B\u5199\u53EF\u9009\u5B57\u6BB5\uFF1F",
1494
+ initialValue: false
1495
+ });
1496
+ cancelIfNeeded(fillOptional);
1497
+ if (fillOptional) {
1498
+ for (const field of optionalFields) {
1499
+ const value = await promptField(field, form[field.key]);
1500
+ if (!isEmpty2(value)) {
1501
+ form[field.key] = value;
1502
+ }
1503
+ const action2 = await promptAction("\u4E0B\u4E00\u6B65");
1504
+ if (action2 === "open_form" || action2 === "submit" || action2 === "cancel") {
1505
+ return { name, form, action: action2 };
1506
+ }
1507
+ }
1508
+ }
1509
+ }
1510
+ const action = await promptAction("\u5B57\u6BB5\u586B\u5199\u5B8C\u6210\uFF0C\u4E0B\u4E00\u6B65");
1511
+ return {
1512
+ name,
1513
+ form,
1514
+ action: action === "continue" ? "submit" : action
1515
+ };
1516
+ }
1517
+
1271
1518
  // src/core/urls.ts
1272
1519
  function stripSlashes(value) {
1273
1520
  return value.trim().replace(/^\/+|\/+$/g, "");
@@ -1300,7 +1547,12 @@ function requireNumericId(value, name) {
1300
1547
  return nextValue;
1301
1548
  }
1302
1549
  function withQuery(path5, query) {
1303
- const params = new URLSearchParams(query);
1550
+ const params = new URLSearchParams();
1551
+ for (const [key, value] of Object.entries(query)) {
1552
+ if (value !== void 0) {
1553
+ params.set(key, value);
1554
+ }
1555
+ }
1304
1556
  return `${path5}?${params.toString()}`;
1305
1557
  }
1306
1558
  function buildBpmaxUrl(profile, kind, params = {}) {
@@ -1329,11 +1581,27 @@ function buildBpmaxUrl(profile, kind, params = {}) {
1329
1581
  });
1330
1582
  result.project_id = projectId;
1331
1583
  result.step_id = stepId;
1584
+ } else if (kind === "project-create") {
1585
+ const flowType = params.flowType || "default";
1586
+ const flowKey = params.flowKey || "";
1587
+ const initValue = params.initValue ? JSON.stringify(params.initValue) : void 0;
1588
+ const query = {
1589
+ flow_type: flowType,
1590
+ flow_key: flowKey,
1591
+ ...flowKey ? { key: flowKey } : {},
1592
+ init_value: initValue
1593
+ };
1594
+ path5 = withQuery(`${basePath}/project/create`, query);
1595
+ result.flow_type = flowType;
1596
+ result.flow_key = flowKey;
1597
+ result.mobile_path = withQuery("/pages/projectCreate/projectCreate", query);
1332
1598
  }
1599
+ const url = `${origin}${path5}`;
1333
1600
  return {
1334
1601
  ...result,
1335
1602
  path: path5,
1336
- url: `${origin}${path5}`
1603
+ url,
1604
+ ...kind === "project-create" ? { pc_url: url } : {}
1337
1605
  };
1338
1606
  }
1339
1607
  function extractProjectId(value) {
@@ -1488,23 +1756,61 @@ function registerProjectCommands(program2) {
1488
1756
  );
1489
1757
  printResult(options.format, result);
1490
1758
  });
1491
- 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("--skip-form-validation", "Skip local form schema validation before submit").option("--format <format>", "Output format", "pretty").action(async (options) => {
1759
+ 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("--interactive", "Guide the user through filling project form fields").option("--skip-form-validation", "Skip local form schema validation before submit").option("--format <format>", "Output format", "pretty").action(async (options) => {
1760
+ if (options.body && options.interactive) {
1761
+ throw new Error("--body and --interactive cannot be used together.");
1762
+ }
1492
1763
  const config = await loadConfig();
1493
1764
  const profile = resolveProfile(config, options.profile);
1494
1765
  const client = createApiClient(profile);
1495
1766
  let payload = parseJson2(options.body);
1496
1767
  if (!options.body) {
1497
- if (!options.name) {
1498
- throw new Error("--name is required unless --body is provided.");
1499
- }
1500
1768
  const flow = await resolveFlowIdentifiers(client, options);
1501
1769
  const { flowDetail, ...flowIdentifiers } = flow;
1502
1770
  const extra = parseJson2(options.json);
1503
1771
  const form = typeof extra.form === "object" && extra.form !== null && !Array.isArray(extra.form) ? extra.form : {};
1772
+ let name = options.name;
1773
+ if (options.interactive) {
1774
+ if (!flowDetail) {
1775
+ throw new Error("--interactive requires --flow-id so the form schema can be discovered.");
1776
+ }
1777
+ const fields = extractFormFields(flowDetail);
1778
+ const promptResult = await promptProjectCreateForm({ name, form: { ...form }, fields });
1779
+ name = promptResult.name;
1780
+ Object.keys(form).forEach((key) => {
1781
+ delete form[key];
1782
+ });
1783
+ Object.assign(form, promptResult.form);
1784
+ if (promptResult.action === "cancel") {
1785
+ printResult(options.format, { cancelled: true });
1786
+ return;
1787
+ }
1788
+ if (promptResult.action === "open_form") {
1789
+ const url = buildBpmaxUrl(profile, "project-create", {
1790
+ flowType: flowIdentifiers.flow_type,
1791
+ flowKey: flowIdentifiers.flow_key,
1792
+ initValue: {
1793
+ project: { name },
1794
+ key_to_value: form
1795
+ }
1796
+ });
1797
+ printResult(options.format, {
1798
+ handoff: true,
1799
+ url: url.url,
1800
+ pc_url: url.pc_url,
1801
+ mobile_path: url.mobile_path,
1802
+ message: "\u5DF2\u751F\u6210 BPMAX \u521B\u5EFA\u9875\u94FE\u63A5\uFF0C\u8BF7\u70B9\u51FB\u7EE7\u7EED\u586B\u5199\u5E76\u63D0\u4EA4\u3002"
1803
+ });
1804
+ return;
1805
+ }
1806
+ }
1807
+ if (!name) {
1808
+ throw new Error("--name is required unless --body is provided.");
1809
+ }
1504
1810
  payload = {
1505
1811
  ...extra,
1506
1812
  ...flowIdentifiers,
1507
- name: options.name,
1813
+ name,
1508
1814
  workspace: profile.config.workspace,
1509
1815
  form
1510
1816
  };
@@ -1916,19 +2222,19 @@ function registerSchemaCommands(program2) {
1916
2222
  }
1917
2223
 
1918
2224
  // src/commands/setup.ts
1919
- import { confirm, text as text2 } from "@clack/prompts";
2225
+ import { confirm as confirm2, text as text3 } from "@clack/prompts";
1920
2226
  async function promptText(current, message, placeholder) {
1921
2227
  if (current?.trim()) {
1922
2228
  return current.trim();
1923
2229
  }
1924
- const value = await text2({ message, placeholder });
2230
+ const value = await text3({ message, placeholder });
1925
2231
  if (typeof value !== "string" || !value.trim()) {
1926
2232
  throw new Error(`${message} is required.`);
1927
2233
  }
1928
2234
  return value.trim();
1929
2235
  }
1930
2236
  async function promptConfirm(message) {
1931
- const value = await confirm({ message, initialValue: true });
2237
+ const value = await confirm2({ message, initialValue: true });
1932
2238
  return value === true;
1933
2239
  }
1934
2240
  function registerSetupCommand(program2, deps = {}) {
@@ -2395,6 +2701,18 @@ async function resolveUrl(load, kind, options) {
2395
2701
  function registerUrlCommands(program2, deps = {}) {
2396
2702
  const load = deps.loadConfig || loadConfig;
2397
2703
  const url = program2.command("url").description("Generate BPMAX web URLs");
2704
+ url.command("project-create").description("Generate a project create URL").requiredOption("--flow-type <flowType>", "Flow type").option("--flow-key <flowKey>", "Flow key", "").option("--workspace <workspace>", "Workspace override").option("--profile <profile>", "Profile name").option("--format <format>", "Output format", "pretty").action(async (options) => {
2705
+ const config = await load();
2706
+ const profile = resolveProfile(config, options.profile);
2707
+ printUrl(
2708
+ options.format,
2709
+ buildBpmaxUrl(profile, "project-create", {
2710
+ workspace: options.workspace,
2711
+ flowType: options.flowType,
2712
+ flowKey: options.flowKey
2713
+ })
2714
+ );
2715
+ });
2398
2716
  url.command("workspace").description("Generate the current workspace URL").option("--profile <profile>", "Profile name").option("--workspace <workspace>", "Workspace override").option("--format <format>", "Output format", "pretty").action(async (options) => {
2399
2717
  await resolveUrl(load, "workspace", options);
2400
2718
  });