sb-mig 5.6.0-beta.1 → 5.6.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.
- package/README.md +20 -19
- package/dist/api/assets/assets.js +2 -15
- package/dist/api/auth/auth.types.d.ts +1 -1
- package/dist/api/components/components.js +2 -1
- package/dist/api/components/components.sync.d.ts +8 -0
- package/dist/api/components/components.sync.js +193 -0
- package/dist/api/data-migration/component-data-migration.js +2 -2
- package/dist/api/datasources/datasource-entries.js +4 -5
- package/dist/api/datasources/datasources.d.ts +5 -2
- package/dist/api/datasources/datasources.js +42 -35
- package/dist/api/datasources/datasources.sync.d.ts +2 -0
- package/dist/api/datasources/datasources.sync.js +11 -0
- package/dist/api/datasources/datasources.types.d.ts +1 -1
- package/dist/api/datasources/index.d.ts +2 -1
- package/dist/api/datasources/index.js +2 -1
- package/dist/api/managementApi.d.ts +2 -2
- package/dist/api/migrate.d.ts +1 -1
- package/dist/api/migrate.js +3 -48
- package/dist/api/plugins/index.d.ts +2 -1
- package/dist/api/plugins/index.js +2 -1
- package/dist/api/plugins/plugins.d.ts +7 -2
- package/dist/api/plugins/plugins.js +28 -15
- package/dist/api/plugins/plugins.sync.d.ts +2 -0
- package/dist/api/plugins/plugins.sync.js +11 -0
- package/dist/api/roles/index.d.ts +2 -1
- package/dist/api/roles/index.js +2 -1
- package/dist/api/roles/roles.d.ts +5 -2
- package/dist/api/roles/roles.js +34 -11
- package/dist/api/roles/roles.sync.d.ts +2 -0
- package/dist/api/roles/roles.sync.js +6 -0
- package/dist/api/roles/roles.types.d.ts +1 -1
- package/dist/api/stories/stories.js +3 -11
- package/dist/api/sync/sync.types.d.ts +30 -0
- package/dist/api/sync/sync.types.js +1 -0
- package/dist/api/testApi.d.ts +2 -2
- package/dist/api/utils/helper-functions.d.ts +5 -1
- package/dist/api/utils/helper-functions.js +6 -1
- package/dist/api/utils/request.d.ts +1 -1
- package/dist/api/utils/request.js +11 -2
- package/dist/api/utils/resolverTransformations.js +2 -57
- package/dist/api-v2/assets/index.d.ts +13 -0
- package/dist/api-v2/assets/index.js +25 -0
- package/dist/api-v2/auth/index.d.ts +3 -0
- package/dist/api-v2/auth/index.js +8 -0
- package/dist/api-v2/client.d.ts +13 -0
- package/dist/api-v2/client.js +17 -0
- package/dist/api-v2/components/index.d.ts +10 -0
- package/dist/api-v2/components/index.js +29 -0
- package/dist/api-v2/datasources/index.d.ts +8 -0
- package/dist/api-v2/datasources/index.js +58 -0
- package/dist/api-v2/discover/discover.d.ts +47 -0
- package/dist/api-v2/discover/discover.js +328 -0
- package/dist/api-v2/discover/index.d.ts +2 -0
- package/dist/api-v2/discover/index.js +1 -0
- package/dist/api-v2/index.d.ts +19 -0
- package/dist/api-v2/index.js +21 -0
- package/dist/api-v2/plugins/index.d.ts +9 -0
- package/dist/api-v2/plugins/index.js +42 -0
- package/dist/api-v2/precompile/index.d.ts +2 -0
- package/dist/api-v2/precompile/index.js +1 -0
- package/dist/api-v2/precompile/precompile.d.ts +65 -0
- package/dist/api-v2/precompile/precompile.js +127 -0
- package/dist/api-v2/presets/index.d.ts +13 -0
- package/dist/api-v2/presets/index.js +25 -0
- package/dist/api-v2/requestConfig.d.ts +5 -0
- package/dist/api-v2/requestConfig.js +34 -0
- package/dist/api-v2/roles/index.d.ts +5 -0
- package/dist/api-v2/roles/index.js +35 -0
- package/dist/api-v2/spaces/index.d.ts +7 -0
- package/dist/api-v2/spaces/index.js +11 -0
- package/dist/api-v2/stories/index.d.ts +34 -0
- package/dist/api-v2/stories/index.js +172 -0
- package/dist/api-v2/stories/types.d.ts +28 -0
- package/dist/api-v2/stories/types.js +1 -0
- package/dist/api-v2/sync/index.d.ts +24 -0
- package/dist/api-v2/sync/index.js +109 -0
- package/dist/api-v2/sync/types.d.ts +1 -0
- package/dist/api-v2/sync/types.js +1 -0
- package/dist/api-v2/test.d.ts +15 -0
- package/dist/api-v2/test.js +21 -0
- package/dist/cli/commands/backup.js +7 -3
- package/dist/cli/commands/copy.js +2 -2
- package/dist/cli/commands/migrate.js +1 -2
- package/dist/cli/commands/migrations.js +2 -2
- package/dist/cli/commands/remove.js +1 -1
- package/dist/cli/commands/revert.js +2 -2
- package/dist/cli/commands/sync.js +1 -2
- package/dist/cli/index.js +1 -1
- package/dist/cli/utils/cli-utils.d.ts +69 -0
- package/dist/cli/utils/cli-utils.js +100 -0
- package/dist/cli/utils/discover.d.ts +3 -22
- package/dist/cli/utils/discover.js +53 -100
- package/dist/config/config.d.ts +2 -39
- package/dist/config/config.types.d.ts +40 -0
- package/dist/config/config.types.js +1 -0
- package/dist/config/defaultConfig.d.ts +1 -1
- package/dist/config/defaultConfig.js +2 -2
- package/dist/utils/array-utils.d.ts +20 -0
- package/dist/utils/array-utils.js +20 -0
- package/dist/utils/async-utils.d.ts +13 -0
- package/dist/utils/async-utils.js +13 -0
- package/dist/utils/date-utils.d.ts +14 -0
- package/dist/utils/date-utils.js +21 -0
- package/dist/utils/files.d.ts +35 -0
- package/dist/utils/files.js +57 -2
- package/dist/utils/main.d.ts +8 -18
- package/dist/utils/main.js +12 -104
- package/dist/utils/migrations.d.ts +9 -3
- package/dist/utils/object-utils.d.ts +46 -0
- package/dist/utils/object-utils.js +71 -0
- package/dist/utils/others.d.ts +6 -9
- package/dist/utils/others.js +8 -15
- package/dist/utils/path-utils.d.ts +89 -0
- package/dist/utils/path-utils.js +106 -0
- package/dist/utils/pkg.d.ts +16 -2
- package/dist/utils/pkg.js +16 -3
- package/dist/utils/string-utils.d.ts +33 -0
- package/dist/utils/string-utils.js +45 -0
- package/dist/utils/transform-utils.d.ts +62 -0
- package/dist/utils/transform-utils.js +113 -0
- package/dist-cjs/api/auth/auth.js +28 -0
- package/dist-cjs/api/auth/auth.types.js +2 -0
- package/dist-cjs/api/components/components.js +202 -0
- package/dist-cjs/api/components/components.sync.js +199 -0
- package/dist-cjs/api/components/components.types.js +2 -0
- package/dist-cjs/api/datasources/datasource-entries.js +166 -0
- package/dist-cjs/api/datasources/datasources.js +166 -0
- package/dist-cjs/api/datasources/datasources.types.js +2 -0
- package/dist-cjs/api/plugins/plugins.js +132 -0
- package/dist-cjs/api/plugins/plugins.types.js +2 -0
- package/dist-cjs/api/presets/componentPresets.js +25 -0
- package/dist-cjs/api/presets/presets.js +92 -0
- package/dist-cjs/api/presets/presets.types.js +2 -0
- package/dist-cjs/api/presets/resolvePresets.js +49 -0
- package/dist-cjs/api/roles/roles.js +131 -0
- package/dist-cjs/api/roles/roles.types.js +2 -0
- package/dist-cjs/api/spaces/spaces.js +34 -0
- package/dist-cjs/api/spaces/spaces.types.js +2 -0
- package/dist-cjs/api/stories/stories.js +214 -0
- package/dist-cjs/api/stories/stories.types.js +2 -0
- package/dist-cjs/api/sync/sync.types.js +2 -0
- package/dist-cjs/api/utils/request.js +48 -0
- package/dist-cjs/api/utils/resolvers.types.js +2 -0
- package/dist-cjs/api-v2/assets/index.js +30 -0
- package/dist-cjs/api-v2/auth/index.js +12 -0
- package/dist-cjs/api-v2/client.js +23 -0
- package/dist-cjs/api-v2/components/index.js +40 -0
- package/dist-cjs/api-v2/datasources/index.js +64 -0
- package/dist-cjs/api-v2/discover/discover.js +368 -0
- package/dist-cjs/api-v2/discover/index.js +9 -0
- package/dist-cjs/api-v2/index.js +60 -0
- package/dist-cjs/api-v2/plugins/index.js +49 -0
- package/dist-cjs/api-v2/precompile/index.js +7 -0
- package/dist-cjs/api-v2/precompile/precompile.js +136 -0
- package/dist-cjs/api-v2/presets/index.js +33 -0
- package/dist-cjs/api-v2/requestConfig.js +37 -0
- package/dist-cjs/api-v2/roles/index.js +41 -0
- package/dist-cjs/api-v2/spaces/index.js +16 -0
- package/dist-cjs/api-v2/stories/index.js +180 -0
- package/dist-cjs/api-v2/stories/types.js +2 -0
- package/dist-cjs/api-v2/sync/index.js +115 -0
- package/dist-cjs/api-v2/sync/types.js +2 -0
- package/dist-cjs/api-v2/test.js +25 -0
- package/dist-cjs/config/config.types.js +2 -0
- package/dist-cjs/config/constants.js +29 -0
- package/dist-cjs/package.json +3 -0
- package/dist-cjs/utils/array-utils.js +24 -0
- package/dist-cjs/utils/logger.js +32 -0
- package/dist-cjs/utils/object-utils.js +77 -0
- package/dist-cjs/utils/path-utils.js +115 -0
- package/package.json +43 -27
- package/dist/utils/pkg-require.d.ts +0 -2
- package/dist/utils/pkg-require.js +0 -4
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.syncComponentsData = syncComponentsData;
|
|
7
|
+
const array_utils_js_1 = require("../../utils/array-utils.js");
|
|
8
|
+
const logger_js_1 = __importDefault(require("../../utils/logger.js"));
|
|
9
|
+
const object_utils_js_1 = require("../../utils/object-utils.js");
|
|
10
|
+
/**
|
|
11
|
+
* Default progress callback that logs to console
|
|
12
|
+
*/
|
|
13
|
+
const defaultProgress = (event) => {
|
|
14
|
+
if (event.type === "start") {
|
|
15
|
+
logger_js_1.default.log(`Starting sync of ${event.total} components...`);
|
|
16
|
+
}
|
|
17
|
+
else if (event.type === "progress" && event.name) {
|
|
18
|
+
const status = event.action === "creating"
|
|
19
|
+
? "Creating"
|
|
20
|
+
: event.action === "updating"
|
|
21
|
+
? "Updating"
|
|
22
|
+
: event.action === "created"
|
|
23
|
+
? "✓ Created"
|
|
24
|
+
: event.action === "updated"
|
|
25
|
+
? "✓ Updated"
|
|
26
|
+
: event.action === "skipped"
|
|
27
|
+
? "⏭ Skipped"
|
|
28
|
+
: "✘ Error";
|
|
29
|
+
logger_js_1.default.log(`[${event.current}/${event.total}] ${status}: ${event.name}`);
|
|
30
|
+
}
|
|
31
|
+
else if (event.type === "complete") {
|
|
32
|
+
logger_js_1.default.success(`Sync complete: ${event.message ?? "done"}`);
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
const components_js_1 = require("./components.js");
|
|
36
|
+
async function ensureComponentGroupsExist(groupNames, config) {
|
|
37
|
+
try {
|
|
38
|
+
const existing = await (0, components_js_1.getAllComponentsGroups)(config);
|
|
39
|
+
const existingNames = new Set((existing ?? []).map((g) => g.name));
|
|
40
|
+
for (const groupName of groupNames) {
|
|
41
|
+
if (!existingNames.has(groupName)) {
|
|
42
|
+
await (0, components_js_1.createComponentsGroup)(groupName, config);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
catch (error) {
|
|
47
|
+
// Log but don't fail - component groups are optional
|
|
48
|
+
logger_js_1.default.warning(`Could not fetch component groups: ${error instanceof Error ? error.message : String(error)}`);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
function resolveGroupUuid(component, remoteGroups) {
|
|
52
|
+
if (!component.component_group_name) {
|
|
53
|
+
return { ...component, component_group_uuid: null };
|
|
54
|
+
}
|
|
55
|
+
const match = remoteGroups.find((g) => g.name === component.component_group_name);
|
|
56
|
+
if (!match)
|
|
57
|
+
return { ...component, component_group_uuid: null };
|
|
58
|
+
return { ...component, component_group_uuid: match.uuid };
|
|
59
|
+
}
|
|
60
|
+
async function syncComponentsData(args, config) {
|
|
61
|
+
const { components, presets, ssot, onProgress } = args;
|
|
62
|
+
const progress = onProgress ?? defaultProgress;
|
|
63
|
+
const result = {
|
|
64
|
+
created: [],
|
|
65
|
+
updated: [],
|
|
66
|
+
skipped: [],
|
|
67
|
+
errors: [],
|
|
68
|
+
};
|
|
69
|
+
if (ssot) {
|
|
70
|
+
const existingComponents = await (0, components_js_1.getAllComponents)(config);
|
|
71
|
+
const existingGroups = await (0, components_js_1.getAllComponentsGroups)(config);
|
|
72
|
+
await Promise.allSettled([
|
|
73
|
+
...(existingComponents ?? []).map((c) => (0, components_js_1.removeComponent)(c, config)),
|
|
74
|
+
...(existingGroups ?? []).map((g) => (0, components_js_1.removeComponentGroup)(g, config)),
|
|
75
|
+
]);
|
|
76
|
+
}
|
|
77
|
+
const nonEmptyComponents = components.filter((c) => !(0, object_utils_js_1.isObjectEmpty)(c));
|
|
78
|
+
const groupsToCheck = (0, array_utils_js_1.uniqueValuesFrom)(nonEmptyComponents
|
|
79
|
+
.filter((c) => c.component_group_name)
|
|
80
|
+
.map((c) => c.component_group_name));
|
|
81
|
+
await ensureComponentGroupsExist(groupsToCheck, config);
|
|
82
|
+
let remoteComponents = [];
|
|
83
|
+
let remoteGroups = [];
|
|
84
|
+
try {
|
|
85
|
+
remoteComponents = (await (0, components_js_1.getAllComponents)(config)) ?? [];
|
|
86
|
+
}
|
|
87
|
+
catch (error) {
|
|
88
|
+
logger_js_1.default.warning(`Could not fetch remote components: ${error instanceof Error ? error.message : String(error)}`);
|
|
89
|
+
}
|
|
90
|
+
try {
|
|
91
|
+
remoteGroups = (await (0, components_js_1.getAllComponentsGroups)(config)) ?? [];
|
|
92
|
+
}
|
|
93
|
+
catch (error) {
|
|
94
|
+
logger_js_1.default.warning(`Could not fetch remote groups: ${error instanceof Error ? error.message : String(error)}`);
|
|
95
|
+
}
|
|
96
|
+
const componentsToUpdate = [];
|
|
97
|
+
const componentsToCreate = [];
|
|
98
|
+
for (const component of nonEmptyComponents) {
|
|
99
|
+
if (!component?.name) {
|
|
100
|
+
result.skipped.push("unknown");
|
|
101
|
+
continue;
|
|
102
|
+
}
|
|
103
|
+
const remote = remoteComponents.find((rc) => rc.name === component.name);
|
|
104
|
+
if (remote) {
|
|
105
|
+
componentsToUpdate.push({ id: remote.id, ...component });
|
|
106
|
+
}
|
|
107
|
+
else {
|
|
108
|
+
componentsToCreate.push(component);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
// Resolve group uuids after ensureComponentGroupsExist
|
|
112
|
+
const updatePayloads = componentsToUpdate.map((c) => resolveGroupUuid(c, remoteGroups));
|
|
113
|
+
const createPayloads = componentsToCreate.map((c) => resolveGroupUuid(c, remoteGroups));
|
|
114
|
+
const totalComponents = updatePayloads.length + createPayloads.length;
|
|
115
|
+
let currentIndex = 0;
|
|
116
|
+
// Report start
|
|
117
|
+
progress({ type: "start", total: totalComponents });
|
|
118
|
+
// Process updates sequentially for progress reporting
|
|
119
|
+
for (const component of updatePayloads) {
|
|
120
|
+
const name = String(component?.name ?? "unknown");
|
|
121
|
+
currentIndex++;
|
|
122
|
+
progress({
|
|
123
|
+
type: "progress",
|
|
124
|
+
current: currentIndex,
|
|
125
|
+
total: totalComponents,
|
|
126
|
+
name,
|
|
127
|
+
action: "updating",
|
|
128
|
+
});
|
|
129
|
+
try {
|
|
130
|
+
await (0, components_js_1.updateComponent)(component, presets, config);
|
|
131
|
+
result.updated.push(name);
|
|
132
|
+
progress({
|
|
133
|
+
type: "progress",
|
|
134
|
+
current: currentIndex,
|
|
135
|
+
total: totalComponents,
|
|
136
|
+
name,
|
|
137
|
+
action: "updated",
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
catch (error) {
|
|
141
|
+
result.errors.push({
|
|
142
|
+
name,
|
|
143
|
+
message: error instanceof Error ? error.message : String(error),
|
|
144
|
+
});
|
|
145
|
+
progress({
|
|
146
|
+
type: "progress",
|
|
147
|
+
current: currentIndex,
|
|
148
|
+
total: totalComponents,
|
|
149
|
+
name,
|
|
150
|
+
action: "error",
|
|
151
|
+
message: error instanceof Error ? error.message : String(error),
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
// Process creates sequentially for progress reporting
|
|
156
|
+
for (const component of createPayloads) {
|
|
157
|
+
const name = String(component?.name ?? "unknown");
|
|
158
|
+
currentIndex++;
|
|
159
|
+
progress({
|
|
160
|
+
type: "progress",
|
|
161
|
+
current: currentIndex,
|
|
162
|
+
total: totalComponents,
|
|
163
|
+
name,
|
|
164
|
+
action: "creating",
|
|
165
|
+
});
|
|
166
|
+
try {
|
|
167
|
+
await (0, components_js_1.createComponent)(component, presets, config);
|
|
168
|
+
result.created.push(name);
|
|
169
|
+
progress({
|
|
170
|
+
type: "progress",
|
|
171
|
+
current: currentIndex,
|
|
172
|
+
total: totalComponents,
|
|
173
|
+
name,
|
|
174
|
+
action: "created",
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
catch (error) {
|
|
178
|
+
result.errors.push({
|
|
179
|
+
name,
|
|
180
|
+
message: error instanceof Error ? error.message : String(error),
|
|
181
|
+
});
|
|
182
|
+
progress({
|
|
183
|
+
type: "progress",
|
|
184
|
+
current: currentIndex,
|
|
185
|
+
total: totalComponents,
|
|
186
|
+
name,
|
|
187
|
+
action: "error",
|
|
188
|
+
message: error instanceof Error ? error.message : String(error),
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
// Report completion
|
|
193
|
+
progress({
|
|
194
|
+
type: "complete",
|
|
195
|
+
total: totalComponents,
|
|
196
|
+
message: `${result.created.length} created, ${result.updated.length} updated, ${result.errors.length} errors`,
|
|
197
|
+
});
|
|
198
|
+
return result;
|
|
199
|
+
}
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.updateDatasourceEntry = exports.createDatasourceEntry = exports.createDatasourceEntries = exports.getDatasourceEntries = void 0;
|
|
7
|
+
const chalk_1 = __importDefault(require("chalk"));
|
|
8
|
+
const logger_js_1 = __importDefault(require("../../utils/logger.js"));
|
|
9
|
+
const object_utils_js_1 = require("../../utils/object-utils.js");
|
|
10
|
+
const datasources_js_1 = require("./datasources.js");
|
|
11
|
+
const _decorateWithDimensions = async (args, config) => {
|
|
12
|
+
const { currentDatasource, dimensionsData, _callback } = args;
|
|
13
|
+
const { spaceId, sbApi } = config;
|
|
14
|
+
// callback for create or update
|
|
15
|
+
await _callback();
|
|
16
|
+
const dimensionValueEntries = Object.entries(dimensionsData.dimensionValues);
|
|
17
|
+
return dimensionValueEntries.map(([name, value]) => {
|
|
18
|
+
const data = dimensionsData.datasourceDimensions.find((dimension) => dimension.name === name);
|
|
19
|
+
const params = {
|
|
20
|
+
datasource_entry: {
|
|
21
|
+
...dimensionsData.finalDatasource_entry,
|
|
22
|
+
id: dimensionsData.finalDatasource_entry.datasource_id,
|
|
23
|
+
dimension_value: value,
|
|
24
|
+
},
|
|
25
|
+
dimension_id: data.id,
|
|
26
|
+
};
|
|
27
|
+
return sbApi
|
|
28
|
+
.put(`spaces/${spaceId}/datasource_entries/${dimensionsData.finalDatasource_entry.id}`, params)
|
|
29
|
+
.then((_) => {
|
|
30
|
+
console.log(`${chalk_1.default.green("✓ Datasource Entry Dimension value for")} ${chalk_1.default.blue(dimensionsData.finalDatasource_entry.name)} and dimension ${chalk_1.default.blue(name)} in ${chalk_1.default.red(currentDatasource.datasource.name)} datasource ${chalk_1.default.green("was successfully updated.")}`);
|
|
31
|
+
return true;
|
|
32
|
+
})
|
|
33
|
+
.catch((err) => {
|
|
34
|
+
logger_js_1.default.error(err);
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
};
|
|
38
|
+
const getDatasourceEntries = async (args, config) => {
|
|
39
|
+
const { datasourceName } = args;
|
|
40
|
+
const { spaceId, sbApi } = config;
|
|
41
|
+
logger_js_1.default.log(`Trying to get '${datasourceName}' datasource entries.`);
|
|
42
|
+
const data = await (0, datasources_js_1.getDatasource)({ datasourceName }, config); // TODO: maybe this step is not needed, i think we can retrieve entries directly using slug (but using delivery api, not management)
|
|
43
|
+
if (data) {
|
|
44
|
+
return sbApi
|
|
45
|
+
.get(`spaces/${spaceId}/datasource_entries/`, {
|
|
46
|
+
datasource_id: data[0].id,
|
|
47
|
+
})
|
|
48
|
+
.then(async (response) => {
|
|
49
|
+
logger_js_1.default.success(`Datasource Entries for '${datasourceName}' datasource successfully retrieved.`);
|
|
50
|
+
const { data } = response;
|
|
51
|
+
return data;
|
|
52
|
+
})
|
|
53
|
+
.catch((err) => logger_js_1.default.error(err));
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
exports.getDatasourceEntries = getDatasourceEntries;
|
|
57
|
+
const createDatasourceEntries = (args, config) => {
|
|
58
|
+
const { datasource_entries, remoteDatasourceEntries, data } = args;
|
|
59
|
+
Promise.all(datasource_entries.map((datasourceEntry) => {
|
|
60
|
+
const datasourceToBeUpdated = remoteDatasourceEntries.datasource_entries.find((remoteDatasourceEntry) => remoteDatasourceEntry.name ===
|
|
61
|
+
Object.values(datasourceEntry)[0]);
|
|
62
|
+
if (datasourceToBeUpdated) {
|
|
63
|
+
return (0, exports.updateDatasourceEntry)({ data, datasourceEntry, datasourceToBeUpdated }, config);
|
|
64
|
+
}
|
|
65
|
+
return (0, exports.createDatasourceEntry)({ data, datasourceEntry }, config);
|
|
66
|
+
}))
|
|
67
|
+
.then((_) => {
|
|
68
|
+
logger_js_1.default.success(`Datasource entries for ${data.datasource.id} datasource id has been successfully synced.`);
|
|
69
|
+
return data;
|
|
70
|
+
})
|
|
71
|
+
.catch((err) => logger_js_1.default.error(err));
|
|
72
|
+
};
|
|
73
|
+
exports.createDatasourceEntries = createDatasourceEntries;
|
|
74
|
+
const _createDatasourceEntry = (args, config) => {
|
|
75
|
+
const { currentDatasource, finalDatasource_entry } = args;
|
|
76
|
+
const { spaceId, sbApi } = config;
|
|
77
|
+
if (config.debug) {
|
|
78
|
+
console.log("############# Entity to Create: ");
|
|
79
|
+
console.log(finalDatasource_entry);
|
|
80
|
+
console.log("################################");
|
|
81
|
+
}
|
|
82
|
+
return sbApi
|
|
83
|
+
.post(`spaces/${spaceId}/datasource_entries/`, {
|
|
84
|
+
datasource_entry: finalDatasource_entry,
|
|
85
|
+
})
|
|
86
|
+
.then(({ data }) => {
|
|
87
|
+
console.log(`${chalk_1.default.green("✓ Datasource Entry")} ${chalk_1.default.blue(data.datasource_entry.name)} in ${chalk_1.default.red(currentDatasource.datasource.name)} datasource ${chalk_1.default.green("was successfully created.")}`);
|
|
88
|
+
return data;
|
|
89
|
+
})
|
|
90
|
+
.catch((err) => {
|
|
91
|
+
if (config.debug) {
|
|
92
|
+
console.log("Full Create error: ");
|
|
93
|
+
console.log(err);
|
|
94
|
+
}
|
|
95
|
+
logger_js_1.default.error(`Unable to create datasource entry in ${currentDatasource.datasource.name} datasource.`);
|
|
96
|
+
});
|
|
97
|
+
};
|
|
98
|
+
const createDatasourceEntry = (args, config) => {
|
|
99
|
+
const { datasourceEntry, data } = args;
|
|
100
|
+
const finalDatasource_entry = {
|
|
101
|
+
name: datasourceEntry.name,
|
|
102
|
+
value: datasourceEntry.value,
|
|
103
|
+
datasource_id: data.datasource.id,
|
|
104
|
+
};
|
|
105
|
+
if ((0, object_utils_js_1.isObjectEmpty)(datasourceEntry.dimension_values)) {
|
|
106
|
+
return _createDatasourceEntry({ currentDatasource: data, finalDatasource_entry }, config);
|
|
107
|
+
}
|
|
108
|
+
else {
|
|
109
|
+
return _decorateWithDimensions({
|
|
110
|
+
currentDatasource: data,
|
|
111
|
+
dimensionsData: {
|
|
112
|
+
finalDatasource_entry,
|
|
113
|
+
dimensionValues: datasourceEntry.dimension_values,
|
|
114
|
+
datasourceDimensions: data.datasource.dimensions,
|
|
115
|
+
},
|
|
116
|
+
_callback: () => _createDatasourceEntry({ currentDatasource: data, finalDatasource_entry }, config),
|
|
117
|
+
}, config);
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
exports.createDatasourceEntry = createDatasourceEntry;
|
|
121
|
+
const _updateDatasourceEntry = (args, config) => {
|
|
122
|
+
const { currentDatasource, finalDatasource_entry } = args;
|
|
123
|
+
const { spaceId, sbApi } = config;
|
|
124
|
+
return sbApi
|
|
125
|
+
.put(`spaces/${spaceId}/datasource_entries/${finalDatasource_entry.id}`, {
|
|
126
|
+
datasource_entry: finalDatasource_entry,
|
|
127
|
+
})
|
|
128
|
+
.then((_) => {
|
|
129
|
+
console.log(`${chalk_1.default.green("✓ Datasource Entry")} ${chalk_1.default.blue(finalDatasource_entry.name)} in ${chalk_1.default.red(currentDatasource.datasource.name)} datasource ${chalk_1.default.green("was successfully updated.")}`);
|
|
130
|
+
return true;
|
|
131
|
+
})
|
|
132
|
+
.catch((err) => {
|
|
133
|
+
if (config.debug) {
|
|
134
|
+
console.log("Full update error: ");
|
|
135
|
+
console.log(err);
|
|
136
|
+
}
|
|
137
|
+
logger_js_1.default.error(`Unable to update datasource entry in ${currentDatasource.datasource.name} datasource.`);
|
|
138
|
+
});
|
|
139
|
+
};
|
|
140
|
+
const updateDatasourceEntry = (args, config) => {
|
|
141
|
+
const { datasourceEntry, datasourceToBeUpdated, data } = args;
|
|
142
|
+
const finalDatasource_entry = {
|
|
143
|
+
name: datasourceEntry.name,
|
|
144
|
+
value: datasourceEntry.value,
|
|
145
|
+
datasource_id: data.datasource.id,
|
|
146
|
+
id: datasourceToBeUpdated.id,
|
|
147
|
+
};
|
|
148
|
+
if ((0, object_utils_js_1.isObjectEmpty)(datasourceEntry.dimension_values)) {
|
|
149
|
+
return _updateDatasourceEntry({ currentDatasource: data, finalDatasource_entry }, config);
|
|
150
|
+
}
|
|
151
|
+
else {
|
|
152
|
+
return _decorateWithDimensions({
|
|
153
|
+
currentDatasource: data,
|
|
154
|
+
_callback: () => _updateDatasourceEntry({
|
|
155
|
+
currentDatasource: data,
|
|
156
|
+
finalDatasource_entry,
|
|
157
|
+
}, config),
|
|
158
|
+
dimensionsData: {
|
|
159
|
+
finalDatasource_entry,
|
|
160
|
+
dimensionValues: datasourceEntry.dimension_values,
|
|
161
|
+
datasourceDimensions: data.datasource.dimensions,
|
|
162
|
+
},
|
|
163
|
+
}, config);
|
|
164
|
+
}
|
|
165
|
+
};
|
|
166
|
+
exports.updateDatasourceEntry = updateDatasourceEntry;
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.syncDatasourcesData = exports.updateDatasource = exports.createDatasource = exports.getDatasource = exports.getAllDatasources = void 0;
|
|
7
|
+
const logger_js_1 = __importDefault(require("../../utils/logger.js"));
|
|
8
|
+
const request_js_1 = require("../utils/request.js");
|
|
9
|
+
const datasource_entries_js_1 = require("./datasource-entries.js");
|
|
10
|
+
// GET
|
|
11
|
+
const getAllDatasources = (config) => {
|
|
12
|
+
const { sbApi, spaceId } = config;
|
|
13
|
+
logger_js_1.default.log("Trying to get all Datasources.");
|
|
14
|
+
return (0, request_js_1.getAllItemsWithPagination)({
|
|
15
|
+
// @ts-ignore
|
|
16
|
+
apiFn: ({ per_page, page }) => {
|
|
17
|
+
return sbApi
|
|
18
|
+
.get(`spaces/${spaceId}/datasources/`)
|
|
19
|
+
.then((res) => {
|
|
20
|
+
if (res.total) {
|
|
21
|
+
logger_js_1.default.log(`Amount of datasources: ${res.total}`);
|
|
22
|
+
}
|
|
23
|
+
return res;
|
|
24
|
+
})
|
|
25
|
+
.catch((err) => {
|
|
26
|
+
if (err.response.status === 404) {
|
|
27
|
+
logger_js_1.default.error(`There is no datasources in your Storyblok ${spaceId} space.`);
|
|
28
|
+
return true;
|
|
29
|
+
}
|
|
30
|
+
else {
|
|
31
|
+
logger_js_1.default.error(err);
|
|
32
|
+
return false;
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
},
|
|
36
|
+
params: {
|
|
37
|
+
spaceId,
|
|
38
|
+
},
|
|
39
|
+
itemsKey: "datasources",
|
|
40
|
+
});
|
|
41
|
+
};
|
|
42
|
+
exports.getAllDatasources = getAllDatasources;
|
|
43
|
+
const getDatasource = (args, config) => {
|
|
44
|
+
const { datasourceName } = args;
|
|
45
|
+
logger_js_1.default.log(`Trying to get '${datasourceName}' datasource.`);
|
|
46
|
+
return (0, exports.getAllDatasources)(config)
|
|
47
|
+
.then((res) => {
|
|
48
|
+
if (res) {
|
|
49
|
+
return res.filter((datasource) => datasource.name === datasourceName);
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
return [];
|
|
53
|
+
}
|
|
54
|
+
})
|
|
55
|
+
.then((res) => {
|
|
56
|
+
if (Array.isArray(res) && res.length === 0) {
|
|
57
|
+
logger_js_1.default.warning(`There is no datasource named '${datasourceName}'`);
|
|
58
|
+
return false;
|
|
59
|
+
}
|
|
60
|
+
return res;
|
|
61
|
+
})
|
|
62
|
+
.catch((err) => logger_js_1.default.error(err));
|
|
63
|
+
};
|
|
64
|
+
exports.getDatasource = getDatasource;
|
|
65
|
+
// POST
|
|
66
|
+
const createDatasource = (args, config) => {
|
|
67
|
+
const { datasource } = args;
|
|
68
|
+
const { sbApi, spaceId } = config;
|
|
69
|
+
const finalDatasource = {
|
|
70
|
+
name: datasource.name,
|
|
71
|
+
slug: datasource.slug,
|
|
72
|
+
dimensions: [...datasource.dimensions],
|
|
73
|
+
dimensions_attributes: [...datasource.dimensions],
|
|
74
|
+
};
|
|
75
|
+
return sbApi
|
|
76
|
+
.post(`spaces/${spaceId}/datasources/`, {
|
|
77
|
+
datasource: finalDatasource,
|
|
78
|
+
})
|
|
79
|
+
.then(({ data }) => {
|
|
80
|
+
logger_js_1.default.success(`Datasource '${data.datasource.name}' with id '${data.datasource.id}' created.`);
|
|
81
|
+
return {
|
|
82
|
+
data,
|
|
83
|
+
datasource_entries: datasource.datasource_entries,
|
|
84
|
+
};
|
|
85
|
+
})
|
|
86
|
+
.catch((err) => logger_js_1.default.error(err));
|
|
87
|
+
};
|
|
88
|
+
exports.createDatasource = createDatasource;
|
|
89
|
+
const updateDatasource = (args, config) => {
|
|
90
|
+
const { datasource, datasourceToBeUpdated } = args;
|
|
91
|
+
const { sbApi, spaceId } = config;
|
|
92
|
+
const dimensionsToCreate = datasource.dimensions.filter((dimension) => {
|
|
93
|
+
const isDimensionInRemoteDatasource = datasourceToBeUpdated.dimensions.find((d) => dimension.name === d.name);
|
|
94
|
+
return !isDimensionInRemoteDatasource;
|
|
95
|
+
});
|
|
96
|
+
return sbApi
|
|
97
|
+
.put(`spaces/${spaceId}/datasources/${datasourceToBeUpdated.id}`, {
|
|
98
|
+
datasource: {
|
|
99
|
+
id: datasourceToBeUpdated.id,
|
|
100
|
+
name: datasource.name,
|
|
101
|
+
slug: datasource.slug,
|
|
102
|
+
dimensions: [
|
|
103
|
+
...datasourceToBeUpdated.dimensions,
|
|
104
|
+
...dimensionsToCreate,
|
|
105
|
+
],
|
|
106
|
+
dimensions_attributes: [
|
|
107
|
+
...datasourceToBeUpdated.dimensions,
|
|
108
|
+
...dimensionsToCreate,
|
|
109
|
+
],
|
|
110
|
+
},
|
|
111
|
+
})
|
|
112
|
+
.then(({ data }) => {
|
|
113
|
+
logger_js_1.default.success(`Datasource '${data.datasource.name}' with id '${data.datasource.id}' created.`);
|
|
114
|
+
return {
|
|
115
|
+
data,
|
|
116
|
+
datasource_entries: datasource.datasource_entries,
|
|
117
|
+
};
|
|
118
|
+
})
|
|
119
|
+
.catch((err) => logger_js_1.default.error(err));
|
|
120
|
+
};
|
|
121
|
+
exports.updateDatasource = updateDatasource;
|
|
122
|
+
// File-based sync wrapper lives in `datasources.sync.ts` to keep this module CJS-safe.
|
|
123
|
+
const syncDatasourcesData = async ({ datasources }, config) => {
|
|
124
|
+
const result = {
|
|
125
|
+
created: [],
|
|
126
|
+
updated: [],
|
|
127
|
+
skipped: [],
|
|
128
|
+
errors: [],
|
|
129
|
+
};
|
|
130
|
+
const remoteDatasourcesRaw = await (0, exports.getAllDatasources)(config);
|
|
131
|
+
const remoteDatasources = Array.isArray(remoteDatasourcesRaw)
|
|
132
|
+
? remoteDatasourcesRaw
|
|
133
|
+
: [];
|
|
134
|
+
for (const datasource of datasources) {
|
|
135
|
+
const name = String(datasource?.name ?? "unknown");
|
|
136
|
+
if (!datasource || typeof datasource !== "object" || !datasource.name) {
|
|
137
|
+
result.skipped.push(name);
|
|
138
|
+
continue;
|
|
139
|
+
}
|
|
140
|
+
try {
|
|
141
|
+
const datasourceToBeUpdated = remoteDatasources.find((remoteDatasource) => datasource.name === remoteDatasource.name);
|
|
142
|
+
const opResult = datasourceToBeUpdated
|
|
143
|
+
? await (0, exports.updateDatasource)({ datasource, datasourceToBeUpdated }, config)
|
|
144
|
+
: await (0, exports.createDatasource)({ datasource }, config);
|
|
145
|
+
if (datasourceToBeUpdated)
|
|
146
|
+
result.updated.push(name);
|
|
147
|
+
else
|
|
148
|
+
result.created.push(name);
|
|
149
|
+
if (opResult?.data?.datasource && opResult?.datasource_entries) {
|
|
150
|
+
const remoteDatasourceEntries = await (0, datasource_entries_js_1.getDatasourceEntries)({
|
|
151
|
+
datasourceName: opResult.data.datasource.name,
|
|
152
|
+
}, config);
|
|
153
|
+
await (0, datasource_entries_js_1.createDatasourceEntries)({
|
|
154
|
+
data: opResult.data,
|
|
155
|
+
datasource_entries: opResult.datasource_entries,
|
|
156
|
+
remoteDatasourceEntries,
|
|
157
|
+
}, config);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
catch (e) {
|
|
161
|
+
result.errors.push({ name, message: String(e) });
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
return result;
|
|
165
|
+
};
|
|
166
|
+
exports.syncDatasourcesData = syncDatasourcesData;
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.syncPluginsData = exports.createPlugin = exports.updatePlugin = exports.getPluginDetails = exports.getPlugin = exports.getAllPlugins = void 0;
|
|
7
|
+
const logger_js_1 = __importDefault(require("../../utils/logger.js"));
|
|
8
|
+
const request_js_1 = require("../utils/request.js");
|
|
9
|
+
const getAllPlugins = (config) => {
|
|
10
|
+
const { sbApi, spaceId } = config;
|
|
11
|
+
logger_js_1.default.log("Trying to get all plugins.");
|
|
12
|
+
return (0, request_js_1.getAllItemsWithPagination)({
|
|
13
|
+
apiFn: ({ per_page, page }) => sbApi
|
|
14
|
+
.get(`field_types`, {
|
|
15
|
+
per_page,
|
|
16
|
+
page,
|
|
17
|
+
})
|
|
18
|
+
.then((res) => {
|
|
19
|
+
logger_js_1.default.log(`Amount of field types: ${res.total}`);
|
|
20
|
+
return res;
|
|
21
|
+
})
|
|
22
|
+
.catch((err) => logger_js_1.default.error(err)),
|
|
23
|
+
params: {},
|
|
24
|
+
itemsKey: "field_types",
|
|
25
|
+
});
|
|
26
|
+
};
|
|
27
|
+
exports.getAllPlugins = getAllPlugins;
|
|
28
|
+
const getPlugin = (pluginName, config) => {
|
|
29
|
+
return (0, exports.getAllPlugins)(config)
|
|
30
|
+
.then((res) => res.find((plugin) => plugin.name === pluginName))
|
|
31
|
+
.then((res) => {
|
|
32
|
+
if (!res) {
|
|
33
|
+
throw Error("Not Found - plugins does not exist");
|
|
34
|
+
}
|
|
35
|
+
if (Array.isArray(res) && res.length === 0) {
|
|
36
|
+
console.info(`There is no plugin named '${pluginName}'`);
|
|
37
|
+
return false;
|
|
38
|
+
}
|
|
39
|
+
return res;
|
|
40
|
+
})
|
|
41
|
+
.then((plugin) => {
|
|
42
|
+
return (0, exports.getPluginDetails)(plugin, config)
|
|
43
|
+
.then((res) => res)
|
|
44
|
+
.catch((err) => console.error(err));
|
|
45
|
+
})
|
|
46
|
+
.catch((err) => {
|
|
47
|
+
logger_js_1.default.warning(err.message);
|
|
48
|
+
return false;
|
|
49
|
+
});
|
|
50
|
+
};
|
|
51
|
+
exports.getPlugin = getPlugin;
|
|
52
|
+
const getPluginDetails = (plugin, config) => {
|
|
53
|
+
const { sbApi } = config;
|
|
54
|
+
console.log(`Trying to get ${plugin.name} details `);
|
|
55
|
+
return sbApi
|
|
56
|
+
.get(`field_types/${plugin.id}`)
|
|
57
|
+
.then((res) => res.data)
|
|
58
|
+
.catch((err) => console.error(err));
|
|
59
|
+
};
|
|
60
|
+
exports.getPluginDetails = getPluginDetails;
|
|
61
|
+
const updatePlugin = ({ plugin, body }, config) => {
|
|
62
|
+
const { sbApi, spaceId } = config;
|
|
63
|
+
return sbApi
|
|
64
|
+
.put(`field_types/${plugin.id}`, {
|
|
65
|
+
publish: true,
|
|
66
|
+
field_type: {
|
|
67
|
+
body,
|
|
68
|
+
compiled_body: "",
|
|
69
|
+
},
|
|
70
|
+
})
|
|
71
|
+
.then((res) => {
|
|
72
|
+
logger_js_1.default.success(`'${plugin.name}' plugin updated!`);
|
|
73
|
+
return res.data;
|
|
74
|
+
})
|
|
75
|
+
.catch((err) => {
|
|
76
|
+
console.log(err);
|
|
77
|
+
console.error("Error happened :()");
|
|
78
|
+
});
|
|
79
|
+
};
|
|
80
|
+
exports.updatePlugin = updatePlugin;
|
|
81
|
+
const createPlugin = (pluginName, config) => {
|
|
82
|
+
const { sbApi, spaceId } = config;
|
|
83
|
+
return sbApi
|
|
84
|
+
.post(`field_types`, {
|
|
85
|
+
publish: true,
|
|
86
|
+
field_type: {
|
|
87
|
+
name: pluginName,
|
|
88
|
+
},
|
|
89
|
+
})
|
|
90
|
+
.then((res) => {
|
|
91
|
+
logger_js_1.default.success(`'${pluginName}' plugin created!`);
|
|
92
|
+
return res.data;
|
|
93
|
+
})
|
|
94
|
+
.catch((err) => {
|
|
95
|
+
console.log(err);
|
|
96
|
+
console.error("Error happened :()");
|
|
97
|
+
});
|
|
98
|
+
};
|
|
99
|
+
exports.createPlugin = createPlugin;
|
|
100
|
+
// File-based sync wrapper lives in `plugins.sync.ts` to keep this module CJS-safe.
|
|
101
|
+
const syncPluginsData = async ({ plugins }, config) => {
|
|
102
|
+
const result = {
|
|
103
|
+
created: [],
|
|
104
|
+
updated: [],
|
|
105
|
+
skipped: [],
|
|
106
|
+
errors: [],
|
|
107
|
+
};
|
|
108
|
+
for (const p of plugins) {
|
|
109
|
+
const name = String(p?.name ?? "unknown");
|
|
110
|
+
if (!p?.name) {
|
|
111
|
+
result.skipped.push(name);
|
|
112
|
+
continue;
|
|
113
|
+
}
|
|
114
|
+
try {
|
|
115
|
+
const plugin = await (0, exports.getPlugin)(name, config);
|
|
116
|
+
if (plugin) {
|
|
117
|
+
await (0, exports.updatePlugin)({ plugin: plugin.field_type, body: p.body }, config);
|
|
118
|
+
result.updated.push(name);
|
|
119
|
+
}
|
|
120
|
+
else {
|
|
121
|
+
const created = await (0, exports.createPlugin)(name, config);
|
|
122
|
+
await (0, exports.updatePlugin)({ plugin: created.field_type, body: p.body }, config);
|
|
123
|
+
result.created.push(name);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
catch (e) {
|
|
127
|
+
result.errors.push({ name, message: String(e) });
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
return result;
|
|
131
|
+
};
|
|
132
|
+
exports.syncPluginsData = syncPluginsData;
|