UNPKG

@enplug/scripts

Version:
158 lines (135 loc) 7.53 kB
const chalk = require('chalk'); const createS3Client = require('../createS3Client'); const determineEnv = require('../determineEnv'); const inquirer = require('inquirer'); const fs = require('fs'); const path = require('path'); const { getCrowdinCredentials, getCrowdinConfig, findCrowdinAppDirectoryId, findCrowdinAppSubFolderId, getFileIdIfExists, uploadFileToCrowdinStorage, updateCrowdinFile, addCrowdinFile, fetchFileFromCrowdin, readFakeTranslationsFile } = require('./crowdin'); const { checkKeys, validateTranslationFile } = require('./transloco'); const { uploadTranslationToS3 } = require('./translation-s3'); const getPackageJson = require('../getPackageJson'); const FAKE_IN_CONTEXT_LANGUAGE = 'eo'; const FAKE_IN_CONTEXT_LANGUAGE_FILE = 'eo-UY.json'; const TRANSLATION_BUCKETS = ['dev-apps.enplug.in', 'apps.enplug.in', 'apps.enplug.com']; async function validateTranslations() { let shouldUpload = true; try { await validateTranslationFile(); } catch { shouldUpload = false; return; } let keysUpToDate = true; try { await checkKeys(); } catch { keysUpToDate = false; } if (shouldUpload && !keysUpToDate) { ({ confirmForceContinue: shouldUpload } = await promptForceContinue()); } return shouldUpload; } async function syncTranslations(s3Client, bucket) { let credentials, config; const isProduction = determineEnv(bucket) === 'production'; if (!s3Client) { s3Client = createS3Client(getPackageJson()); } try { credentials = getCrowdinCredentials(); config = getCrowdinConfig(); } catch {} if (!credentials || !credentials.token) { console.error(`${chalk.red.bold('Crowdin credentials not provided')}`); console.log(`Make sure that the ${chalk.default.yellow('dev.private.json')} file contains ${chalk.default.yellow('crowdinCredentials: { token }')}`); throw new Error('Crowdin credentials not provided'); } if (credentials && config) { const { confirmUpload } = await promptUpload(config.crowdinPath, isProduction); if (confirmUpload) { const s3EnPath = path.join('/i18n/', config.crowdinPath); const rootPath = __dirname.split('node_modules')[0]; const enTranslationPath = path.join(rootPath, config.localPath); const enTranslation = fs.readFileSync(enTranslationPath); await uploadTranslationToS3(s3Client, bucket, s3EnPath, enTranslation); if (isProduction) { const crowdinPath = config.crowdinPath.startsWith('/') ? config.crowdinPath.substr(1) : config.crowdinPath; const crowdinPathSections = crowdinPath.split('/'); const appName = crowdinPathSections[1]; const appDirectoryId = await findCrowdinAppDirectoryId(credentials, crowdinPath, appName); if(appDirectoryId === undefined) { console.error(`\n${chalk.red.bold('Directory does not exist')} ${chalk.yellow.bold(`${appName}`)}`); console.log('Create the directory in the Crowdin panel first.'); return; } if(crowdinPathSections[0] === 'apps' && appDirectoryId) { if(crowdinPathSections.length !== 4 && crowdinPathSections[3] !== 'en.json') { console.error(`\n${chalk.red.bold('Crowdin path provided does not match defined format, make sure the path is as follows /apps/{app-id}/dashboard/en.json or /apps/{app-id}/app/en.json')}`); return; } const subfolderName = crowdinPathSections[2]; //dashboard or app folder const folderId = await findCrowdinAppSubFolderId(credentials, appDirectoryId, subfolderName); if(folderId === undefined) { console.error(`Could not find ${chalk.yellow.bold(`[${subfolderName}]`)} folder`); } const fileId = await getFileIdIfExists(credentials, folderId, crowdinPathSections[3]); // en.json file id to update if(fileId) { const storageId = await uploadFileToCrowdinStorage(credentials, config.localPath); if(fileId && storageId) { await updateCrowdinFile(credentials, crowdinPath, storageId.data.data.id, fileId); await updateFrakeTranslations(credentials, fileId, s3EnPath, s3Client, bucket); } } else { const storageId = await uploadFileToCrowdinStorage(credentials, config.localPath); if(storageId) { const result = await addCrowdinFile(credentials, crowdinPath, storageId.data.data.id, folderId); await updateFrakeTranslations(credentials, result.data.data.id, s3EnPath, s3Client, bucket); } } } else if((crowdinPathSections[0] === 'dashboard' || crowdinPathSections[0] === 'player-web' || crowdinPathSections[0] === 'components') && appDirectoryId) { if(crowdinPathSections[1] === 'en.json') { const fileId = await getFileIdIfExists(credentials, appDirectoryId, crowdinPathSections[1]); // en.json file id to update if(fileId) { const storageId = await uploadFileToCrowdinStorage(credentials, config.localPath); if(fileId && storageId) { await updateCrowdinFile(credentials, crowdinPath, storageId.data.data.id, fileId); await updateFrakeTranslations(credentials, fileId, s3EnPath, s3Client, bucket); } } else { console.error(`Could not upload ${chalk.yellow.bold(`${rowdinPathSections[1]}`)}. Only en.json supported.`); } } else { // data-connector, regions-map and uploader in dashboard folder const folderId = await findCrowdinAppDirectoryId(credentials, crowdinPath, crowdinPathSections[1]); if(folderId === undefined) { console.error(`Could not find ${chalk.yellow.bold(`${folderId}`)} folder`); } const fileId = await getFileIdIfExists(credentials, folderId, crowdinPathSections[2]); // en.json file id to update const storageId = await uploadFileToCrowdinStorage(credentials, config.localPath); if(fileId && storageId) { await updateCrowdinFile(credentials, crowdinPath, storageId.data.data.id, fileId); await updateFrakeTranslations(credentials, fileId, s3EnPath, s3Client, bucket); } } } } } } } async function updateFrakeTranslations(credentials, fileId, s3EnPath, s3Client, bucket) { const { data: fakeTranslationUrl } = await fetchFileFromCrowdin(credentials, FAKE_IN_CONTEXT_LANGUAGE, fileId); const s3TranslationsPath = path.parse(s3EnPath).dir; const s3FakeTranslationPath = path.join(s3TranslationsPath, FAKE_IN_CONTEXT_LANGUAGE_FILE); const fakeTranslation = await readFakeTranslationsFile(fakeTranslationUrl.data.url); await uploadTranslationToS3(s3Client, bucket, s3FakeTranslationPath, JSON.stringify(fakeTranslation.data)); } async function promptForceContinue() { return inquirer.prompt({ type: 'confirm', name: 'confirmForceContinue', message: `Do you want to continue anyway?`, default: false }); } async function promptUpload(crowdinPath, uploadToCrowdin = false) { const targets = ['S3']; if (uploadToCrowdin) { targets.unshift('Crowdin'); } const targetsString = targets.join(' and '); return inquirer.prompt({ type: 'confirm', name: 'confirmUpload', message: `Do you confirm the upload to ${targetsString} ${chalk.bold.yellow(`[${crowdinPath}]`)}?` }); } module.exports = { TRANSLATION_BUCKETS, syncTranslations, validateTranslations };