cms-to-static updates
- tags are no longer in media-list, remove from default params - rename existing list directory to the current time as a backup - add sorting option
This commit is contained in:
parent
b52f39826f
commit
b0119c61d7
1 changed files with 79 additions and 7 deletions
|
@ -9,6 +9,7 @@ import {
|
|||
import { finished } from 'stream/promises';
|
||||
import {
|
||||
mkdir,
|
||||
rename,
|
||||
unlink,
|
||||
writeFile,
|
||||
} from 'fs/promises';
|
||||
|
@ -22,6 +23,32 @@ import { log } from '../src/log.js';
|
|||
*/
|
||||
const getValidId = (str) => (str || '').replace(/( |\/|\\)/gm,'_');
|
||||
|
||||
/**
|
||||
* Gets an array sort function based on the provided type
|
||||
* @param {string} type the sort function type
|
||||
* @returns array sort function
|
||||
*/
|
||||
const getSortFunction = (type) => {
|
||||
switch (type) {
|
||||
case 'date': {
|
||||
return (a, b) => {
|
||||
try {
|
||||
let dA = new Date(a.date || '');
|
||||
let dB = new Date(b.date || '');
|
||||
return dA.getTime() - dB.getTime();
|
||||
} catch (err) {
|
||||
log('Error parsing dates: ', 'error', 'cms-to-static::getSortFunction::date', err);
|
||||
return ('' + a.date).localeCompare(b.date || '');
|
||||
}
|
||||
};
|
||||
}
|
||||
case 'title':
|
||||
default: {
|
||||
return (a, b) => ('' + a.title || 'untitled').localeCompare(b.title || 'untitled');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves the given url to the given destination
|
||||
* @param {string} url the url of the file to save
|
||||
|
@ -129,6 +156,28 @@ const mapStrapiResponseToMackenzii = async (inVal) => {
|
|||
return new Promise(resolve => resolve(outVal));
|
||||
};
|
||||
|
||||
/**
|
||||
* Sorts the entries in the list based on the provided sort method
|
||||
* @param {StrapiMediaList} inVal the media list to sort
|
||||
* @param {[string, string]} sortMethod the sort method; first string is the field to sort by, the second string is the order (asc/desc)
|
||||
* @returns {Promise<StrapiMediaList>} the updated media list
|
||||
*/
|
||||
const sortVariants = async (inVal, sortMethod) => {
|
||||
let outVal;
|
||||
|
||||
if (sortMethod.length > 0) {
|
||||
outVal = JSON.parse(JSON.stringify(inVal));
|
||||
outVal.data.entries = outVal.data.entries.sort(getSortFunction(sortMethod[0]));
|
||||
if (sortMethod[1] === 'desc') {
|
||||
outVal.data.entries = outVal.data.entries.reverse();
|
||||
}
|
||||
} else {
|
||||
outVal = inVal;
|
||||
}
|
||||
|
||||
return new Promise(resolve => resolve(outVal));
|
||||
};
|
||||
|
||||
(async () => {
|
||||
console.log('NOTE: This is currently only built to support gallery lists. Article lists and other content are not supported');
|
||||
const rl = readlinePromises.createInterface(process.stdin, process.stdout);
|
||||
|
@ -143,7 +192,27 @@ const mapStrapiResponseToMackenzii = async (inVal) => {
|
|||
rl.close();
|
||||
return;
|
||||
} else if (!fetchUrl.includes('?')) {
|
||||
fetchUrl += '?populate[0]=entries&populate[1]=entries.variants&populate[2]=tags&populate[3]=tags.value'
|
||||
fetchUrl += '?populate[0]=entries&populate[1]=entries.variants'
|
||||
}
|
||||
}
|
||||
|
||||
let sort = '';
|
||||
let sortSplit = [];
|
||||
try {
|
||||
sort = await rl.question('Enter entry sorting method (valid options are "date" and "title" without quotes followed by a colon and either "asc" or "desc" without quotes): ');
|
||||
} catch (err) {
|
||||
log('There was an error: ', 'error', 'cms-to-static::main::Q2', err);
|
||||
} finally {
|
||||
if (sort !== '') {
|
||||
sortSplit = sort.split(/\.|\,|\:|\ |\-/);
|
||||
if (
|
||||
sortSplit.length < 2
|
||||
|| !['date','title'].includes(sortSplit[0])
|
||||
|| !['asc', 'desc'].includes(sortSplit[1])
|
||||
) {
|
||||
log(`Provided function ${sortSplit[0]} or order ${sortSplit[1]} not valid. Defaulting to title:asc`, 'error', 'cms-to-static::main::Q2');
|
||||
sortSplit = ['title', 'asc'];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -152,16 +221,18 @@ const mapStrapiResponseToMackenzii = async (inVal) => {
|
|||
destination = await rl.question('Enter the directory to save the response\n(note: this should be a path relative to "sites";\n{your/input} will become sites/{your/input}): ');
|
||||
destination = 'sites/' + destination;
|
||||
} catch (err) {
|
||||
log('There was an error: ', 'error', 'cms-to-static::main::Q2', err);
|
||||
log('There was an error: ', 'error', 'cms-to-static::main::Q3', err);
|
||||
} finally {
|
||||
rl.close();
|
||||
if (!destination) {
|
||||
log('An invalid path was given. Quitting...', 'log', 'cms-to-static::main::Q2');
|
||||
log('An invalid path was given. Quitting...', 'log', 'cms-to-static::main::Q3');
|
||||
return;
|
||||
} else {
|
||||
if (!existsSync(destination)) {
|
||||
await mkdir(destination, { recursive: true });
|
||||
if (existsSync(destination)) {
|
||||
const split = destination.split('/');
|
||||
await rename(destination, `${split.slice(0, -1).join('/')}/${split[split.length - 1]}_${Date.now()}`);
|
||||
}
|
||||
await mkdir(destination, { recursive: true });
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -181,7 +252,8 @@ const mapStrapiResponseToMackenzii = async (inVal) => {
|
|||
try {
|
||||
await writeFile(`${destination}/in.json`, JSON.stringify(res, null, 2));
|
||||
|
||||
mapStrapiResponseToMackenzii(res)
|
||||
sortVariants(res, sortSplit)
|
||||
.then(list => mapStrapiResponseToMackenzii(list))
|
||||
.then(list => saveVariantsToDestination(list, fetchUrl, destination, destination.replace('sites/', '').split('/').slice(1).join('/')))
|
||||
.then(list => writeFile(`${destination}/out.json`, JSON.stringify(list, null, 2)));
|
||||
log('Done!', 'log', 'cms-to-static::main::writing');
|
||||
|
|
Loading…
Add table
Reference in a new issue