From c770c5426814c24a812383b67d575d9f9bd8dd1d Mon Sep 17 00:00:00 2001 From: Lightling Date: Mon, 14 Oct 2024 18:35:03 -0400 Subject: [PATCH] begin working on script --- projects/sites/package.json | 1 + projects/sites/scripts/cms-to-static.js | 73 +++++++++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 projects/sites/scripts/cms-to-static.js diff --git a/projects/sites/package.json b/projects/sites/package.json index 858e466..c40fada 100644 --- a/projects/sites/package.json +++ b/projects/sites/package.json @@ -7,6 +7,7 @@ "symlink-dir": "6.0.0" }, "scripts": { + "cms-to-static": "node scripts/cms-to-static.js", "set-current": "node scripts/set-current.js" } } diff --git a/projects/sites/scripts/cms-to-static.js b/projects/sites/scripts/cms-to-static.js new file mode 100644 index 0000000..f45d914 --- /dev/null +++ b/projects/sites/scripts/cms-to-static.js @@ -0,0 +1,73 @@ +import readlinePromises from 'readline/promises'; +import { existsSync } from 'fs'; +import { mkdir, writeFile } from 'fs/promises'; + +const mapper = (inVal) => { + const { data } = inVal; + const outVal = {}; + + outVal.tags = {}; + data.tags.forEach(tag => { + outVal.tags[tag.key] = { + category: tag.value.category, + displayName: tag.value.displayName, + }; + }); + + return outVal; +}; + +(async () => { + const rl = readlinePromises.createInterface(process.stdin, process.stdout); + let fetchUrl = ''; + try { + fetchUrl = await rl.question('Enter the URL to fetch from: '); + } catch (err) { + console.error('There was an error: ', err); + } finally { + if (!fetchUrl) { + console.log('Invalid URL. Quitting...'); + rl.close(); + return; + } + } + + let destination = ''; + try { + destination = await rl.question('Enter the directory to save the response (note: start from sites/{your-site-here} for relative pathing): '); + } catch (err) { + console.error('There was an error: ', err); + } finally { + rl.close(); + if (!destination) { + console.log('Invalid path. Quitting...'); + return; + } else { + if (!existsSync(destination)) { + await mkdir(destination); + } + } + } + + let res = {}; + try { + res = await (await fetch(fetchUrl, { + method: 'GET', + headers: { + 'Content-Type': 'application/json', + }, + })).json(); + } catch (err) { + console.error('There was an error: ', err); + } + + console.log(res); + console.log('Writing...'); + try { + await writeFile(`${destination}/in.json`, JSON.stringify(res, null, 2)); + let val = mapper(res); + await writeFile(`${destination}/out.json`, JSON.stringify(val, null, 2)); + } catch (err) { + console.error('There was an error: ', err) + } +})();