storyblok 4.6.3 → 4.6.4

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
@@ -9,7 +9,7 @@ import { Spinner } from '@topcli/spinner';
9
9
  import { select, password, input, confirm } from '@inquirer/prompts';
10
10
  import { ManagementApiClient } from '@storyblok/management-api-client';
11
11
  import { RateLimit, Sema } from 'async-sema';
12
- import fs, { mkdir, writeFile, readFile as readFile$1, access, readdir } from 'node:fs/promises';
12
+ import fs, { mkdir, writeFile, readFile as readFile$1, appendFile, access, readdir } from 'node:fs/promises';
13
13
  import path, { join, parse, resolve } from 'node:path';
14
14
  import filenamify from 'filenamify';
15
15
  import { exec, spawn } from 'node:child_process';
@@ -668,6 +668,22 @@ const saveToFile = async (filePath, data, options) => {
668
668
  handleFileSystemError("write", writeError);
669
669
  }
670
670
  };
671
+ const appendToFile = async (filePath, data, options) => {
672
+ const resolvedPath = parse(filePath).dir;
673
+ try {
674
+ await mkdir(resolvedPath, { recursive: true });
675
+ } catch (mkdirError) {
676
+ handleFileSystemError("mkdir", mkdirError);
677
+ return;
678
+ }
679
+ try {
680
+ const dataWithNewline = data.endsWith("\n") ? data : `${data}
681
+ `;
682
+ await appendFile(filePath, dataWithNewline, options);
683
+ } catch (writeError) {
684
+ handleFileSystemError("write", writeError);
685
+ }
686
+ };
671
687
  const readFile = async (filePath) => {
672
688
  try {
673
689
  return await readFile$1(filePath, "utf8");
@@ -3048,69 +3064,58 @@ async function getMigrationFunction(fileName, space, basePath) {
3048
3064
  }
3049
3065
  }
3050
3066
  function applyMigrationToAllBlocks(content, migrationFunction, targetComponent) {
3067
+ let processed = false;
3051
3068
  if (!content || typeof content !== "object") {
3052
- return false;
3069
+ return processed;
3053
3070
  }
3054
- let modified = false;
3055
3071
  const baseTargetComponent = targetComponent.split(".")[0];
3072
+ let migratedContent = null;
3056
3073
  if (content.component === baseTargetComponent) {
3057
- const migratedContent = migrationFunction({ ...content });
3058
- Object.assign(content, migratedContent);
3059
- modified = true;
3074
+ migratedContent = migrationFunction({ ...content });
3075
+ processed = true;
3060
3076
  }
3061
- for (const key in content) {
3062
- if (Object.prototype.hasOwnProperty.call(content, key)) {
3063
- const value = content[key];
3064
- if (Array.isArray(value)) {
3065
- for (let i = 0; i < value.length; i++) {
3066
- if (value[i] && typeof value[i] === "object") {
3067
- const blockModified = applyMigrationToAllBlocks(value[i], migrationFunction, targetComponent);
3068
- modified = modified || blockModified;
3069
- }
3077
+ for (const key of Object.keys(content)) {
3078
+ if (migratedContent) {
3079
+ if (!(key in migratedContent)) {
3080
+ delete content[key];
3081
+ continue;
3082
+ }
3083
+ content[key] = migratedContent[key];
3084
+ }
3085
+ if (Array.isArray(content[key])) {
3086
+ for (const value of content[key]) {
3087
+ if (value && typeof value === "object") {
3088
+ const blockProcessed = applyMigrationToAllBlocks(value, migrationFunction, targetComponent);
3089
+ processed = processed || blockProcessed;
3070
3090
  }
3071
- } else if (value && typeof value === "object") {
3072
- const blockModified = applyMigrationToAllBlocks(value, migrationFunction, targetComponent);
3073
- modified = modified || blockModified;
3074
3091
  }
3092
+ } else if (content[key] && typeof content[key] === "object") {
3093
+ const blockProcessed = applyMigrationToAllBlocks(content[key], migrationFunction, targetComponent);
3094
+ processed = processed || blockProcessed;
3075
3095
  }
3076
3096
  }
3077
- return modified;
3097
+ return processed;
3078
3098
  }
3079
3099
 
3080
3100
  async function saveRollbackData({
3081
3101
  space,
3082
3102
  path,
3083
- stories,
3103
+ story,
3104
+ migrationTimestamp,
3084
3105
  migrationFile
3085
3106
  }) {
3086
3107
  const rollbackData = {
3087
- stories: stories.map((story) => ({
3088
- storyId: story.id,
3089
- name: story.name,
3090
- content: story.content
3091
- }))
3108
+ storyId: story.id,
3109
+ name: story.name,
3110
+ content: story.content
3092
3111
  };
3093
3112
  const rollbacksPath = resolvePath(path, `migrations/${space}/rollbacks`);
3094
- const timestamp = Date.now();
3095
- const rollbackFileName = `${migrationFile.replace(".js", "")}.${timestamp}.json`;
3113
+ const rollbackFileName = `${migrationFile.replace(".js", "")}.${migrationTimestamp}.jsonl`;
3096
3114
  const rollbackFilePath = join(rollbacksPath, rollbackFileName);
3097
- try {
3098
- await saveToFile(
3099
- rollbackFilePath,
3100
- JSON.stringify(rollbackData, null, 2)
3101
- );
3102
- } catch (error) {
3103
- if (error.code === "ENOENT") {
3104
- const fs = await import('node:fs/promises');
3105
- await fs.mkdir(rollbacksPath, { recursive: true });
3106
- await saveToFile(
3107
- rollbackFilePath,
3108
- JSON.stringify(rollbackData, null, 2)
3109
- );
3110
- } else {
3111
- throw error;
3112
- }
3113
- }
3115
+ await appendToFile(
3116
+ rollbackFilePath,
3117
+ JSON.stringify(rollbackData)
3118
+ );
3114
3119
  }
3115
3120
  async function readRollbackFile({
3116
3121
  space,
@@ -3120,8 +3125,10 @@ async function readRollbackFile({
3120
3125
  try {
3121
3126
  const resolvedPath = resolvePath(path, `migrations/${space}/rollbacks`);
3122
3127
  const rollbackFilePath = join(resolvedPath, migrationFile);
3123
- const filePath = rollbackFilePath.endsWith(".json") ? rollbackFilePath : `${rollbackFilePath}.json`;
3124
- return JSON.parse(await readFile$1(filePath, "utf-8"));
3128
+ const filePath = rollbackFilePath.endsWith(".jsonl") ? rollbackFilePath : `${rollbackFilePath}.jsonl`;
3129
+ return {
3130
+ stories: (await readFile$1(filePath, "utf-8")).trim().split("\n").filter(Boolean).map((x) => JSON.parse(x))
3131
+ };
3125
3132
  } catch (error) {
3126
3133
  throw new CommandError(`Failed to read rollback file: ${error.message}`);
3127
3134
  }
@@ -3140,6 +3147,7 @@ class MigrationStream extends Transform {
3140
3147
  totalProcessed: 0
3141
3148
  };
3142
3149
  }
3150
+ timestamp = Date.now();
3143
3151
  results;
3144
3152
  migrationFunctions = /* @__PURE__ */ new Map();
3145
3153
  totalProcessed = 0;
@@ -3206,16 +3214,17 @@ class MigrationStream extends Transform {
3206
3214
  await saveRollbackData({
3207
3215
  space: this.options.space,
3208
3216
  path: this.options.path,
3209
- stories: [{ id: story.id, name: story.name || "", content: story.content }],
3217
+ story: { id: story.id, name: story.name || "", content: story.content },
3218
+ migrationTimestamp: this.timestamp,
3210
3219
  migrationFile: migrationFile.name
3211
3220
  });
3212
3221
  const storyContent = structuredClone(story.content);
3213
- const originalContentHash = hash(story.content);
3222
+ const originalContentHash = hash(storyContent);
3214
3223
  const targetComponent = this.options.componentName || getComponentNameFromFilename(migrationFile.name);
3215
- const modified = applyMigrationToAllBlocks(storyContent, migrationFunction, targetComponent);
3224
+ const processed = applyMigrationToAllBlocks(storyContent, migrationFunction, targetComponent);
3216
3225
  const newContentHash = hash(storyContent);
3217
3226
  const contentChanged = originalContentHash !== newContentHash;
3218
- if (modified && contentChanged) {
3227
+ if (processed && contentChanged) {
3219
3228
  this.results.successful.push({
3220
3229
  storyId: story.id,
3221
3230
  name: story.name,
@@ -3227,7 +3236,7 @@ class MigrationStream extends Transform {
3227
3236
  name: story.name,
3228
3237
  content: storyContent
3229
3238
  };
3230
- } else if (modified && !contentChanged) {
3239
+ } else if (processed && !contentChanged) {
3231
3240
  this.results.skipped.push({
3232
3241
  storyId: story.id,
3233
3242
  name: story.name,
@@ -3535,6 +3544,13 @@ migrationsCommand.command("rollback [migrationFile]").description("Rollback a mi
3535
3544
  handleError(new CommandError(`Please provide the space as argument --space YOUR_SPACE_ID.`), verbose);
3536
3545
  return;
3537
3546
  }
3547
+ const { password, region } = state;
3548
+ mapiClient({
3549
+ token: {
3550
+ accessToken: password
3551
+ },
3552
+ region
3553
+ });
3538
3554
  try {
3539
3555
  const rollbackData = await readRollbackFile({
3540
3556
  space,
@@ -5141,7 +5157,7 @@ program$1.command(`${commands.CREATE} [project-path]`).alias("c").description(`S
5141
5157
  konsola.br();
5142
5158
  });
5143
5159
 
5144
- const version = "4.6.3";
5160
+ const version = "4.6.4";
5145
5161
  const pkg = {
5146
5162
  version: version};
5147
5163