generated from nventive/Template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
101 lines (87 loc) · 3.02 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
const fs = require("fs").promises;
const path = require("path");
const parseCSV = require("csv-parse/sync");
const fetch = require("node-fetch");
/**
* Load CSV files, parse them, and generate JSON files for localization.
* @param {string} configPath - Path to the configuration file.
* @param {string} currentDir - Path to the root folder.
*/
async function loadCsv(configPath, currentDir) {
const { exportPath, tabsUrl, localesKey } = require(configPath);
console.log("[+] IMPORTING LOCALES");
try {
const responses = await Promise.all(
tabsUrl.map(async (urltab) => {
const response = await fetch(urltab);
return await response.text();
})
);
const resolvedExportPath = path.resolve(currentDir, exportPath);
const rows = responses.flatMap((response) => getParsedCSV(response));
await handleResponse(localesKey, rows, resolvedExportPath);
} catch (error) {
console.error("Error fetching or processing CSV files:", error);
}
}
/**
* Parse a CSV string into an array of objects.
* @param {string} file - CSV file content as a string.
* @returns {Object[]} Parsed CSV data.
*/
function getParsedCSV(file) {
return parseCSV.parse(file, {
columns: (header) => header.map((col) => col.split(" ")[0].toLowerCase()),
});
}
/**
* Handle the parsed CSV data and generate JSON files for each locale.
* @param {string[]} localesKey - Array of locale keys.
* @param {Object[]} rows - Parsed CSV data.
* @param {string} exportPath - Path to export JSON files.
*/
async function handleResponse(localesKey, rows, exportPath) {
await localesKey.forEach(async (localeKey) => {
const content = writeTranslation(localesKey, rows, localeKey);
await createJson(exportPath, localeKey, `{\n${content}\n}\n`);
});
}
/**
* Write translation content for a specific locale.
* @param {string[]} localesKey - Array of locale keys.
* @param {Object[]} rows - Parsed CSV data.
* @param {string} locale - Current locale key.
* @returns {string} Translation content as a JSON string.
*/
function writeTranslation(localesKey, rows, locale) {
const fallback =
localesKey[(localesKey.indexOf(locale) + 1) % localesKey.length];
return rows
.map((row) => {
let { key } = row;
if (!key) return;
key = key.replace(/\s+/g, "");
const newRow =
row[locale]?.replace(/"/g, "'").replace(/(?:\r\n|\r|\n)/g, "<br>") ||
row[fallback];
return newRow ? ` "${key}": "${newRow}"` : undefined;
})
.filter(Boolean)
.join(",\n");
}
/**
* Create a JSON file with the given content.
* @param {string} exportPath - Path to export JSON files.
* @param {string} locale - Locale key.
* @param {string} content - JSON content as a string.
*/
async function createJson(exportPath, locale, content) {
try {
const filePath = `${exportPath}/${locale}.json`;
await fs.writeFile(filePath, content);
console.log(`JSON in ${locale} is saved.`);
} catch (error) {
console.error(`Error saving JSON for ${locale}:`, error);
}
}
module.exports = loadCsv;