-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
47 lines (40 loc) · 1.28 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
const fs = require("fs");
const path = require("path");
// Function to add 'id' property as the first property of charName object
function addIdProperty(filePath) {
try {
const fileContent = require(filePath);
const charName = fileContent.default || fileContent; // handle ES6 module exports
// Add 'id' property as the first property
charName.id = charName.name;
const updatedContent = JSON.stringify(
{ id: charName.id, ...charName },
null,
2
);
// Write updated content back to the file
fs.writeFileSync(filePath, `export default ${updatedContent};\n`, "utf8");
console.log(
`'id' property added to ${path.basename(filePath)} successfully.`
);
} catch (error) {
console.error(
`Error processing ${path.basename(filePath)}: ${error.message}`
);
}
}
// Directory containing the character TypeScript files
const charactersFolderPath = "./characters";
// Read files in the characters folder and add 'id' property
fs.readdir(charactersFolderPath, (err, files) => {
if (err) {
console.error(`Error reading folder: ${err.message}`);
return;
}
files.forEach((file) => {
if (file.endsWith(".ts")) {
const filePath = path.join(charactersFolderPath, file);
addIdProperty(filePath);
}
});
});