-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpropertiesToJson.js
72 lines (61 loc) · 2.25 KB
/
propertiesToJson.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
const fs = require('fs');
const path = require('path');
const os = require('os');
const process = require('process');
const INPUT_PATH = './i18n/new/';
const OUTPUT_PATH = './packages/pco-policies-and-settings/src/i18n';
const INPUT_FILE_FORMAT = '.properties';
const OUTPUT_FILE_FORMAT = '.json';
const filePathList = [];
fs.readdir(INPUT_PATH, 'utf8', (err, files) => {
if (err) { console.error(err); }
files.forEach((file) => {
const filePath = path.join(INPUT_PATH, file);
const stats = fs.statSync(filePath);
if (stats.isFile()) {
filePathList.push(filePath);
}
});
console.log(filePathList);
function getMessageStringFromFile(file) {
const messageString = fs.readFileSync(file, 'utf-8');
return messageString.trim();
}
function stringToObj(string) {
const messageList = string.split('\n');
const obj = {};
messageList.forEach((item) => {
if (item.trim() === '' || /#.?/.test(item)) {
return false;
}
const index = item.indexOf('=');
const key = item.substring(0, index);
const value = item.substring(index + 1).replace(/^\s*|\s*$/g, ''); // delete null character at start or end
obj[key] = value;
});
return obj;
}
function getFileName(filePath) {
const propertiesIndex = filePath.lastIndexOf(INPUT_FILE_FORMAT);
if (propertiesIndex < 0) {
throw new Error('File format is invalid, only accept properties file');
}
let dirIndex;
if (os.platform().indexOf('win32') !== -1) {
dirIndex = filePath.lastIndexOf('\\');
} else {
dirIndex = filePath.lastIndexOf('/');
}
return filePath.substring(dirIndex + 5, propertiesIndex);
}
filePathList.forEach((filePath) => {
// only transform en_US by default, it will transform all files if pass argu "-all"
if (process.argv[2] !== '-all' && filePath.indexOf('en_US') === -1) { return; }
const messageString = getMessageStringFromFile(filePath);
const messagesObj = stringToObj(messageString);
const fileName = getFileName(filePath);
const outputFilePath = path.join(OUTPUT_PATH, `${fileName}${OUTPUT_FILE_FORMAT}`);
console.log(`output: ${outputFilePath}`);
fs.writeFileSync(outputFilePath, JSON.stringify(messagesObj, null, 4), 'utf-8');
});
});