bpmax-cli 0.1.18 → 0.1.20

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/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.18",
815
+ version: "0.1.20",
816
816
  description: "CLI for BPMAX environments with multi-profile config and local login callback flow",
817
817
  type: "module",
818
818
  bin: {
@@ -1450,12 +1450,12 @@ async function promptField(field, currentValue) {
1450
1450
  }
1451
1451
  return value;
1452
1452
  }
1453
- async function promptAction(message) {
1453
+ async function promptAction(message, allowFormHandoff) {
1454
1454
  const value = await select({
1455
1455
  message,
1456
1456
  options: [
1457
1457
  { value: "continue", label: "\u7EE7\u7EED\u586B\u5199" },
1458
- { value: "open_form", label: "\u6253\u5F00 BPMAX \u8868\u5355\u9875\u7EE7\u7EED\u586B\u5199" },
1458
+ ...allowFormHandoff ? [{ value: "open_form", label: "\u6253\u5F00 BPMAX \u8868\u5355\u9875\u7EE7\u7EED\u586B\u5199" }] : [],
1459
1459
  { value: "submit", label: "\u76F4\u63A5\u63D0\u4EA4" },
1460
1460
  { value: "cancel", label: "\u53D6\u6D88" }
1461
1461
  ],
@@ -1465,6 +1465,7 @@ async function promptAction(message) {
1465
1465
  return value;
1466
1466
  }
1467
1467
  async function promptProjectCreateForm(input) {
1468
+ const allowFormHandoff = input.allowFormHandoff === true;
1468
1469
  let name = input.name;
1469
1470
  if (!name?.trim()) {
1470
1471
  const value = await text2({
@@ -1483,7 +1484,7 @@ async function promptProjectCreateForm(input) {
1483
1484
  const optionalFields = input.fields.filter((field) => !field.required);
1484
1485
  for (const field of requiredFields) {
1485
1486
  form[field.key] = await promptField(field, form[field.key]);
1486
- const action2 = await promptAction("\u4E0B\u4E00\u6B65");
1487
+ const action2 = await promptAction("\u4E0B\u4E00\u6B65", allowFormHandoff);
1487
1488
  if (action2 === "open_form" || action2 === "submit" || action2 === "cancel") {
1488
1489
  return { name, form, action: action2 };
1489
1490
  }
@@ -1500,14 +1501,14 @@ async function promptProjectCreateForm(input) {
1500
1501
  if (!isEmpty2(value)) {
1501
1502
  form[field.key] = value;
1502
1503
  }
1503
- const action2 = await promptAction("\u4E0B\u4E00\u6B65");
1504
+ const action2 = await promptAction("\u4E0B\u4E00\u6B65", allowFormHandoff);
1504
1505
  if (action2 === "open_form" || action2 === "submit" || action2 === "cancel") {
1505
1506
  return { name, form, action: action2 };
1506
1507
  }
1507
1508
  }
1508
1509
  }
1509
1510
  }
1510
- const action = await promptAction("\u5B57\u6BB5\u586B\u5199\u5B8C\u6210\uFF0C\u4E0B\u4E00\u6B65");
1511
+ const action = await promptAction("\u5B57\u6BB5\u586B\u5199\u5B8C\u6210\uFF0C\u4E0B\u4E00\u6B65", allowFormHandoff);
1511
1512
  return {
1512
1513
  name,
1513
1514
  form,
@@ -1515,11 +1516,103 @@ async function promptProjectCreateForm(input) {
1515
1516
  };
1516
1517
  }
1517
1518
 
1519
+ // src/core/project-form.ts
1520
+ function isRecord3(value) {
1521
+ return typeof value === "object" && value !== null && !Array.isArray(value);
1522
+ }
1523
+ function clone(value) {
1524
+ return JSON.parse(JSON.stringify(value));
1525
+ }
1526
+ function getFlowFormTemplateId(flowDetail) {
1527
+ const data = isRecord3(flowDetail?.data) ? flowDetail.data : void 0;
1528
+ const meta = isRecord3(data?.meta) ? data.meta : void 0;
1529
+ const templateId = meta?.form_template;
1530
+ if (typeof templateId === "number" && Number.isFinite(templateId)) {
1531
+ return templateId;
1532
+ }
1533
+ if (typeof templateId === "string" && templateId.trim()) {
1534
+ const parsed = Number(templateId);
1535
+ return Number.isFinite(parsed) ? parsed : void 0;
1536
+ }
1537
+ return void 0;
1538
+ }
1539
+ async function fetchProjectFormTemplate(client, flowDetail) {
1540
+ const templateId = getFlowFormTemplateId(flowDetail);
1541
+ if (!templateId) {
1542
+ return void 0;
1543
+ }
1544
+ const template = await apiRequest(client, "GET", `/form_template/${templateId}`);
1545
+ return { templateId, template };
1546
+ }
1547
+ function hasAutoInitLambda(value, seen = /* @__PURE__ */ new WeakSet()) {
1548
+ if (Array.isArray(value)) {
1549
+ return value.some((item) => hasAutoInitLambda(item, seen));
1550
+ }
1551
+ if (!isRecord3(value)) {
1552
+ return false;
1553
+ }
1554
+ if (seen.has(value)) {
1555
+ return false;
1556
+ }
1557
+ seen.add(value);
1558
+ if (value.lambdaInit === true && typeof value.lambda === "string" && value.lambda.trim().length > 0) {
1559
+ return true;
1560
+ }
1561
+ return Object.values(value).some((item) => hasAutoInitLambda(item, seen));
1562
+ }
1563
+ function setControlValue(control, value) {
1564
+ const next = clone(control);
1565
+ next.value = {
1566
+ ...isRecord3(next.value) ? next.value : {},
1567
+ type: next.value?.type ?? next.field_type,
1568
+ value
1569
+ };
1570
+ return next;
1571
+ }
1572
+ function applyRowValues(rowTemplate, rowValue) {
1573
+ const source = isRecord3(rowValue) ? rowValue : {};
1574
+ return rowTemplate.map((control) => {
1575
+ if (control.key && Object.prototype.hasOwnProperty.call(source, control.key)) {
1576
+ return setControlValue(control, source[control.key]);
1577
+ }
1578
+ return clone(control);
1579
+ });
1580
+ }
1581
+ function buildProjectFormValue(template, form) {
1582
+ const values = Array.isArray(template.value) ? template.value : [];
1583
+ return values.map((control) => {
1584
+ const key = control.key;
1585
+ if (!key || !Object.prototype.hasOwnProperty.call(form, key)) {
1586
+ return clone(control);
1587
+ }
1588
+ const inputValue = form[key];
1589
+ if (control.field_type === "sub_form") {
1590
+ const subFormRows = control.value?.value;
1591
+ const rowTemplate = Array.isArray(subFormRows) && Array.isArray(subFormRows[0]) ? subFormRows[0] : [];
1592
+ const rows = Array.isArray(inputValue) ? inputValue.map((row) => applyRowValues(rowTemplate, row)) : [];
1593
+ return setControlValue(control, rows);
1594
+ }
1595
+ return setControlValue(control, inputValue);
1596
+ });
1597
+ }
1598
+ async function buildProjectFormPayload(client, flowDetail, form, knownTemplate) {
1599
+ const templateResult = knownTemplate ?? await fetchProjectFormTemplate(client, flowDetail);
1600
+ if (!templateResult) {
1601
+ return {};
1602
+ }
1603
+ return {
1604
+ projectForm: {
1605
+ template_id: templateResult.templateId,
1606
+ value: buildProjectFormValue(templateResult.template, form)
1607
+ }
1608
+ };
1609
+ }
1610
+
1518
1611
  // src/core/urls.ts
1519
1612
  function stripSlashes(value) {
1520
1613
  return value.trim().replace(/^\/+|\/+$/g, "");
1521
1614
  }
1522
- function isRecord3(value) {
1615
+ function isRecord4(value) {
1523
1616
  return typeof value === "object" && value !== null && !Array.isArray(value);
1524
1617
  }
1525
1618
  function normalizeWorkspaceForUrl(workspace) {
@@ -1605,7 +1698,7 @@ function buildBpmaxUrl(profile, kind, params = {}) {
1605
1698
  };
1606
1699
  }
1607
1700
  function extractProjectId(value) {
1608
- if (!isRecord3(value)) {
1701
+ if (!isRecord4(value)) {
1609
1702
  return void 0;
1610
1703
  }
1611
1704
  const candidates = [value.id, value.project_id, value.projectId];
@@ -1617,7 +1710,7 @@ function extractProjectId(value) {
1617
1710
  return /^\d+$/.test(projectId) ? projectId : void 0;
1618
1711
  }
1619
1712
  function attachProjectDetailUrl(profile, value) {
1620
- if (!isRecord3(value) || typeof value.url === "string") {
1713
+ if (!isRecord4(value) || typeof value.url === "string") {
1621
1714
  return value;
1622
1715
  }
1623
1716
  const projectId = extractProjectId(value);
@@ -1682,6 +1775,19 @@ async function validateCreateForm(client, options) {
1682
1775
  flowDetail = flows[0];
1683
1776
  }
1684
1777
  const fields = extractFormFields(flowDetail);
1778
+ if (fields.length === 0) {
1779
+ const templateId = getFlowFormTemplateId(flowDetail);
1780
+ if (templateId) {
1781
+ const template = await apiRequest(client, "GET", `/form_template/${templateId}`);
1782
+ const templateFields = extractFormFields(template);
1783
+ const result2 = validateFormAgainstFields(options.form, templateFields);
1784
+ if (result2.errors.length > 0) {
1785
+ throw new Error(`Form validation failed:
1786
+ - ${result2.errors.join("\n- ")}`);
1787
+ }
1788
+ return;
1789
+ }
1790
+ }
1685
1791
  if (fields.length === 0) {
1686
1792
  throw new Error(
1687
1793
  [
@@ -1696,6 +1802,24 @@ async function validateCreateForm(client, options) {
1696
1802
  - ${result.errors.join("\n- ")}`);
1697
1803
  }
1698
1804
  }
1805
+ function printCreateFormHandoff(format, profile, flowIdentifiers, options) {
1806
+ const url = buildBpmaxUrl(profile, "project-create", {
1807
+ flowType: flowIdentifiers.flow_type,
1808
+ flowKey: flowIdentifiers.flow_key,
1809
+ initValue: {
1810
+ project: { name: options.name },
1811
+ key_to_value: options.form
1812
+ }
1813
+ });
1814
+ printResult(format, {
1815
+ handoff: true,
1816
+ ...options.reason ? { reason: options.reason } : {},
1817
+ url: url.url,
1818
+ pc_url: url.pc_url,
1819
+ mobile_path: url.mobile_path,
1820
+ message: options.message
1821
+ });
1822
+ }
1699
1823
  function registerProjectCommands(program2) {
1700
1824
  const project = program2.command("project").description("Project commands");
1701
1825
  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) => {
@@ -1756,7 +1880,7 @@ function registerProjectCommands(program2) {
1756
1880
  );
1757
1881
  printResult(options.format, result);
1758
1882
  });
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) => {
1883
+ 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("--allow-form-handoff", "Allow interactive mode to hand off to the BPMAX web create form").option("--skip-form-validation", "Skip local form schema validation before submit").option("--format <format>", "Output format", "pretty").action(async (options) => {
1760
1884
  if (options.body && options.interactive) {
1761
1885
  throw new Error("--body and --interactive cannot be used together.");
1762
1886
  }
@@ -1770,12 +1894,18 @@ function registerProjectCommands(program2) {
1770
1894
  const extra = parseJson2(options.json);
1771
1895
  const form = typeof extra.form === "object" && extra.form !== null && !Array.isArray(extra.form) ? extra.form : {};
1772
1896
  let name = options.name;
1897
+ let interactiveTemplate;
1773
1898
  if (options.interactive) {
1774
1899
  if (!flowDetail) {
1775
1900
  throw new Error("--interactive requires --flow-id so the form schema can be discovered.");
1776
1901
  }
1777
1902
  const fields = extractFormFields(flowDetail);
1778
- const promptResult = await promptProjectCreateForm({ name, form: { ...form }, fields });
1903
+ const promptResult = await promptProjectCreateForm({
1904
+ name,
1905
+ form: { ...form },
1906
+ fields,
1907
+ allowFormHandoff: options.allowFormHandoff === true
1908
+ });
1779
1909
  name = promptResult.name;
1780
1910
  Object.keys(form).forEach((key) => {
1781
1911
  delete form[key];
@@ -1785,20 +1915,23 @@ function registerProjectCommands(program2) {
1785
1915
  printResult(options.format, { cancelled: true });
1786
1916
  return;
1787
1917
  }
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
- }
1918
+ interactiveTemplate = await fetchProjectFormTemplate(client, flowDetail);
1919
+ if (interactiveTemplate && hasAutoInitLambda(interactiveTemplate.template)) {
1920
+ printCreateFormHandoff(options.format, profile, flowIdentifiers, {
1921
+ name,
1922
+ form,
1923
+ reason: "form_template_auto_init_lambda",
1924
+ message: "\u68C0\u6D4B\u5230\u8868\u5355\u6A21\u677F\u5305\u542B\u81EA\u52A8\u521D\u59CB\u5316\u811A\u672C\uFF0C\u5DF2\u751F\u6210 BPMAX \u521B\u5EFA\u9875\u94FE\u63A5\uFF0C\u8BF7\u70B9\u51FB\u7EE7\u7EED\u586B\u5199\u5E76\u63D0\u4EA4\u3002"
1796
1925
  });
1797
- printResult(options.format, {
1798
- handoff: true,
1799
- url: url.url,
1800
- pc_url: url.pc_url,
1801
- mobile_path: url.mobile_path,
1926
+ return;
1927
+ }
1928
+ if (promptResult.action === "open_form") {
1929
+ if (!options.allowFormHandoff) {
1930
+ throw new Error("Form handoff requires --allow-form-handoff. Continue interactive filling by default.");
1931
+ }
1932
+ printCreateFormHandoff(options.format, profile, flowIdentifiers, {
1933
+ name,
1934
+ form,
1802
1935
  message: "\u5DF2\u751F\u6210 BPMAX \u521B\u5EFA\u9875\u94FE\u63A5\uFF0C\u8BF7\u70B9\u51FB\u7EE7\u7EED\u586B\u5199\u5E76\u63D0\u4EA4\u3002"
1803
1936
  });
1804
1937
  return;
@@ -1807,12 +1940,15 @@ function registerProjectCommands(program2) {
1807
1940
  if (!name) {
1808
1941
  throw new Error("--name is required unless --body is provided.");
1809
1942
  }
1943
+ const { form: _rawForm, ...projectExtra } = extra;
1944
+ const { flow_key: flowKey, ...projectFlowIdentifiers } = flowIdentifiers;
1810
1945
  payload = {
1811
- ...extra,
1812
- ...flowIdentifiers,
1946
+ ...projectExtra,
1947
+ ...projectFlowIdentifiers,
1948
+ key: flowKey,
1813
1949
  name,
1814
1950
  workspace: profile.config.workspace,
1815
- form
1951
+ ...projectExtra.projectForm ? {} : await buildProjectFormPayload(client, flowDetail, form, interactiveTemplate)
1816
1952
  };
1817
1953
  if (!options.skipFormValidation) {
1818
1954
  await validateCreateForm(client, { flowId: options.flowId, flowDetail, form });
@@ -1821,7 +1957,7 @@ function registerProjectCommands(program2) {
1821
1957
  const result = await apiRequest(
1822
1958
  client,
1823
1959
  "POST",
1824
- "/project/open-api/create",
1960
+ "/project",
1825
1961
  {
1826
1962
  data: payload
1827
1963
  }
@@ -1867,7 +2003,7 @@ function getRecipesDir(options) {
1867
2003
  const workspaceDirName = normalizeWorkspaceForPath(options.workspace || DEFAULT_WORKSPACE);
1868
2004
  return path3.join((options.getProfileDir || getProfileDataDir)(options.profileName), "recipes", workspaceDirName);
1869
2005
  }
1870
- function isRecord4(value) {
2006
+ function isRecord5(value) {
1871
2007
  return typeof value === "object" && value !== null && !Array.isArray(value);
1872
2008
  }
1873
2009
  function asString(value, fieldName) {
@@ -1877,7 +2013,7 @@ function asString(value, fieldName) {
1877
2013
  return value;
1878
2014
  }
1879
2015
  function parseRecipeDefinition(input) {
1880
- if (!isRecord4(input)) {
2016
+ if (!isRecord5(input)) {
1881
2017
  throw new Error("recipe.json must be a JSON object.");
1882
2018
  }
1883
2019
  if (input.version !== 1) {
@@ -1885,7 +2021,7 @@ function parseRecipeDefinition(input) {
1885
2021
  }
1886
2022
  const name = asString(input.name, "recipe.name");
1887
2023
  assertSafeName("Recipe name", name);
1888
- if (!isRecord4(input.command)) {
2024
+ if (!isRecord5(input.command)) {
1889
2025
  throw new Error("recipe.command must be an object.");
1890
2026
  }
1891
2027
  const argv = input.command.argv;
@@ -1893,7 +2029,7 @@ function parseRecipeDefinition(input) {
1893
2029
  throw new Error("recipe.command.argv must be a non-empty string array.");
1894
2030
  }
1895
2031
  const variables = Array.isArray(input.variables) ? input.variables.map((item, index) => {
1896
- if (!isRecord4(item)) {
2032
+ if (!isRecord5(item)) {
1897
2033
  throw new Error(`recipe.variables[${index}] must be an object.`);
1898
2034
  }
1899
2035
  const variableName = asString(item.name, `recipe.variables[${index}].name`);
@@ -1969,7 +2105,7 @@ function parseVarsJson(input) {
1969
2105
  return {};
1970
2106
  }
1971
2107
  const parsed = JSON.parse(input);
1972
- if (!isRecord4(parsed)) {
2108
+ if (!isRecord5(parsed)) {
1973
2109
  throw new Error("--vars must be a JSON object.");
1974
2110
  }
1975
2111
  const variables = {};
@@ -2202,7 +2338,7 @@ var schemaEntries = [
2202
2338
  { name: "project.list", description: "List projects.", method: "POST", path: "/api/project/list" },
2203
2339
  { name: "project.get", description: "Get project detail.", method: "GET", path: "/api/project/:id" },
2204
2340
  { name: "project.my-step", description: "List my pending steps.", method: "GET", path: "/api/project/my_step" },
2205
- { name: "project.create", description: "Create a project with local form schema validation when form payload is provided.", method: "POST", path: "/api/project/open-api/create" },
2341
+ { name: "project.create", description: "Create a project with local form schema validation when form payload is provided.", method: "POST", path: "/api/project" },
2206
2342
  { name: "step.get", description: "Get step detail.", method: "GET", path: "/api/step/:id" }
2207
2343
  ];
2208
2344