UNPKG

@dvhb/react-intl-messages

Version:

Library for parsing source files and extract react-intl messages

98 lines (97 loc) 3.72 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const path = require("path"); const base_1 = require("../base"); const utils_1 = require("../utils"); class Clean extends base_1.Base { async getKeys() { const { flags: { token, projectId }, } = this.parse(Clean); const headers = { 'x-api-token': token, 'content-type': 'application/json' }; try { const response = await utils_1.request({ headers, qs: { limit: 5000 }, url: `https://api.lokalise.co/api2/projects/${projectId}/keys`, method: 'GET', }); return response.keys; } catch (e) { utils_1.showError(e); } } async removeKeys(keys) { const { flags: { token, projectId }, } = this.parse(Clean); const headers = { 'x-api-token': token, 'content-type': 'application/json' }; try { const response = await utils_1.request({ headers, url: `https://api.lokalise.co/api2/projects/${projectId}/keys`, method: 'DELETE', body: { keys }, }); return response.keys_removed; } catch (e) { utils_1.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 utils_1.request({ headers, url: `https://api.lokalise.co/api2/projects/${projectId}/snapshots`, method: 'POST', body: { title: 'API snapshot' }, }); return response.snapshot.snapshot_id; } catch (e) { utils_1.showError(e); } } async run() { const { flags: { messagesDir }, } = this.parse(Clean); const start = new Date(); const remoteKeys = await this.getKeys(); if (!remoteKeys) return; utils_1.showInfo(`Get ${remoteKeys.length} keys from Lokalise`); const defaultFilePath = path.join(messagesDir, '_default.json'); const defaultFile = await utils_1.readFile(defaultFilePath); const localKeys = JSON.parse(defaultFile).map((key) => key.id); utils_1.showInfo(`Found ${localKeys.length} keys keys in ${defaultFilePath}`); const unusedKeys = []; remoteKeys.forEach(key => { if (!localKeys.includes(key.key_name.ios)) { unusedKeys.push(key.key_id); } }); utils_1.showInfo(`Found ${unusedKeys.length} unused keys`); if (unusedKeys.length > 0) { const snapshotId = await this.createSnapshot(); if (snapshotId) { utils_1.showInfo(`Created snapshot: ${snapshotId}`); const removedKeys = await this.removeKeys(unusedKeys); if (removedKeys) { utils_1.showInfo('Unused keys removed from lokalise'); } else { utils_1.showInfo(removedKeys); } } else { utils_1.showError('Something wrong with snapshot'); return; } } const end = new Date(); const time = end.getTime() - start.getTime(); utils_1.showInfo(`Finished in ${time} ms`); } } exports.default = Clean; Clean.description = 'Clean lokalise for unused translation keys'; Clean.flags = Object.assign(Object.assign({}, base_1.Base.flags), base_1.Base.providersFlags);