139 lines
3.1 KiB
JavaScript
139 lines
3.1 KiB
JavaScript
import fs from 'node:fs';
|
|
import path from 'node:path';
|
|
|
|
import csvToJson from 'convert-csv-to-json';
|
|
|
|
const langs = ['da', 'de', 'en', 'fi', 'no', 'sv'];
|
|
const csvHeaders = {
|
|
current: 'Current URL',
|
|
redirect: 'Redirect URL',
|
|
};
|
|
|
|
function csvFilePath(lang) {
|
|
return `${import.meta.dirname}/csv/${lang}.csv`;
|
|
}
|
|
|
|
function jsonFilePath(lang) {
|
|
return `${import.meta.dirname}/json/${lang}.json`;
|
|
}
|
|
|
|
function outputFilepath(lang) {
|
|
return path.resolve(
|
|
import.meta.dirname,
|
|
`../netlify/functions/data/${lang}.json`
|
|
);
|
|
}
|
|
|
|
function removeDomain(str) {
|
|
return str.replace(
|
|
/^https?:\/\/((www|test|stage|prod)\.)?scandichotels.(com|de|dk|fi|no|se)/,
|
|
''
|
|
);
|
|
}
|
|
|
|
function akamaiRedirect(str) {
|
|
return str.replace(
|
|
/^https?:\/\/((www|test|stage|prod)\.)?scandichotels.(com|de|dk|fi|no|se)/,
|
|
(...match) => {
|
|
if (match[3]) {
|
|
switch (match[3]) {
|
|
case 'com':
|
|
return '/en';
|
|
case 'de':
|
|
return '/de';
|
|
case 'dk':
|
|
return '/da';
|
|
case 'fi':
|
|
return '/fi';
|
|
case 'no':
|
|
return '/no';
|
|
case 'se':
|
|
return '/sv';
|
|
}
|
|
}
|
|
return '';
|
|
}
|
|
);
|
|
}
|
|
|
|
function checkPrerequisites() {
|
|
const missingLangs = langs.reduce((acc, lang) => {
|
|
const filepath = csvFilePath(lang);
|
|
if (!fs.existsSync(filepath)) {
|
|
acc.push(filepath);
|
|
}
|
|
return acc;
|
|
}, []);
|
|
|
|
if (missingLangs.length > 0) {
|
|
console.error(`Missing CSV file:\n${missingLangs.join('\n')}`);
|
|
process.exit(1);
|
|
}
|
|
}
|
|
|
|
// convert-csv-to-json writes async without callback support
|
|
// so we workaround it be overriding console.log which it uses when it is done
|
|
async function convertCsvToJson() {
|
|
return new Promise((resolve, reject) => {
|
|
const _consoleLog = console.log;
|
|
let resolved = 0;
|
|
console.log = function (str) {
|
|
if (str.indexOf('File saved:') >= 0) {
|
|
resolved++;
|
|
}
|
|
|
|
if (resolved === langs.length) {
|
|
console.log = _consoleLog;
|
|
resolve();
|
|
}
|
|
};
|
|
|
|
for (const lang of langs) {
|
|
csvToJson
|
|
.utf8Encoding()
|
|
.fieldDelimiter(',')
|
|
.generateJsonFileFromCsv(csvFilePath(lang), jsonFilePath(lang));
|
|
}
|
|
|
|
setTimeout(() => {
|
|
reject('timeout');
|
|
}, 5000);
|
|
});
|
|
}
|
|
|
|
async function makeOutput() {
|
|
for (const lang of langs) {
|
|
try {
|
|
const json = JSON.parse(
|
|
fs.readFileSync(jsonFilePath(lang), {
|
|
encoding: 'utf-8',
|
|
})
|
|
);
|
|
|
|
if (Array.isArray(json)) {
|
|
const finalUrls = json.reduce((acc, url) => {
|
|
const from = removeDomain(akamaiRedirect(url[csvHeaders.current]));
|
|
const to = removeDomain(url[csvHeaders.redirect]);
|
|
|
|
return {
|
|
...acc,
|
|
[from]: to,
|
|
};
|
|
}, {});
|
|
|
|
fs.writeFileSync(outputFilepath(lang), JSON.stringify(finalUrls), {
|
|
encoding: 'utf-8',
|
|
});
|
|
} else {
|
|
throw new Error(`JSON was not an array: ${jsonFilePath(lang)}`);
|
|
}
|
|
} catch (e) {
|
|
console.error(e);
|
|
}
|
|
}
|
|
}
|
|
|
|
checkPrerequisites();
|
|
await convertCsvToJson();
|
|
await makeOutput();
|