Skip to content

Commit 2dc6568

Browse files
committed
feat: refactor sortFiles to handle multiple JSONC files
1 parent 5c6ba1a commit 2dc6568

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

src/build/sorter.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,25 @@ function sortObjectKeys<T>(obj: T): T {
1717
}
1818

1919
export function sortFiles() {
20-
const filePath = new URL(
21-
"../../inputfiles/overridingTypes.jsonc",
22-
import.meta.url,
23-
); // Replace with your JSONC file path
20+
const files = ["overridingTypes", "removedTypes", "addedTypes"];
21+
files.forEach((file) => {
22+
const filePath = new URL(`../../inputfiles/${file}.jsonc`, import.meta.url); // Replace with your JSONC file path
2423

25-
// Read the JSONC file content
26-
const fileContent = fs.readFileSync(filePath, "utf-8");
24+
// Read the JSONC file content
25+
const fileContent = fs.readFileSync(filePath, "utf-8");
2726

28-
// Parse the JSONC file while preserving comments
29-
const parsed = commentJson.parse(fileContent, undefined, true);
27+
// Parse the JSONC file while preserving comments
28+
const parsed = commentJson.parse(fileContent, undefined, true);
3029

31-
// Sort the object keys alphabetically (recursively)
32-
const sortedObject = sortObjectKeys(parsed);
30+
// Sort the object keys alphabetically (recursively)
31+
const sortedObject = sortObjectKeys(parsed);
3332

34-
// Stringify the sorted object back to JSONC format (preserving comments)
35-
const sortedJsonC = commentJson.stringify(sortedObject, null, 2);
33+
// Stringify the sorted object back to JSONC format (preserving comments)
34+
const sortedJsonC = commentJson.stringify(sortedObject, null, 2);
3635

37-
// Write the sorted content back to the file
38-
fs.writeFileSync(filePath, sortedJsonC, "utf-8");
36+
// Write the sorted content back to the file
37+
fs.writeFileSync(filePath, sortedJsonC, "utf-8");
38+
});
3939

4040
console.log("JSONC file keys sorted successfully.");
4141
}

0 commit comments

Comments
 (0)