@jacebenson/jsn 5.4.0-dev.152.1 → 5.4.0-dev.153.1

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jacebenson/jsn",
3
- "version": "5.4.0-dev.152.1",
3
+ "version": "5.4.0-dev.153.1",
4
4
  "description": "A command-line interface for ServiceNow that follows the Unix philosophy: simple, composable, and scriptable.",
5
5
  "type": "module",
6
6
  "bin": {
@@ -101,7 +101,10 @@ export function flowsCmd(wrap) {
101
101
  if (identifier === undefined) return;
102
102
  const record = await resolveRecord(app.sdk, { table: 'sys_hub_flow', identifier, matchField: 'name', resource: 'Flow' });
103
103
  const flowId = unwrapSysId(record);
104
- const scope = argv.scope || getStringField(record, 'sys_scope') || 'global';
104
+ // resolveRecord reads display values; sending `Global` as a
105
+ // transaction scope is not a scope id. Prefer the raw value.
106
+ const rawScope = record?.sys_scope && typeof record.sys_scope === 'object' ? record.sys_scope.value : record?.sys_scope;
107
+ const scope = argv.scope || rawScope || 'global';
105
108
  const result = await readCatalogItemVariables(app.sdk, app.getEffectiveInstance(), flowId, catalogItem, { scope });
106
109
  app.ok(result, {
107
110
  summary: `${result.count} catalog variable(s) on ${catalogItem}; trigger catalog_variables input: ${result.catalog_variables_input || '(none)'}`,
@@ -204,17 +204,35 @@ export function buildFlowProperties(manifest) {
204
204
  // placed on that stage. `componentIndexes` is why a placement update must run
205
205
  // after the actions exist.
206
206
  const STAGE_DURATION = '1970-01-01 00:00:00';
207
- const STAGE_STATES = Object.freeze({
208
- pending: 'Pending - has not started',
209
- in_progress: 'In progress',
210
- skipped: 'Skipped',
211
- complete: 'Completed',
212
- error: 'Error',
213
- });
207
+ // ProcessFlow returns `states` as a list of {label, name}; the REST stage map
208
+ // (PUT /processflow/flow/{id}/stages) returns it keyed by name. Sending the
209
+ // keyed form is not what Flow Designer sends, so use the list on insert.
210
+ const STAGE_STATES = [
211
+ { label: 'Pending - has not started', name: 'pending' },
212
+ { label: 'In progress', name: 'in_progress' },
213
+ { label: 'Skipped', name: 'skipped' },
214
+ { label: 'Completed', name: 'complete' },
215
+ { label: 'Error', name: 'error' },
216
+ ];
214
217
  const STAGE_KEYS = ['label', 'value', 'order', 'system', 'componentIndexes', 'stageId', 'duration', 'states'];
215
218
 
216
219
  const asBool = (value) => value === true || value === 'true';
217
220
 
221
+ /**
222
+ * Stages as a list, whichever shape the definition uses.
223
+ *
224
+ * ProcessFlow readback carries `stages` as an object keyed by stageId; the REST
225
+ * stage map uses the same shape while a definition built in code may hold an
226
+ * array. Treating only the array form as data made a live flow look like it had
227
+ * no stages at all.
228
+ */
229
+ export function stageList(definition) {
230
+ const stages = definition?.stages;
231
+ if (Array.isArray(stages)) return stages;
232
+ if (stages && typeof stages === 'object') return Object.values(stages).filter(stage => stage && typeof stage === 'object');
233
+ return [];
234
+ }
235
+
218
236
  function stageValueFromLabel(label) {
219
237
  return String(label).trim().toLowerCase().replace(/[^a-z0-9]+/g, '_').replace(/^_+|_+$/g, '');
220
238
  }
@@ -254,7 +272,7 @@ export function validateStages(stages, { update = false } = {}) {
254
272
  return stages;
255
273
  }
256
274
 
257
- export function buildStageInserts(stages = [], idFactory = () => randomUUID()) {
275
+ export function buildStageInserts(stages = [], { idFactory = () => randomUUID(), flowId } = {}) {
258
276
  return stages
259
277
  .map((stage, index) => ({ stage, index }))
260
278
  .sort((a, b) => (a.stage.order ?? a.index) - (b.stage.order ?? b.index))
@@ -262,10 +280,14 @@ export function buildStageInserts(stages = [], idFactory = () => randomUUID()) {
262
280
  stageId: idFactory(),
263
281
  label: String(stage.label).trim(),
264
282
  value: stage.value ?? stageValueFromLabel(stage.label),
283
+ type: 'standard',
284
+ duration: stage.duration ?? STAGE_DURATION,
285
+ ancestorArrPos: '0',
286
+ ...(flowId ? { flow: flowId } : {}),
287
+ alwaysShow: true,
265
288
  order: stage.order ?? index,
266
289
  system: stage.system ?? false,
267
290
  componentIndexes: stage.componentIndexes ?? [],
268
- duration: stage.duration ?? STAGE_DURATION,
269
291
  states: stage.states ?? STAGE_STATES,
270
292
  }));
271
293
  }
@@ -530,7 +552,7 @@ function actionTypeIdOf(action) {
530
552
  */
531
553
  function assertStagesPlaced(processflow, stageInserts, flowId) {
532
554
  if (!stageInserts.length) return;
533
- const saved = findArray(processflow, 'stages');
555
+ const saved = stageList(processflow);
534
556
  if (!saved.length) throw new Error(`Flow ${flowId} stage read-back returned no stages after placing ${stageInserts.length}`);
535
557
  for (const expected of stageInserts) {
536
558
  const matches = saved.filter(stage => stringValue(stage.stageId || stage.stage_id) === expected.stageId);
@@ -1116,7 +1138,7 @@ export async function createFlowFromManifest(sdk, instance, inputManifest, optio
1116
1138
 
1117
1139
  // Stages carry componentIndexes that name flow-step orders, so placement
1118
1140
  // runs after the actions exist — the same order Flow Designer uses.
1119
- const stageInserts = buildStageInserts(manifest.stages || [], idFactory);
1141
+ const stageInserts = buildStageInserts(manifest.stages || [], { idFactory, flowId });
1120
1142
  if (stageInserts.length) {
1121
1143
  const stageResponse = await mutationContext('stage_placement', readUpdateSet, snapshots, () => sdk.request(`${instance}${GRAPHQL_ENDPOINT}`, {
1122
1144
  method: 'POST',
@@ -1190,7 +1212,7 @@ export async function updateFlowFromManifest(sdk, instance, identifier, inputMan
1190
1212
  const snapshots = [];
1191
1213
  const triggerUpdates = [];
1192
1214
  const actionUpdates = [];
1193
- const stageInserts = buildStageInserts((manifest.stages || []).filter(stage => stage.stageId === undefined), options.idFactory || (() => randomUUID()));
1215
+ const stageInserts = buildStageInserts((manifest.stages || []).filter(stage => stage.stageId === undefined), { idFactory: options.idFactory || (() => randomUUID()), flowId });
1194
1216
  const stageUpdates = buildStageUpdates((manifest.stages || []).filter(stage => stage.stageId !== undefined));
1195
1217
  const changedActionInputs = new Map();
1196
1218
  const actionTargets = new Map();
@@ -1219,7 +1241,7 @@ export async function updateFlowFromManifest(sdk, instance, identifier, inputMan
1219
1241
  changedActionInputs.set(uiId, new Set(Object.keys(requested.inputs)));
1220
1242
  for (const [input, after] of Object.entries(requested.inputs)) changes.push({ component: 'action', order: requested.order, type: requested.type, input, before: inputRawValue(inputList(existing).find(field => field.name === input)), after });
1221
1243
  }
1222
- const savedStages = findArray(beforeDefinition, 'stages');
1244
+ const savedStages = stageList(beforeDefinition);
1223
1245
  const savedStageById = new Map(savedStages.map(stage => [stringValue(stage.stageId || stage.stage_id), stage]));
1224
1246
  for (const update of stageUpdates) {
1225
1247
  const before = savedStageById.get(update.stageId);
@@ -1274,7 +1296,7 @@ export async function updateFlowFromManifest(sdk, instance, identifier, inputMan
1274
1296
  }
1275
1297
  if (stageInserts.length || stageUpdates.length) {
1276
1298
  const requestedStageIds = new Set([...stageInserts.map(stage => stage.stageId), ...stageUpdates.map(stage => stage.stageId)]);
1277
- const untouched = definition => findArray(definition, 'stages')
1299
+ const untouched = definition => stageList(definition)
1278
1300
  .filter(stage => !requestedStageIds.has(stringValue(stage.stageId || stage.stage_id)));
1279
1301
  if (!isDeepStrictEqual(untouched(beforeDefinition), untouched(processflow))) throw new Error(`Flow ${flowId} changed omitted stages; preservation failed`);
1280
1302
  assertStagesPlaced(processflow, [...stageInserts, ...stageUpdates.map(update => ({