-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
90 additions
and
420 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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,74 @@ | ||
// this script is used to clean up the unused keys in the i18n file | ||
// just run `node packages/frontend/i18n/cleanup.mjs` | ||
|
||
import fs from 'node:fs'; | ||
import path from 'node:path'; | ||
import { fileURLToPath } from 'node:url'; | ||
|
||
import { glob } from 'glob'; | ||
|
||
const REPO_ROOT = path.resolve( | ||
fileURLToPath(import.meta.url), | ||
'..', | ||
'..', | ||
'..', | ||
'..' | ||
); | ||
|
||
const files = await glob('packages/frontend/**/src/**/*.{js,tsx,ts}', { | ||
ignore: [ | ||
'**/node_modules/**', | ||
'**/packages/frontend/i18n/**', | ||
'**/dist/**', | ||
'**/lib/**', | ||
], | ||
cwd: REPO_ROOT, | ||
absolute: true, | ||
}); | ||
|
||
const filesWithContent = files.map(file => { | ||
return { | ||
path: file, | ||
content: fs.readFileSync(file, 'utf8'), | ||
}; | ||
}); | ||
|
||
const enjson = JSON.parse( | ||
fs.readFileSync( | ||
path.join(REPO_ROOT, 'packages/frontend/i18n/src/resources/en.json'), | ||
'utf8' | ||
) | ||
); | ||
|
||
const keys = Object.keys(enjson).filter( | ||
// exceptions | ||
key => !key.startsWith('com.affine.payment.modal.') | ||
); | ||
|
||
const unusedKeys = keys.filter(key => { | ||
const regex1 = new RegExp(`[\`'"]${key.replace('.', '\\.')}[\`'"]`, 'g'); | ||
// some place use i18n key like `t[`com.affine.modal.${var}`]` | ||
// com.affine.modal.confirm -> com.affine.modal. | ||
const keyWithoutLastDot = key.replace(/(?<=\.)[^.]+$/, ''); | ||
const regex2 = new RegExp( | ||
`[\`'"]${keyWithoutLastDot.replace('.', '\\.')}`, | ||
'g' | ||
); | ||
for (const file of filesWithContent) { | ||
const match = file.content.match(regex1) || file.content.match(regex2); | ||
if (match) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
}); | ||
|
||
for (const key of unusedKeys) { | ||
delete enjson[key]; | ||
} | ||
|
||
// write back to file | ||
fs.writeFileSync( | ||
path.join(REPO_ROOT, 'packages/frontend/i18n/src/resources/en.json'), | ||
JSON.stringify(enjson, null, 2) | ||
); |
This file contains 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
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.