Skip to content
This repository was archived by the owner on Jan 19, 2026. It is now read-only.

Commit 4e786c9

Browse files
Merge pull request #257 from storyblok/feature/sync-presets
feature sync presets
2 parents 5ea5aaa + 98cd57d commit 4e786c9

2 files changed

Lines changed: 128 additions & 10 deletions

File tree

src/cli.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ program
209209
target
210210
})
211211

212-
console.log(chalk.green('✓') + ' Sync data between spaces successfully completed')
212+
console.log('\n' + chalk.green('✓') + ' Sync data between spaces successfully completed')
213213
} catch (e) {
214214
console.error(chalk.red('X') + ' An error ocurred when sync spaces')
215215
console.error(e)

src/tasks/sync.js

Lines changed: 127 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -172,41 +172,158 @@ const SyncSpaces = {
172172
},
173173

174174
async syncComponents () {
175-
console.log(chalk.green('✓') + ' Syncing components...')
176-
this.targetComponents = await this.client.get(`spaces/${this.targetSpaceId}/components`)
177-
this.sourceComponents = await this.client.get(`spaces/${this.sourceSpaceId}/components`)
175+
let sourcePresets = []
176+
177+
console.log(chalk.green('-') + ' Syncing components...')
178+
179+
try {
180+
this.targetComponents = await this.getComponents(this.targetSpaceId)
181+
this.sourceComponents = await this.getComponents(this.sourceSpaceId)
182+
183+
sourcePresets = await this.getPresets(this.sourceSpaceId)
184+
} catch (e) {
185+
console.error('An error ocurred when load data to sync components and presets' + e.message)
186+
187+
return Promise.reject(e)
188+
}
178189

179190
for (var i = 0; i < this.sourceComponents.data.components.length; i++) {
191+
console.log()
180192
const component = this.sourceComponents.data.components[i]
193+
console.log(chalk.blue('-') + ` Processing component ${component.name}`)
194+
195+
const componentPresets = this.getComponentPresets(
196+
sourcePresets, component
197+
)
181198

182199
delete component.id
183200
delete component.created_at
184201

185202
// Create new component on target space
186203
try {
187-
await this.client.post(`spaces/${this.targetSpaceId}/components`, {
188-
component: component
189-
})
204+
const componentCreated = await this.createComponent(
205+
this.targetSpaceId, component
206+
)
207+
190208
console.log(chalk.green('✓') + ` Component ${component.name} synced`)
209+
210+
if (componentPresets.length) {
211+
await this.createPresets(componentPresets, componentCreated.id)
212+
}
191213
} catch (e) {
192214
if (e.response.status === 422) {
193-
await this.client.put(`spaces/${this.targetSpaceId}/components/${this.getTargetComponentId(component.name)}`, {
215+
console.log(chalk.yellow('-') + ` Component ${component.name} already exists, updating it...`)
216+
217+
const componentTarget = this.getTargetComponent(component.name)
218+
await this.client.put(`spaces/${this.targetSpaceId}/components/${componentTarget.id}`, {
194219
component: component
195220
})
196221
console.log(chalk.green('✓') + ` Component ${component.name} synced`)
222+
223+
const presetsToSave = this.filterPresetsFromTargetComponent(
224+
componentPresets || [],
225+
componentTarget.all_presets || []
226+
)
227+
228+
if (presetsToSave.length) {
229+
await this.createPresets(presetsToSave, componentTarget.id)
230+
return
231+
}
232+
233+
console.log(chalk.blue('✓') + ' Presets were already in sync')
197234
} else {
198235
console.error(chalk.red('X') + ` Component ${component.name} sync failed`)
236+
console.error(e.message)
199237
}
200238
}
201239
}
202240
},
203241

204-
getTargetComponentId (name) {
242+
createComponent (spaceId, componentData) {
243+
return this
244+
.client
245+
.post(`spaces/${spaceId}/components`, {
246+
component: componentData
247+
})
248+
.then(response => {
249+
const component = response.data.component || {}
250+
251+
return component
252+
})
253+
.catch(error => Promise.reject(error))
254+
},
255+
256+
getComponents (spaceId) {
257+
return this.client.get(`spaces/${spaceId}/components`)
258+
},
259+
260+
getTargetComponent (name) {
205261
const comps = this.targetComponents.data.components.filter((comp) => {
206262
return comp.name === name
207263
})
208264

209-
return comps[0].id
265+
return comps[0]
266+
},
267+
268+
filterPresetsFromTargetComponent (presets, targetPresets) {
269+
console.log(chalk.blue('-') + ' Checking target presets to sync')
270+
const targetPresetsNames = targetPresets.map(preset => {
271+
return preset.name.toLowerCase()
272+
})
273+
274+
return presets.filter(preset => {
275+
return !targetPresetsNames.includes(preset.name.toLowerCase())
276+
})
277+
},
278+
279+
async getPresets (spaceId) {
280+
console.log(`${chalk.green('-')} Load presets from space #${spaceId}`)
281+
282+
try {
283+
const response = await this.client.get(
284+
`spaces/${spaceId}/presets`
285+
)
286+
287+
return response.data.presets || []
288+
} catch (e) {
289+
console.error('An error ocurred when load presets ' + e.message)
290+
291+
return Promise.reject(e)
292+
}
293+
},
294+
295+
getComponentPresets (sourcePresets = [], component = {}) {
296+
console.log(`${chalk.green('-')} Get presets from component ${component.name}`)
297+
298+
return sourcePresets.filter(preset => {
299+
return preset.component_id === component.id
300+
})
301+
},
302+
303+
async createPresets (presets = [], componentId) {
304+
const presetsSize = presets.length
305+
console.log(`${chalk.green('-')} Syncing ${presetsSize} presets to space #${this.targetSpaceId}`)
306+
307+
try {
308+
for (let i = 0; i < presetsSize; i++) {
309+
const presetData = presets[i]
310+
311+
await this.client.post(`spaces/${this.targetSpaceId}/presets`, {
312+
preset: {
313+
name: presetData.name,
314+
component_id: componentId,
315+
space_id: this.targetSpaceId,
316+
preset: presetData.preset
317+
}
318+
})
319+
}
320+
321+
console.log(`${chalk.green('✓')} ${presetsSize} presets sync in space (#${this.targetSpaceId})`)
322+
} catch (e) {
323+
console.error('An error ocurred when save the presets' + e.message)
324+
325+
return Promise.reject(e)
326+
}
210327
}
211328
}
212329

@@ -221,6 +338,7 @@ const sync = (types, options) => {
221338

222339
const tasks = types.map(_type => {
223340
const command = `sync${capitalize(_type)}`
341+
224342
return () => SyncSpaces[command]()
225343
})
226344

0 commit comments

Comments
 (0)