🔧 chore: improve i18n workflow diff script (#8153)

* 🔧 chore: Add writeJSONWithPrettier utility and update locale generation scripts

* 🔧 chore: Implement genRemoveDiff utility for locale diff analysis and update workflow

* chore: update script

* chore: revert

* 🔧 chore: Refactor genDiff to improve diff analysis and streamline locale processing
This commit is contained in:
𝑾𝒖𝒙𝒉
2025-06-12 14:54:05 +08:00
committed by GitHub
parent ac4b61af91
commit 8953523575
4 changed files with 26 additions and 13 deletions
+2 -1
View File
@@ -54,7 +54,7 @@
"dev:desktop": "next dev --turbopack -p 3015",
"docs:i18n": "lobe-i18n md && npm run lint:md && npm run lint:mdx && prettier -c --write locales/**/*",
"docs:seo": "lobe-seo && npm run lint:mdx",
"i18n": "npm run workflow:i18n && lobe-i18n",
"i18n": "npm run workflow:i18n && lobe-i18n && prettier -c --write \"locales/**\"",
"lint": "npm run lint:ts && npm run lint:style && npm run type-check && npm run lint:circular",
"lint:circular": "dpdm src/**/*.ts --no-warning --no-tree --exit-code circular:1 --no-progress -T true --skip-dynamic-imports circular",
"lint:md": "remark . --silent --output",
@@ -282,6 +282,7 @@
"@next/bundle-analyzer": "^15.3.3",
"@next/eslint-plugin-next": "^15.3.3",
"@peculiar/webcrypto": "^1.5.0",
"@prettier/sync": "^0.6.1",
"@semantic-release/exec": "^6.0.3",
"@testing-library/jest-dom": "^6.6.3",
"@testing-library/react": "^16.3.0",
+2 -2
View File
@@ -2,7 +2,7 @@ import { consola } from 'consola';
import { colors } from 'consola/utils';
import { entryLocaleJsonFilepath, i18nConfig, srcDefaultLocales } from './const';
import { tagWhite, writeJSON } from './utils';
import { tagWhite, writeJSONWithPrettier } from './utils';
export const genDefaultLocale = () => {
consola.info(`Default locale is ${i18nConfig.entryLocale}...`);
@@ -13,7 +13,7 @@ export const genDefaultLocale = () => {
for (const [ns, value] of data) {
const filepath = entryLocaleJsonFilepath(`${ns}.json`);
writeJSON(filepath, value);
writeJSONWithPrettier(filepath, value);
consola.success(tagWhite(ns), colors.gray(filepath));
}
};
+8 -9
View File
@@ -10,10 +10,10 @@ import {
outputLocaleJsonFilepath,
srcDefaultLocales,
} from './const';
import { readJSON, tagWhite, writeJSON } from './utils';
import { readJSON, tagWhite, writeJSONWithPrettier } from './utils';
export const genDiff = () => {
consola.start(`Diff between Dev/Prod local...`);
consola.start(`Remove diff analysis...`);
const resources = require(srcDefaultLocales);
const data = Object.entries(resources.default);
@@ -21,27 +21,26 @@ export const genDiff = () => {
for (const [ns, devJSON] of data) {
const filepath = entryLocaleJsonFilepath(`${ns}.json`);
if (!existsSync(filepath)) continue;
const prodJSON = readJSON(filepath);
const previousProdJSON = readJSON(filepath);
const diffResult = diff(prodJSON, devJSON as any);
const remove = diffResult.filter((item) => item.op === 'remove');
if (remove.length === 0) {
const diffResult = diff(previousProdJSON, devJSON as any);
if (diffResult.length === 0) {
consola.success(tagWhite(ns), colors.gray(filepath));
continue;
}
const clearLocals = [];
for (const locale of [i18nConfig.entryLocale, ...i18nConfig.outputLocales]) {
for (const locale of i18nConfig.outputLocales) {
const localeFilepath = outputLocaleJsonFilepath(locale, `${ns}.json`);
if (!existsSync(localeFilepath)) continue;
const localeJSON = readJSON(localeFilepath);
for (const item of remove) {
for (const item of diffResult) {
unset(localeJSON, item.path);
}
writeJSON(localeFilepath, localeJSON);
writeJSONWithPrettier(localeFilepath, localeJSON);
clearLocals.push(locale);
}
consola.info('clear', clearLocals);
+14 -1
View File
@@ -2,9 +2,13 @@ import { consola } from 'consola';
import { colors } from 'consola/utils';
import { readFileSync, writeFileSync } from 'node:fs';
import { resolve } from 'node:path';
import prettier from "@prettier/sync";
import i18nConfig from '../../.i18nrc';
let prettierOptions = prettier.resolveConfig(
resolve(__dirname, '../../.prettierrc.js')
);
export const readJSON = (filePath: string) => {
const data = readFileSync(filePath, 'utf8');
return JSON.parse(data);
@@ -15,6 +19,15 @@ export const writeJSON = (filePath: string, data: any) => {
writeFileSync(filePath, jsonStr, 'utf8');
};
export const writeJSONWithPrettier = (filePath: string, data: any) => {
const jsonStr = JSON.stringify(data, null, 2);
const formatted = prettier.format(jsonStr, {
...prettierOptions,
parser: 'json',
});
writeFileSync(filePath, formatted, 'utf8');
};
export const genResourcesContent = (locales: string[]) => {
let index = '';
let indexObj = '';