sb-mig 5.0.5 → 5.0.6

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.
@@ -1,6 +1,7 @@
1
1
  import type { RequestBaseConfig } from "../utils/request.js";
2
2
  export type MigrateFrom = "file" | "space";
3
- interface MigrateStories {
3
+ interface MigrateItems {
4
+ itemType: "story" | "preset";
4
5
  from: string;
5
6
  to: string;
6
7
  migrateFrom: MigrateFrom;
@@ -10,7 +11,7 @@ interface MigrateStories {
10
11
  export type MapperDefinition = (data: any) => any;
11
12
  export declare const prepareStoriesFromLocalFile: ({ from }: any) => any;
12
13
  export declare const prepareMigrationConfig: ({ migrationConfig }: any) => any;
13
- export declare const migrateAllComponentsDataInStories: ({ migrationConfig, migrateFrom, from, to, }: Omit<MigrateStories, "componentsToMigrate">, config: RequestBaseConfig) => Promise<void>;
14
- export declare const doTheMigration: ({ from, storiesToMigrate, componentsToMigrate, migrationConfigFileContent, migrationConfig, to, }: any, config: RequestBaseConfig) => Promise<void>;
15
- export declare const migrateProvidedComponentsDataInStories: ({ migrationConfig, migrateFrom, from, to, componentsToMigrate, }: MigrateStories, config: RequestBaseConfig) => Promise<void>;
14
+ export declare const migrateAllComponentsDataInStories: ({ itemType, migrationConfig, migrateFrom, from, to, }: Omit<MigrateItems, "componentsToMigrate">, config: RequestBaseConfig) => Promise<void>;
15
+ export declare const doTheMigration: ({ itemType, from, itemsToMigrate, componentsToMigrate, migrationConfigFileContent, migrationConfig, to, }: any, config: RequestBaseConfig) => Promise<void>;
16
+ export declare const migrateProvidedComponentsDataInStories: ({ itemType, migrationConfig, migrateFrom, from, to, componentsToMigrate, }: MigrateItems, config: RequestBaseConfig) => Promise<void>;
16
17
  export {};
@@ -6,7 +6,7 @@ import { createAndSaveToFile } from "../../utils/files.js";
6
6
  import Logger from "../../utils/logger.js";
7
7
  import { getFilesContentWithRequire, isObjectEmpty } from "../../utils/main.js";
8
8
  import { modifyOrCreateAppliedMigrationsFile } from "../../utils/migrations.js";
9
- import { getAllStories, updateStories } from "../stories/index.js";
9
+ import { managementApi } from "../managementApi.js";
10
10
  function replaceComponentData({ parent, key, components, mapper, depth, maxDepth, sumOfReplacing, }) {
11
11
  let currentMaxDepth = depth;
12
12
  if (storyblokConfig.debug) {
@@ -101,19 +101,19 @@ export const prepareMigrationConfig = ({ migrationConfig }) => {
101
101
  Logger.success(`Migration config loaded.`);
102
102
  return migrationConfigFileContent;
103
103
  };
104
- export const migrateAllComponentsDataInStories = async ({ migrationConfig, migrateFrom, from, to, }, config) => {
105
- Logger.warning(`Trying to migrate all stories from ${migrateFrom}, ${from} to ${to}...`);
104
+ export const migrateAllComponentsDataInStories = async ({ itemType, migrationConfig, migrateFrom, from, to, }, config) => {
105
+ Logger.warning(`Trying to migrate all ${itemType} from ${migrateFrom}, ${from} to ${to}...`);
106
106
  const migrationConfigFileContent = prepareMigrationConfig({
107
107
  migrationConfig,
108
108
  });
109
109
  // Taking every component defined in const config = {} in migration config file
110
110
  const componentsToMigrate = Object.keys(migrationConfigFileContent);
111
- console.log(componentsToMigrate);
112
111
  if (storyblokConfig.debug) {
113
112
  Logger.warning("_________ Components in stories to migrate ___________");
114
113
  console.log(componentsToMigrate);
115
114
  }
116
115
  await migrateProvidedComponentsDataInStories({
116
+ itemType,
117
117
  migrationConfig,
118
118
  migrateFrom,
119
119
  from,
@@ -121,15 +121,14 @@ export const migrateAllComponentsDataInStories = async ({ migrationConfig, migra
121
121
  componentsToMigrate,
122
122
  }, config);
123
123
  };
124
- export const doTheMigration = async ({ from, storiesToMigrate, componentsToMigrate, migrationConfigFileContent, migrationConfig, to, }, config) => {
125
- console.log(to);
124
+ export const doTheMigration = async ({ itemType = "story", from, itemsToMigrate, componentsToMigrate, migrationConfigFileContent, migrationConfig, to, }, config) => {
126
125
  const arrayOfMaxDepths = [];
127
- const migratedStories = storiesToMigrate.map((stories, index) => {
126
+ const migratedItems = itemsToMigrate.map((item, index) => {
128
127
  const sumOfReplacing = {};
129
128
  if (storyblokConfig.debug) {
130
129
  Logger.success(`# ${index} #`);
131
130
  }
132
- const json = stories.story.content;
131
+ const json = itemType === "story" ? item[itemType].content : item[itemType];
133
132
  const maxDepth = replaceComponentData({
134
133
  parent: { root: json },
135
134
  key: "root",
@@ -142,7 +141,9 @@ export const doTheMigration = async ({ from, storiesToMigrate, componentsToMigra
142
141
  arrayOfMaxDepths.push(maxDepth);
143
142
  if (Object.keys(sumOfReplacing).length > 0) {
144
143
  console.log(" ");
145
- console.log(`Migration in ${chalk.magenta(stories.story.full_slug)} page: `);
144
+ console.log(`Migration in ${chalk.magenta(itemType === "story"
145
+ ? item[itemType].full_slug
146
+ : item[itemType].name)} page: `);
146
147
  componentsToMigrate.forEach((component) => {
147
148
  if (sumOfReplacing[component]) {
148
149
  console.log(`${chalk.blue(component)} component data was replaced: ${sumOfReplacing[component]} times.`);
@@ -151,11 +152,15 @@ export const doTheMigration = async ({ from, storiesToMigrate, componentsToMigra
151
152
  }
152
153
  if (Object.keys(sumOfReplacing).length > 0) {
153
154
  return {
154
- ...stories,
155
- story: {
156
- ...stories.story,
157
- content: json,
158
- },
155
+ ...item,
156
+ [itemType]: itemType === "story"
157
+ ? {
158
+ ...item[itemType],
159
+ content: json,
160
+ }
161
+ : {
162
+ ...json,
163
+ },
159
164
  };
160
165
  }
161
166
  else {
@@ -174,25 +179,35 @@ export const doTheMigration = async ({ from, storiesToMigrate, componentsToMigra
174
179
  console.log(" ");
175
180
  }
176
181
  const isListEmpty = (list) => list.filter((item) => item).length;
177
- if (!isListEmpty(migratedStories)) {
182
+ if (!isListEmpty(migratedItems)) {
178
183
  console.log("# No Stories to update #");
179
184
  }
180
185
  else {
181
- console.log(`${migratedStories.length} stories to migrate`);
186
+ console.log(`${migratedItems.length} stories to migrate`);
182
187
  }
188
+ const notNullMigratedItems = migratedItems.filter((item) => item);
183
189
  // Saving result with migrated version of stories into file
184
190
  await createAndSaveToFile({
185
191
  ext: "cjs",
186
192
  filename: `${from}---migrated`,
187
193
  folder: "migrations",
188
- res: migratedStories.filter((item) => item),
194
+ res: notNullMigratedItems,
189
195
  });
190
196
  await modifyOrCreateAppliedMigrationsFile(migrationConfig);
191
- await updateStories({
192
- stories: migratedStories,
193
- spaceId: to,
194
- options: { publish: false },
195
- }, config);
197
+ if (itemType === "story") {
198
+ await managementApi.stories.updateStories({
199
+ stories: notNullMigratedItems,
200
+ spaceId: to,
201
+ options: { publish: false },
202
+ }, config);
203
+ }
204
+ else if (itemType === "preset") {
205
+ await managementApi.presets.updatePresets({
206
+ presets: notNullMigratedItems,
207
+ spaceId: to,
208
+ options: {},
209
+ }, config);
210
+ }
196
211
  };
197
212
  const saveBackupStoriesToFile = async ({ res, folder, filename, }) => {
198
213
  await createAndSaveToFile({
@@ -204,16 +219,16 @@ const saveBackupStoriesToFile = async ({ res, folder, filename, }) => {
204
219
  res: res,
205
220
  });
206
221
  };
207
- export const migrateProvidedComponentsDataInStories = async ({ migrationConfig, migrateFrom, from, to, componentsToMigrate, }, config) => {
222
+ export const migrateProvidedComponentsDataInStories = async ({ itemType, migrationConfig, migrateFrom, from, to, componentsToMigrate, }, config) => {
208
223
  const migrationConfigFileContent = prepareMigrationConfig({
209
224
  migrationConfig,
210
225
  });
211
226
  if (migrateFrom === "file") {
212
227
  Logger.log("Migrating using file....");
213
228
  // Get all stories to be migrated from file
214
- const storiesToMigrate = prepareStoriesFromLocalFile({ from });
229
+ const itemsToMigrate = prepareStoriesFromLocalFile({ from });
215
230
  await doTheMigration({
216
- storiesToMigrate,
231
+ itemsToMigrate,
217
232
  componentsToMigrate,
218
233
  migrationConfigFileContent,
219
234
  to,
@@ -221,19 +236,29 @@ export const migrateProvidedComponentsDataInStories = async ({ migrationConfig,
221
236
  }
222
237
  else if (migrateFrom === "space") {
223
238
  // Get all stories to be migrated from storyblok space
224
- const storiesToMigrate = await getAllStories({
225
- ...config,
226
- spaceId: from,
227
- });
239
+ let itemsToMigrate = [];
240
+ if (itemType === "story") {
241
+ itemsToMigrate = await managementApi.stories.getAllStories({
242
+ ...config,
243
+ spaceId: from,
244
+ });
245
+ }
246
+ else if (itemType === "preset") {
247
+ itemsToMigrate = await managementApi.presets.getAllPresets({
248
+ ...config,
249
+ spaceId: from,
250
+ });
251
+ }
228
252
  const backupFolder = path.join("backup", "stories");
229
253
  // save stories to file as backup
230
254
  await saveBackupStoriesToFile({
231
255
  filename: `before__${migrationConfig}__${from}`,
232
256
  folder: backupFolder,
233
- res: storiesToMigrate,
257
+ res: itemsToMigrate,
234
258
  });
235
259
  await doTheMigration({
236
- storiesToMigrate,
260
+ itemType,
261
+ itemsToMigrate,
237
262
  componentsToMigrate,
238
263
  migrationConfigFileContent,
239
264
  migrationConfig,
@@ -40,10 +40,11 @@ export declare const managementApi: {
40
40
  };
41
41
  presets: {
42
42
  getComponentPresets: (componentName: string | undefined, config: import("./utils/request.js").RequestBaseConfig) => Promise<false | any[]>;
43
- getPreset: (presetId: string | undefined, config: import("./utils/request.js").RequestBaseConfig) => Promise<any>;
43
+ getPreset: import("./presets/presets.types.js").GetPresetById;
44
44
  createPreset: (p: any, config: import("./utils/request.js").RequestBaseConfig) => Promise<any>;
45
45
  getAllPresets: (config: import("./utils/request.js").RequestBaseConfig) => Promise<any[]>;
46
- updatePreset: (p: any, config: import("./utils/request.js").RequestBaseConfig) => Promise<any>;
46
+ updatePreset: import("./presets/presets.types.js").UpdatePreset;
47
+ updatePresets: import("./presets/presets.types.js").UpdatePresets;
47
48
  };
48
49
  roles: {
49
50
  createRole: import("./roles/roles.types.js").CreateRole;
@@ -1,6 +1,5 @@
1
1
  import Logger from "../../utils/logger.js";
2
2
  import { managementApi } from "../managementApi.js";
3
- import { getPreset } from "./presets.js";
4
3
  export const getComponentPresets = (componentName, config) => {
5
4
  Logger.log(`Trying to get all '${componentName}' presets.`);
6
5
  return managementApi.components
@@ -12,7 +11,9 @@ export const getComponentPresets = (componentName, config) => {
12
11
  Logger.warning(`There is no presets for: '${componentName}' component`);
13
12
  return false;
14
13
  }
15
- return Promise.all(componentPresets[0].all_presets.map((preset) => getPreset(preset.id, config).catch((err) => Logger.error(err))));
14
+ return Promise.all(componentPresets[0].all_presets.map((preset) => managementApi.presets
15
+ .getPreset({ presetId: preset.id }, config)
16
+ .catch((err) => Logger.error(err))));
16
17
  }
17
18
  Logger.warning(`There is no '${componentName}' component`);
18
19
  return false;
@@ -1,2 +1,2 @@
1
1
  export { getComponentPresets } from "./componentPresets.js";
2
- export { getPreset, createPreset, getAllPresets, updatePreset, } from "./presets.js";
2
+ export { getPreset, createPreset, getAllPresets, updatePreset, updatePresets, } from "./presets.js";
@@ -1,2 +1,2 @@
1
1
  export { getComponentPresets } from "./componentPresets.js";
2
- export { getPreset, createPreset, getAllPresets, updatePreset, } from "./presets.js";
2
+ export { getPreset, createPreset, getAllPresets, updatePreset, updatePresets, } from "./presets.js";
@@ -1,5 +1,7 @@
1
+ import type { GetPresetById, UpdatePreset, UpdatePresets } from "./presets.types.js";
1
2
  import type { RequestBaseConfig } from "../utils/request.js";
2
- export declare const getPreset: (presetId: string | undefined, config: RequestBaseConfig) => Promise<any>;
3
+ export declare const getPreset: GetPresetById;
3
4
  export declare const getAllPresets: (config: RequestBaseConfig) => Promise<any[]>;
4
5
  export declare const createPreset: (p: any, config: RequestBaseConfig) => Promise<any>;
5
- export declare const updatePreset: (p: any, config: RequestBaseConfig) => Promise<any>;
6
+ export declare const updatePreset: UpdatePreset;
7
+ export declare const updatePresets: UpdatePresets;
@@ -1,7 +1,8 @@
1
1
  import Logger from "../../utils/logger.js";
2
2
  import { getAllItemsWithPagination } from "../utils/request.js";
3
3
  // GET
4
- export const getPreset = (presetId, config) => {
4
+ export const getPreset = (args, config) => {
5
+ const { presetId } = args;
5
6
  const { spaceId, sbApi } = config;
6
7
  Logger.log(`Trying to get preset by id: ${presetId}`);
7
8
  return sbApi
@@ -53,7 +54,8 @@ export const createPreset = (p, config) => {
53
54
  });
54
55
  };
55
56
  // UPDATE
56
- export const updatePreset = (p, config) => {
57
+ export const updatePreset = (args, config) => {
58
+ const { p } = args;
57
59
  const { spaceId, sbApi } = config;
58
60
  return sbApi
59
61
  .put(`spaces/${spaceId}/presets/${p.preset.id}`, {
@@ -67,3 +69,14 @@ export const updatePreset = (p, config) => {
67
69
  Logger.error(`Error happened. Preset: '${p.preset.name}' with '${p.preset.id}' id has been not updated.`);
68
70
  });
69
71
  };
72
+ export const updatePresets = (args, config) => {
73
+ const { sbApi } = config;
74
+ const { presets, spaceId } = args;
75
+ return Promise.allSettled(presets.map(async (item) => {
76
+ return updatePreset({
77
+ p: {
78
+ preset: item,
79
+ },
80
+ }, { sbApi, spaceId });
81
+ }));
82
+ };
@@ -0,0 +1,14 @@
1
+ import type { RequestBaseConfig } from "../utils/request.js";
2
+ export type UpdatePresets = (args: {
3
+ presets: any;
4
+ options: {
5
+ publish?: boolean;
6
+ };
7
+ spaceId: string;
8
+ }, config: RequestBaseConfig) => Promise<any>;
9
+ export type UpdatePreset = (args: {
10
+ p: any;
11
+ }, config: RequestBaseConfig) => Promise<any>;
12
+ export type GetPresetById = (args: {
13
+ presetId: string | undefined;
14
+ }, config: RequestBaseConfig) => Promise<any>;
@@ -0,0 +1 @@
1
+ export {};
@@ -222,7 +222,7 @@ export const backup = async (props) => {
222
222
  if (isIt("empty")) {
223
223
  const presetToBackup = unpackOne(input);
224
224
  managementApi.presets
225
- .getPreset(presetToBackup, apiConfig)
225
+ .getPreset({ presetId: presetToBackup }, apiConfig)
226
226
  .then(async (res) => {
227
227
  await createAndSaveToFile({
228
228
  ext: "json",
@@ -1,11 +1,14 @@
1
1
  import { migrateAllComponentsDataInStories, migrateProvidedComponentsDataInStories, } from "../../api/data-migration/component-data-migration.js";
2
+ import { managementApi } from "../../api/managementApi.js";
2
3
  import { backupStories } from "../../api/stories/backup.js";
4
+ import { createAndSaveToFile } from "../../utils/files.js";
3
5
  import Logger from "../../utils/logger.js";
4
6
  import { isItFactory, unpackElements } from "../../utils/main.js";
5
7
  import { askForConfirmation, getFrom, getTo } from "../../utils/others.js";
6
8
  import { apiConfig } from "../api-config.js";
7
9
  const MIGRATE_COMMANDS = {
8
10
  content: "content",
11
+ presets: "presets",
9
12
  };
10
13
  export const migrate = async (props) => {
11
14
  const { input, flags } = props;
@@ -23,7 +26,7 @@ export const migrate = async (props) => {
23
26
  ]);
24
27
  Logger.warning(`This feature is in BETA. Use it at your own risk. The API might change in the future. (Probably in a standard Prisma like migration way)`);
25
28
  switch (command) {
26
- case MIGRATE_COMMANDS.content:
29
+ case MIGRATE_COMMANDS.content: {
27
30
  Logger.log(`Migrating content with command: ${command}`);
28
31
  const from = getFrom(flags);
29
32
  const to = getTo(flags);
@@ -40,6 +43,7 @@ export const migrate = async (props) => {
40
43
  }, apiConfig);
41
44
  // Migrating provided components
42
45
  await migrateProvidedComponentsDataInStories({
46
+ itemType: "story",
43
47
  from,
44
48
  to,
45
49
  migrateFrom,
@@ -53,21 +57,15 @@ export const migrate = async (props) => {
53
57
  else if (isIt("all")) {
54
58
  const migrateFrom = flags["migrateFrom"];
55
59
  const dryRun = flags["dryRun"];
56
- console.log({
57
- from,
58
- to,
59
- migrateFrom,
60
- migrationConfig,
61
- dryRun,
62
- });
63
60
  await askForConfirmation("Are you sure you want to MIGRATE content (stories) in your space ? (it will overwrite stories)", async () => {
64
61
  Logger.warning("Preparing to migrate...");
65
62
  // await backupStories({
66
63
  // filename: `${from}--backup-before-migration___${migrationConfig}`,
67
64
  // suffix: ".sb.stories",
68
65
  // spaceId: from,
69
- // });
66
+ // }, apiConfig);
70
67
  await migrateAllComponentsDataInStories({
68
+ itemType: "story",
71
69
  from,
72
70
  to,
73
71
  migrateFrom,
@@ -83,6 +81,46 @@ export const migrate = async (props) => {
83
81
  console.log(flags);
84
82
  }
85
83
  break;
84
+ }
85
+ case MIGRATE_COMMANDS.presets: {
86
+ Logger.log(`Migrating content with command: ${command}`);
87
+ const from = getFrom(flags);
88
+ const to = getTo(flags);
89
+ const migrationConfig = flags["migration"];
90
+ console.log("Migrating with presets");
91
+ if (isIt("all")) {
92
+ const migrateFrom = flags["migrateFrom"];
93
+ const dryRun = flags["dryRun"];
94
+ await askForConfirmation("Are you sure you want to MIGRATE presets in your space ? (it will overwrite them)", async () => {
95
+ Logger.warning("Preparing to migrate...");
96
+ // backupPresets({
97
+ // filename: `${from}--backup-before-migration___${migrationConfig}`,
98
+ // suffix: ".sb.presets",
99
+ // spaceId: from,
100
+ // }, apiConfig)
101
+ const response = await managementApi.presets.getAllPresets(apiConfig);
102
+ await createAndSaveToFile({
103
+ filename: "presets-backup",
104
+ res: response,
105
+ });
106
+ await migrateAllComponentsDataInStories({
107
+ itemType: "preset",
108
+ from,
109
+ to,
110
+ migrateFrom,
111
+ migrationConfig,
112
+ }, apiConfig);
113
+ }, () => {
114
+ Logger.warning("Migration not started, exiting the program...");
115
+ }, flags["yes"]);
116
+ }
117
+ else {
118
+ Logger.error("Wrong combination of flags. check help for more info.");
119
+ console.log("Passed flags: ");
120
+ console.log(flags);
121
+ }
122
+ break;
123
+ }
86
124
  default:
87
125
  console.log(`no command like that: ${command}`);
88
126
  }
@@ -344,8 +344,6 @@ export const discoverStories = (request) => {
344
344
  const pattern = path.join(`${directory}`, `${normalizeDiscover({
345
345
  segments: onlyLocalComponentsDirectories,
346
346
  })}`, "**", `${normalizeDiscover({ segments: request.fileNames })}.${storyblokConfig.storiesExt}`);
347
- console.log("Discover stories pattern: ");
348
- console.log(pattern);
349
347
  listOfFiles = glob.sync(pattern.replace(/\\/g, "/"), {
350
348
  follow: true,
351
349
  });
@@ -370,7 +368,6 @@ export const discoverMigrationConfig = (request) => {
370
368
  pattern = path.join(`${directory}`, `${normalizeDiscover({
371
369
  segments: storyblokConfig.componentsDirectories,
372
370
  })}`, "**", `${normalizeDiscover({ segments: request.fileNames })}.${storyblokConfig.migrationConfigExt}`);
373
- console.log(pattern);
374
371
  listOfFiles = glob.sync(pattern.replace(/\\/g, "/"), {
375
372
  follow: true,
376
373
  });
@@ -71,8 +71,6 @@ export const copyFile = async (src, dest) => {
71
71
  * */
72
72
  export const createAndSaveToFile = async (args) => {
73
73
  const { ext = "json", datestamp = false, prefix = "", suffix = "", path = null, filename = "", folder = "default", res, } = args;
74
- console.log("DUPA DUPA DUPA DUPA DUPA DUPA DUPA DUPA DUPA DUPA ");
75
- console.log(args);
76
74
  if (!path) {
77
75
  const timestamp = generateDatestamp(new Date());
78
76
  const finalFilename = `${prefix}${filename}${datestamp ? `__${timestamp}` : ""}`;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sb-mig",
3
- "version": "5.0.5",
3
+ "version": "5.0.6",
4
4
  "description": "CLI to rule the world. (and handle stuff related to Storyblok CMS)",
5
5
  "author": "Marcin Krawczyk <marckraw@icloud.com>",
6
6
  "license": "MIT",