storyblok 4.16.2 → 4.16.3

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
@@ -681,6 +681,7 @@ const API_ACTIONS = {
681
681
  push_datasource: "Failed to push datasource",
682
682
  update_datasource: "Failed to update datasource",
683
683
  delete_datasource: "Failed to delete datasource",
684
+ delete_datasource_entry: "Failed to delete datasource entry",
684
685
  create_space: "Failed to create space",
685
686
  pull_spaces: "Failed to pull spaces",
686
687
  fetch_blueprints: "Failed to fetch blueprints from GitHub"
@@ -5882,7 +5883,7 @@ const upsertDatasource = async (space, datasource, existingId) => {
5882
5883
  return await pushDatasource(space, datasource);
5883
5884
  }
5884
5885
  };
5885
- const pushDatasourceEntry = async (spaceId, datasourceId, entry) => {
5886
+ const pushDatasourceEntry = async (spaceId, datasourceId, entry, position) => {
5886
5887
  try {
5887
5888
  const client = getMapiClient();
5888
5889
  const { data } = await client.datasourceEntries.create({
@@ -5892,7 +5893,8 @@ const pushDatasourceEntry = async (spaceId, datasourceId, entry) => {
5892
5893
  body: {
5893
5894
  datasource_entry: {
5894
5895
  ...entry,
5895
- datasource_id: datasourceId
5896
+ datasource_id: datasourceId,
5897
+ ...position != null && { position }
5896
5898
  }
5897
5899
  },
5898
5900
  throwOnError: true
@@ -5902,7 +5904,7 @@ const pushDatasourceEntry = async (spaceId, datasourceId, entry) => {
5902
5904
  handleAPIError("push_datasource", error, `Failed to push datasource entry ${entry.name}`);
5903
5905
  }
5904
5906
  };
5905
- const updateDatasourceEntry = async (spaceId, entryId, entry) => {
5907
+ const updateDatasourceEntry = async (spaceId, entryId, entry, position) => {
5906
5908
  try {
5907
5909
  const client = getMapiClient();
5908
5910
  await client.datasourceEntries.updateDatasourceEntry({
@@ -5911,7 +5913,10 @@ const updateDatasourceEntry = async (spaceId, entryId, entry) => {
5911
5913
  datasource_entry_id: entryId
5912
5914
  },
5913
5915
  body: {
5914
- datasource_entry: entry
5916
+ datasource_entry: {
5917
+ ...entry,
5918
+ ...position != null && { position }
5919
+ }
5915
5920
  },
5916
5921
  throwOnError: true
5917
5922
  });
@@ -5919,12 +5924,26 @@ const updateDatasourceEntry = async (spaceId, entryId, entry) => {
5919
5924
  handleAPIError("update_datasource", error, `Failed to update datasource entry ${entry.name}`);
5920
5925
  }
5921
5926
  };
5922
- const upsertDatasourceEntry = async (space, datasourceId, entry, existingId) => {
5927
+ const upsertDatasourceEntry = async (space, datasourceId, entry, existingId, position) => {
5923
5928
  if (existingId) {
5924
- await updateDatasourceEntry(space, existingId, entry);
5929
+ await updateDatasourceEntry(space, existingId, entry, position);
5925
5930
  return void 0;
5926
5931
  } else {
5927
- return await pushDatasourceEntry(space, datasourceId, entry);
5932
+ return await pushDatasourceEntry(space, datasourceId, entry, position);
5933
+ }
5934
+ };
5935
+ const deleteDatasourceEntry = async (spaceId, entryId) => {
5936
+ try {
5937
+ const client = getMapiClient();
5938
+ await client.datasourceEntries.delete({
5939
+ path: {
5940
+ space_id: spaceId,
5941
+ datasource_entry_id: entryId
5942
+ },
5943
+ throwOnError: true
5944
+ });
5945
+ } catch (error) {
5946
+ handleAPIError("delete_datasource_entry", error, `Failed to delete datasource entry ${entryId}`);
5928
5947
  }
5929
5948
  };
5930
5949
  const readDatasourcesFiles = async (options) => {
@@ -6187,23 +6206,23 @@ pullCmd$2.action(async (datasourceName, options, command) => {
6187
6206
  handleError(new CommandError(`Please provide the space as argument --space YOUR_SPACE_ID.`), verbose);
6188
6207
  return;
6189
6208
  }
6190
- const spinnerDatasources = new Spinner({
6191
- verbose: !isVitest
6192
- });
6209
+ const ui = getUI();
6210
+ const logger = getLogger();
6211
+ logger.info("Pulling datasources started", { space, datasourceName });
6212
+ const spinnerDatasources = ui.createSpinner(`Fetching ${chalk.hex(colorPalette.DATASOURCES)("datasources")}`);
6193
6213
  try {
6194
- spinnerDatasources.start(`Fetching ${chalk.hex(colorPalette.DATASOURCES)("datasources")}`);
6195
6214
  let datasources;
6196
6215
  if (datasourceName) {
6197
6216
  const datasource = await fetchDatasource(space, datasourceName);
6198
6217
  if (!datasource) {
6199
- konsola.warn(`No datasource found with name "${datasourceName}"`);
6218
+ spinnerDatasources.failed(`No datasource found with name "${datasourceName}"`);
6200
6219
  return;
6201
6220
  }
6202
6221
  datasources = [datasource];
6203
6222
  } else {
6204
6223
  datasources = await fetchDatasources(space);
6205
6224
  if (!datasources || datasources.length === 0) {
6206
- konsola.warn(`No datasources found in the space ${space}`);
6225
+ spinnerDatasources.failed(`No datasources found in the space ${space}`);
6207
6226
  return;
6208
6227
  }
6209
6228
  }
@@ -6237,6 +6256,8 @@ pullCmd$2.action(async (datasourceName, options, command) => {
6237
6256
  spinnerDatasources.failed(`Fetching ${chalk.hex(colorPalette.DATASOURCES)("Datasources")} - Failed`);
6238
6257
  konsola.br();
6239
6258
  handleError(error, verbose);
6259
+ } finally {
6260
+ logger.info("Pulling datasources finished", { space, datasourceName });
6240
6261
  }
6241
6262
  });
6242
6263
 
@@ -6254,6 +6275,8 @@ pushCmd$2.action(async (datasourceName, options, command) => {
6254
6275
  handleError(new CommandError(`Please provide the target space as argument --space TARGET_SPACE_ID.`), verbose);
6255
6276
  return;
6256
6277
  }
6278
+ const logger = getLogger();
6279
+ logger.info("Pushing datasources started", { space, fromSpace, datasourceName, filter });
6257
6280
  konsola.info(`Attempting to push datasources ${chalk.bold("from")} space ${chalk.hex(colorPalette.DATASOURCES)(fromSpace)} ${chalk.bold("to")} ${chalk.hex(colorPalette.DATASOURCES)(space)}`);
6258
6281
  konsola.br();
6259
6282
  try {
@@ -6297,26 +6320,44 @@ pushCmd$2.action(async (datasourceName, options, command) => {
6297
6320
  successful: [],
6298
6321
  failed: []
6299
6322
  };
6323
+ const ui = getUI();
6300
6324
  for (const datasource of spaceState.local.datasources) {
6301
- const spinner = new Spinner({
6302
- verbose: !isVitest
6303
- });
6304
- spinner.start(`Pushing ${chalk.hex(colorPalette.DATASOURCES)(datasource.name)}`);
6325
+ const spinner = ui.createSpinner(`Pushing ${chalk.hex(colorPalette.DATASOURCES)(datasource.name)}`);
6305
6326
  const existingDatasource = spaceState.target.datasources.get(datasource.name);
6306
6327
  const existingId = existingDatasource?.id;
6307
6328
  const { entries, ...datasourceDefinition } = datasource;
6308
6329
  const result = await upsertDatasource(space, datasourceDefinition, existingId);
6309
6330
  if (result) {
6310
6331
  results.successful.push(datasource.name);
6311
- if (entries && entries.length > 0) {
6312
- for (const entry of entries) {
6313
- const existingEntryId = existingDatasource?.entries?.find((e) => e.name === entry.name)?.id;
6314
- try {
6315
- await upsertDatasourceEntry(space, result.id, entry, existingEntryId);
6316
- } catch (entryError) {
6317
- results.failed.push({ name: datasource.name, error: entryError });
6318
- spinner.failed(`${chalk.hex(colorPalette.DATASOURCES)(datasource.name)} - Failed in ${spinner.elapsedTime.toFixed(2)}ms`);
6319
- }
6332
+ const localEntries = entries ?? [];
6333
+ const existingEntries = existingDatasource?.entries ?? [];
6334
+ const existingEntryMap = new Map(existingEntries.map((e, idx) => [e.name, { entry: e, position: idx + 1 }]));
6335
+ for (let i = 0; i < localEntries.length; i++) {
6336
+ const entry = localEntries[i];
6337
+ const existing = existingEntryMap.get(entry.name);
6338
+ const existingEntryId = existing?.entry.id;
6339
+ const targetPosition = i + 1;
6340
+ if (existing && existing.entry.value === entry.value && existing.entry.dimension_value === entry.dimension_value && existing.position === targetPosition) {
6341
+ logger.info("Skipped datasource entry (unchanged)", { datasource: datasource.name, entry: entry.name, position: targetPosition });
6342
+ continue;
6343
+ }
6344
+ try {
6345
+ await upsertDatasourceEntry(space, result.id, entry, existingEntryId, i + 1);
6346
+ logger.info(existingEntryId ? "Updated datasource entry" : "Created datasource entry", { datasource: datasource.name, entry: entry.name, position: i + 1 });
6347
+ } catch (entryError) {
6348
+ results.failed.push({ name: datasource.name, error: entryError });
6349
+ spinner.failed(`${chalk.hex(colorPalette.DATASOURCES)(datasource.name)} - Failed in ${spinner.elapsedTime.toFixed(2)}ms`);
6350
+ }
6351
+ }
6352
+ const localEntryNames = new Set(localEntries.map((e) => e.name));
6353
+ const staleEntries = existingEntries.filter((e) => !localEntryNames.has(e.name));
6354
+ for (const stale of staleEntries) {
6355
+ try {
6356
+ await deleteDatasourceEntry(space, stale.id);
6357
+ logger.info("Deleted datasource entry", { datasource: datasource.name, entry: stale.name, entryId: stale.id });
6358
+ } catch (entryError) {
6359
+ results.failed.push({ name: datasource.name, error: entryError });
6360
+ spinner.failed(`${chalk.hex(colorPalette.DATASOURCES)(datasource.name)} - Failed in ${spinner.elapsedTime.toFixed(2)}ms`);
6320
6361
  }
6321
6362
  }
6322
6363
  spinner.succeed(`${chalk.hex(colorPalette.DATASOURCES)(datasource.name)} - Completed in ${spinner.elapsedTime.toFixed(2)}ms`);
@@ -6337,6 +6378,8 @@ pushCmd$2.action(async (datasourceName, options, command) => {
6337
6378
  }
6338
6379
  } catch (error) {
6339
6380
  handleError(error, verbose);
6381
+ } finally {
6382
+ logger.info("Pushing datasources finished", { space, fromSpace });
6340
6383
  }
6341
6384
  });
6342
6385
 
@@ -6376,14 +6419,14 @@ deleteCmd.action(async (name, options, command) => {
6376
6419
  handleError(new CommandError("Please provide the space as argument --space YOUR_SPACE_ID."), verbose);
6377
6420
  return;
6378
6421
  }
6379
- const spinner = new Spinner({
6380
- verbose: !isVitest
6381
- });
6422
+ const ui = getUI();
6423
+ const logger = getLogger();
6424
+ logger.info("Deleting datasource started", { space, name, id: options.id });
6382
6425
  try {
6383
6426
  if (options.id) {
6384
- spinner.start(`Deleting datasource...`);
6427
+ const spinner = ui.createSpinner(`Deleting datasource...`);
6385
6428
  await deleteDatasource(space, options.id);
6386
- spinner.succeed();
6429
+ spinner.succeed(`Datasource deleted`);
6387
6430
  konsola.ok(`Datasource ${chalk.hex(colorPalette.DATASOURCES)(options.id)} deleted successfully from space ${space}.`);
6388
6431
  } else {
6389
6432
  const datasource = await fetchDatasource(space, name);
@@ -6404,21 +6447,19 @@ deleteCmd.action(async (name, options, command) => {
6404
6447
  default: false
6405
6448
  });
6406
6449
  if (!confirmed) {
6407
- spinner.failed("Deletion aborted by user.");
6408
6450
  konsola.warn("Deletion aborted by user.");
6409
6451
  return;
6410
6452
  }
6411
6453
  }
6412
- spinner.start(`Deleting datasource...`);
6454
+ const spinner = ui.createSpinner(`Deleting datasource...`);
6413
6455
  await deleteDatasource(space, datasource.id.toString());
6414
- spinner.succeed();
6456
+ spinner.succeed(`Datasource deleted`);
6415
6457
  konsola.ok(`Datasource ${chalk.hex(colorPalette.DATASOURCES)(name)} deleted successfully from space ${space}.`);
6416
6458
  }
6417
6459
  } catch (error) {
6418
- spinner.failed(
6419
- `Failed to delete datasource ${chalk.hex(colorPalette.DATASOURCES)(options.id ? options.id : name)}`
6420
- );
6421
6460
  handleError(error, verbose);
6461
+ } finally {
6462
+ logger.info("Deleting datasource finished", { space, name, id: options.id });
6422
6463
  }
6423
6464
  });
6424
6465
 
@@ -9164,7 +9205,7 @@ pushCmd.action(async (options, command) => {
9164
9205
  })
9165
9206
  );
9166
9207
  } catch (maybeError) {
9167
- handleError(toError(maybeError));
9208
+ handleError(toError(maybeError), verbose);
9168
9209
  } finally {
9169
9210
  logger.info("Pushing stories finished", summary);
9170
9211
  ui.stopAllProgressBars();