-
Notifications
You must be signed in to change notification settings - Fork 15
/
modifyJson.js
33 lines (27 loc) · 1.02 KB
/
modifyJson.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
/**
* Modifies JSON files in a specified directory by converting the file names to PascalCase
* and updating the file contents with the modified JSON data.
* @param {string} directoryPath - The path to the directory containing the JSON files.
*/
const fs = require('fs');
const path = require('path');
const directoryPath = path.join(__dirname, 'public/static/locales/pt-BR');
fs.readdir(directoryPath, (err, files) => {
if (err) {
return console.log('Unable to scan directory: ' + err);
}
files.forEach((file) => {
const filePath = path.join(directoryPath, file);
const rawData = fs.readFileSync(filePath);
const jsonData = JSON.parse(rawData);
const fileNameWithoutExtension = path.basename(file, '.json');
const pascalCaseFileName = fileNameWithoutExtension
.split(/[-_]/)
.map((word) => word.charAt(0).toUpperCase() + word.slice(1))
.join('');
const newData = {
[pascalCaseFileName]: jsonData,
};
fs.writeFileSync(filePath, JSON.stringify(newData, null, 2));
});
});