-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
93 lines (76 loc) · 2.92 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
const cloneDeep = require(`lodash.clonedeep`);
const isEqual = require(`lodash.isequal`);
const config = require(`./config`);
const discover = require(`./utils/discover`);
const migrate = require(`./utils/migrate`);
const componentService = require(`./services/component`);
const storyService = require(`./services/story`);
const unpaginate = require(`./utils/unpaginate`);
if (config.dryRun) {
// eslint-disable-next-line no-console
console.warn(`Dry run mode activated!`);
}
function contentTypesFromComponents(components) {
return components.map(x => x.name);
}
async function runComponentMigrations({ components }) {
const { data: { components: remoteComponents } } = await componentService.list();
for (const component of components) {
const remoteComponent = remoteComponents
.find(x => x.name === component.name);
if (remoteComponent) {
if (config.dryRun) {
// eslint-disable-next-line no-console
console.info(`${component.display_name || component.name} component would've been updated`);
continue;
}
const mappedComponent = { id: remoteComponent.id, ...component };
await componentService.update({ component: mappedComponent });
// eslint-disable-next-line no-console
console.log(`${component.display_name || component.name} component has been updated`);
continue;
}
if (config.dryRun) {
// eslint-disable-next-line no-console
console.info(`${component.display_name || component.name} component would've been created`);
continue;
}
await componentService.create({ component });
// eslint-disable-next-line no-console
console.log(`${component.display_name || component.name} component has been created`);
}
}
async function runContentMigrations({ components }) {
const contentTypes = contentTypesFromComponents(components);
const storyPages = await unpaginate({ cb: storyService.list, params: { contentTypes } });
const stories = storyPages.reduce((prev, next) => [...prev, ...next.stories], []);
for (const originalStory of stories) {
const story = cloneDeep(originalStory);
const componentName = story.content.component;
const component = discover.componentByName(componentName);
if (!component) {
throw new Error(`No component found for name "${componentName}"`);
}
migrate({
component,
content: story.content,
});
if (isEqual(story, originalStory)) {
// eslint-disable-next-line no-console
console.info(`Story "${story.id}" has not changed and was skipped`);
continue;
}
if (config.dryRun) {
// eslint-disable-next-line no-console
console.info(`Story "${story.id}" would've been updated`);
continue;
}
await storyService.update({ story });
// eslint-disable-next-line no-console
console.log(`Story "${story.id}" has been updated`);
}
}
module.exports = {
runComponentMigrations,
runContentMigrations,
};