UNPKG

i18nexus-cli

Version:

Command line interface (CLI) for accessing the i18nexus API

159 lines (125 loc) 4.22 kB
const fs = require('fs'); const colors = require('colors'); const handleError = require('../handleError'); const handleFetch = require('../handleFetch'); const baseUrl = require('../baseUrl'); const getProject = require('../getProject'); const cleanDirectory = path => { if (!fs.existsSync(path)) { return; } // as safety precaution, only delete folders that match regex const regex = /^[a-z]{2}(-[A-Z]{2,4})?$/; const contents = fs.readdirSync(path); contents.forEach(name => { if (regex.test(name)) { fs.rmSync(`${path}/${name}`, { recursive: true }); } }); }; const pull = async (opt, internalOptions = {}) => { const { logging = true, successLog = 'Translations downloaded successfully' } = internalOptions; let path = opt.path; const project = await getProject(opt.apiKey); const projectLibrary = project.library; if (!path) { if (!projectLibrary) { console.log( colors.red( 'There was a problem fetching your translations. Please try again in a moment.' ) ); return process.exit(1); } if (projectLibrary === 'i18next') { const hasAppDir = fs.existsSync(`${process.cwd()}/app`) || fs.existsSync(`${process.cwd()}/src/app`); const usingAppRouter = hasAppDir && (fs.existsSync(`${process.cwd()}/.next`) || fs.existsSync(`${process.cwd()}/next.config.js`) || fs.existsSync(`${process.cwd()}/next.config.ts`)); if (usingAppRouter) { path = `${process.cwd()}/locales`; } else { path = `${process.cwd()}/public/locales`; } } else { path = `${process.cwd()}/messages`; } } const usingVersion = opt.version !== 'latest'; if (usingVersion && opt.confirmed) { console.log(colors.red('The --confirmed flag cannot be used with version')); return process.exit(1); } if (logging) { console.log(`Downloading translations to ${path}...`); } const lngResponse = await handleFetch( `${baseUrl}/project_resources/languages.json?api_key=${opt.apiKey}` ); if (lngResponse.status !== 200) { return handleError( lngResponse, usingVersion ? 'There was a problem fetching your translations. Please ensure you are using the correct API key and a valid version number.' : 'There was a problem fetching your translations. Please try again in a moment.' ); } const languageCodes = (await lngResponse.json()).collection.map( lng => lng.full_code ); const translations = {}; for (let index = 0; index < languageCodes.length; index++) { const lng = languageCodes[index]; let url = usingVersion ? `https://cdn.i18nexus.com/versions/${opt.version}/translations/${lng}.json` : `${baseUrl}/project_resources/translations/${lng}.json`; url += `?api_key=${opt.apiKey}`; if (opt.confirmed) { url += '&confirmed=true'; } const response = await handleFetch(url); if (response.status !== 200) { return handleError( response, usingVersion ? 'There was a problem fetching your translations. Please ensure you are using the correct API key and a valid version number.' : 'There was a problem fetching your translations. Please try again in a moment.' ); } const lngTranslations = await response.json(); translations[lng] = lngTranslations; } if (opt.clean) { cleanDirectory(path); } const spacing = opt.compact ? 0 : 2; if (projectLibrary === 'next-intl') { for (let lng in translations) { fs.mkdirSync(path, { recursive: true }); fs.writeFileSync( `${path}/${lng}.json`, JSON.stringify(translations[lng], null, spacing) ); } } else { for (let lng in translations) { const lngFilePath = `${path}/${lng}`; fs.mkdirSync(lngFilePath, { recursive: true }); for (let namespace in translations[lng]) { fs.writeFileSync( `${lngFilePath}/${namespace}.json`, JSON.stringify(translations[lng][namespace], null, spacing) ); } } } console.log(colors.green(successLog)); }; module.exports = pull;