/
remove-strings.ts
105 lines (91 loc) · 2.64 KB
/
remove-strings.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// 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';
import path from 'path';
import { MONTH } from '../util/durations';
import { isOlderThan } from '../util/timestamp';
import { DELETED_REGEXP } from './constants';
const ROOT_DIR = path.join(__dirname, '..', '..');
const MESSAGES_FILE = path.join(ROOT_DIR, '_locales', 'en', 'messages.json');
const limitter = pLimit(10);
async function main() {
const messages = JSON.parse(await fs.readFile(MESSAGES_FILE, 'utf-8'));
const stillUsed = new Set<string>();
await Promise.all(
Object.keys(messages).map(key =>
limitter(async () => {
const value = messages[key];
const match = (value as Record<string, string>).description?.match(
DELETED_REGEXP
);
if (!match) {
return;
}
const deletedAt = new Date(match[1]).getTime();
if (!isOlderThan(deletedAt, MONTH)) {
return;
}
// Find uses in either:
// - `i18n('key')`
// - `<Intl 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;
}
}
})
)
);
if (stillUsed.size !== 0) {
console.error(
`ERROR: Didn't remove ${stillUsed.size} strings because of errors above`,
Array.from(stillUsed)
.map(str => `- ${str}`)
.join('\n')
);
console.error('ERROR: Not saving changes');
process.exit(1);
}
await fs.writeFile(MESSAGES_FILE, `${JSON.stringify(messages, null, 2)}\n`);
}
main().catch(err => {
console.error(err);
process.exit(1);
});