sb-mig 3.1.3 → 3.1.7
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/api/componentPresets.d.ts +1 -0
- package/dist/api/componentPresets.js +18 -0
- package/dist/api/components.d.ts +5 -0
- package/dist/api/components.js +63 -0
- package/dist/api/config.d.ts +2 -0
- package/dist/api/config.js +4 -0
- package/dist/api/datasources.d.ts +24 -0
- package/dist/api/datasources.js +181 -0
- package/dist/api/migrate.d.ts +17 -0
- package/dist/api/migrate.js +139 -0
- package/dist/api/mutateComponents.d.ts +2 -0
- package/dist/api/mutateComponents.js +45 -0
- package/dist/api/presets.d.ts +4 -0
- package/dist/api/presets.js +52 -0
- package/dist/api/resolvePresets.d.ts +2 -0
- package/dist/api/resolvePresets.js +41 -0
- package/dist/api/roles.d.ts +14 -0
- package/dist/api/roles.js +125 -0
- package/dist/cli-descriptions.d.ts +4 -0
- package/dist/cli-descriptions.js +72 -0
- package/dist/commands/backup.d.ts +2 -0
- package/dist/commands/backup.js +201 -0
- package/dist/commands/debug.d.ts +1 -0
- package/dist/commands/debug.js +5 -0
- package/dist/commands/sync.d.ts +2 -0
- package/dist/commands/sync.js +58 -0
- package/dist/config/config.d.ts +20 -0
- package/dist/config/config.js +32 -0
- package/dist/index.d.ts +2 -0
- package/dist/utils/discover.d.ts +57 -0
- package/dist/utils/discover.js +481 -0
- package/dist/utils/files.d.ts +12 -0
- package/dist/utils/files.js +54 -0
- package/dist/utils/interfaces.d.ts +4 -0
- package/dist/utils/interfaces.js +1 -0
- package/dist/utils/logger.d.ts +8 -0
- package/dist/utils/logger.js +20 -0
- package/dist/utils/main.d.ts +13 -0
- package/dist/utils/main.js +28 -0
- package/dist/utils/others.d.ts +1 -0
- package/dist/utils/others.js +1 -0
- package/package.json +9 -8
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const getComponentPresets: (componentName: string | undefined) => Promise<false | any[]>;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import Logger from "../utils/logger.js";
|
|
2
|
+
import { getAllComponents } from "./components.js";
|
|
3
|
+
import { getPreset } from "./presets.js";
|
|
4
|
+
export const getComponentPresets = (componentName) => {
|
|
5
|
+
Logger.log(`Trying to get all '${componentName}' presets.`);
|
|
6
|
+
return getAllComponents().then(async (res) => {
|
|
7
|
+
const componentPresets = res.components.filter((component) => component.name === componentName);
|
|
8
|
+
if (componentPresets.length > 0) {
|
|
9
|
+
if (componentPresets[0].all_presets.length === 0) {
|
|
10
|
+
Logger.warning(`There is no presets for: '${componentName}' component`);
|
|
11
|
+
return false;
|
|
12
|
+
}
|
|
13
|
+
return Promise.all(componentPresets[0].all_presets.map((preset) => getPreset(preset.id).catch((err) => Logger.error(err))));
|
|
14
|
+
}
|
|
15
|
+
Logger.warning(`There is no '${componentName}' component`);
|
|
16
|
+
return false;
|
|
17
|
+
});
|
|
18
|
+
};
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export declare const getAllComponents: () => Promise<any>;
|
|
2
|
+
export declare const getComponent: (componentName: string | undefined) => Promise<any>;
|
|
3
|
+
export declare const getComponentsGroup: (groupName: string | undefined) => Promise<any>;
|
|
4
|
+
export declare const getAllComponentsGroups: () => Promise<any>;
|
|
5
|
+
export declare const createComponentsGroup: (groupName: string) => Promise<any>;
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import storyblokConfig from "../config/config.js";
|
|
2
|
+
import { sbApi } from "./config.js";
|
|
3
|
+
const { spaceId } = storyblokConfig;
|
|
4
|
+
// GET
|
|
5
|
+
export const getAllComponents = () => {
|
|
6
|
+
console.log("Trying to get all components.");
|
|
7
|
+
return sbApi
|
|
8
|
+
.get(`spaces/${spaceId}/components/`)
|
|
9
|
+
.then((res) => res.data)
|
|
10
|
+
.catch((err) => console.error(err));
|
|
11
|
+
};
|
|
12
|
+
export const getComponent = (componentName) => {
|
|
13
|
+
console.log(`Trying to get '${componentName}' component.`);
|
|
14
|
+
return getAllComponents()
|
|
15
|
+
.then((res) => res.components.filter((component) => component.name === componentName))
|
|
16
|
+
.then((res) => {
|
|
17
|
+
if (Array.isArray(res) && res.length === 0) {
|
|
18
|
+
console.info(`There is no component named '${componentName}'`);
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
21
|
+
return res;
|
|
22
|
+
})
|
|
23
|
+
.catch((err) => console.error(err));
|
|
24
|
+
};
|
|
25
|
+
export const getComponentsGroup = (groupName) => {
|
|
26
|
+
console.log(`Trying to get '${groupName}' group.`);
|
|
27
|
+
return getAllComponentsGroups()
|
|
28
|
+
.then((res) => {
|
|
29
|
+
return res.component_groups.filter((group) => group.name === groupName);
|
|
30
|
+
})
|
|
31
|
+
.then((res) => {
|
|
32
|
+
if (Array.isArray(res) && res.length === 0) {
|
|
33
|
+
console.info(`There is no group named '${groupName}'`);
|
|
34
|
+
return false;
|
|
35
|
+
}
|
|
36
|
+
return res;
|
|
37
|
+
})
|
|
38
|
+
.catch((err) => console.error(err));
|
|
39
|
+
};
|
|
40
|
+
export const getAllComponentsGroups = async () => {
|
|
41
|
+
console.log("Trying to get all groups.");
|
|
42
|
+
return sbApi
|
|
43
|
+
.get(`spaces/${spaceId}/component_groups/`)
|
|
44
|
+
.then((response) => response.data)
|
|
45
|
+
.catch((err) => console.error(err));
|
|
46
|
+
};
|
|
47
|
+
export const createComponentsGroup = (groupName) => {
|
|
48
|
+
console.log(`Trying to create '${groupName}' group`);
|
|
49
|
+
return sbApi
|
|
50
|
+
.post(`spaces/${spaceId}/component_groups/`, {
|
|
51
|
+
component_group: {
|
|
52
|
+
name: groupName,
|
|
53
|
+
},
|
|
54
|
+
})
|
|
55
|
+
.then((res) => {
|
|
56
|
+
console.info(`'${groupName}' created with uuid: ${res.data.component_group.uuid}`);
|
|
57
|
+
return res.data;
|
|
58
|
+
})
|
|
59
|
+
.catch((err) => {
|
|
60
|
+
console.log(err.message);
|
|
61
|
+
console.error("Error happened :()");
|
|
62
|
+
});
|
|
63
|
+
};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export declare const getAllDatasources: () => Promise<any>;
|
|
2
|
+
export declare const getDatasource: (datasourceName: string | undefined) => Promise<any>;
|
|
3
|
+
export declare const getDatasourceEntries: (datasourceName: string) => Promise<any>;
|
|
4
|
+
export declare const createDatasource: (datasource: any) => Promise<void | {
|
|
5
|
+
data: any;
|
|
6
|
+
datasource_entries: any;
|
|
7
|
+
}>;
|
|
8
|
+
export declare const createDatasourceEntry: (datasourceEntry: any, datasourceId: string) => Promise<any>;
|
|
9
|
+
export declare const updateDatasourceEntry: (datasourceEntry: any, datasourceId: string, datasourceToBeUpdated: any) => Promise<any>;
|
|
10
|
+
export declare const updateDatasource: (datasource: any, temp: any) => Promise<void | {
|
|
11
|
+
data: any;
|
|
12
|
+
datasource_entries: any;
|
|
13
|
+
}>;
|
|
14
|
+
export declare const createDatasourceEntries: (datasourceId: string, datasource_entries: any, remoteDatasourceEntries: any) => void;
|
|
15
|
+
interface SyncDatasources {
|
|
16
|
+
providedDatasources: string[];
|
|
17
|
+
}
|
|
18
|
+
export declare const syncDatasources: ({ providedDatasources, }: SyncDatasources) => Promise<void>;
|
|
19
|
+
interface SyncProvidedDatasources {
|
|
20
|
+
datasources: string[];
|
|
21
|
+
}
|
|
22
|
+
export declare const syncProvidedDatasources: ({ datasources, }: SyncProvidedDatasources) => void;
|
|
23
|
+
export declare const syncAllDatasources: () => void;
|
|
24
|
+
export {};
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
import Logger from "../utils/logger.js";
|
|
2
|
+
import storyblokConfig from "../config/config.js";
|
|
3
|
+
import { sbApi } from "./config.js";
|
|
4
|
+
import { LOOKUP_TYPE, SCOPE, discoverManyDatasources, discoverDatasources, } from "../utils/discover.js";
|
|
5
|
+
import { getFilesContentWithRequire } from "../utils/main.js";
|
|
6
|
+
const { spaceId } = storyblokConfig;
|
|
7
|
+
// GET
|
|
8
|
+
export const getAllDatasources = () => {
|
|
9
|
+
Logger.log("Trying to get all Datasources.");
|
|
10
|
+
return sbApi
|
|
11
|
+
.get(`spaces/${spaceId}/datasources/`)
|
|
12
|
+
.then(({ data }) => data)
|
|
13
|
+
.catch((err) => {
|
|
14
|
+
if (err.response.status === 404) {
|
|
15
|
+
Logger.error(`There is no datasources in your Storyblok ${spaceId} space.`);
|
|
16
|
+
return true;
|
|
17
|
+
}
|
|
18
|
+
else {
|
|
19
|
+
Logger.error(err);
|
|
20
|
+
return false;
|
|
21
|
+
}
|
|
22
|
+
});
|
|
23
|
+
};
|
|
24
|
+
export const getDatasource = (datasourceName) => {
|
|
25
|
+
Logger.log(`Trying to get '${datasourceName}' datasource.`);
|
|
26
|
+
return getAllDatasources()
|
|
27
|
+
.then((res) => {
|
|
28
|
+
if (res) {
|
|
29
|
+
return res.datasources.filter((datasource) => datasource.name === datasourceName);
|
|
30
|
+
}
|
|
31
|
+
})
|
|
32
|
+
.then((res) => {
|
|
33
|
+
if (Array.isArray(res) && res.length === 0) {
|
|
34
|
+
Logger.warning(`There is no datasource named '${datasourceName}'`);
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
return res;
|
|
38
|
+
})
|
|
39
|
+
.catch((err) => Logger.error(err));
|
|
40
|
+
};
|
|
41
|
+
export const getDatasourceEntries = async (datasourceName) => {
|
|
42
|
+
Logger.log(`Trying to get '${datasourceName}' datasource entries.`);
|
|
43
|
+
const data = await getDatasource(datasourceName);
|
|
44
|
+
if (data) {
|
|
45
|
+
return sbApi
|
|
46
|
+
.get(`spaces/${spaceId}/datasource_entries/?datasource_id=${data[0].id}`)
|
|
47
|
+
.then(async ({ data }) => data)
|
|
48
|
+
.catch((err) => Logger.error(err));
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
export const createDatasource = (datasource) => sbApi
|
|
52
|
+
.post(`spaces/${spaceId}/datasources/`, {
|
|
53
|
+
datasource: {
|
|
54
|
+
name: datasource.name,
|
|
55
|
+
slug: datasource.slug,
|
|
56
|
+
},
|
|
57
|
+
})
|
|
58
|
+
.then(({ data }) => ({
|
|
59
|
+
data,
|
|
60
|
+
datasource_entries: datasource.datasource_entries,
|
|
61
|
+
}))
|
|
62
|
+
.catch((err) => Logger.error(err));
|
|
63
|
+
export const createDatasourceEntry = (datasourceEntry, datasourceId) => {
|
|
64
|
+
return sbApi
|
|
65
|
+
.post(`spaces/${spaceId}/datasource_entries/`, {
|
|
66
|
+
datasource_entry: {
|
|
67
|
+
name: Object.values(datasourceEntry)[0],
|
|
68
|
+
value: Object.values(datasourceEntry)[1],
|
|
69
|
+
datasource_id: datasourceId,
|
|
70
|
+
},
|
|
71
|
+
})
|
|
72
|
+
.then(({ data }) => {
|
|
73
|
+
return data;
|
|
74
|
+
})
|
|
75
|
+
.catch((err) => Logger.error(err));
|
|
76
|
+
};
|
|
77
|
+
export const updateDatasourceEntry = (datasourceEntry, datasourceId, datasourceToBeUpdated) => {
|
|
78
|
+
return sbApi
|
|
79
|
+
.put(`spaces/${spaceId}/datasource_entries/${datasourceToBeUpdated.id}`, {
|
|
80
|
+
datasource_entry: {
|
|
81
|
+
name: Object.values(datasourceEntry)[0],
|
|
82
|
+
value: Object.values(datasourceEntry)[1],
|
|
83
|
+
datasource_id: datasourceId,
|
|
84
|
+
id: datasourceToBeUpdated.id,
|
|
85
|
+
},
|
|
86
|
+
})
|
|
87
|
+
.then(({ data }) => {
|
|
88
|
+
return data;
|
|
89
|
+
})
|
|
90
|
+
.catch((err) => Logger.error(err));
|
|
91
|
+
};
|
|
92
|
+
export const updateDatasource = (datasource, temp) => sbApi
|
|
93
|
+
.put(`spaces/${spaceId}/datasources/${temp.id}`, {
|
|
94
|
+
datasource: {
|
|
95
|
+
id: temp.id,
|
|
96
|
+
name: datasource.name,
|
|
97
|
+
slug: datasource.slug,
|
|
98
|
+
},
|
|
99
|
+
})
|
|
100
|
+
.then(({ data }) => {
|
|
101
|
+
return {
|
|
102
|
+
data,
|
|
103
|
+
datasource_entries: datasource.datasource_entries,
|
|
104
|
+
};
|
|
105
|
+
})
|
|
106
|
+
.catch((err) => Logger.error(err));
|
|
107
|
+
export const createDatasourceEntries = (datasourceId, datasource_entries, remoteDatasourceEntries) => {
|
|
108
|
+
Promise.all(datasource_entries.map((datasourceEntry) => {
|
|
109
|
+
const datasourceEntriesToBeUpdated = remoteDatasourceEntries.datasource_entries.find((remoteDatasourceEntry) => remoteDatasourceEntry.name ===
|
|
110
|
+
Object.values(datasourceEntry)[0]);
|
|
111
|
+
if (datasourceEntriesToBeUpdated) {
|
|
112
|
+
return updateDatasourceEntry(datasourceEntry, datasourceId, datasourceEntriesToBeUpdated);
|
|
113
|
+
}
|
|
114
|
+
return createDatasourceEntry(datasourceEntry, datasourceId);
|
|
115
|
+
}))
|
|
116
|
+
.then(({ data }) => {
|
|
117
|
+
Logger.success(`Datasource entries for ${datasourceId} datasource id has been successfully synced.`);
|
|
118
|
+
return data;
|
|
119
|
+
})
|
|
120
|
+
.catch((err) => Logger.error(err));
|
|
121
|
+
};
|
|
122
|
+
export const syncDatasources = async ({ providedDatasources, }) => {
|
|
123
|
+
Logger.log(`Trying to sync provided datasources: ${providedDatasources}`);
|
|
124
|
+
const providedDatasourcesContent = getFilesContentWithRequire({
|
|
125
|
+
files: providedDatasources,
|
|
126
|
+
});
|
|
127
|
+
const remoteDatasources = await getAllDatasources();
|
|
128
|
+
Promise.all(providedDatasourcesContent.map((datasource) => {
|
|
129
|
+
const datasourceToBeUpdated = remoteDatasources.datasources.find((remoteDatasource) => datasource.name === remoteDatasource.name);
|
|
130
|
+
if (datasourceToBeUpdated) {
|
|
131
|
+
return updateDatasource(datasource, datasourceToBeUpdated);
|
|
132
|
+
}
|
|
133
|
+
return createDatasource(datasource);
|
|
134
|
+
}))
|
|
135
|
+
.then((res) => {
|
|
136
|
+
res.map(async ({ data, datasource_entries }) => {
|
|
137
|
+
const remoteDatasourceEntries = await getDatasourceEntries(data.datasource.name);
|
|
138
|
+
createDatasourceEntries(data.datasource.id, datasource_entries, remoteDatasourceEntries);
|
|
139
|
+
});
|
|
140
|
+
return res;
|
|
141
|
+
})
|
|
142
|
+
.catch((err) => {
|
|
143
|
+
console.log(err);
|
|
144
|
+
Logger.warning("There is error inside promise.all from datasource");
|
|
145
|
+
return false;
|
|
146
|
+
});
|
|
147
|
+
};
|
|
148
|
+
export const syncProvidedDatasources = ({ datasources, }) => {
|
|
149
|
+
const allLocalDatasources = discoverManyDatasources({
|
|
150
|
+
scope: SCOPE.local,
|
|
151
|
+
type: LOOKUP_TYPE.fileName,
|
|
152
|
+
fileNames: datasources,
|
|
153
|
+
});
|
|
154
|
+
const allExternalDatasources = discoverManyDatasources({
|
|
155
|
+
scope: SCOPE.external,
|
|
156
|
+
type: LOOKUP_TYPE.fileName,
|
|
157
|
+
fileNames: datasources,
|
|
158
|
+
});
|
|
159
|
+
syncDatasources({
|
|
160
|
+
providedDatasources: [
|
|
161
|
+
...allLocalDatasources,
|
|
162
|
+
...allExternalDatasources,
|
|
163
|
+
],
|
|
164
|
+
});
|
|
165
|
+
};
|
|
166
|
+
export const syncAllDatasources = () => {
|
|
167
|
+
const allLocalDatasources = discoverDatasources({
|
|
168
|
+
scope: SCOPE.local,
|
|
169
|
+
type: LOOKUP_TYPE.fileName,
|
|
170
|
+
});
|
|
171
|
+
const allExternalDatasources = discoverDatasources({
|
|
172
|
+
scope: SCOPE.external,
|
|
173
|
+
type: LOOKUP_TYPE.fileName,
|
|
174
|
+
});
|
|
175
|
+
syncDatasources({
|
|
176
|
+
providedDatasources: [
|
|
177
|
+
...allLocalDatasources,
|
|
178
|
+
...allExternalDatasources,
|
|
179
|
+
],
|
|
180
|
+
});
|
|
181
|
+
};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { OneComponent } from "../utils/discover.js";
|
|
2
|
+
interface SyncComponents {
|
|
3
|
+
specifiedComponents: OneComponent[];
|
|
4
|
+
presets: boolean;
|
|
5
|
+
}
|
|
6
|
+
export declare const syncComponents: ({ specifiedComponents, presets, }: SyncComponents) => Promise<void>;
|
|
7
|
+
interface SyncAllComponents {
|
|
8
|
+
presets: boolean;
|
|
9
|
+
}
|
|
10
|
+
interface SyncProvidedComponents {
|
|
11
|
+
presets: boolean;
|
|
12
|
+
components: string[];
|
|
13
|
+
packageName: boolean;
|
|
14
|
+
}
|
|
15
|
+
export declare const syncProvidedComponents: ({ components, presets, packageName, }: SyncProvidedComponents) => void;
|
|
16
|
+
export declare const syncAllComponents: ({ presets }: SyncAllComponents) => void;
|
|
17
|
+
export {};
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import Logger from "../utils/logger.js";
|
|
2
|
+
import { getAllComponentsGroups, createComponentsGroup, getAllComponents, } from "./components.js";
|
|
3
|
+
import { updateComponent, createComponent } from "./mutateComponents.js";
|
|
4
|
+
import { discoverManyByPackageName, LOOKUP_TYPE, SCOPE, compare, discover, discoverMany, } from "../utils/discover.js";
|
|
5
|
+
import { getFileContent } from "../utils/main.js";
|
|
6
|
+
const _uniqueValuesFrom = (array) => [...new Set(array)];
|
|
7
|
+
const _checkAndPrepareGroups = async (groupsToCheck) => {
|
|
8
|
+
const componentsGroups = await getAllComponentsGroups();
|
|
9
|
+
const groupExist = (groupName) => componentsGroups.component_groups.find((group) => group.name === groupName);
|
|
10
|
+
groupsToCheck.forEach(async (groupName) => {
|
|
11
|
+
if (!groupExist(groupName)) {
|
|
12
|
+
await createComponentsGroup(groupName);
|
|
13
|
+
}
|
|
14
|
+
});
|
|
15
|
+
};
|
|
16
|
+
const _resolveGroups = async (component, existedGroups, remoteComponentsGroups) => {
|
|
17
|
+
if (!component.component_group_name) {
|
|
18
|
+
return { ...component, component_group_uuid: null };
|
|
19
|
+
}
|
|
20
|
+
const componentsGroup = existedGroups.find((group) => component.component_group_name === group);
|
|
21
|
+
if (componentsGroup) {
|
|
22
|
+
const component_group_uuid = remoteComponentsGroups.component_groups.find((remoteComponentsGroup) => remoteComponentsGroup.name === componentsGroup).uuid;
|
|
23
|
+
return { ...component, component_group_uuid };
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
export const syncComponents = async ({ specifiedComponents, presets, }) => {
|
|
27
|
+
Logger.log("sync2Components: ");
|
|
28
|
+
const specifiedComponentsContent = await Promise.all(specifiedComponents.map((component) => getFileContent({ file: component.path })));
|
|
29
|
+
const groupsToCheck = _uniqueValuesFrom(specifiedComponentsContent
|
|
30
|
+
.filter((component) => component.component_group_name)
|
|
31
|
+
.map((component) => component.component_group_name));
|
|
32
|
+
console.log(groupsToCheck);
|
|
33
|
+
await _checkAndPrepareGroups(groupsToCheck);
|
|
34
|
+
// after checkAndPrepareGroups remoteComponents will have synced groups with local groups
|
|
35
|
+
// updates of the groups had to happen before creation of them, cause creation/updates of components
|
|
36
|
+
// happens async, so if one component will have the same group, as other one
|
|
37
|
+
// it will be race of condition kinda issue - we will never now, if the group for current processed component
|
|
38
|
+
// already exist or is being created by other request
|
|
39
|
+
const remoteComponents = await getAllComponents();
|
|
40
|
+
const componentsToUpdate = [];
|
|
41
|
+
const componentsToCreate = [];
|
|
42
|
+
for (const component of specifiedComponentsContent) {
|
|
43
|
+
const shouldBeUpdated = remoteComponents.components.find((remoteComponent) => component.name === remoteComponent.name);
|
|
44
|
+
if (shouldBeUpdated) {
|
|
45
|
+
componentsToUpdate.push({ id: shouldBeUpdated.id, ...component });
|
|
46
|
+
}
|
|
47
|
+
else {
|
|
48
|
+
componentsToCreate.push(component);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
const componentsGroups = await getAllComponentsGroups();
|
|
52
|
+
componentsToUpdate.length > 0 &&
|
|
53
|
+
Promise.all(componentsToUpdate.map((component) => _resolveGroups(component, groupsToCheck, componentsGroups))).then((res) => {
|
|
54
|
+
Logger.log("Components to update after check: ");
|
|
55
|
+
res.map((component) => {
|
|
56
|
+
Logger.warning(` ${component.name}`);
|
|
57
|
+
updateComponent(component, presets);
|
|
58
|
+
});
|
|
59
|
+
});
|
|
60
|
+
componentsToCreate.length > 0 &&
|
|
61
|
+
Promise.all(componentsToCreate.map((component) => _resolveGroups(component, groupsToCheck, componentsGroups))).then((res) => {
|
|
62
|
+
Logger.log("Components to create after check: ");
|
|
63
|
+
res.map((component) => {
|
|
64
|
+
Logger.warning(` ${component.name}`);
|
|
65
|
+
createComponent(component, presets);
|
|
66
|
+
});
|
|
67
|
+
});
|
|
68
|
+
};
|
|
69
|
+
export const syncProvidedComponents = ({ components, presets, packageName, }) => {
|
|
70
|
+
if (!packageName) {
|
|
71
|
+
// #1: discover all external .sb.js files
|
|
72
|
+
const allLocalSbComponentsSchemaFiles = discoverMany({
|
|
73
|
+
scope: SCOPE.local,
|
|
74
|
+
type: LOOKUP_TYPE.fileName,
|
|
75
|
+
fileNames: components,
|
|
76
|
+
});
|
|
77
|
+
// #2: discover all local .sb.js files
|
|
78
|
+
const allExternalSbComponentsSchemaFiles = discoverMany({
|
|
79
|
+
scope: SCOPE.external,
|
|
80
|
+
type: LOOKUP_TYPE.fileName,
|
|
81
|
+
fileNames: components,
|
|
82
|
+
});
|
|
83
|
+
// #3: compare results, prefer local ones (so we have to create final external paths array and local array of things to sync from where)
|
|
84
|
+
const { local, external } = compare({
|
|
85
|
+
local: allLocalSbComponentsSchemaFiles,
|
|
86
|
+
external: allExternalSbComponentsSchemaFiles,
|
|
87
|
+
});
|
|
88
|
+
// #4: sync - do all stuff already done (groups resolving, and so on)
|
|
89
|
+
syncComponents({
|
|
90
|
+
presets,
|
|
91
|
+
specifiedComponents: [...local, ...external],
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
else {
|
|
95
|
+
// implement discovering and syncrhonizing with packageName
|
|
96
|
+
// #1: discover all external .sb.js files
|
|
97
|
+
const allLocalSbComponentsSchemaFiles = discoverManyByPackageName({
|
|
98
|
+
scope: SCOPE.local,
|
|
99
|
+
packageNames: components,
|
|
100
|
+
});
|
|
101
|
+
// #2: discover all local .sb.js files
|
|
102
|
+
const allExternalSbComponentsSchemaFiles = discoverManyByPackageName({
|
|
103
|
+
scope: SCOPE.external,
|
|
104
|
+
packageNames: components,
|
|
105
|
+
});
|
|
106
|
+
// #3: compare results, prefer local ones (so we have to create final external paths array and local array of things to sync from where)
|
|
107
|
+
const { local, external } = compare({
|
|
108
|
+
local: allLocalSbComponentsSchemaFiles,
|
|
109
|
+
external: allExternalSbComponentsSchemaFiles,
|
|
110
|
+
});
|
|
111
|
+
// #4: sync - do all stuff already done (groups resolving, and so on)
|
|
112
|
+
syncComponents({
|
|
113
|
+
presets,
|
|
114
|
+
specifiedComponents: [...local, ...external],
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
export const syncAllComponents = ({ presets }) => {
|
|
119
|
+
// #1: discover all external .sb.js files
|
|
120
|
+
const allLocalSbComponentsSchemaFiles = discover({
|
|
121
|
+
scope: SCOPE.local,
|
|
122
|
+
type: LOOKUP_TYPE.fileName,
|
|
123
|
+
});
|
|
124
|
+
// #2: discover all local .sb.js files
|
|
125
|
+
const allExternalSbComponentsSchemaFiles = discover({
|
|
126
|
+
scope: SCOPE.external,
|
|
127
|
+
type: LOOKUP_TYPE.fileName,
|
|
128
|
+
});
|
|
129
|
+
// #3: compare results, prefare local ones (so we have to create final external paths array and local array of things to sync from where)
|
|
130
|
+
const { local, external } = compare({
|
|
131
|
+
local: allLocalSbComponentsSchemaFiles,
|
|
132
|
+
external: allExternalSbComponentsSchemaFiles,
|
|
133
|
+
});
|
|
134
|
+
// #4: sync - do all stuff already done (groups resolving, and so on)
|
|
135
|
+
syncComponents({
|
|
136
|
+
presets,
|
|
137
|
+
specifiedComponents: [...local, ...external],
|
|
138
|
+
});
|
|
139
|
+
};
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import Logger from "../utils/logger.js";
|
|
2
|
+
import storyblokConfig from "../config/config.js";
|
|
3
|
+
import { sbApi } from "./config.js";
|
|
4
|
+
import _resolvePresets from "./resolvePresets.js";
|
|
5
|
+
const { spaceId } = storyblokConfig;
|
|
6
|
+
// UPDATE
|
|
7
|
+
export const updateComponent = (component, presets) => {
|
|
8
|
+
Logger.log(`Trying to update '${component.name}' with id ${component.id}`);
|
|
9
|
+
const componentWithPresets = component;
|
|
10
|
+
const { all_presets, ...componentWithoutPresets } = componentWithPresets;
|
|
11
|
+
sbApi
|
|
12
|
+
.put(`spaces/${spaceId}/components/${component.id}`, {
|
|
13
|
+
component: componentWithoutPresets,
|
|
14
|
+
})
|
|
15
|
+
.then((res) => {
|
|
16
|
+
Logger.success(`Component '${component.name}' has been updated.`);
|
|
17
|
+
if (presets) {
|
|
18
|
+
_resolvePresets(res, all_presets, component);
|
|
19
|
+
}
|
|
20
|
+
})
|
|
21
|
+
.catch((err) => {
|
|
22
|
+
Logger.error("error happened... :(");
|
|
23
|
+
console.log(`${err.message} in migration of ${component.name} in updateComponent function`);
|
|
24
|
+
});
|
|
25
|
+
};
|
|
26
|
+
// CREATE
|
|
27
|
+
export const createComponent = (component, presets) => {
|
|
28
|
+
Logger.log(`Trying to create '${component.name}'`);
|
|
29
|
+
const componentWithPresets = component;
|
|
30
|
+
const { all_presets, ...componentWithoutPresets } = componentWithPresets;
|
|
31
|
+
sbApi
|
|
32
|
+
.post(`spaces/${spaceId}/components/`, {
|
|
33
|
+
component: componentWithoutPresets,
|
|
34
|
+
})
|
|
35
|
+
.then((res) => {
|
|
36
|
+
Logger.success(`Component '${component.name}' has been created.`);
|
|
37
|
+
if (presets) {
|
|
38
|
+
_resolvePresets(res, all_presets, component);
|
|
39
|
+
}
|
|
40
|
+
})
|
|
41
|
+
.catch((err) => {
|
|
42
|
+
Logger.error("error happened... :(");
|
|
43
|
+
console.log(`${err.message} in migration of ${component.name} in createComponent function`);
|
|
44
|
+
});
|
|
45
|
+
};
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import Logger from "../utils/logger.js";
|
|
2
|
+
import storyblokConfig from "../config/config.js";
|
|
3
|
+
import { sbApi } from "./config.js";
|
|
4
|
+
const { spaceId } = storyblokConfig;
|
|
5
|
+
// GET
|
|
6
|
+
export const getPreset = (presetId) => {
|
|
7
|
+
Logger.log(`Trying to get preset by id: ${presetId}`);
|
|
8
|
+
return sbApi
|
|
9
|
+
.get(`spaces/${spaceId}/presets/${presetId}`)
|
|
10
|
+
.then((response) => response.data)
|
|
11
|
+
.then((response) => {
|
|
12
|
+
if (Array.isArray(response.presets)) {
|
|
13
|
+
Logger.warning(`There is no preset for '${presetId}' preset id`);
|
|
14
|
+
return false;
|
|
15
|
+
}
|
|
16
|
+
return response;
|
|
17
|
+
})
|
|
18
|
+
.catch((err) => Logger.error(err));
|
|
19
|
+
};
|
|
20
|
+
export const getAllPresets = () => {
|
|
21
|
+
Logger.log("Trying to get all Presets.");
|
|
22
|
+
return sbApi
|
|
23
|
+
.get(`spaces/${spaceId}/presets/`)
|
|
24
|
+
.then((response) => response.data)
|
|
25
|
+
.catch((err) => Logger.error(err));
|
|
26
|
+
};
|
|
27
|
+
// CREATE
|
|
28
|
+
export const createPreset = (p) => {
|
|
29
|
+
sbApi
|
|
30
|
+
.post(`spaces/${spaceId}/presets/`, {
|
|
31
|
+
preset: p.preset,
|
|
32
|
+
})
|
|
33
|
+
.then(() => {
|
|
34
|
+
Logger.warning(`Preset: '${p.preset.name}' has been created.`);
|
|
35
|
+
})
|
|
36
|
+
.catch(() => {
|
|
37
|
+
Logger.error(`Error happened. Preset: '${p.preset.name}' has been not created.`);
|
|
38
|
+
});
|
|
39
|
+
};
|
|
40
|
+
// UPDATE
|
|
41
|
+
export const updatePreset = (p) => {
|
|
42
|
+
sbApi
|
|
43
|
+
.put(`spaces/${spaceId}/presets/${p.preset.id}`, {
|
|
44
|
+
preset: p.preset,
|
|
45
|
+
})
|
|
46
|
+
.then(() => {
|
|
47
|
+
Logger.warning(`Preset: '${p.preset.name}' with '${p.preset.id}' id has been updated.`);
|
|
48
|
+
})
|
|
49
|
+
.catch(() => {
|
|
50
|
+
Logger.error(`Error happened. Preset: '${p.preset.name}' with '${p.preset.id}' id has been not updated.`);
|
|
51
|
+
});
|
|
52
|
+
};
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import Logger from "../utils/logger.js";
|
|
2
|
+
import { updatePreset, createPreset } from "./presets.js";
|
|
3
|
+
import { getComponentPresets } from "./componentPresets.js";
|
|
4
|
+
const _resolvePresets = async (res, all_presets, component) => {
|
|
5
|
+
const componentId = res.data.component.id;
|
|
6
|
+
if (all_presets && all_presets.length > 0) {
|
|
7
|
+
const all_presets_modified = all_presets.map((p) => {
|
|
8
|
+
return { preset: { ...p.preset, component_id: componentId } };
|
|
9
|
+
});
|
|
10
|
+
Logger.log(`Checking preset for '${component.name}' component`);
|
|
11
|
+
const allRemoteComponentPresets = await getComponentPresets(component.name);
|
|
12
|
+
const presetsToUpdate = [];
|
|
13
|
+
const presetsToCreate = [];
|
|
14
|
+
for (const componentPreset of all_presets_modified) {
|
|
15
|
+
const shouldBeUpdated = allRemoteComponentPresets &&
|
|
16
|
+
allRemoteComponentPresets.find((remotePreset) => componentPreset.preset.name === remotePreset.preset.name);
|
|
17
|
+
if (shouldBeUpdated) {
|
|
18
|
+
presetsToUpdate.push({
|
|
19
|
+
...componentPreset,
|
|
20
|
+
preset: {
|
|
21
|
+
id: shouldBeUpdated.preset.id,
|
|
22
|
+
...componentPreset.preset,
|
|
23
|
+
},
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
presetsToCreate.push(componentPreset);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
presetsToUpdate.map((preset) => {
|
|
31
|
+
updatePreset(preset);
|
|
32
|
+
});
|
|
33
|
+
presetsToCreate.map((preset) => {
|
|
34
|
+
createPreset(preset);
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
else {
|
|
38
|
+
Logger.warning("There are no presets for this component.");
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
export default _resolvePresets;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { OneComponent } from "../utils/discover.js";
|
|
2
|
+
export declare const createRole: (role: any) => void;
|
|
3
|
+
export declare const updateRole: (role: any) => void;
|
|
4
|
+
export declare const getAllRoles: () => Promise<any>;
|
|
5
|
+
export declare const getRole: (roleName: string | undefined) => Promise<any>;
|
|
6
|
+
interface SyncRoles {
|
|
7
|
+
specifiedRoles: OneComponent[];
|
|
8
|
+
}
|
|
9
|
+
export declare const syncRoles: ({ specifiedRoles }: SyncRoles) => Promise<void>;
|
|
10
|
+
export declare const syncAllRoles: () => Promise<void>;
|
|
11
|
+
export declare const syncProvidedRoles: ({ roles }: {
|
|
12
|
+
roles: string[];
|
|
13
|
+
}) => void;
|
|
14
|
+
export {};
|