@kiwicom/smart-faq
Version:
76 lines (65 loc) • 2.12 kB
JavaScript
// @flow
const { spawnSync } = require('child_process');
require('dotenv').config();
const request = require('request-promise-native');
const argv = require('minimist')(process.argv.slice(2));
const prepareRequest = language => {
const projectId = process.env.PHRASE_PROJECT_ID;
const accessToken = process.env.PHRASE_ACCESS_TOKEN;
const options = {
method: 'GET',
url: `https://api.phraseapp.com/api/v2/projects/${projectId}/locales/${language}/download`,
qs: {
file_format: 'simple_json',
branch: argv.branch || '',
},
headers: {
authorization: `token ${accessToken}`,
},
json: true,
};
return request(options);
};
const filterSmartfaqKeys = async () => {
try {
const feKeys = await prepareRequest('en-GB');
return Object.keys(feKeys).filter(key => key.includes('smartfaq.'));
} catch (e) {
console.log('Error donwloading translations to check.', e);
}
};
const checkTranslations = (description, keys1, keys2) => {
const missingKeys = [];
keys1.forEach(localKey => {
if (!keys2.includes(localKey)) missingKeys.push(localKey);
});
if (missingKeys.length) {
console.log(`MISSING TRANSLATIONS (${description})`);
console.log(missingKeys);
throw new Error(`Missing translations (${description})`);
}
};
const checkTranslationsPhraseApp = async () => {
const localKeys = Object.keys(require('../data/tkeys.json')).filter(key =>
key.includes('smartfaq.'),
);
const remoteKeys = await filterSmartfaqKeys();
checkTranslations('PhraseApp', localKeys, remoteKeys);
};
const checkTranslationsEnKeys = async () => {
const localKeys = Object.keys(require('../data/tkeys.json')).filter(key =>
key.includes('smartfaq.'),
);
const enKeys = Object.keys(require('../src/translations/enKeys.json'));
checkTranslations('enKeys', localKeys, enKeys);
};
spawnSync('yarn', ['translations:keys']);
Promise.all([checkTranslationsPhraseApp(), checkTranslationsEnKeys()])
.then(() => {
console.log('Success');
process.exit(0);
})
.catch(e => {
console.log('Fail:', e);
process.exit(1);
});