-
-
Notifications
You must be signed in to change notification settings - Fork 290
feat(i18n): add en.meta.json generation script
#1761
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
shuuji3
wants to merge
6
commits into
npmx-dev:main
Choose a base branch
from
shuuji3:feat/generate-en-meta-json
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+351
−0
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c6e5e1f
feat(i18n): add `en.meta.json` generation script
shuuji3 8f1acd7
ci: add `i18n-meta` workflow to update `en.meta.json`
shuuji3 7ce664d
refactor: swap makeEnMetaJson() args order
shuuji3 e6e0701
refactor: mitigate coderabbit concerns and refine codes
shuuji3 8d6b2a6
fix: correct null handling
shuuji3 c57e869
Merge branch 'main' into feat/generate-en-meta-json
shuuji3 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| name: i18n-meta | ||
|
|
||
| on: | ||
| push: | ||
| branches: | ||
| - main | ||
| paths: | ||
| - 'i18n/locales/en.json' | ||
|
|
||
| permissions: | ||
| contents: write | ||
|
|
||
| jobs: | ||
| update: | ||
| name: 🌐 Update en.meta.json | ||
| runs-on: ubuntu-24.04-arm | ||
|
|
||
| steps: | ||
| - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 | ||
| with: | ||
| fetch-depth: 2 | ||
|
|
||
| - uses: actions/setup-node@6044e13b5dc448c55e2357c09f80417699197238 # v6.2.0 | ||
| with: | ||
| node-version: lts/* | ||
|
|
||
| - uses: pnpm/action-setup@1e1c8eafbd745f64b1ef30a7d7ed7965034c486c # 1e1c8eafbd745f64b1ef30a7d7ed7965034c486c | ||
| name: 🟧 Install pnpm | ||
| with: | ||
| cache: true | ||
|
|
||
| - name: 📦 Install dependencies | ||
| run: pnpm install | ||
|
|
||
| - name: 🌐 Update i18n metadata | ||
| run: pnpm i18n:meta:update-en-meta | ||
|
|
||
| - name: ⬆︎ Commit and Push changes | ||
| run: | | ||
| git config --global user.name "github-actions[bot]" | ||
| git config user.email "41898282+github-actions[bot]@users.noreply.github.com" | ||
| git add i18n/locales/en.meta.json | ||
| if ! git diff --cached --quiet; then | ||
| git commit -m 'chore(i18n): update `en.meta.json` [skip ci]' | ||
| git push | ||
| else | ||
| echo "No changes in en.meta.json, skipping commit." | ||
| fi |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| import { parseArgs } from 'node:util' | ||
| import { updateEnMetaJson } from './update-en-meta-json.ts' | ||
|
|
||
| function showHelp() { | ||
| const scriptName = process.env.npm_lifecycle_event || 'node scripts/i18n-meta/cli.ts' | ||
| console.log(`Usage: pnpm run ${scriptName} update-en-meta`) | ||
| } | ||
|
|
||
| function main() { | ||
| const { positionals } = parseArgs({ allowPositionals: true }) | ||
|
|
||
| if (positionals[0] !== 'update-en-meta') { | ||
| showHelp() | ||
| return | ||
| } | ||
|
|
||
| updateEnMetaJson() | ||
| } | ||
|
|
||
| main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| export type EnJson = { | ||
| [key: string]: string | EnJson | ||
| } | ||
|
|
||
| export type MetaEntry = { | ||
| text: string | ||
| commit: string | ||
| } | ||
|
|
||
| export type EnMetaJson = { | ||
| $meta?: { | ||
| last_updated_commit: string | ||
| updated_at: string | ||
| } | ||
| [key: string]: string | MetaEntry | EnMetaJson | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| import { writeFileSync } from 'node:fs' | ||
| import { resolve } from 'node:path' | ||
| import dot from 'dot-object' | ||
| import { | ||
| checkTranslationChanges, | ||
| createUpdatedEnMetaJson, | ||
| getCurrentCommitHash, | ||
| getNewEnJson, | ||
| getOldEnMetaJson, | ||
| } from './utils.ts' | ||
| import type { EnJson, EnMetaJson, MetaEntry } from './types.d.ts' | ||
|
|
||
| const enJsonPath = resolve('i18n/locales/en.json') | ||
| const enMetaJsonPath = resolve('i18n/locales/en.meta.json') | ||
|
|
||
| /** | ||
| * Update a metadata JSON file for English translations. | ||
| */ | ||
| export function updateEnMetaJson() { | ||
| const newEnJson = getNewEnJson(enJsonPath) | ||
| const oldEnMetaJson = getOldEnMetaJson(enMetaJsonPath) | ||
|
|
||
| const currentCommitHash = getCurrentCommitHash() | ||
| if (!currentCommitHash) { | ||
| console.error('❌ Commit hash missing. Skipping update to protect existing metadata.') | ||
| process.exit(1) | ||
| } | ||
| const enMetaJson = makeEnMetaJson(oldEnMetaJson, newEnJson, currentCommitHash) | ||
|
|
||
| const hasChanges = checkTranslationChanges(oldEnMetaJson, enMetaJson) | ||
| if (!hasChanges) { | ||
| console.info('ℹ️ No translation changes – en.meta.json left untouched') | ||
| return | ||
| } | ||
|
|
||
| const updatedEnMetaJson = createUpdatedEnMetaJson(currentCommitHash, enMetaJson) | ||
|
|
||
| writeFileSync(enMetaJsonPath, JSON.stringify(updatedEnMetaJson, null, 2) + '\n', 'utf-8') | ||
| console.log(`✅ Updated en.meta.json – last_updated_commit: ${currentCommitHash}`) | ||
| } | ||
|
|
||
| export function makeEnMetaJson( | ||
| oldMetaEnJson: EnMetaJson, | ||
| newEnJson: EnJson, | ||
| latestCommitHash: string, | ||
| ): EnMetaJson { | ||
| const newFlat = dot.dot(newEnJson) as Record<string, string> | ||
| const oldMetaFlat = dot.dot(oldMetaEnJson) as Record<string, string> | ||
| const metaFlat: Record<string, MetaEntry> = {} | ||
|
|
||
| for (const key in newFlat) { | ||
| const newText = newFlat[key] | ||
|
|
||
| const lastSeenText = oldMetaFlat[`${key}.text`] | ||
| const lastCommit = oldMetaFlat[`${key}.commit`] | ||
|
|
||
| if (newText === lastSeenText) { | ||
| metaFlat[key] = { text: newText, commit: lastCommit ?? latestCommitHash } | ||
| } else { | ||
| metaFlat[key] = { text: newText, commit: latestCommitHash } | ||
| } | ||
| } | ||
| return dot.object(metaFlat) as EnMetaJson | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| import { execSync } from 'node:child_process' | ||
| import * as fs from 'node:fs' | ||
| import type { EnJson, EnMetaJson } from './types.d.ts' | ||
|
|
||
| export function getCurrentCommitHash() { | ||
| return git('rev-parse HEAD') | ||
| } | ||
|
|
||
| export function getNewEnJson(enJsonPath: string): EnJson { | ||
| if (fs.existsSync(enJsonPath)) { | ||
| return readJson<EnJson>(enJsonPath) | ||
| } | ||
| return {} as EnJson | ||
| } | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| export function getOldEnMetaJson(enMetaJsonPath: string): EnMetaJson { | ||
| if (fs.existsSync(enMetaJsonPath)) { | ||
| return readJson<EnMetaJson>(enMetaJsonPath) | ||
| } | ||
| return {} as EnMetaJson | ||
| } | ||
|
|
||
| export function checkTranslationChanges(oldMeta: EnMetaJson, newMeta: EnMetaJson): boolean { | ||
| const oldObj = omitMeta(oldMeta) | ||
| const newObj = omitMeta(newMeta) | ||
| return JSON.stringify(oldObj) !== JSON.stringify(newObj) | ||
| } | ||
|
|
||
| export function createUpdatedEnMetaJson(commitHash: string, content: EnMetaJson): EnMetaJson { | ||
| return { | ||
| $meta: { | ||
| last_updated_commit: commitHash, | ||
| updated_at: new Date().toISOString(), | ||
| }, | ||
| ...omitMeta(content), | ||
| } | ||
| } | ||
|
|
||
| function git(command: string) { | ||
| try { | ||
| return execSync(`git ${command}`, { encoding: 'utf-8' }).trim() | ||
| } catch { | ||
| console.error(`🚨 Git command failed: git ${command}`) | ||
| return null | ||
| } | ||
| } | ||
|
|
||
| function readJson<T>(filePath: string): T { | ||
| return JSON.parse(fs.readFileSync(filePath, 'utf-8')) as T | ||
| } | ||
|
|
||
| function omitMeta<T extends object>(obj: T): Omit<T, '$meta'> { | ||
| const { $meta: _, ...rest } = obj as T & { $meta?: unknown } | ||
| return rest | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| { | ||
| "compilerOptions": { | ||
| "target": "ES2022", | ||
| "module": "nodenext", | ||
| "strict": true, | ||
| "esModuleInterop": true, | ||
| "skipLibCheck": true, | ||
| "noEmit": true, | ||
| "allowImportingTsExtensions": true, | ||
| "declaration": true, | ||
| "types": ["node"], | ||
| "declarationMap": true | ||
| }, | ||
| "include": ["**/*.ts"], | ||
| "exclude": ["node_modules", "dist"] | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.