UNPKG

poem-tm-cli

Version:

CLI application for the Poem Project

164 lines (154 loc) 4.38 kB
#!/usr/bin/env node const program = require('commander'); const prompt = require('prompt-sync')(); const axios = require('axios'); const fs = require('fs').promises; const path = require('path'); const hiddenQuestion = require('./hidden-pass'); const BASE_URL = 'https://poem-core.herokuapp.com'; program .version('1.0.0') .description('CLI for Poem Translation Memory') .name('poem-cli'); // Commands program .command('list') .description('List all published translation entries') .action(async () => { const email = prompt('Email address: '); const password = await hiddenQuestion('Password: '); console.log('Fetching data...'); axios.post(`${BASE_URL}/auth/local`, { identifier: email, password: password, }) // Handle successful auth .then(res => { const token = res.data.jwt; axios.get(`${BASE_URL}/translations`, { headers: { Authorization: `Bearer ${token}` } }) // Handle successful data fetch .then(res => { console.log('\nKey\t\t\tDefault Value\n'); res.data.forEach(el => { console.log(`${el.key}\t\t\t${el.enGb.text}`); }); }) // Handle data fetch error .catch(error => { console.log(`Data fetch failed: ${error}`); }) }) // Handle auth error .catch(error => { console.log(`Authentication failed: ${error}`); }) }); program .command('pull') .description('Pull all translations to a i18n folder in the current directory folder') .action(async () => { const email = prompt('Email address: '); const password = await hiddenQuestion('Password: '); console.log('Fetching data...'); axios.post(`${BASE_URL}/auth/local`, { identifier: email, password: password, }) // Handle successful auth .then(res => { const token = res.data.jwt; axios.get(`${BASE_URL}/translations`, { headers: { Authorization: `Bearer ${token}` } }) // Handle successful data fetch .then(async res => { const library = { zhHansCn: {}, zhHantTw: {}, cs: {}, nlBe: {}, enGb: {}, enUs: {}, fr: {}, frCh: {}, de: {}, hu: {}, it: {}, itCh: {}, ja: {}, ko: {}, ms: {}, pl: {}, ptBr: {}, ro: {}, ru: {}, es: {}, th: {}, trTr: {} }; // Populate library await res.data.forEach(el => { Object.entries(library).forEach(([key, value]) => { value[el.key] = !!el[key] && el[key].approved && el[key].text || el.enGb.text; }); }); try { await fs.mkdir('i18n'); console.log('Created i18n folder...'); } catch { console.log('Found i18n folder...'); } // Write files Object.entries(library).forEach( async ([key, value]) => { const filename = `${key.slice(0,2)} ${key.slice(2, 6)} ${key.slice(6)}` .toLowerCase() .split(' ') .filter(i => i) .join('-'); await fs.writeFile(path.join('i18n', `${filename}.json`), JSON.stringify(value, undefined, 2)); }); console.log('Files generated successfully!'); }) // Handle data fetch error .catch(error => { console.log(`Data fetch failed: ${error}`); }) }) // Handle auth error .catch(error => { console.log(`Authentication failed: ${error}`); }) }); program .command('forgot-password') .description('Generates a code to reset the password') .action(() => { const email = prompt('Email address: '); axios.post(`${BASE_URL}/auth/forgot-password`, { email: email }) // Handle success .then(res => { console.log('We sent you an email.') }) // Handle auth error .catch(error => { console.log(error); }) }); program .command('joke') .description("Get good vibes from a Cris' random joke") .action(() => { const url = "https://icanhazdadjoke.com/"; axios.get(url, { headers: { Accept: "application/json" } }) .then(res => {console.log(res.data.joke); }); }) program.parse(process.argv);