-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.ts
77 lines (63 loc) · 2.25 KB
/
update.ts
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
import * as fs from 'fs';
import * as https from 'https';
class BundleSource{
public url: string;
public dictionaries: string[];
constructor(url: string, dictionaries: string[]){
this.url = url;
this.dictionaries = dictionaries;
}
filename(): string{
return `bundles/${this.url.substr(this.url.lastIndexOf('/') + 1)}`;
}
}
const sources: BundleSource[] = [
new BundleSource('https://raw.githubusercontent.com/Anuken/Mindustry/master/core/assets/bundles/bundle_ru.properties',
['dictionaries/dict-ru.txt']),
new BundleSource('https://raw.githubusercontent.com/Anuken/Mindustry/master/core/assets/bundles/bundle_uk_UA.properties',
['dictionaries/dict-ua.txt']),
new BundleSource('https://raw.githubusercontent.com/Anuken/Mindustry/master/core/assets/bundles/bundle.properties',
['dictionaries/dict-en.txt', 'dictionaries/dict-chars.txt'])
];
interface Properties{
[key: string]: string
}
function readProperties(path: string): Properties{
const obj: Properties = {};
fs.readFileSync(path, 'utf8').split('\n')
.filter(value => !!value)
.forEach(value => {
const spl = value.split(/\s+=\s+/g);
obj[spl[0]] = spl[1];
});
return obj;
}
function escape(text: string): string{
return text.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}
function replace(arr: string[], obj: Properties): void{
arr.forEach((value, index) => {
Object.keys(obj).forEach(k => {
const spl = value.split(' = ');
spl[1] && (value = `${spl[0]} = ${spl[1].replace(new RegExp(escape(k), 'gui'), obj[k])}`);
})
arr[index] = value;
});
}
function writeFile(path: string, data: string[]): void{
const file = fs.createWriteStream(path);
data.forEach(value => file.write(`${value}\n`));
file.end();
}
sources.forEach(value => {
let buffer = '';
https.get(value.url, res => {
res.on('data', data => buffer += data);
res.on('end', () => {
const lines: string[] = buffer.split('\n');
value.dictionaries.map(value1 => readProperties(value1))
.forEach(dict => replace(lines, dict));
writeFile(value.filename(), lines);
});
});
});