sb-mig 4.1.0-beta.2 → 4.1.0-beta.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.
@@ -5,6 +5,7 @@ export declare const removeComponentGroup: ({ componentGroup, }: {
5
5
  componentGroup: any;
6
6
  }) => Promise<any>;
7
7
  export declare const getAllComponents: () => Promise<any>;
8
+ export declare const getAllPlugins: () => Promise<any>;
8
9
  export declare const getComponent: (componentName: string | undefined) => Promise<any>;
9
10
  export declare const getComponentsGroup: (groupName: string | undefined) => Promise<any>;
10
11
  export declare const getAllComponentsGroups: () => Promise<any>;
@@ -26,6 +26,14 @@ export const getAllComponents = () => {
26
26
  .then((res) => res.data)
27
27
  .catch((err) => console.error(err));
28
28
  };
29
+ // GET
30
+ export const getAllPlugins = () => {
31
+ console.log("Trying to get all plugins.");
32
+ return sbApi
33
+ .get(`field_types`)
34
+ .then((res) => res.data)
35
+ .catch((err) => console.error(err));
36
+ };
29
37
  export const getComponent = (componentName) => {
30
38
  console.log(`Trying to get '${componentName}' component.`);
31
39
  return getAllComponents()
@@ -12,6 +12,9 @@ interface SyncProvidedComponents {
12
12
  components: string[];
13
13
  packageName: boolean;
14
14
  }
15
+ interface SyncProvidedPlugins {
16
+ plugins: string[];
17
+ }
15
18
  export declare const syncProvidedComponents: ({ components, presets, packageName, }: SyncProvidedComponents) => void;
16
19
  export declare const syncAllComponents: ({ presets }: SyncAllComponents) => void;
17
20
  export declare const removeAllComponents: () => Promise<any[]>;
@@ -25,4 +28,5 @@ export declare const syncContent: ({ from, to, }: {
25
28
  export declare const removeAllStories: ({ spaceId }: {
26
29
  spaceId: number;
27
30
  }) => Promise<any[]>;
31
+ export declare const syncProvidedPlugins: ({ plugins }: SyncProvidedPlugins) => Promise<any>;
28
32
  export {};
@@ -4,6 +4,8 @@ import { updateComponent, createComponent } from "./mutateComponents.js";
4
4
  import { discoverManyByPackageName, LOOKUP_TYPE, SCOPE, compare, discover, discoverMany, } from "../utils/discover.js";
5
5
  import { getFileContentWithRequire } from "../utils/main.js";
6
6
  import { createStory, getAllStories, removeStory } from "./stories.js";
7
+ import { createPlugin, getPlugin, updatePlugin } from "./plugins.js";
8
+ import { readFile } from "../utils/files.js";
7
9
  const _uniqueValuesFrom = (array) => [...new Set(array)];
8
10
  const _checkAndPrepareGroups = async (groupsToCheck) => {
9
11
  const componentsGroups = await getAllComponentsGroups();
@@ -175,3 +177,21 @@ export const removeAllStories = async ({ spaceId }) => {
175
177
  const allResponses = Promise.all(stories.map(async (story) => await removeStory({ spaceId, storyId: story.story.id })));
176
178
  return allResponses;
177
179
  };
180
+ export const syncProvidedPlugins = async ({ plugins }) => {
181
+ const body = await readFile("dist/export.js");
182
+ if (plugins.length === 1) {
183
+ const pluginName = plugins[0];
184
+ const plugin = await getPlugin(pluginName);
185
+ if (plugin) {
186
+ console.log("Plugin exist.");
187
+ console.log("Start updating plugin...");
188
+ return await updatePlugin({ plugin: plugin.field_type, body });
189
+ }
190
+ else {
191
+ console.log("Start creating plugin...");
192
+ const { field_type } = await createPlugin(pluginName);
193
+ console.log("Start updating plugin...");
194
+ return await updatePlugin({ plugin: field_type, body });
195
+ }
196
+ }
197
+ };
@@ -0,0 +1,13 @@
1
+ export declare const getAllPlugins: () => Promise<any>;
2
+ export declare const getPlugin: (pluginName: string | undefined) => Promise<any>;
3
+ export declare const getPluginDetails: (plugin: any) => Promise<any>;
4
+ interface UpdatePlugin {
5
+ plugin: {
6
+ id: number;
7
+ name: string;
8
+ };
9
+ body?: string;
10
+ }
11
+ export declare const updatePlugin: ({ plugin, body }: UpdatePlugin) => Promise<any>;
12
+ export declare const createPlugin: (pluginName: string) => Promise<any>;
13
+ export {};
@@ -0,0 +1,76 @@
1
+ // GET
2
+ import { sbApi } from "./config.js";
3
+ import Logger from "../utils/logger.js";
4
+ export const getAllPlugins = () => {
5
+ console.log("Trying to get all plugins.");
6
+ return sbApi
7
+ .get(`field_types?per_page=100`)
8
+ .then((res) => {
9
+ console.log(`Amount of field typess: ${res.data.field_types.length}`);
10
+ return res.data;
11
+ })
12
+ .catch((err) => console.error(err));
13
+ };
14
+ export const getPlugin = (pluginName) => {
15
+ return getAllPlugins()
16
+ .then((res) => res.field_types.find((plugin) => plugin.name === pluginName))
17
+ .then((res) => {
18
+ if (!res) {
19
+ throw Error("Not Found - plugins does not exist");
20
+ }
21
+ if (Array.isArray(res) && res.length === 0) {
22
+ console.info(`There is no plugin named '${pluginName}'`);
23
+ return false;
24
+ }
25
+ return res;
26
+ })
27
+ .then((plugin) => {
28
+ return getPluginDetails(plugin)
29
+ .then((res) => res)
30
+ .catch((err) => console.error(err));
31
+ })
32
+ .catch((err) => {
33
+ Logger.warning(err.message);
34
+ return false;
35
+ });
36
+ };
37
+ export const getPluginDetails = (plugin) => {
38
+ console.log(`Trying to get ${plugin.name} details `);
39
+ return sbApi
40
+ .get(`field_types/${plugin.id}`)
41
+ .then((res) => res.data)
42
+ .catch((err) => console.error(err));
43
+ };
44
+ export const updatePlugin = ({ plugin, body }) => {
45
+ return sbApi
46
+ .put(`field_types/${plugin.id}`, {
47
+ field_type: {
48
+ body,
49
+ compiled_body: "",
50
+ },
51
+ })
52
+ .then((res) => {
53
+ Logger.success(`'${plugin.name}' plugin updated!`);
54
+ return res.data;
55
+ })
56
+ .catch((err) => {
57
+ console.log(err);
58
+ console.error("Error happened :()");
59
+ });
60
+ };
61
+ export const createPlugin = (pluginName) => {
62
+ return sbApi
63
+ .post(`field_types`, {
64
+ field_type: {
65
+ name: pluginName,
66
+ },
67
+ })
68
+ .then((res) => {
69
+ Logger.success(`'${pluginName}' plugin created!`);
70
+ return res.data;
71
+ })
72
+ .catch((err) => {
73
+ console.log(err);
74
+ console.error("Error happened :()");
75
+ });
76
+ };
@@ -1,5 +1,5 @@
1
1
  export declare const mainDescription = "\n USAGE\n $ sb-mig [command]\n \n COMMANDS\n sync Synchronize components, datasources or roles with Storyblok space.\n backup Command for backing up anything related to Storyblok\n debug Output extra debugging information\n help This screen\n \n Examples\n $ sb-mig sync components --all\n $ sb-mig debug \n";
2
- export declare const syncDescription = "\n Usage\n $ sb-mig sync [components|roles|datasources] [space separated file names] or --all --packageName\n \n Description\n Synchronize components or roles with Storyblok space.\n \n COMMANDS\n components - sync components\n roles - sync roles\n datasources - sync datasources\n \n FLAGS\n --all - Sync all components\n --packageName - Sync based on package name, instead of file name (package can have multiple schema files to sync)\n --presets - Pass it, if u want to sync also with presets (will take longer) \n \n EXAMPLES\n $ sb-mig sync components --all\n $ sb-mig sync components --all --presets\n $ sb-mig sync components accordion accordion-item\n $ sb-mig sync components accordion accordion-item --presets\n $ sb-mig sync components @storyblok-components/accordion --packageName\n $ sb-mig sync components @storyblok-components/accordion --packageName --presets\n $ sb-mig sync roles --all\n $ sb-mig sync datasources --all\n";
2
+ export declare const syncDescription = "\n Usage\n $ sb-mig sync [components|roles|datasources|plugins] [space separated file names] or --all --packageName\n \n Description\n Synchronize components or roles with Storyblok space.\n \n COMMANDS\n components - sync components\n roles - sync roles\n datasources - sync datasources\n plugins - sync plugins\n \n FLAGS\n --all - Sync all components, roles, datasources \n --packageName - Sync based on package name, instead of file name (package can have multiple schema files to sync) *for components only\n --presets - Pass it, if u want to sync also with presets (will take longer) *for components only\n \n EXAMPLES\n $ sb-mig sync components --all\n $ sb-mig sync components --all --presets\n $ sb-mig sync components accordion accordion-item\n $ sb-mig sync components accordion accordion-item --presets\n $ sb-mig sync components @storyblok-components/accordion --packageName\n $ sb-mig sync components @storyblok-components/accordion --packageName --presets\n $ sb-mig sync roles --all\n $ sb-mig sync datasources --all\n $ sb-mig sync plugins my-awesome-plugin - (you have to be in catalog which has ./dist/export.js file with compiled plugin)\n";
3
3
  export declare const removeDescription = "\n Usage\n $ sb-mig remove [components|roles|datasources] [space separated file names] or --all \n \n Description\n Remove components or roles with Storyblok space.\n \n COMMANDS\n components - remove components\n roles - remove roles\n datasources - remove datasources\n \n FLAGS\n --all - Remove all components \n \n EXAMPLES\n $ sb-mig remove components --all\n $ sb-mig remove components accordion accordion-item\n $ sb-mig remove roles --all\n $ sb-mig remove datasources --all\n";
4
- export declare const backupDescription = "\n Usage\n $ sb-mig backup [components|component-groups|roles|datasources|presets|component-presets] component-name --one or --all\n Description\n Command for backing up anything related to Storyblok\n \n COMMANDS\n components - backup components\n component-groups - backup component-groups\n roles - backup components\n datasources - backup components\n presets - backup presets\n component-presets - backup component presets\n \n FLAGS\n --all - Backup all \n --one - Backup one \n \n EXAMPLES\n $ sb-mig backup components --all\n $ sb-mig backup components accordion --one \n $ sb-mig backup datasources --all\n $ sb-mig backup roles admin --one\n";
4
+ export declare const backupDescription = "\n Usage\n $ sb-mig backup [components|component-groups|roles|datasources|presets|component-presets] component-name --one or --all\n Description\n Command for backing up anything related to Storyblok\n \n COMMANDS\n components - backup components\n component-groups - backup component-groups\n roles - backup components\n datasources - backup components\n presets - backup presets\n component-presets - backup component presets\n plugins - backup plugins\n\n \n FLAGS\n --all - Backup all \n --one - Backup one \n \n EXAMPLES\n $ sb-mig backup components --all\n $ sb-mig backup components accordion --one \n $ sb-mig backup datasources --all\n $ sb-mig backup roles admin --one\n $ sb-mig backup plugins --all\n $ sb-mig backup plugins my-awesome-plugin --one\n";
5
5
  export declare const debugDescription = "\n Usage\n $ sb-mig debug\n Description\n Output extra debugging information\n";
@@ -14,7 +14,7 @@ export const mainDescription = `
14
14
  `;
15
15
  export const syncDescription = `
16
16
  Usage
17
- $ sb-mig sync [components|roles|datasources] [space separated file names] or --all --packageName
17
+ $ sb-mig sync [components|roles|datasources|plugins] [space separated file names] or --all --packageName
18
18
 
19
19
  Description
20
20
  Synchronize components or roles with Storyblok space.
@@ -23,11 +23,12 @@ export const syncDescription = `
23
23
  components - sync components
24
24
  roles - sync roles
25
25
  datasources - sync datasources
26
+ plugins - sync plugins
26
27
 
27
28
  FLAGS
28
- --all - Sync all components
29
- --packageName - Sync based on package name, instead of file name (package can have multiple schema files to sync)
30
- --presets - Pass it, if u want to sync also with presets (will take longer)
29
+ --all - Sync all components, roles, datasources
30
+ --packageName - Sync based on package name, instead of file name (package can have multiple schema files to sync) *for components only
31
+ --presets - Pass it, if u want to sync also with presets (will take longer) *for components only
31
32
 
32
33
  EXAMPLES
33
34
  $ sb-mig sync components --all
@@ -38,6 +39,7 @@ export const syncDescription = `
38
39
  $ sb-mig sync components @storyblok-components/accordion --packageName --presets
39
40
  $ sb-mig sync roles --all
40
41
  $ sb-mig sync datasources --all
42
+ $ sb-mig sync plugins my-awesome-plugin - (you have to be in catalog which has ./dist/export.js file with compiled plugin)
41
43
  `;
42
44
  export const removeDescription = `
43
45
  Usage
@@ -73,6 +75,8 @@ export const backupDescription = `
73
75
  datasources - backup components
74
76
  presets - backup presets
75
77
  component-presets - backup component presets
78
+ plugins - backup plugins
79
+
76
80
 
77
81
  FLAGS
78
82
  --all - Backup all
@@ -83,6 +87,8 @@ export const backupDescription = `
83
87
  $ sb-mig backup components accordion --one
84
88
  $ sb-mig backup datasources --all
85
89
  $ sb-mig backup roles admin --one
90
+ $ sb-mig backup plugins --all
91
+ $ sb-mig backup plugins my-awesome-plugin --one
86
92
  `;
87
93
  export const debugDescription = `
88
94
  Usage
@@ -6,6 +6,7 @@ import { getAllDatasources, getDatasource } from "../api/datasources.js";
6
6
  import { getAllRoles, getRole } from "../api/roles.js";
7
7
  import { getComponentPresets } from "../api/componentPresets.js";
8
8
  import { getAllPresets, getPreset } from "../api/presets.js";
9
+ import { getAllPlugins, getPlugin } from "../api/plugins.js";
9
10
  const BACKUP_COMMANDS = {
10
11
  components: "components",
11
12
  componentGroups: "component-groups",
@@ -13,6 +14,7 @@ const BACKUP_COMMANDS = {
13
14
  presets: "presets",
14
15
  componentPresets: "component-presets",
15
16
  roles: "roles",
17
+ plugins: "plugins",
16
18
  };
17
19
  export const backup = (props) => {
18
20
  const { input, flags } = props;
@@ -38,14 +40,13 @@ export const backup = (props) => {
38
40
  const componentToBackup = unpackOne(input);
39
41
  getComponent(componentToBackup)
40
42
  .then(async (res) => {
41
- console.log("############");
42
- console.log(res);
43
- console.log("############");
44
- await createAndSaveToFile({
45
- prefix: "component-",
46
- folder: "components",
47
- res,
48
- });
43
+ if (res) {
44
+ await createAndSaveToFile({
45
+ prefix: "component-",
46
+ folder: "components",
47
+ res,
48
+ });
49
+ }
49
50
  })
50
51
  .catch((err) => {
51
52
  console.log(err);
@@ -198,6 +199,40 @@ export const backup = (props) => {
198
199
  });
199
200
  }
200
201
  break;
202
+ case BACKUP_COMMANDS.plugins:
203
+ if (flags["one"]) {
204
+ const pluginToBackup = unpackOne(input);
205
+ getPlugin(pluginToBackup)
206
+ .then(async (res) => {
207
+ if (res) {
208
+ await createAndSaveToFile({
209
+ prefix: `plugin-${pluginToBackup}-`,
210
+ folder: "plugins",
211
+ res,
212
+ });
213
+ }
214
+ })
215
+ .catch((err) => {
216
+ console.log(err);
217
+ Logger.error("error happened... :(");
218
+ });
219
+ }
220
+ if (flags["all"]) {
221
+ getAllPlugins()
222
+ .then(async (res) => {
223
+ await createAndSaveToFile({
224
+ prefix: "all-plugins-",
225
+ folder: "plugins",
226
+ res,
227
+ });
228
+ })
229
+ .catch((err) => {
230
+ console.log(err);
231
+ Logger.error("error happened... :(");
232
+ });
233
+ return;
234
+ }
235
+ break;
201
236
  default:
202
237
  console.log(`no command like that: ${command}`);
203
238
  }
@@ -1,7 +1,7 @@
1
1
  import Logger from "../utils/logger.js";
2
2
  import { unpackElements } from "../utils/main.js";
3
3
  import storyblokConfig from "../config/config.js";
4
- import { removeAllComponents, syncAllComponents, syncContent, syncProvidedComponents, } from "../api/migrate.js";
4
+ import { removeAllComponents, syncAllComponents, syncContent, syncProvidedComponents, removeAllStories, syncProvidedPlugins, } from "../api/migrate.js";
5
5
  import { syncAllRoles, syncProvidedRoles } from "../api/roles.js";
6
6
  import { syncAllDatasources, syncProvidedDatasources, } from "../api/datasources.js";
7
7
  const SYNC_COMMANDS = {
@@ -9,6 +9,7 @@ const SYNC_COMMANDS = {
9
9
  components: "components",
10
10
  roles: "roles",
11
11
  datasources: "datasources",
12
+ plugins: "plugins",
12
13
  };
13
14
  export const sync = async (props) => {
14
15
  const { input, flags } = props;
@@ -61,10 +62,44 @@ export const sync = async (props) => {
61
62
  break;
62
63
  case SYNC_COMMANDS.story:
63
64
  Logger.log(`Syncing story with command: ${command}`);
64
- if (flags["from"] && flags["to"]) {
65
- Logger.warning(`sync story... from space-id: ${flags.from} to space-id: ${flags.to} with command: ${command}`);
66
- console.log(await syncContent({ from: flags.from, to: flags.to }));
67
- // await removeAllStories({spaceId: flags.to})
65
+ if (flags["all"]) {
66
+ if (!flags["from"] && !flags["to"]) {
67
+ Logger.warning(`sync story... from boilerplateSpaceId: ${storyblokConfig.boilerplateSpaceId} to working dir spaceid: ${storyblokConfig.spaceId} with command: ${command}`);
68
+ await removeAllStories({
69
+ spaceId: storyblokConfig.spaceId,
70
+ });
71
+ await syncContent({
72
+ from: storyblokConfig.boilerplateSpaceId,
73
+ to: storyblokConfig.spaceId,
74
+ });
75
+ }
76
+ if (flags["from"] && !flags["to"]) {
77
+ Logger.warning(`sync story... from: ${flags.from} to working dir spaceid: ${storyblokConfig.spaceId} with command: ${command}`);
78
+ await removeAllStories({
79
+ spaceId: storyblokConfig.spaceId,
80
+ });
81
+ await syncContent({
82
+ from: flags.from,
83
+ to: storyblokConfig.spaceId,
84
+ });
85
+ }
86
+ if (flags["from"] && flags["to"]) {
87
+ Logger.warning(`sync story... from space-id: ${flags.from} to space-id: ${flags.to} with command: ${command}`);
88
+ await removeAllStories({
89
+ spaceId: storyblokConfig.spaceId,
90
+ });
91
+ await syncContent({ from: flags.from, to: flags.to });
92
+ }
93
+ }
94
+ break;
95
+ case SYNC_COMMANDS.plugins:
96
+ Logger.warning(`sync plugins... with command: ${command}`);
97
+ if (!flags["all"]) {
98
+ Logger.warning("Synchronizing PROVIDED plugins...");
99
+ const pluginsToSync = unpackElements(input);
100
+ syncProvidedPlugins({
101
+ plugins: pluginsToSync,
102
+ });
68
103
  }
69
104
  break;
70
105
  default:
@@ -31,13 +31,13 @@ const defaultConfig = {
31
31
  schemaFileExt: pkg.type === "module" ? "sb.js" : "sb.cjs",
32
32
  datasourceExt: pkg.type === "module" ? "sb.datasource.js" : "sb.datasource.cjs",
33
33
  rolesExt: pkg.type === "module" ? "sb.roles.js" : "sb.roles.cjs",
34
- storyblokApiUrl: "https://api.storyblok.com/v1",
34
+ storyblokApiUrl: "https://mapi.storyblok.com/v1",
35
35
  oauthToken: process.env["STORYBLOK_OAUTH_TOKEN"] ?? "",
36
36
  spaceId: process.env["STORYBLOK_SPACE_ID"] ?? "",
37
37
  accessToken: process.env["GATSBY_STORYBLOK_ACCESS_TOKEN"] ||
38
38
  process.env["NEXT_PUBLIC_STORYBLOK_ACCESS_TOKEN"] ||
39
39
  "",
40
- boilerplateSpaceId: 108084, // this is id of nextjs-web-ui-sb-boilerplate
40
+ boilerplateSpaceId: 172677, // this is id of Content seed for nextjs boilerplate space
41
41
  };
42
42
  const filePath = path.resolve(process.cwd(), "storyblok.config");
43
43
  const customConfig = await getStoryblokConfigContent({
@@ -9,4 +9,5 @@ interface CreateAndSaveToFile {
9
9
  res: any;
10
10
  }
11
11
  export declare const createAndSaveToFile: ({ prefix, folder, res, }: CreateAndSaveToFile) => Promise<void>;
12
+ export declare const readFile: (path: string) => Promise<string | undefined>;
12
13
  export {};
@@ -52,3 +52,15 @@ export const createAndSaveToFile = async ({ prefix, folder, res, }) => {
52
52
  await createJsonFile(JSON.stringify(res, undefined, 2), `${storyblokConfig.sbmigWorkingDirectory}/${folder}/${filename}.json`);
53
53
  Logger.success(`All groups written to a file: ${filename}`);
54
54
  };
55
+ export const readFile = async (path) => {
56
+ const absolutePath = `${process.cwd()}/${path}`;
57
+ try {
58
+ const result = await fs.promises.readFile(absolutePath);
59
+ return result.toString();
60
+ }
61
+ catch (e) {
62
+ console.log(e);
63
+ console.error("Error happened while reading file.");
64
+ return;
65
+ }
66
+ };
@@ -1 +1 @@
1
- export const generateDatestamp = (datestamp) => `${datestamp.getFullYear()}-${datestamp.getMonth()}-${datestamp.getDay()}_${datestamp.getTime()}`;
1
+ export const generateDatestamp = (datestamp) => `${datestamp.getFullYear()}-${datestamp.getMonth()}-${datestamp.getDay()}_${datestamp.getHours()}-${datestamp.getMinutes()}-${datestamp.getSeconds()}`;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sb-mig",
3
- "version": "4.1.0-beta.2",
3
+ "version": "4.1.0-beta.3",
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",
@@ -52,7 +52,7 @@
52
52
  "glob": "^8.0.3",
53
53
  "meow": "^10.1.3",
54
54
  "ncp": "^2.0.0",
55
- "storyblok-js-client": "^4.5.2"
55
+ "storyblok-js-client": "^4.5.7"
56
56
  },
57
57
  "devDependencies": {
58
58
  "@commitlint/cli": "^17.0.3",