Comparing version 3.13.1 to 3.14.0
{ | ||
"name": "storyblok", | ||
"version": "3.13.1", | ||
"version": "3.14.0", | ||
"description": "A simple CLI to start Storyblok from your command line.", | ||
@@ -40,2 +40,3 @@ "keywords": [ | ||
"path": "^0.12.7", | ||
"simple-uuid": "^0.0.1", | ||
"storyblok-js-client": "^4.5.6", | ||
@@ -42,0 +43,0 @@ "update-notifier": "^5.1.0", |
const chalk = require('chalk') | ||
const StoryblokClient = require('storyblok-js-client') | ||
const UUID = require('simple-uuid') | ||
const api = require('../../utils/api') | ||
@@ -14,5 +15,3 @@ class SyncDatasources { | ||
this.oauthToken = options.oauthToken | ||
this.client = new StoryblokClient({ | ||
oauthToken: options.oauthToken | ||
}) | ||
this.client = api.getClient() | ||
} | ||
@@ -45,5 +44,6 @@ | ||
async getDatasourceEntries (spaceId, datasourceId) { | ||
async getDatasourceEntries (spaceId, datasourceId, dimensionId = null) { | ||
const dimensionQuery = dimensionId ? `&dimension=${dimensionId}` : '' | ||
try { | ||
const entriesFirstPage = await this.client.get(`spaces/${spaceId}/datasource_entries/?datasource_id=${datasourceId}`) | ||
const entriesFirstPage = await this.client.get(`spaces/${spaceId}/datasource_entries/?datasource_id=${datasourceId}${dimensionQuery}`) | ||
const entriesRequets = [] | ||
@@ -93,5 +93,5 @@ for (let i = 2; i <= Math.ceil(entriesFirstPage.total / 25); i++) { | ||
async syncDatasourceEntries (sourceId, targetId) { | ||
async syncDatasourceEntries (datasourceId, targetId) { | ||
try { | ||
const sourceEntries = await this.getDatasourceEntries(this.sourceSpaceId, sourceId) | ||
const sourceEntries = await this.getDatasourceEntries(this.sourceSpaceId, datasourceId) | ||
const targetEntries = await this.getDatasourceEntries(this.targetSpaceId, targetId) | ||
@@ -132,2 +132,3 @@ const updateEntries = targetEntries.filter(e => sourceEntries.map(se => se.name).includes(e.name)) | ||
try { | ||
console.log(` ${chalk.green('-')} Creating datasource ${datasourcesToAdd[i].name} (${datasourcesToAdd[i].slug})`) | ||
/* Create the datasource */ | ||
@@ -139,7 +140,20 @@ const newDatasource = await this.client.post(`spaces/${this.targetSpaceId}/datasources`, { | ||
await this.syncDatasourceEntries(datasourcesToAdd[i].id, newDatasource.data.datasource.id) | ||
console.log(chalk.green('✓') + ' Created datasource ' + datasourcesToAdd[i].name) | ||
if (datasourcesToAdd[i].dimensions.length) { | ||
console.log( | ||
` ${chalk.blue('-')} Creating dimensions...` | ||
) | ||
const { data } = await this.createDatasourcesDimensions(datasourcesToAdd[i].dimensions, newDatasource.data.datasource) | ||
await this.syncDatasourceEntries(datasourcesToAdd[i].id, newDatasource.data.datasource.id) | ||
console.log( | ||
` ${chalk.blue('-')} Sync dimensions values...` | ||
) | ||
await this.syncDatasourceDimensionsValues(datasourcesToAdd[i], data.datasource) | ||
console.log(` ${chalk.green('✓')} Created datasource ${datasourcesToAdd[i].name}`) | ||
} else { | ||
await this.syncDatasourceEntries(datasourcesToAdd[i].id, newDatasource.data.datasource.id) | ||
console.log(` ${chalk.green('✓')} Created datasource ${datasourcesToAdd[i].name}`) | ||
} | ||
} catch (err) { | ||
console.error( | ||
`${chalk.red('X')} Datasource ${datasourcesToAdd[i].name} creation failed: ${err.message}` | ||
`${chalk.red('X')} Datasource ${datasourcesToAdd[i].name} creation failed: ${err.response.data.error || err.message}` | ||
) | ||
@@ -162,2 +176,3 @@ } | ||
const sourceDatasource = this.sourceDatasources.find(d => d.slug === datasourcesToUpdate[i].slug) | ||
await this.client.put(`spaces/${this.targetSpaceId}/datasources/${datasourcesToUpdate[i].id}`, { | ||
@@ -168,4 +183,25 @@ name: sourceDatasource.name, | ||
await this.syncDatasourceEntries(sourceDatasource.id, datasourcesToUpdate[i].id) | ||
console.log(chalk.green('✓') + ' Updated datasource ' + datasourcesToUpdate[i].name) | ||
if (datasourcesToUpdate[i].dimensions.length) { | ||
console.log(` ${chalk.blue('-')} Updating datasources dimensions ${datasourcesToUpdate[i].name}...`) | ||
const sourceDimensionsNames = sourceDatasource.dimensions.map((dimension) => dimension.name) | ||
const targetDimensionsNames = datasourcesToUpdate[i].dimensions.map((dimension) => dimension.name) | ||
const intersection = sourceDimensionsNames.filter(item => !targetDimensionsNames.includes(item)) | ||
let datasourceToSyncDimensionsValues = datasourcesToUpdate[i] | ||
if (intersection) { | ||
const dimensionsToCreate = sourceDatasource.dimensions.filter((dimension) => { | ||
if (intersection.includes(dimension.name)) return dimension | ||
}) | ||
const { data } = await this.createDatasourcesDimensions(dimensionsToCreate, datasourcesToUpdate[i], true) | ||
datasourceToSyncDimensionsValues = data.datasource | ||
} | ||
await this.syncDatasourceEntries(sourceDatasource.id, datasourcesToUpdate[i].id) | ||
await this.syncDatasourceDimensionsValues(sourceDatasource, datasourceToSyncDimensionsValues) | ||
console.log(`${chalk.green('✓')} Updated datasource ${datasourcesToUpdate[i].name}`) | ||
} else { | ||
await this.syncDatasourceEntries(sourceDatasource.id, datasourcesToUpdate[i].id) | ||
console.log(`${chalk.green('✓')} Updated datasource ${datasourcesToUpdate[i].name}`) | ||
} | ||
} catch (err) { | ||
@@ -178,4 +214,100 @@ console.error( | ||
} | ||
async createDatasourcesDimensions (dimensions, datasource, isToUpdate = false) { | ||
const newDimensions = dimensions.map((dimension) => { | ||
return { | ||
name: dimension.name, | ||
entry_value: dimension.entry_value, | ||
datasource_id: datasource.id, | ||
_uid: UUID() | ||
} | ||
}) | ||
let payload = null | ||
if (isToUpdate) { | ||
payload = { | ||
dimensions: [...datasource.dimensions, ...newDimensions], | ||
dimensions_attributes: [...datasource.dimensions, ...newDimensions] | ||
} | ||
} else { | ||
payload = { | ||
dimensions: newDimensions, | ||
dimensions_attributes: newDimensions | ||
} | ||
} | ||
try { | ||
return await this.client.put(`spaces/${this.targetSpaceId}/datasources/${datasource.id}`, { | ||
...datasource, | ||
...payload | ||
}) | ||
} catch (error) { | ||
console.error(error) | ||
} | ||
} | ||
async syncDatasourceDimensionsValues (sourceDatasource, targetDatasource) { | ||
const sourceEntriesPromisses = [] | ||
const targetEmptyEntriesPromisses = [] | ||
try { | ||
for (let index = 0; index < sourceDatasource.dimensions.length; index++) { | ||
const targetDimensionId = targetDatasource.dimensions[index].id | ||
sourceEntriesPromisses.push(...await this.getDatasourceEntries(this.sourceSpaceId, sourceDatasource.id, sourceDatasource.dimensions[index].id)) | ||
targetEmptyEntriesPromisses.push( | ||
...await this.getDatasourceEntries(this.targetSpaceId, targetDatasource.id, targetDimensionId).then((res) => { | ||
return res.map((entry) => { | ||
return { | ||
...entry, | ||
target_dimension_id: targetDimensionId | ||
} | ||
}) | ||
}) | ||
) | ||
} | ||
await Promise.all(sourceEntriesPromisses) | ||
await Promise.all(targetEmptyEntriesPromisses) | ||
const targetEntriesPromisses = [] | ||
while (sourceEntriesPromisses.length !== 0) { | ||
const currentSourceEntry = sourceEntriesPromisses[0] | ||
const targetEntryIndex = targetEmptyEntriesPromisses.findIndex((tEntry) => tEntry.name === currentSourceEntry.name) | ||
const currentTargetEntry = targetEmptyEntriesPromisses[targetEntryIndex] | ||
const valuesAreEqual = currentTargetEntry.dimension_value === currentSourceEntry.dimension_value | ||
if (valuesAreEqual) { | ||
sourceEntriesPromisses.shift() | ||
targetEmptyEntriesPromisses.splice(targetEntryIndex, 1) | ||
} else { | ||
const payload = { | ||
...currentTargetEntry, | ||
dimension_value: currentSourceEntry.dimension_value | ||
} | ||
targetEntriesPromisses.push(await this.syncDimensionEntryValues(currentTargetEntry.target_dimension_id, currentTargetEntry.id, payload)) | ||
sourceEntriesPromisses.shift() | ||
targetEmptyEntriesPromisses.splice(targetEntryIndex, 1) | ||
} | ||
} | ||
await Promise.all(targetEntriesPromisses) | ||
} catch (error) { | ||
console.error(` ${chalk.red('X')} Sync dimensions values failed: ${error.response.data.error || error.message || error}`) | ||
} | ||
} | ||
async syncDimensionEntryValues (dimensionId = null, datasourceEntryId = null, payload = null) { | ||
try { | ||
await this.client.put(`spaces/${this.targetSpaceId}/datasource_entries/${datasourceEntryId}`, { | ||
datasource_entry: payload, | ||
dimension_id: dimensionId | ||
}) | ||
} catch (error) { | ||
console.error(` ${chalk.red('X')} Sync entry error ${payload.name} sync failed: ${error.response.data.error || error.message}`) | ||
} | ||
} | ||
} | ||
module.exports = SyncDatasources |
@@ -46,3 +46,3 @@ const pSeries = require('p-series') | ||
console.log(chalk.green('✓') + ' Syncing stories...') | ||
var targetFolders = await this.client.getAll(`spaces/${this.targetSpaceId}/stories`, { | ||
const targetFolders = await this.client.getAll(`spaces/${this.targetSpaceId}/stories`, { | ||
folder_only: 1, | ||
@@ -52,3 +52,3 @@ sort_by: 'slug:asc' | ||
var folderMapping = {} | ||
const folderMapping = {} | ||
@@ -60,3 +60,3 @@ for (let i = 0; i < targetFolders.length; i++) { | ||
var all = await this.client.getAll(`spaces/${this.sourceSpaceId}/stories`, { | ||
const all = await this.client.getAll(`spaces/${this.sourceSpaceId}/stories`, { | ||
story_only: 1 | ||
@@ -68,6 +68,6 @@ }) | ||
var storyResult = await this.client.get('spaces/' + this.sourceSpaceId + '/stories/' + all[i].id) | ||
var sourceStory = storyResult.data.story | ||
var slugs = sourceStory.full_slug.split('/') | ||
var folderId = 0 | ||
const { data } = await this.client.get('spaces/' + this.sourceSpaceId + '/stories/' + all[i].id) | ||
const sourceStory = data.story | ||
const slugs = sourceStory.full_slug.split('/') | ||
let folderId = 0 | ||
@@ -94,3 +94,3 @@ if (slugs.length > 1) { | ||
force_update: '1', | ||
...(sourceStory.published ? { published: 1 } : {}) | ||
...(sourceStory.published ? { publish: 1 } : {}) | ||
} | ||
@@ -97,0 +97,0 @@ |
184175
4992
20
+ Addedsimple-uuid@^0.0.1
+ Addedsimple-uuid@0.0.1(transitive)