Skip to content

Commit

Permalink
same order of keys after update
Browse files Browse the repository at this point in the history
  • Loading branch information
while1618 committed Aug 27, 2024
1 parent bd66aa3 commit fbbac39
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/translate/translate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ export abstract class Translate {
if (fs.existsSync(this.saveTo) && !argv.override) {
const existingTranslation = JSON.parse(fs.readFileSync(this.saveTo, 'utf-8')) as JSONObj;
content = extend(true, existingTranslation, content) as JSONObj;
content = this.reorderJSONKeys(this.fileForTranslation, content);
message = `File updated: ${this.saveTo}`;
}
this.writeToFile(content, message);
Expand Down Expand Up @@ -216,6 +217,25 @@ export abstract class Translate {
return value.replace(Translate.skipWordRegex, () => `${this.skippedWords.shift()}`);
}

private reorderJSONKeys(reference: JSONObj, target: JSONObj): JSONObj {
const reordered: JSONObj = {};

Object.keys(reference).forEach((key) => {
if (key in target) {
const refValue = reference[key];
const targetValue = target[key];

if (typeof refValue === 'object' && typeof targetValue === 'object') {
reordered[key] = this.reorderJSONKeys(refValue, targetValue);
} else {
reordered[key] = targetValue;
}
}
});

return reordered;
}

private writeToFile = (content: JSONObj, message: string): void => {
try {
fs.writeFileSync(this.saveTo, JSON.stringify(content, null, argv.spaces));
Expand Down

0 comments on commit fbbac39

Please sign in to comment.