Skip to content

Commit 85cac60

Browse files
author
ETLaurent
committed
migrate existing palette data (wip)
1 parent 3fb6154 commit 85cac60

1 file changed

Lines changed: 115 additions & 0 deletions

File tree

  • packages/apostrophe/modules/@apostrophecms/styles/lib

‎packages/apostrophe/modules/@apostrophecms/styles/lib/methods.js‎

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ module.exports = self => {
7272
self.apos.migration.add('migrate legacy global palette fields', () => {
7373
self.shouldMigrateLegacyGlobalPaletteFields = true;
7474
});
75+
self.apos.migration.add('migrate palette into styles', self.migratePaletteIntoStyles);
7576
},
7677
async removeDuplicatePalettesMigration() {
7778
// There was formerly a bug that permitted a profusion of palettes
@@ -154,6 +155,120 @@ module.exports = self => {
154155
// mirror the stylesheet to @apostrophecms/global
155156
await self.apos.doc.db.updateOne({ _id: globalDocId }, { $set });
156157
}
158+
},
159+
async migratePaletteIntoStyles() {
160+
const locales = Object.keys(self.apos.i18n.locales);
161+
const aposLocales = [
162+
...locales.map(locale => `${locale}:draft`),
163+
...locales.map(locale => `${locale}:previous`),
164+
...locales.map(locale => `${locale}:published`)
165+
];
166+
167+
const nonStylesFields = [
168+
'archived',
169+
...Object
170+
.values(self.fieldsGroups)
171+
.flatMap(fieldsGroup => fieldsGroup.fields || [])
172+
];
173+
174+
const stylesFields = _.difference(Object.keys(self.fields), nonStylesFields);
175+
if (!stylesFields.length) {
176+
return;
177+
}
178+
179+
await Promise.all(aposLocales.map(async aposLocale => {
180+
const req = self.apos.task.getReq({ aposLocale });
181+
const stylesDoc = await self.find(req).toObject();
182+
if (!stylesDoc) {
183+
return;
184+
}
185+
186+
const paletteDoc = await self.apos.doc.db.findOne({
187+
type: '@apostrophecms-pro/palette',
188+
aposLocale
189+
});
190+
if (!paletteDoc) {
191+
return;
192+
}
193+
194+
console.info('migrating existing palette into styles for aposLocale', aposLocale);
195+
196+
for (const fieldName in stylesFields) {
197+
console.log('fieldName', fieldName);
198+
stylesDoc[fieldName] = paletteDoc[fieldName];
199+
}
200+
201+
console.dir(stylesDoc, { depth: 9 });
202+
203+
await self.update(req, stylesDoc);
204+
}));
205+
206+
/* for (const aposLocale of aposLocales) { */
207+
/* const paletteDoc = await self.apos.doc.db.findOne({ */
208+
/* type: '@apostrophecms-pro/palette', */
209+
/* aposLocale */
210+
/* }); */
211+
/**/
212+
/* if (!paletteDoc) { */
213+
/* continue; */
214+
/* } */
215+
/**/
216+
/* const stylesDoc = await self.apos.doc.db.findOne({ */
217+
/* type: self.name, */
218+
/* aposLocale */
219+
/* }); */
220+
/**/
221+
/* if (!stylesDoc) { */
222+
/* continue; */
223+
/* } */
224+
/**/
225+
/* const updatedStylesDoc = { ...stylesDoc }; */
226+
/**/
227+
/* for (const fieldName in self.fields) { */
228+
/* if (fieldName.startsWith('palette')) { */
229+
/* updatedStylesDoc[fieldName] = paletteDoc[fieldName]; */
230+
/* } */
231+
/* } */
232+
/**/
233+
/* await self.apos.doc.db.updateOne( */
234+
/* { _id: stylesDoc._id }, */
235+
/* { $set: updatedStylesDoc } */
236+
/* ); */
237+
/* } */
157238
}
158239
};
159240
};
241+
242+
/* await self.apos.doc.db.updateMany( */
243+
/* { type: '@apostrophecms-pro/palette' }, */
244+
/* { */
245+
/* $set: { */
246+
/* type: '@apostrophecms/styles', */
247+
/* slug: self.slug, */
248+
/* title: 'styles', */
249+
/* highSearchText: { */
250+
/* $replaceAll: { */
251+
/* input: '$highSearchText', */
252+
/* find: 'palette', */
253+
/* replacement: 'styles' */
254+
/* } */
255+
/* }, */
256+
/* highSearchWords: { */
257+
/* $map: { */
258+
/* input: '$highSearchWords', */
259+
/* as: 'word', */
260+
/* in: { $cond: [ { $eq: [ '$$word', 'palette' ] }, 'styles', '$$word' ] } */
261+
/* } */
262+
/* }, */
263+
/* lowSearchText: { */
264+
/* $replaceAll: { */
265+
/* input: '$lowSearchText', */
266+
/* find: 'palette', */
267+
/* replacement: 'styles' */
268+
/* } */
269+
/* } */
270+
/* } */
271+
/* } */
272+
/* ); */
273+
/* } */
274+
/**/

0 commit comments

Comments
 (0)