-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·98 lines (76 loc) · 2.72 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
#!/usr/bin/env node
'use strict'
const fs = require('fs');
const path = require('path');
const prompt = require('prompt-sync')();
const {
getPathFromObject,
getSourceFilePath,
compareAndWriteFiles,
clearJSON,
logger
} = require('./utils');
const NODE_MAJOR_VERSION = Number(process.versions.node.split('.')[0]);
if (NODE_MAJOR_VERSION < 20) {
logger.warning('NodeJS version >20 supported.');
process.exit(0);
}
const translations = {};
let allTranslationFiles = [];
const dirPath = prompt('Enter i18n directory! ');
try {
const isDirectory = fs.lstatSync(dirPath).isDirectory();
if (!isDirectory) {
logger.error(`${dirPath} is not a directory.`)
process.exit(0);
}
} catch (e) {
logger.error(`${dirPath} is not a directory.`)
process.exit(0);
}
try {
fs.readdirSync(dirPath, {
withFileTypes: true
}).forEach(locale => {
if (locale.isDirectory() && !locale.name.startsWith('.')) {
const filePath = getPathFromObject(locale);
const fileNames = fs.readdirSync(filePath, { withFileTypes: true });
const localeFiles = [];
for (const fileName of fileNames) {
if (fileName.isFile() && fileName.name.endsWith('.json')) {
allTranslationFiles.push(fileName.name);
localeFiles.push(fileName.name);
}
}
translations[locale.name] = localeFiles;
}
});
allTranslationFiles = [... new Set(allTranslationFiles)];
for (const locale in translations) {
const localeTranslations = translations[locale];
const filesToAdd = allTranslationFiles.filter(fileName => !localeTranslations.includes(fileName));
for (const fileName of filesToAdd) {
const sourceFilePath = getSourceFilePath(locale, fileName);
const targetFilePath = `${dirPath}${path.sep}${locale}${path.sep}${fileName}`;
fs.copyFileSync(sourceFilePath, targetFilePath);
const file = require(targetFilePath);
const newFileObject = JSON.parse(JSON.stringify(clearJSON(file)));
fs.writeFileSync(targetFilePath, JSON.stringify(newFileObject, null, 2));
}
}
// eniig hadgalsnaa unshdag bish function bicheed shineer avdag bolgoh.
for (const fileName of allTranslationFiles) {
const filePaths = [];
for (const locale in translations) {
const p = `${dirPath}${path.sep}${locale}${path.sep}${fileName}`;
filePaths.push(p);
}
compareAndWriteFiles(filePaths);
}
} catch (e) {
console.log('Error>>', e.message);
logger.error('Error');
process.exit(1);
}
logger.success('Success');
process.exit(0);