signal-desktop/ts/scripts/remove-strings.ts

106 lines
2.6 KiB
TypeScript
Raw Normal View History

2023-03-16 20:19:43 +00:00
// Copyright 2023 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import chalk from 'chalk';
import execa from 'execa';
import fs from 'fs/promises';
import pLimit from 'p-limit';
2023-03-16 20:19:43 +00:00
import path from 'path';
import { MONTH } from '../util/durations';
import { isOlderThan } from '../util/timestamp';
import { DELETED_REGEXP } from './constants';
2023-03-16 20:19:43 +00:00
const ROOT_DIR = path.join(__dirname, '..', '..');
const MESSAGES_FILE = path.join(ROOT_DIR, '_locales', 'en', 'messages.json');
const limitter = pLimit(10);
2023-03-16 20:19:43 +00:00
async function main() {
const messages = JSON.parse(await fs.readFile(MESSAGES_FILE, 'utf-8'));
2023-03-16 20:19:43 +00:00
const stillUsed = new Set<string>();
2023-03-16 20:19:43 +00:00
await Promise.all(
Object.keys(messages).map(key =>
limitter(async () => {
const value = messages[key];
const match = (value as Record<string, string>).description?.match(
2024-03-04 18:03:11 +00:00
DELETED_REGEXP
);
if (!match) {
return;
}
const deletedAt = new Date(match[1]).getTime();
if (!isOlderThan(deletedAt, MONTH)) {
return;
}
2023-03-16 20:19:43 +00:00
// Find uses in either:
// - `i18n('key')`
2024-05-15 21:48:02 +00:00
// - `<I18n id="key"/>`
try {
const result = await execa(
'git',
// prettier-ignore
[
'grep',
'--extended-regexp',
`'${key}'|id="${key}"`,
'--',
'**',
':!\\_locales/**',
':!\\sticker-creator/**',
],
{
cwd: ROOT_DIR,
stdin: 'ignore',
stdout: 'pipe',
stderr: 'inherit',
}
);
// Match found
console.error(
chalk.red(
`ERROR: String is still used: "${key}", deleted on ${match[1]}`
)
);
console.error(result.stdout.trim());
console.error('');
stillUsed.add(key);
} catch (error) {
if (error.exitCode === 1) {
console.log(
chalk.dim(`Removing string: "${key}", deleted on ${match[1]}`)
);
delete messages[key];
} else {
throw error;
}
}
})
)
2023-03-16 20:19:43 +00:00
);
if (stillUsed.size !== 0) {
2023-03-16 20:19:43 +00:00
console.error(
`ERROR: Didn't remove ${stillUsed.size} strings because of errors above`,
Array.from(stillUsed)
.map(str => `- ${str}`)
.join('\n')
2023-03-16 20:19:43 +00:00
);
console.error('ERROR: Not saving changes');
process.exit(1);
2023-03-16 20:19:43 +00:00
}
await fs.writeFile(MESSAGES_FILE, `${JSON.stringify(messages, null, 2)}\n`);
2023-03-16 20:19:43 +00:00
}
main().catch(err => {
console.error(err);
2023-03-16 20:19:43 +00:00
process.exit(1);
});