Skip to content

Commit

Permalink
feat(ci): unused translations auto cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
martykan committed Jul 22, 2024
1 parent 9864432 commit 93cb74a
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 5 deletions.
1 change: 1 addition & 0 deletions .github/workflows/bot-crowdin-sync.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ jobs:
git config --global user.name "trezor-ci"
git config --global user.email "${{ secrets.TREZOR_BOT_EMAIL }}"
git checkout -B ${{ env.BRANCH_NAME }}
yarn workspace @trezor/suite translations:list-unused --cleanup
yarn workspace @trezor/suite translations:download --token=${{ secrets.CROWDIN_PERSONAL_TOKEN }}
yarn workspace @trezor/suite translations:backport-en
yarn workspace @trezor/suite translations:format
Expand Down
58 changes: 53 additions & 5 deletions packages/suite-data/src/translations/list-unused.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
import { execSync } from 'child_process';
import path from 'path';
import fs from 'fs';

// See comment in list-duplicates.ts
// eslint-disable-next-line import/no-extraneous-dependencies
import messages from '@trezor/suite/src/support/messages';

console.log('unused messages: ');

const rootDir = path.join(__dirname, '..', '..', '..', '..');
function execLocal(cmd: string) {
return execSync(cmd, {
encoding: 'utf-8',
cwd: rootDir,
});
}

const unused: string[] = [];

const ignore = [
Expand Down Expand Up @@ -50,10 +59,7 @@ for (const message in messages) {
const cmd = `grep ${includeExtensions} ${excludeDir} --exclude=messages.ts -r "${message}" -w ./`;

try {
execSync(cmd, {
encoding: 'utf-8',
cwd: path.join(__dirname, '..', '..', '..', '..'),
});
execLocal(cmd);
} catch (err) {
unused.push(message);
}
Expand All @@ -63,5 +69,47 @@ for (const message in messages) {
if (unused.length) {
console.log('there are unused messages!');
console.log(unused);
process.exit(1);

if (process.argv.includes('--cleanup')) {
console.log('cleaning up...');
const pathToMessages = path.join(
__dirname,
'..',
'..',
'..',
'suite',
'src',
'support',
'messages.ts',
);
let messagesContent = fs.readFileSync(pathToMessages, 'utf-8');

for (const message of unused) {
const regex = new RegExp(`\\s+${message}:\\s+\\{[^}]*\\},?\\n`, 'g');
messagesContent = messagesContent.replace(regex, '');
}
fs.writeFileSync(pathToMessages, messagesContent);
execLocal(`yarn prettier --write ${pathToMessages}`);

if (process.argv.includes('--pr')) {
// Create a PR
console.log('creating PR...');
const dateCode = new Date()
.toISOString()
.replace(/[^0-9]/g, '')
.slice(0, 12);
const branchName = 'chore/remove-unused-messages-' + dateCode;
const title = 'chore(suite-data): remove unused messages';
const body = 'This PR removes unused localization messages from Suite';
execLocal(`git checkout -b ${branchName}`);
execLocal(`git add ${pathToMessages}`);
execLocal(`git commit -m "${title}"`);
execLocal(`git push origin ${branchName}`);
execLocal(
`gh pr create --repo trezor/trezor-suite --title "${title}" --body "${body}" --base develop --head ${branchName}`,
);
}
} else {
process.exit(1);
}
}

0 comments on commit 93cb74a

Please sign in to comment.