mirror of
https://github.com/elyby/accounts-frontend.git
synced 2025-02-15 23:07:59 +05:30
Enable prettier for the i18n-crowdin script
This commit is contained in:
parent
270f4c9c4d
commit
b25e888781
@ -1,5 +1,5 @@
|
|||||||
|
/* eslint-disable jsdoc/require-param */
|
||||||
/* eslint-env node */
|
/* eslint-env node */
|
||||||
/* eslint-disable */
|
|
||||||
|
|
||||||
import chalk from 'chalk';
|
import chalk from 'chalk';
|
||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
@ -78,8 +78,8 @@ function toInternalLocale(code: string): string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Форматирует входящий объект с переводами в итоговую строку в том формате, в каком они
|
* Formats the incoming hash with the translations in the module string in the format
|
||||||
* хранятся в самом приложении
|
* as they are stored in the application itself
|
||||||
*/
|
*/
|
||||||
function serializeToModule(translates: Record<string, any>): string {
|
function serializeToModule(translates: Record<string, any>): string {
|
||||||
const src = JSON5.stringify(sortByKeys(translates), null, 4);
|
const src = JSON5.stringify(sortByKeys(translates), null, 4);
|
||||||
@ -111,40 +111,41 @@ function getLocaleFilePath(languageId: string): string {
|
|||||||
return path.join(LANG_DIR, `${toInternalLocale(languageId)}.json`);
|
return path.join(LANG_DIR, `${toInternalLocale(languageId)}.json`);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function findDirectoryId(path: string, branchId?: number): Promise<number|undefined> {
|
async function findDirectoryId(directoryPath: string, branchId?: number): Promise<number | undefined> {
|
||||||
const { data: dirsResponse } = await crowdin.sourceFilesApi.listProjectDirectories(PROJECT_ID, branchId);
|
const { data: dirsResponse } = await crowdin.sourceFilesApi.listProjectDirectories(PROJECT_ID, branchId);
|
||||||
const dirs = dirsResponse.map((dirData) => dirData.data);
|
const dirs = dirsResponse.map((dirData) => dirData.data);
|
||||||
|
|
||||||
const result = path.split('/').reduce((parentDir, dirName) => {
|
const result = directoryPath.split('/').reduce((parentDir, dirName) => {
|
||||||
// directoryId is nullable when a directory has no parent
|
// directoryId is nullable when a directory has no parent
|
||||||
return dirs.find((dir) => dir.directoryId === parentDir && dir.name === dirName)?.id;
|
return dirs.find((dir) => dir.directoryId === parentDir && dir.name === dirName)?.id;
|
||||||
}, null as number|null|undefined);
|
}, null as number | null | undefined);
|
||||||
|
|
||||||
return result || undefined;
|
return result || undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function findFileId(filePath: string, branchId?: number): Promise<number|undefined> {
|
async function findFileId(filePath: string, branchId?: number): Promise<number | undefined> {
|
||||||
const fileName = path.basename(filePath);
|
const fileName = path.basename(filePath);
|
||||||
const dirPath = path.dirname(filePath);
|
const dirPath = path.dirname(filePath);
|
||||||
let directoryId: number|null = null;
|
let directoryId: number | null = null;
|
||||||
|
|
||||||
if (dirPath !== '') {
|
if (dirPath !== '') {
|
||||||
directoryId = await findDirectoryId(dirPath, branchId) || null;
|
directoryId = (await findDirectoryId(dirPath, branchId)) || null;
|
||||||
}
|
}
|
||||||
|
|
||||||
// We're receiving files list without branch filter until https://github.com/crowdin/crowdin-api-client-js/issues/63
|
// We're receiving files list without branch filter until https://github.com/crowdin/crowdin-api-client-js/issues/63
|
||||||
// will be resolved. But right now it doesn't matter because for each branch directories will have its own ids,
|
// will be resolved. But right now it doesn't matter because for each branch directories will have its own ids,
|
||||||
// so if the file is stored into the some directory, algorithm will find correct file.
|
// so if the file is stored into the some directory, algorithm will find correct file.
|
||||||
const { data: filesResponse } = await crowdin.sourceFilesApi.listProjectFiles(PROJECT_ID/*, branchId*/);
|
const { data: filesResponse } = await crowdin.sourceFilesApi.listProjectFiles(PROJECT_ID /*, branchId*/);
|
||||||
const files = filesResponse.map((fileData) => fileData.data);
|
const files = filesResponse.map((fileData) => fileData.data);
|
||||||
|
|
||||||
return files.find((file) => file.directoryId === directoryId && file.name === fileName)?.id;
|
return files.find((file) => file.directoryId === directoryId && file.name === fileName)?.id;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function findBranchId(branchName: string): Promise<number|undefined> {
|
async function findBranchId(branchName: string): Promise<number | undefined> {
|
||||||
const { data: branchesList } = await crowdin.sourceFilesApi.listProjectBranches(PROJECT_ID, branchName);
|
const { data: branchesList } = await crowdin.sourceFilesApi.listProjectBranches(PROJECT_ID, branchName);
|
||||||
const branch = branchesList.find(({ data: branch }) => branch.name === branchName);
|
const branchData = branchesList.find(({ data: branch }) => branch.name === branchName);
|
||||||
|
|
||||||
return branch?.data.id;
|
return branchData?.data.id;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function ensureDirectory(dirPath: string, branchId?: number): Promise<number> {
|
async function ensureDirectory(dirPath: string, branchId?: number): Promise<number> {
|
||||||
@ -154,11 +155,13 @@ async function ensureDirectory(dirPath: string, branchId?: number): Promise<numb
|
|||||||
return dirPath.split('/').reduce(async (parentDirPromise, name) => {
|
return dirPath.split('/').reduce(async (parentDirPromise, name) => {
|
||||||
const parentDir = await parentDirPromise;
|
const parentDir = await parentDirPromise;
|
||||||
const directoryId = dirs.find((dir) => dir.directoryId === parentDir && dir.name === name)?.id;
|
const directoryId = dirs.find((dir) => dir.directoryId === parentDir && dir.name === name)?.id;
|
||||||
|
|
||||||
if (directoryId) {
|
if (directoryId) {
|
||||||
return directoryId;
|
return directoryId;
|
||||||
}
|
}
|
||||||
|
|
||||||
const createDirRequest: SourceFilesModel.CreateDirectoryRequest = { name };
|
const createDirRequest: SourceFilesModel.CreateDirectoryRequest = { name };
|
||||||
|
|
||||||
if (directoryId) {
|
if (directoryId) {
|
||||||
createDirRequest['directoryId'] = directoryId;
|
createDirRequest['directoryId'] = directoryId;
|
||||||
} else if (branchId) {
|
} else if (branchId) {
|
||||||
@ -175,10 +178,17 @@ async function ensureDirectory(dirPath: string, branchId?: number): Promise<numb
|
|||||||
async function pull(): Promise<void> {
|
async function pull(): Promise<void> {
|
||||||
const { branch: branchName } = getRepoInfo();
|
const { branch: branchName } = getRepoInfo();
|
||||||
const isMasterBranch = branchName === 'master';
|
const isMasterBranch = branchName === 'master';
|
||||||
let branchId: number|undefined;
|
let branchId: number | undefined;
|
||||||
|
|
||||||
if (!isMasterBranch) {
|
if (!isMasterBranch) {
|
||||||
console.log(`Current branch isn't ${chalk.green('master')}, will try to pull translates from the ${chalk.green(branchName)} branch`);
|
console.log(
|
||||||
|
[
|
||||||
|
`Current branch isn't ${chalk.green('master')},`,
|
||||||
|
`will try to pull translates from the ${chalk.green(branchName)} branch`,
|
||||||
|
].join(' '),
|
||||||
|
);
|
||||||
branchId = await findBranchId(branchName);
|
branchId = await findBranchId(branchName);
|
||||||
|
|
||||||
if (!branchId) {
|
if (!branchId) {
|
||||||
console.log(`Branch ${chalk.green(branchName)} isn't found, will use ${chalk.green('master')} instead`);
|
console.log(`Branch ${chalk.green(branchName)} isn't found, will use ${chalk.green('master')} instead`);
|
||||||
}
|
}
|
||||||
@ -186,6 +196,7 @@ async function pull(): Promise<void> {
|
|||||||
|
|
||||||
console.log('Loading file info...');
|
console.log('Loading file info...');
|
||||||
const fileId = await findFileId(CROWDIN_FILE_PATH, branchId);
|
const fileId = await findFileId(CROWDIN_FILE_PATH, branchId);
|
||||||
|
|
||||||
if (!fileId) {
|
if (!fileId) {
|
||||||
throw new Error('Cannot find the file');
|
throw new Error('Cannot find the file');
|
||||||
}
|
}
|
||||||
@ -206,6 +217,7 @@ async function pull(): Promise<void> {
|
|||||||
|
|
||||||
translationProgress.forEach(({ data: { languageId, approvalProgress } }) => {
|
translationProgress.forEach(({ data: { languageId, approvalProgress } }) => {
|
||||||
const locale = toInternalLocale(languageId);
|
const locale = toInternalLocale(languageId);
|
||||||
|
|
||||||
if (releasedLocales.includes(locale) || approvalProgress >= MIN_RELEASE_PROGRESS) {
|
if (releasedLocales.includes(locale) || approvalProgress >= MIN_RELEASE_PROGRESS) {
|
||||||
localesToPull.push(languageId);
|
localesToPull.push(languageId);
|
||||||
indexFileEntries[locale] = {
|
indexFileEntries[locale] = {
|
||||||
@ -227,22 +239,26 @@ async function pull(): Promise<void> {
|
|||||||
});
|
});
|
||||||
let downloadingReady = 0;
|
let downloadingReady = 0;
|
||||||
|
|
||||||
const promises = localesToPull.map(async (languageId): Promise<void> => {
|
const promises = localesToPull.map(
|
||||||
const { data: { url } } = await crowdin.translationsApi.buildProjectFileTranslation(PROJECT_ID, fileId, {
|
async (languageId): Promise<void> => {
|
||||||
targetLanguageId: languageId,
|
const {
|
||||||
exportApprovedOnly: true,
|
data: { url },
|
||||||
});
|
} = await crowdin.translationsApi.buildProjectFileTranslation(PROJECT_ID, fileId, {
|
||||||
|
targetLanguageId: languageId,
|
||||||
|
exportApprovedOnly: true,
|
||||||
|
});
|
||||||
|
|
||||||
const { data: fileContents } = await axios.get(url, {
|
const { data: fileContents } = await axios.get(url, {
|
||||||
// Disable response parsing
|
// Disable response parsing
|
||||||
transformResponse: [],
|
transformResponse: [],
|
||||||
});
|
});
|
||||||
fs.writeFileSync(getLocaleFilePath(languageId), fileContents);
|
fs.writeFileSync(getLocaleFilePath(languageId), fileContents);
|
||||||
|
|
||||||
downloadingProgressBar.update(++downloadingReady / localesToPull.length, {
|
downloadingProgressBar.update(++downloadingReady / localesToPull.length, {
|
||||||
cCurrent: downloadingReady,
|
cCurrent: downloadingReady,
|
||||||
});
|
});
|
||||||
});
|
},
|
||||||
|
);
|
||||||
|
|
||||||
await Promise.all(promises);
|
await Promise.all(promises);
|
||||||
|
|
||||||
@ -255,19 +271,29 @@ async function pull(): Promise<void> {
|
|||||||
|
|
||||||
async function push(): Promise<void> {
|
async function push(): Promise<void> {
|
||||||
if (!fs.existsSync(getLocaleFilePath(SOURCE_LANG))) {
|
if (!fs.existsSync(getLocaleFilePath(SOURCE_LANG))) {
|
||||||
console.error(chalk.red(`File for the source language doesn't exists. Run ${chalk.green('yarn i18n:extract')} to generate the source language file.`));
|
console.error(
|
||||||
|
chalk.red(
|
||||||
|
`File for the source language doesn't exists. Run ${chalk.green(
|
||||||
|
'yarn i18n:extract',
|
||||||
|
)} to generate the source language file.`,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const questions: Array<DistinctQuestion> = [{
|
const questions: Array<DistinctQuestion> = [
|
||||||
name: 'disapproveTranslates',
|
{
|
||||||
type: 'confirm',
|
name: 'disapproveTranslates',
|
||||||
default: true,
|
type: 'confirm',
|
||||||
message: 'Disapprove changed lines?',
|
default: true,
|
||||||
}];
|
message: 'Disapprove changed lines?',
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
const { branch: branchName } = getRepoInfo();
|
const { branch: branchName } = getRepoInfo();
|
||||||
const isMasterBranch = branchName === 'master';
|
const isMasterBranch = branchName === 'master';
|
||||||
|
|
||||||
if (!isMasterBranch) {
|
if (!isMasterBranch) {
|
||||||
questions.push({
|
questions.push({
|
||||||
name: 'publishInBranch',
|
name: 'publishInBranch',
|
||||||
@ -281,21 +307,23 @@ async function push(): Promise<void> {
|
|||||||
let publishInBranch = isMasterBranch;
|
let publishInBranch = isMasterBranch;
|
||||||
try {
|
try {
|
||||||
const answers = await prompt(questions);
|
const answers = await prompt(questions);
|
||||||
disapproveTranslates = answers[0];
|
disapproveTranslates = answers[0]; // eslint-disable-line prefer-destructuring
|
||||||
publishInBranch = answers[1] || false;
|
publishInBranch = answers[1] || false;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
// okay if it's tty error
|
// okay if it's a tty error
|
||||||
if (!err.isTtyError) {
|
if (!err.isTtyError) {
|
||||||
throw err;
|
throw err;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let branchId: number|undefined;
|
let branchId: number | undefined;
|
||||||
|
|
||||||
if (publishInBranch) {
|
if (publishInBranch) {
|
||||||
console.log('Loading the branch info...');
|
console.log('Loading the branch info...');
|
||||||
branchId = await findBranchId(branchName);
|
branchId = await findBranchId(branchName);
|
||||||
|
|
||||||
if (!branchId) {
|
if (!branchId) {
|
||||||
console.log('Branch doesn\'t exists. Creating...');
|
console.log("Branch doesn't exists. Creating...");
|
||||||
const { data: branchResponse } = await crowdin.sourceFilesApi.createBranch(PROJECT_ID, {
|
const { data: branchResponse } = await crowdin.sourceFilesApi.createBranch(PROJECT_ID, {
|
||||||
name: branchName,
|
name: branchName,
|
||||||
});
|
});
|
||||||
@ -303,13 +331,15 @@ async function push(): Promise<void> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log("Loading the file info...");
|
console.log('Loading the file info...');
|
||||||
const fileId = await findFileId(CROWDIN_FILE_PATH, branchId);
|
const fileId = await findFileId(CROWDIN_FILE_PATH, branchId);
|
||||||
let dirId: number|undefined;
|
let dirId: number | undefined;
|
||||||
|
|
||||||
if (!fileId) {
|
if (!fileId) {
|
||||||
const dirPath = path.dirname(CROWDIN_FILE_PATH);
|
const dirPath = path.dirname(CROWDIN_FILE_PATH);
|
||||||
|
|
||||||
if (dirPath !== '') {
|
if (dirPath !== '') {
|
||||||
console.log("Ensuring necessary directories structure...");
|
console.log('Ensuring necessary directories structure...');
|
||||||
dirId = await ensureDirectory(dirPath, branchId);
|
dirId = await ensureDirectory(dirPath, branchId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -347,9 +377,9 @@ async function push(): Promise<void> {
|
|||||||
console.log(ch.green('Success'));
|
console.log(ch.green('Success'));
|
||||||
}
|
}
|
||||||
|
|
||||||
(async() => {
|
(async () => {
|
||||||
try {
|
try {
|
||||||
const action = process.argv[2];
|
const action = process.argv[2]; // eslint-disable-line prefer-destructuring
|
||||||
|
|
||||||
switch (action) {
|
switch (action) {
|
||||||
case 'pull':
|
case 'pull':
|
||||||
|
Loading…
x
Reference in New Issue
Block a user