/
clean.ts
111 lines (100 loc) · 3.11 KB
/
clean.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
106
107
108
109
110
111
import * as path from 'path';
import { Base } from '../base';
import { readFile, request, showError, showInfo } from '../utils';
import { LokaliseKey } from '../providers/lokalise';
export default class Clean extends Base {
static description = 'Clean lokalise for unused translation keys';
static flags = {
...Base.flags,
...Base.providersFlags,
};
async getKeys() {
const {
flags: { token, projectId },
} = this.parse(Clean);
const headers = { 'x-api-token': token, 'content-type': 'application/json' };
try {
const response = await request<{ keys: LokaliseKey[] }>({
headers,
qs: { limit: 5000 },
url: `https://api.lokalise.co/api2/projects/${projectId}/keys`,
method: 'GET',
});
return response.keys;
} catch (e) {
showError(e);
}
}
async removeKeys(keys: number[]) {
const {
flags: { token, projectId },
} = this.parse(Clean);
const headers = { 'x-api-token': token, 'content-type': 'application/json' };
try {
const response = await request<any>({
headers,
url: `https://api.lokalise.co/api2/projects/${projectId}/keys`,
method: 'DELETE',
body: { keys },
});
return response.keys_removed;
} catch (e) {
showError(e);
}
}
async createSnapshot() {
const {
flags: { token, projectId },
} = this.parse(Clean);
const headers = { 'x-api-token': token, 'content-type': 'application/json' };
try {
const response = await request<any>({
headers,
url: `https://api.lokalise.co/api2/projects/${projectId}/snapshots`,
method: 'POST',
body: { title: 'API snapshot' },
});
return response.snapshot.snapshot_id;
} catch (e) {
showError(e);
}
}
async run() {
const {
flags: { messagesDir },
} = this.parse(Clean);
const start = new Date();
const remoteKeys = await this.getKeys();
if (!remoteKeys) return;
showInfo(`Get ${remoteKeys.length} keys from Lokalise`);
const defaultFilePath = path.join(messagesDir, '_default.json');
const defaultFile = await readFile(defaultFilePath);
const localKeys = JSON.parse(defaultFile).map((key: { id: any }) => key.id);
showInfo(`Found ${localKeys.length} keys keys in ${defaultFilePath}`);
const unusedKeys: number[] = [];
remoteKeys.forEach(key => {
if (!localKeys.includes(key.key_name.ios)) {
unusedKeys.push(key.key_id);
}
});
showInfo(`Found ${unusedKeys.length} unused keys`);
if (unusedKeys.length > 0) {
const snapshotId = await this.createSnapshot();
if (snapshotId) {
showInfo(`Created snapshot: ${snapshotId}`);
const removedKeys = await this.removeKeys(unusedKeys);
if (removedKeys) {
showInfo('Unused keys removed from lokalise');
} else {
showInfo(removedKeys);
}
} else {
showError('Something wrong with snapshot');
return;
}
}
const end = new Date();
const time = end.getTime() - start.getTime();
showInfo(`Finished in ${time} ms`);
}
}