-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreadAndMerge.js
98 lines (78 loc) · 2.35 KB
/
readAndMerge.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
const fs = require('fs');
const mkdirp = require('mkdirp')
const neatCsv = require('neat-csv'); // https://github.com/sindresorhus/neat-csv
const fastCsv = require('fast-csv'); // https://c2fo.io/fast-csv/
let masterKeys = []
let strArray = [];
// const targets = require('./targets.js');
const targets = [
{ code:'no',name:'Norwegian',out:['nb-NO'] }
]
async function writeFile(strArray, output, inputFile) {
const dir = `./results/${output}`;
mkdirp.sync(dir);
const outputFile = `${dir}/${inputFile}`;
return new Promise(resolve => {
const ws = fs.createWriteStream(outputFile);
fastCsv
.write(strArray, { headers: false, quoteColumns: true })
.pipe(ws);
ws.on('finish', () => ws.close(resolve))
});
}
async function readFile(file) {
return new Promise((resolve, reject) => {
fs.readFile(file, 'utf8', function (err, data) {
if (err) {
reject(err);
}
resolve(data);
});
});
}
async function process(data, loc) {
const keys = await neatCsv(data);
// let strArray = [];
for (let k = 0; k < keys.length; k++) {
const key = keys[k];
// console.log(key)
const obj = {
Key: key.Key,
en: key.en
}
if(masterKeys.indexOf(key.Key) < 0) {
masterKeys.push(key.Key)
obj[loc] = key[loc]
strArray.push(obj)
}
}
// console.log(strArray)
}
async function main() {
for (let i = 0; i < targets.length; i++) {
const target = targets[i];
for (let j = 0; j < target.out.length; j++) {
masterKeys = []
strArray = [];
const loc = target.out[j];
const obj = {
Key: 'Key',
en: 'en'
}
obj[loc] = loc;
strArray.push(obj);
const inputFile = `19.1.Console.${loc}.csv`;
// const inputFile = `19.1.Console.BatchActions.${loc}.csv`;
const inputFolder = `C:\\Users\\styso\\Downloads\\june22\\${loc}\\`;
const file = `${inputFolder}${inputFile}`;
let data = await readFile(file);
await process(data, loc);
const blueFolder = `C:\\projects\\blue\\source\\i18n\\Data\\${loc}\\`;
const blueFile = `${blueFolder}${inputFile}`;
data = await readFile(blueFile);
await process(data, loc);
await writeFile(strArray, loc, inputFile);
}
}
}
main();