kintone-migrator 0.35.9 → 0.36.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/dist/index.mjs CHANGED
@@ -1399,6 +1399,14 @@ const FieldCode = { create: (code) => {
1399
1399
  if (hasInvalidFieldCodeChars(code)) throw new BusinessRuleError(FormSchemaErrorCode.FsInvalidFieldCode, `Field code "${sanitizeForDisplay$1(code)}" contains invalid characters`);
1400
1400
  return code;
1401
1401
  } };
1402
+ const LOOKUP_CAPABLE_TYPES = /* @__PURE__ */ new Set([
1403
+ "SINGLE_LINE_TEXT",
1404
+ "NUMBER",
1405
+ "LINK"
1406
+ ]);
1407
+ function isLookupCapableField(field) {
1408
+ return LOOKUP_CAPABLE_TYPES.has(field.type);
1409
+ }
1402
1410
  //#endregion
1403
1411
  //#region src/core/adapters/kintone/formConfigurator.ts
1404
1412
  const KNOWN_FIELD_TYPES = /* @__PURE__ */ new Set([
@@ -1490,11 +1498,19 @@ function readKintoneQueryClause(value, fieldPath) {
1490
1498
  assertString(value, fieldPath);
1491
1499
  return value;
1492
1500
  }
1493
- /** Reads `relatedApp` by its app ID; the returned `code` is dropped. */
1501
+ /** Reads `relatedApp`; a missing `code` is `""`, as for an app without one. */
1494
1502
  function fromKintoneRelatedApp(value, fieldPath) {
1495
1503
  assertRecord(value, fieldPath);
1496
1504
  assertString(value.app, `${fieldPath}.app`);
1497
- return { app: value.app };
1505
+ if (value.code === void 0) return {
1506
+ app: value.app,
1507
+ code: ""
1508
+ };
1509
+ assertString(value.code, `${fieldPath}.code`);
1510
+ return {
1511
+ app: value.app,
1512
+ code: value.code
1513
+ };
1498
1514
  }
1499
1515
  function fromKintoneLookup(value, fieldPath) {
1500
1516
  assertRecord(value, fieldPath);
@@ -2923,6 +2939,23 @@ function printFieldDiffResult(result, title) {
2923
2939
  function printAppHeader(appName, appId) {
2924
2940
  p.log.step(`\n=== [${pc.bold(appName)}] (app: ${appId}) ===`);
2925
2941
  }
2942
+ function printDeployState(state) {
2943
+ switch (state.kind) {
2944
+ case "upToDate":
2945
+ p.log.success(`No undeployed changes (preview and published are both at revision ${state.revision}).`);
2946
+ return;
2947
+ case "pending":
2948
+ p.log.warn(`Undeployed changes are pending: preview is at revision ${state.previewRevision}, published is at revision ${state.publishedRevision}. A deploy would publish them.\nA deploy that is still in progress also shows up this way.`);
2949
+ return;
2950
+ case "neverDeployed":
2951
+ p.log.warn(`The app has never been deployed (preview revision ${state.previewRevision}). A deploy would publish its entire configuration.`);
2952
+ return;
2953
+ }
2954
+ }
2955
+ function printAppsWithUndeployedChanges(appNames) {
2956
+ if (appNames.length === 0) return;
2957
+ p.log.warn(`Undeployed changes in ${appNames.length} app(s): ${appNames.map((name) => pc.cyan(name)).join(", ")}`);
2958
+ }
2926
2959
  function printMultiAppResult(result) {
2927
2960
  p.log.step("");
2928
2961
  for (const r of result.results) switch (r.status) {
@@ -3061,7 +3094,8 @@ async function routeMultiApp(values, handlers) {
3061
3094
  return;
3062
3095
  }
3063
3096
  if (target.mode === "list-apps") {
3064
- printAvailableApps(target.config);
3097
+ if (handlers.noAppSelected) await handlers.noAppSelected(target.config);
3098
+ else printAvailableApps(target.config);
3065
3099
  return;
3066
3100
  }
3067
3101
  if (target.mode === "single-app") {
@@ -5853,6 +5887,109 @@ function collectSubtableInnerFieldCodes(fields) {
5853
5887
  return innerCodes;
5854
5888
  }
5855
5889
  //#endregion
5890
+ //#region src/core/domain/formSchema/services/relatedAppProjection.ts
5891
+ function mapLookupProperties(properties, fieldCode, replace) {
5892
+ const { lookup, ...rest } = properties;
5893
+ if (lookup === void 0) return rest;
5894
+ return {
5895
+ ...rest,
5896
+ lookup: {
5897
+ ...lookup,
5898
+ relatedApp: replace(lookup.relatedApp, fieldCode)
5899
+ }
5900
+ };
5901
+ }
5902
+ /**
5903
+ * Rebuilds `field` with each related app replaced by `replace`, including those
5904
+ * of the fields inside a subtable.
5905
+ */
5906
+ function mapFieldRelatedApps(field, replace) {
5907
+ switch (field.type) {
5908
+ case "SINGLE_LINE_TEXT": return {
5909
+ ...field,
5910
+ properties: mapLookupProperties(field.properties, field.code, replace)
5911
+ };
5912
+ case "NUMBER": return {
5913
+ ...field,
5914
+ properties: mapLookupProperties(field.properties, field.code, replace)
5915
+ };
5916
+ case "LINK": return {
5917
+ ...field,
5918
+ properties: mapLookupProperties(field.properties, field.code, replace)
5919
+ };
5920
+ case "REFERENCE_TABLE": {
5921
+ const { referenceTable } = field.properties;
5922
+ return {
5923
+ ...field,
5924
+ properties: { referenceTable: {
5925
+ ...referenceTable,
5926
+ relatedApp: replace(referenceTable.relatedApp, field.code)
5927
+ } }
5928
+ };
5929
+ }
5930
+ case "SUBTABLE": return {
5931
+ ...field,
5932
+ properties: { fields: mapFieldsRelatedApps(field.properties.fields, replace) }
5933
+ };
5934
+ default: return field;
5935
+ }
5936
+ }
5937
+ /** Rebuilds `fields` with each related app replaced by `replace`. */
5938
+ function mapFieldsRelatedApps(fields, replace) {
5939
+ return new Map(Array.from(fields, ([code, field]) => [code, mapFieldRelatedApps(field, replace)]));
5940
+ }
5941
+ function relatedAppOf(field) {
5942
+ if (isLookupCapableField(field)) return field.properties.lookup?.relatedApp;
5943
+ return field.type === "REFERENCE_TABLE" ? field.properties.referenceTable.relatedApp : void 0;
5944
+ }
5945
+ /**
5946
+ * The related app of each field that has one, keyed by field code. A schema
5947
+ * and a remote field map both hold subtable inner fields at the top level too,
5948
+ * and field codes are unique across the form, so the top level covers them.
5949
+ */
5950
+ function relatedAppsByField(fields) {
5951
+ const result = /* @__PURE__ */ new Map();
5952
+ for (const [code, field] of fields) {
5953
+ const relatedApp = relatedAppOf(field);
5954
+ if (relatedApp !== void 0) result.set(code, relatedApp);
5955
+ }
5956
+ return result;
5957
+ }
5958
+ /**
5959
+ * Refers to the remote related app the way `reference` does: by app code when
5960
+ * `reference` does and the app has one, by app ID otherwise.
5961
+ */
5962
+ function projectRelatedApp(remote, reference) {
5963
+ return reference?.code !== void 0 && remote.code !== "" ? { code: remote.code } : { app: remote.app };
5964
+ }
5965
+ /** Whether `relatedApp` refers to the app `remote` reports. */
5966
+ function refersTo(relatedApp, remote) {
5967
+ return relatedApp.code !== void 0 ? relatedApp.code === remote.code : relatedApp.app === remote.app;
5968
+ }
5969
+ /**
5970
+ * Turns the remote fields into schema fields, referring to each related app the
5971
+ * way the same-code field of `reference` does (see {@link projectRelatedApp}).
5972
+ * Without a `reference`, every related app is referred to by app ID.
5973
+ */
5974
+ function projectRemoteFields(remote, reference) {
5975
+ const referenceApps = reference === void 0 ? /* @__PURE__ */ new Map() : relatedAppsByField(reference);
5976
+ return mapFieldsRelatedApps(remote, (relatedApp, code) => projectRelatedApp(relatedApp, referenceApps.get(code)));
5977
+ }
5978
+ /**
5979
+ * Restates the base fields the way {@link projectRemoteFields} states the remote
5980
+ * ones, so that a base and a remote referring to the same app compare equal
5981
+ * however each was written. A base related app that does not refer to the app
5982
+ * of the same-code remote field is a real difference and stays as it is.
5983
+ */
5984
+ function restateBaseFields(base, remote, reference) {
5985
+ const remoteApps = relatedAppsByField(remote);
5986
+ const referenceApps = reference === void 0 ? /* @__PURE__ */ new Map() : relatedAppsByField(reference);
5987
+ return mapFieldsRelatedApps(base, (relatedApp, code) => {
5988
+ const remoteApp = remoteApps.get(code);
5989
+ return remoteApp !== void 0 && refersTo(relatedApp, remoteApp) ? projectRelatedApp(remoteApp, referenceApps.get(code)) : relatedApp;
5990
+ });
5991
+ }
5992
+ //#endregion
5856
5993
  //#region src/core/domain/formSchema/services/subtableFieldSplitter.ts
5857
5994
  function splitSubtableInnerFields(desired, current) {
5858
5995
  const newInnerFields = /* @__PURE__ */ new Map();
@@ -5895,7 +6032,8 @@ function processModifiedEntry(after, before, fieldsToUpdate, innerFieldsToDelete
5895
6032
  * concurrent change yields a 409 conflict.
5896
6033
  */
5897
6034
  async function applySchemaChanges(schema, { container, expectedRevision }) {
5898
- const [currentFields, currentLayout] = await Promise.all([container.formConfigurator.getFields(), container.formConfigurator.getLayout()]);
6035
+ const [remoteFields, currentLayout] = await Promise.all([container.formConfigurator.getFields(), container.formConfigurator.getLayout()]);
6036
+ const currentFields = projectRemoteFields(remoteFields, schema.fields);
5899
6037
  const diff = DiffDetector.detect(schema, currentFields);
5900
6038
  const enrichedCurrentLayout = enrichLayoutWithFields(currentLayout, currentFields);
5901
6039
  const hasLayoutChanges = DiffDetector.detectLayoutChanges(schema.layout, enrichedCurrentLayout);
@@ -5950,17 +6088,9 @@ const VALID_REFERENCE_TABLE_SIZES = /* @__PURE__ */ new Set([
5950
6088
  "40",
5951
6089
  "50"
5952
6090
  ]);
5953
- const LOOKUP_FIELD_TYPES = /* @__PURE__ */ new Set([
5954
- "SINGLE_LINE_TEXT",
5955
- "NUMBER",
5956
- "LINK"
5957
- ]);
5958
6091
  function isSelectionField(field) {
5959
6092
  return SELECTION_TYPES.has(field.type);
5960
6093
  }
5961
- function isLookupCapableField(field) {
5962
- return LOOKUP_FIELD_TYPES.has(field.type);
5963
- }
5964
6094
  function issue(severity, fieldCode, fieldType, rule, message) {
5965
6095
  return {
5966
6096
  severity,
@@ -6014,12 +6144,18 @@ function validateReferenceTableSize(field) {
6014
6144
  if (size !== void 0 && !VALID_REFERENCE_TABLE_SIZES.has(size)) return [issue("error", field.code, field.type, "INVALID_REFERENCE_TABLE_SIZE", `Field "${field.code}" has invalid referenceTable.size "${size}". Valid values: ${[...VALID_REFERENCE_TABLE_SIZES].join(", ")}`)];
6015
6145
  return [];
6016
6146
  }
6147
+ /** The key `relatedApp` refers to the related app by, when its value is empty. */
6148
+ function emptyRelatedAppKey(relatedApp) {
6149
+ const [key, value] = relatedApp.code !== void 0 ? ["code", relatedApp.code] : ["app", relatedApp.app];
6150
+ return value.trim().length === 0 ? key : void 0;
6151
+ }
6017
6152
  function validateLookupStructure(field) {
6018
6153
  if (!isLookupCapableField(field)) return [];
6019
6154
  const { lookup } = field.properties;
6020
6155
  if (lookup === void 0) return [];
6021
6156
  const issues = [];
6022
- if (lookup.relatedApp.app.trim().length === 0) issues.push(issue("error", field.code, field.type, "INVALID_LOOKUP", `Field "${field.code}" lookup must have a non-empty "relatedApp.app"`));
6157
+ const emptyKey = emptyRelatedAppKey(lookup.relatedApp);
6158
+ if (emptyKey !== void 0) issues.push(issue("error", field.code, field.type, "INVALID_LOOKUP", `Field "${field.code}" lookup must have a non-empty "relatedApp.${emptyKey}"`));
6023
6159
  if (lookup.relatedKeyField.trim().length === 0) issues.push(issue("error", field.code, field.type, "INVALID_LOOKUP", `Field "${field.code}" lookup must have a non-empty "relatedKeyField"`));
6024
6160
  lookup.fieldMappings.forEach((mapping, i) => {
6025
6161
  for (const key of ["field", "relatedField"]) if (mapping[key].trim().length === 0) issues.push(issue("error", field.code, field.type, "INVALID_LOOKUP", `Field "${field.code}" lookup must have a non-empty "fieldMappings[${i}].${key}"`));
@@ -6028,8 +6164,8 @@ function validateLookupStructure(field) {
6028
6164
  }
6029
6165
  function validateReferenceTableRelatedApp(field) {
6030
6166
  if (field.type !== "REFERENCE_TABLE") return [];
6031
- const { app } = field.properties.referenceTable.relatedApp;
6032
- if (app.trim().length === 0) return [issue("error", field.code, field.type, "EMPTY_RELATED_APP", `Field "${field.code}" referenceTable.relatedApp.app must be non-empty`)];
6167
+ const emptyKey = emptyRelatedAppKey(field.properties.referenceTable.relatedApp);
6168
+ if (emptyKey !== void 0) return [issue("error", field.code, field.type, "EMPTY_RELATED_APP", `Field "${field.code}" referenceTable.relatedApp.${emptyKey} must be non-empty`)];
6033
6169
  return [];
6034
6170
  }
6035
6171
  function validateSubtableHasInnerFields(field) {
@@ -6087,6 +6223,7 @@ const Schema = { create: (fields, layout) => {
6087
6223
  } };
6088
6224
  //#endregion
6089
6225
  //#region src/core/domain/formSchema/services/schemaParser.ts
6226
+ const SCHEMA_FILE_OPTIONS = { relatedAppWithBothKeys: "reject" };
6090
6227
  const VALID_UNIT_POSITIONS = /* @__PURE__ */ new Set(["BEFORE", "AFTER"]);
6091
6228
  const VALID_CALC_FORMATS = /* @__PURE__ */ new Set([
6092
6229
  "NUMBER",
@@ -6326,11 +6463,19 @@ function optionalLookupList(code, key, value) {
6326
6463
  if (list === void 0) throw invalidLookup(code, `must have a "${key}" array`);
6327
6464
  return list;
6328
6465
  }
6329
- /** Reads `relatedApp` by its app ID; a written `code` is dropped. */
6330
- function parseRelatedApp(raw) {
6331
- return { app: String(raw.app ?? "") };
6466
+ /**
6467
+ * Reads `relatedApp` (written at `path`), which refers to the related app by
6468
+ * exactly one of `app` and `code`. A key whose value is omitted or `null` is not
6469
+ * written.
6470
+ */
6471
+ function parseRelatedApp(raw, path, { relatedAppWithBothKeys }, invalid) {
6472
+ const app = optionalString(raw.app);
6473
+ const code = optionalString(raw.code);
6474
+ if (app !== void 0 && (code === void 0 || relatedAppWithBothKeys === "app")) return { app };
6475
+ if (code !== void 0 && app === void 0) return { code };
6476
+ throw invalid(app === void 0 ? `must have "${path}.app" or "${path}.code"` : `must have only one of "${path}.app" and "${path}.code"`);
6332
6477
  }
6333
- function parseLookup(code, raw) {
6478
+ function parseLookup(code, raw, options) {
6334
6479
  if (!isRecord(raw)) throw invalidLookup(code, "must be an object");
6335
6480
  if (!isRecord(raw.relatedApp)) throw invalidLookup(code, "must have a \"relatedApp\" object");
6336
6481
  const rawFieldMappings = optionalLookupList(code, "fieldMappings", raw.fieldMappings);
@@ -6343,7 +6488,7 @@ function parseLookup(code, raw) {
6343
6488
  };
6344
6489
  });
6345
6490
  return {
6346
- relatedApp: parseRelatedApp(raw.relatedApp),
6491
+ relatedApp: parseRelatedApp(raw.relatedApp, "relatedApp", options, (detail) => invalidLookup(code, detail)),
6347
6492
  relatedKeyField: String(raw.relatedKeyField ?? ""),
6348
6493
  fieldMappings,
6349
6494
  lookupPickerFields,
@@ -6351,7 +6496,7 @@ function parseLookup(code, raw) {
6351
6496
  sort: optionalQueryClause(raw.sort)
6352
6497
  };
6353
6498
  }
6354
- function parseReferenceTableField(raw, code, base) {
6499
+ function parseReferenceTableField(raw, code, base, options) {
6355
6500
  if (!isRecord(raw.referenceTable)) throw new BusinessRuleError(FormSchemaErrorCode.FsInvalidSchemaStructure, `Field "${code}" of type REFERENCE_TABLE must have a "referenceTable" property`);
6356
6501
  const refTable = raw.referenceTable;
6357
6502
  if (!isRecord(refTable.relatedApp)) throw new BusinessRuleError(FormSchemaErrorCode.FsInvalidSchemaStructure, `Field "${code}" of type REFERENCE_TABLE must have "referenceTable.relatedApp"`);
@@ -6368,7 +6513,7 @@ function parseReferenceTableField(raw, code, base) {
6368
6513
  ...base,
6369
6514
  type: "REFERENCE_TABLE",
6370
6515
  properties: { referenceTable: {
6371
- relatedApp: parseRelatedApp(refTable.relatedApp),
6516
+ relatedApp: parseRelatedApp(refTable.relatedApp, "referenceTable.relatedApp", options, (detail) => new BusinessRuleError(FormSchemaErrorCode.FsInvalidSchemaStructure, `Field "${code}" of type REFERENCE_TABLE ${detail}`)),
6372
6517
  condition: {
6373
6518
  field: FieldCode.create(conditionField),
6374
6519
  relatedField: FieldCode.create(conditionRelatedField)
@@ -6380,7 +6525,7 @@ function parseReferenceTableField(raw, code, base) {
6380
6525
  } }
6381
6526
  };
6382
6527
  }
6383
- function parseFieldDefinitionFromFlat(raw) {
6528
+ function parseFieldDefinitionFromFlat(raw, options) {
6384
6529
  const code = String(raw.code);
6385
6530
  const type = String(raw.type);
6386
6531
  if (!VALID_FIELD_TYPES.has(type)) throw new BusinessRuleError(FormSchemaErrorCode.FsInvalidFieldType, `Invalid field type "${type}" for field "${code}"`);
@@ -6395,7 +6540,7 @@ function parseFieldDefinitionFromFlat(raw) {
6395
6540
  const rawFields = Array.isArray(raw.fields) ? raw.fields : [];
6396
6541
  const subFields = /* @__PURE__ */ new Map();
6397
6542
  for (const subRaw of rawFields) {
6398
- const subDef = parseFieldDefinitionFromFlat(subRaw);
6543
+ const subDef = parseFieldDefinitionFromFlat(subRaw, options);
6399
6544
  subFields.set(subDef.code, subDef);
6400
6545
  }
6401
6546
  return {
@@ -6404,11 +6549,11 @@ function parseFieldDefinitionFromFlat(raw) {
6404
6549
  properties: { fields: subFields }
6405
6550
  };
6406
6551
  }
6407
- if (fieldType === "REFERENCE_TABLE") return parseReferenceTableField(raw, code, base);
6552
+ if (fieldType === "REFERENCE_TABLE") return parseReferenceTableField(raw, code, base, options);
6408
6553
  const { lookup, ...others } = extractProperties(raw);
6409
6554
  const properties = lookup === void 0 ? others : {
6410
6555
  ...others,
6411
- lookup: parseLookup(code, lookup)
6556
+ lookup: parseLookup(code, lookup, options)
6412
6557
  };
6413
6558
  validateFieldProperties(code, fieldType, properties);
6414
6559
  return buildFieldDefinition(base, fieldType, properties);
@@ -6440,7 +6585,7 @@ function parseDecorationElement(raw) {
6440
6585
  default: throw new BusinessRuleError(FormSchemaErrorCode.FsInvalidDecorationElement, `Unknown decoration element type: "${type}"`);
6441
6586
  }
6442
6587
  }
6443
- function parseLayoutElement(raw) {
6588
+ function parseLayoutElement(raw, options) {
6444
6589
  const type = String(raw.type);
6445
6590
  if (DECORATION_TYPES.has(type)) return parseDecorationElement(raw);
6446
6591
  if (SYSTEM_FIELD_TYPES.has(type)) return {
@@ -6449,7 +6594,7 @@ function parseLayoutElement(raw) {
6449
6594
  type,
6450
6595
  ...raw.size !== void 0 ? { size: parseSize(raw.size) } : {}
6451
6596
  };
6452
- const field = parseFieldDefinitionFromFlat(raw);
6597
+ const field = parseFieldDefinitionFromFlat(raw, options);
6453
6598
  const size = parseSize(raw.size);
6454
6599
  return {
6455
6600
  kind: "field",
@@ -6457,12 +6602,12 @@ function parseLayoutElement(raw) {
6457
6602
  ...size !== void 0 ? { size } : {}
6458
6603
  };
6459
6604
  }
6460
- function parseLayoutRow(raw) {
6605
+ function parseLayoutRow(raw, options) {
6461
6606
  if (raw.type !== "ROW") throw new BusinessRuleError(FormSchemaErrorCode.FsInvalidLayoutStructure, `Expected layout row type "ROW", got "${String(raw.type)}"`);
6462
6607
  if (raw.fields !== void 0 && !Array.isArray(raw.fields)) throw new BusinessRuleError(FormSchemaErrorCode.FsInvalidLayoutStructure, `ROW "fields" must be an array, got ${typeof raw.fields}`);
6463
6608
  return {
6464
6609
  type: "ROW",
6465
- fields: (Array.isArray(raw.fields) ? raw.fields : []).map(parseLayoutElement)
6610
+ fields: (Array.isArray(raw.fields) ? raw.fields : []).map((r) => parseLayoutElement(r, options))
6466
6611
  };
6467
6612
  }
6468
6613
  function collectFieldsFromElements(elements) {
@@ -6484,7 +6629,7 @@ function mergeFieldMaps(target, source) {
6484
6629
  }
6485
6630
  return merged;
6486
6631
  }
6487
- function parseGroupLayoutItem(raw) {
6632
+ function parseGroupLayoutItem(raw, options) {
6488
6633
  const code = FieldCode.create(String(raw.code));
6489
6634
  const label = String(raw.label ?? "");
6490
6635
  const noLabel = typeof raw.noLabel === "boolean" ? raw.noLabel : void 0;
@@ -6493,7 +6638,7 @@ function parseGroupLayoutItem(raw) {
6493
6638
  let groupFields = /* @__PURE__ */ new Map();
6494
6639
  const layout = [];
6495
6640
  for (const r of rawLayout) {
6496
- const row = parseLayoutRow(r);
6641
+ const row = parseLayoutRow(r, options);
6497
6642
  const rowFields = collectFieldsFromElements(row.fields);
6498
6643
  groupFields = mergeFieldMaps(groupFields, rowFields);
6499
6644
  layout.push(row);
@@ -6519,11 +6664,11 @@ function parseGroupLayoutItem(raw) {
6519
6664
  fields: groupFields
6520
6665
  };
6521
6666
  }
6522
- function parseSubtableLayoutItem(raw) {
6667
+ function parseSubtableLayoutItem(raw, options) {
6523
6668
  const code = FieldCode.create(String(raw.code));
6524
6669
  const label = String(raw.label ?? "");
6525
6670
  const noLabel = typeof raw.noLabel === "boolean" ? raw.noLabel : void 0;
6526
- const elements = (Array.isArray(raw.fields) ? raw.fields : []).map(parseLayoutElement);
6671
+ const elements = (Array.isArray(raw.fields) ? raw.fields : []).map((r) => parseLayoutElement(r, options));
6527
6672
  const subFields = collectFieldsFromElements(elements);
6528
6673
  const subtableDef = {
6529
6674
  code,
@@ -6546,23 +6691,23 @@ function parseSubtableLayoutItem(raw) {
6546
6691
  fields: allFields
6547
6692
  };
6548
6693
  }
6549
- function parseLayoutItem(raw) {
6694
+ function parseLayoutItem(raw, options) {
6550
6695
  const type = String(raw.type);
6551
6696
  switch (type) {
6552
6697
  case "ROW": {
6553
- const row = parseLayoutRow(raw);
6698
+ const row = parseLayoutRow(raw, options);
6554
6699
  return {
6555
6700
  item: row,
6556
6701
  fields: collectFieldsFromElements(row.fields)
6557
6702
  };
6558
6703
  }
6559
- case "GROUP": return parseGroupLayoutItem(raw);
6560
- case "SUBTABLE": return parseSubtableLayoutItem(raw);
6704
+ case "GROUP": return parseGroupLayoutItem(raw, options);
6705
+ case "SUBTABLE": return parseSubtableLayoutItem(raw, options);
6561
6706
  case "REFERENCE_TABLE": {
6562
6707
  const code = FieldCode.create(String(raw.code));
6563
6708
  const label = String(raw.label ?? "");
6564
6709
  const noLabel = typeof raw.noLabel === "boolean" ? raw.noLabel : void 0;
6565
- const fieldDef = parseFieldDefinitionFromFlat(raw);
6710
+ const fieldDef = parseFieldDefinitionFromFlat(raw, options);
6566
6711
  const allFields = /* @__PURE__ */ new Map();
6567
6712
  allFields.set(code, fieldDef);
6568
6713
  return {
@@ -6578,7 +6723,7 @@ function parseLayoutItem(raw) {
6578
6723
  default: throw new BusinessRuleError(FormSchemaErrorCode.FsInvalidLayoutStructure, `Unknown layout item type: "${type}"`);
6579
6724
  }
6580
6725
  }
6581
- const SchemaParser = { parse: (parsed) => {
6726
+ const SchemaParser = { parse: (parsed, options = SCHEMA_FILE_OPTIONS) => {
6582
6727
  if (!isRecord(parsed)) throw new BusinessRuleError(FormSchemaErrorCode.FsInvalidSchemaStructure, "Schema must be an object");
6583
6728
  const obj = parsed;
6584
6729
  if ("fields" in obj && !("layout" in obj)) throw new BusinessRuleError(FormSchemaErrorCode.FsInvalidSchemaStructure, "\"fields\" key detected. Schema format has changed. Please use \"capture\" to generate a new format schema.");
@@ -6589,7 +6734,7 @@ const SchemaParser = { parse: (parsed) => {
6589
6734
  for (let i = 0; i < rawLayout.length; i++) {
6590
6735
  const rawItem = rawLayout[i];
6591
6736
  if (!isRecord(rawItem)) throw new BusinessRuleError(FormSchemaErrorCode.FsInvalidLayoutStructure, `Layout item at index ${i} must be an object`);
6592
- const result = parseLayoutItem(rawItem);
6737
+ const result = parseLayoutItem(rawItem, options);
6593
6738
  layout.push(result.item);
6594
6739
  fieldMap = mergeFieldMaps(fieldMap, result.fields);
6595
6740
  }
@@ -9420,7 +9565,8 @@ async function detectDiff({ container, input }) {
9420
9565
  const result = await container.schemaStorage.get();
9421
9566
  if (!result.exists) throw new ValidationError(ValidationErrorCode.InvalidInput, "Schema file not found");
9422
9567
  const schema = parseSchemaText(container.configCodec, result.content);
9423
- const [currentFields, currentLayout] = await Promise.all([container.formConfigurator.getFields(target), container.formConfigurator.getLayout(target)]);
9568
+ const [remoteFields, currentLayout] = await Promise.all([container.formConfigurator.getFields(target), container.formConfigurator.getLayout(target)]);
9569
+ const currentFields = projectRemoteFields(remoteFields, schema.fields);
9424
9570
  const diff = DiffDetector.detect(schema, currentFields);
9425
9571
  const enrichedCurrentLayout = enrichLayoutWithFields(currentLayout, currentFields);
9426
9572
  const hasLayoutChanges = DiffDetector.detectLayoutChanges(schema.layout, enrichedCurrentLayout);
@@ -11323,12 +11469,30 @@ const SchemaSerializer = { serialize: (layout, fields) => {
11323
11469
  } };
11324
11470
  //#endregion
11325
11471
  //#region src/core/application/formSchema/captureSchema.ts
11472
+ /**
11473
+ * The existing schema file, which decides how the captured related apps are
11474
+ * referred to; `undefined` when there is none or it cannot be parsed.
11475
+ */
11476
+ function readReferenceSchema(container, existing) {
11477
+ if (!existing.exists) return void 0;
11478
+ try {
11479
+ return parseSchemaText(container.configCodec, existing.content);
11480
+ } catch (error) {
11481
+ if (isValidationError(error)) return void 0;
11482
+ throw error;
11483
+ }
11484
+ }
11326
11485
  async function captureSchema({ container }) {
11327
- const [currentFields, currentLayout] = await Promise.all([container.formConfigurator.getFields(), container.formConfigurator.getLayout()]);
11486
+ const [remoteFields, currentLayout, existingResult] = await Promise.all([
11487
+ container.formConfigurator.getFields(),
11488
+ container.formConfigurator.getLayout(),
11489
+ container.schemaStorage.get()
11490
+ ]);
11491
+ const currentFields = projectRemoteFields(remoteFields, readReferenceSchema(container, existingResult)?.fields);
11328
11492
  const enrichedLayout = enrichLayoutWithFields(currentLayout, currentFields);
11329
11493
  return {
11330
11494
  schemaText: stringifyConfig(container.configCodec, SchemaSerializer.serialize(enrichedLayout, currentFields)),
11331
- hasExistingSchema: (await container.schemaStorage.get()).exists
11495
+ hasExistingSchema: existingResult.exists
11332
11496
  };
11333
11497
  }
11334
11498
  //#endregion
@@ -11932,6 +12096,169 @@ var capture_default$11 = define({
11932
12096
  }
11933
12097
  });
11934
12098
  //#endregion
12099
+ //#region src/core/domain/appRevision/services/deployStateEvaluator.ts
12100
+ const DeployStateEvaluator = {
12101
+ /** Derives the {@link DeployState} from the revisions of both generations. */
12102
+ evaluate: ({ preview, published }) => {
12103
+ if (published.kind === "neverDeployed") return {
12104
+ kind: "neverDeployed",
12105
+ previewRevision: preview
12106
+ };
12107
+ if (published.revision === preview) return {
12108
+ kind: "upToDate",
12109
+ revision: preview
12110
+ };
12111
+ return {
12112
+ kind: "pending",
12113
+ previewRevision: preview,
12114
+ publishedRevision: published.revision
12115
+ };
12116
+ },
12117
+ /** True when a deploy would publish something that is waiting in preview. */
12118
+ hasUndeployedChanges: (state) => state.kind !== "upToDate"
12119
+ };
12120
+ //#endregion
12121
+ //#region src/core/application/appRevision/checkDeployState.ts
12122
+ /** Tells whether the app's preview holds changes a deploy would publish. */
12123
+ async function checkDeployState({ container }) {
12124
+ const generations = await container.appRevisionGenerationsReader.read();
12125
+ return DeployStateEvaluator.evaluate(generations);
12126
+ }
12127
+ //#endregion
12128
+ //#region src/core/adapters/kintone/appRevisionGenerationsReader.ts
12129
+ const KINTONE_APP_NOT_FOUND_CODE = "GAIA_AP01";
12130
+ /**
12131
+ * Reads both generations' revisions through `getAppAcl`.
12132
+ *
12133
+ * The app revision is shared by every settings endpoint, so any getter would
12134
+ * do. `getAppAcl` is documented to need only app management permission for
12135
+ * both generations, whereas published getters such as `getFormFields`,
12136
+ * `getViews` and `getAppSettings` are documented to need record view (or add)
12137
+ * permission; an API token with app management permission alone is refused
12138
+ * by several of them (`GAIA_NO01`).
12139
+ */
12140
+ var KintoneAppRevisionGenerationsReader = class {
12141
+ client;
12142
+ appId;
12143
+ constructor(client, appId) {
12144
+ this.client = client;
12145
+ this.appId = appId;
12146
+ }
12147
+ async read() {
12148
+ const preview = await this.readPreview();
12149
+ const published = await this.readPublished();
12150
+ if (published.kind === "neverDeployed") await this.readPreview();
12151
+ return {
12152
+ preview,
12153
+ published
12154
+ };
12155
+ }
12156
+ async readPreview() {
12157
+ try {
12158
+ return await this.fetchRevision(true);
12159
+ } catch (error) {
12160
+ throw wrapKintoneError(error, "Failed to get the preview app revision");
12161
+ }
12162
+ }
12163
+ async readPublished() {
12164
+ try {
12165
+ return {
12166
+ kind: "deployed",
12167
+ revision: await this.fetchRevision(false)
12168
+ };
12169
+ } catch (error) {
12170
+ if (error instanceof KintoneRestAPIError && error.code === KINTONE_APP_NOT_FOUND_CODE) return { kind: "neverDeployed" };
12171
+ throw wrapKintoneError(error, "Failed to get the published app revision");
12172
+ }
12173
+ }
12174
+ async fetchRevision(preview) {
12175
+ const { revision } = await this.client.app.getAppAcl({
12176
+ app: this.appId,
12177
+ preview
12178
+ });
12179
+ if (!revision) throw new SystemError(SystemErrorCode.ExternalApiError, "kintone API did not return an app revision");
12180
+ return revision;
12181
+ }
12182
+ };
12183
+ //#endregion
12184
+ //#region src/core/application/container/deployStateCli.ts
12185
+ function createDeployStateCliContainer(config) {
12186
+ return { appRevisionGenerationsReader: new KintoneAppRevisionGenerationsReader(config.client ?? createKintoneClient(config), config.appId) };
12187
+ }
12188
+ //#endregion
12189
+ //#region src/cli/commands/check-pending.ts
12190
+ const checkPendingArgs = {
12191
+ domain: kintoneArgs.domain,
12192
+ username: kintoneArgs.username,
12193
+ password: kintoneArgs.password,
12194
+ "api-token": kintoneArgs["api-token"],
12195
+ "app-id": kintoneArgs["app-id"],
12196
+ "guest-space-id": kintoneArgs["guest-space-id"],
12197
+ ...multiAppArgs
12198
+ };
12199
+ async function runCheck(config) {
12200
+ const container = createDeployStateCliContainer(config);
12201
+ const s = p.spinner();
12202
+ s.start("Checking for undeployed changes...");
12203
+ let state;
12204
+ try {
12205
+ state = await checkDeployState({ container });
12206
+ } catch (error) {
12207
+ s.stop("Check failed.");
12208
+ throw error;
12209
+ }
12210
+ s.stop("Check complete.");
12211
+ printDeployState(state);
12212
+ return state;
12213
+ }
12214
+ async function runSingleCheck(config) {
12215
+ const state = await runCheck(config);
12216
+ if (DeployStateEvaluator.hasUndeployedChanges(state)) process.exitCode = 1;
12217
+ }
12218
+ var check_pending_default = define({
12219
+ name: "check-pending",
12220
+ description: "Check whether the kintone app has undeployed changes that a deploy would publish",
12221
+ args: checkPendingArgs,
12222
+ run: async (ctx) => {
12223
+ try {
12224
+ const values = ctx.values;
12225
+ await routeMultiApp(values, {
12226
+ singleLegacy: async () => {
12227
+ await runSingleCheck(resolveConfig(values));
12228
+ },
12229
+ singleApp: async (app, projectConfig) => {
12230
+ await runSingleCheck(resolveAppCliConfig(app, projectConfig, values));
12231
+ },
12232
+ multiApp: async (plan, projectConfig) => {
12233
+ const appsWithUndeployedChanges = [];
12234
+ try {
12235
+ await runMultiAppWithHeaders(plan, async (app) => {
12236
+ const state = await runCheck(resolveAppCliConfig(app, projectConfig, values));
12237
+ if (DeployStateEvaluator.hasUndeployedChanges(state)) appsWithUndeployedChanges.push(app.name);
12238
+ });
12239
+ } catch (error) {
12240
+ printAppsWithUndeployedChanges(appsWithUndeployedChanges);
12241
+ throw error;
12242
+ }
12243
+ if (appsWithUndeployedChanges.length > 0) {
12244
+ printAppsWithUndeployedChanges(appsWithUndeployedChanges);
12245
+ process.exitCode = 1;
12246
+ return;
12247
+ }
12248
+ p.log.success("No undeployed changes in any app.");
12249
+ },
12250
+ noAppSelected: async (projectConfig) => {
12251
+ printAvailableApps(projectConfig);
12252
+ p.log.error(`No app was checked. Pass ${pc.cyan("--app <name>")} or ${pc.cyan("--all")} to check for undeployed changes.`);
12253
+ process.exitCode = 1;
12254
+ }
12255
+ });
12256
+ } catch (error) {
12257
+ handleCliError(error);
12258
+ }
12259
+ }
12260
+ });
12261
+ //#endregion
11935
12262
  //#region src/cli/customizeConfig.ts
11936
12263
  const CUSTOMIZE_STATE_FILE = "customize.yaml";
11937
12264
  const customizeArgs = {
@@ -15591,6 +15918,7 @@ function resolveFieldEntry(entry, resolution) {
15591
15918
  }
15592
15919
  //#endregion
15593
15920
  //#region src/core/domain/formSchema/services/schemaStateParser.ts
15921
+ const STATE_OPTIONS = { relatedAppWithBothKeys: "app" };
15594
15922
  /**
15595
15923
  * Parses pre-parsed (codec-decoded) data into a {@link SchemaState}.
15596
15924
  *
@@ -15601,7 +15929,9 @@ function resolveFieldEntry(entry, resolution) {
15601
15929
  * `revision` field alongside the captured `layout`. revision is now stored
15602
15930
  * separately in `state/<appName>/revision.yaml`, so any residual top-level
15603
15931
  * `revision` is stripped and ignored here — the snapshot is parsed from the
15604
- * remainder. The domain layer does not depend on YAML; the application layer
15932
+ * remainder. Older state files also hold a lookup's `relatedApp` as both `app`
15933
+ * and `code`; it is read by `app` (see {@link SchemaParseOptions}). The domain
15934
+ * layer does not depend on YAML; the application layer
15605
15935
  * handles codec decoding before calling this.
15606
15936
  */
15607
15937
  const SchemaStateParser = { parse: (parsed) => {
@@ -15609,7 +15939,7 @@ const SchemaStateParser = { parse: (parsed) => {
15609
15939
  const { revision: _legacyRevision, ...withoutRevision } = parsed;
15610
15940
  return withoutRevision;
15611
15941
  })() : parsed;
15612
- return { schema: SchemaParser.parse(rest) };
15942
+ return { schema: SchemaParser.parse(rest, STATE_OPTIONS) };
15613
15943
  } };
15614
15944
  //#endregion
15615
15945
  //#region src/core/domain/formSchema/services/schemaStateSerializer.ts
@@ -15666,14 +15996,20 @@ async function loadThreeWayInputs(container) {
15666
15996
  container.formConfigurator.getRevision()
15667
15997
  ]);
15668
15998
  const local = localResult.exists ? parseSchemaText(container.configCodec, localResult.content) : void 0;
15669
- const enrichedRemoteLayout = enrichLayoutWithFields(remoteLayout, remoteFields);
15999
+ const projectedRemoteFields = projectRemoteFields(remoteFields, local?.fields);
16000
+ const enrichedRemoteLayout = enrichLayoutWithFields(remoteLayout, projectedRemoteFields);
16001
+ const remote = Schema.create(projectedRemoteFields, enrichedRemoteLayout);
15670
16002
  return {
15671
- state,
16003
+ state: state === void 0 ? void 0 : { schema: restateBase(state.schema, remoteFields, local) },
15672
16004
  local,
15673
- remote: Schema.create(remoteFields, enrichedRemoteLayout),
16005
+ remote,
15674
16006
  remoteRevision
15675
16007
  };
15676
16008
  }
16009
+ function restateBase(base, remoteFields, local) {
16010
+ const fields = restateBaseFields(base.fields, remoteFields, local?.fields);
16011
+ return Schema.create(fields, enrichLayoutWithFields(base.layout, fields));
16012
+ }
15677
16013
  //#endregion
15678
16014
  //#region src/core/application/formSchema/pullSchema.ts
15679
16015
  function serializeSchema(container, schema) {
@@ -19505,6 +19841,7 @@ await cli(process.argv.slice(2), main, {
19505
19841
  diff: diff_default$9,
19506
19842
  pull: pull_default$5,
19507
19843
  push: push_default$6,
19844
+ "check-pending": check_pending_default,
19508
19845
  schema: schema_default,
19509
19846
  seed: seed_default,
19510
19847
  customize: customize_default,