Skip to content

Commit

Permalink
Refactor writer
Browse files Browse the repository at this point in the history
  • Loading branch information
andrii-bodnar committed Jul 21, 2023
1 parent aaa8385 commit b861b92
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 86 deletions.
114 changes: 78 additions & 36 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

60 changes: 11 additions & 49 deletions src/translator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@ import {Config, CredentialsConfig} from './config';
import {Logger} from './logger';
import BuildRequest = TranslationsModel.BuildRequest;
import {baseName, downloadZipAndUnzip} from './utils';
import {toPlatformPath} from '@actions/core';
import {Writer} from './writer';

export class Translator {
private placeholderStart = '<!-- README-TRANSLATE-LANGUAGES-START -->';
private placeholderEnd = '<!-- README-TRANSLATE-LANGUAGES-END -->';

private credentials: CredentialsConfig;
private config: Config;
private logger: Logger;

private crowdin: crowdin;
private writer: Writer;

constructor(credentials: CredentialsConfig, config: Config, logger: Logger) {
this.credentials = credentials;
Expand All @@ -24,21 +23,24 @@ export class Translator {
token: this.credentials.token,
organization: this.credentials.organization
});

this.writer = new Writer(credentials, config, logger);
}

public async translate(): Promise<void> {
this.logger.log('info', 'Start...');

const switcher = await this.renderSwitcher();
const project = await this.crowdin.projectsGroupsApi.getProject(this.credentials.projectId);
this.writer.setProjectLanguages(project.data.targetLanguages);

await this.addLanguageSwitcher(this.config.file, switcher);
await this.writer.addLanguageSwitcher(this.config.file);
await this.uploadSources();

const translationsUrl = await this.downloadTranslations();
const translationFiles = await downloadZipAndUnzip(translationsUrl, this.config.destination);

translationFiles.map(async (file: string): Promise<void> => {
await this.addLanguageSwitcher(`${this.config.destination}/${file}`, switcher);
await this.writer.addLanguageSwitcher(`${this.config.destination}/${file}`);
});

this.logger.log('info', 'Done!');
Expand Down Expand Up @@ -96,46 +98,6 @@ export class Translator {
}
}

private async addLanguageSwitcher(file: string, switcher: string): Promise<void> {
if (!this.config.languageSwitcher) {
return;
}

this.logger.log('info', `Adding language switcher to ${file}...`);

let fileContents = fs.readFileSync(toPlatformPath(file)).toString();

if (!fileContents.includes(this.placeholderStart) || !fileContents.includes(this.placeholderEnd)) {
this.logger.log(
'warning',
`Skipped! Please add ${this.placeholderStart} and ${this.placeholderEnd} to your README.md`
);

return;
}

const sliceFrom = fileContents.indexOf(this.placeholderStart) + this.placeholderStart.length;
const sliceTo = fileContents.indexOf(this.placeholderEnd);

fileContents = `${fileContents.slice(0, sliceFrom)}\n${switcher}\n${fileContents.slice(sliceTo)}`;

this.logger.log('debug', fileContents);

fs.writeFileSync(toPlatformPath(file), fileContents);
}

private async renderSwitcher(): Promise<string> {
const project = await this.crowdin.projectsGroupsApi.getProject(this.credentials.projectId);

let languages: string[] = [];

project.data.targetLanguages.map(language => {
languages.push(`[${language.name}](${this.config.destination}/README.${language.locale}.md)`);
});

return languages.join(' | ');
}

private async downloadTranslations(): Promise<string> {
this.logger.log('info', 'Downloading translations...');

Expand Down Expand Up @@ -172,7 +134,7 @@ export class Translator {
private async getFile(): Promise<SourceFilesModel.File | undefined> {
const files = await this.crowdin.sourceFilesApi.listProjectFiles(this.credentials.projectId);

const sourceFile = files.data.find(file => file.data.path === this.getFilePath());
const sourceFile = files.data.find(file => file.data.path === this.getCrowdinFilePath());

return sourceFile?.data;
}
Expand All @@ -185,7 +147,7 @@ export class Translator {
return crowdinBranch?.data;
}

private getFilePath(): string {
private getCrowdinFilePath(): string {
const fileName = baseName(this.config.file);

if (this.config.branch) {
Expand Down
69 changes: 69 additions & 0 deletions src/writer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import fs from 'fs';
import {Logger} from './logger';
import {Config, CredentialsConfig} from './config';
import {toPlatformPath} from '@actions/core';
import {LanguagesModel} from '@crowdin/crowdin-api-client/out/languages';

export class Writer {
private placeholderStart = '<!-- README-TRANSLATE-LANGUAGES-START -->';
private placeholderEnd = '<!-- README-TRANSLATE-LANGUAGES-END -->';

private credentials: CredentialsConfig;
private config: Config;
private logger: Logger;

private projectLanguages: LanguagesModel.Language[] = [];
private switcher = '';

constructor(credentials: CredentialsConfig, config: Config, logger: Logger) {
this.credentials = credentials;
this.config = config;
this.logger = logger;
}

public setProjectLanguages(projectLanguages: LanguagesModel.Language[]): void {
this.projectLanguages = projectLanguages;
}

public async addLanguageSwitcher(file: string): Promise<void> {
if (!this.config.languageSwitcher) {
return;
}

if (this.switcher.length === 0) {
this.switcher = await this.renderSwitcher();
}

this.logger.log('info', `Adding language switcher to ${file}...`);

let fileContents = fs.readFileSync(toPlatformPath(file)).toString();

if (!fileContents.includes(this.placeholderStart) || !fileContents.includes(this.placeholderEnd)) {
this.logger.log(
'warning',
`Skipped! Please add ${this.placeholderStart} and ${this.placeholderEnd} to your README.md`
);

return;
}

const sliceFrom = fileContents.indexOf(this.placeholderStart) + this.placeholderStart.length;
const sliceTo = fileContents.indexOf(this.placeholderEnd);

fileContents = `${fileContents.slice(0, sliceFrom)}\n${this.switcher}\n${fileContents.slice(sliceTo)}`;

this.logger.log('debug', fileContents);

fs.writeFileSync(toPlatformPath(file), fileContents);
}

private async renderSwitcher(): Promise<string> {
let languages: string[] = [];

this.projectLanguages.map(language => {
languages.push(`[${language.name}](${this.config.destination}/README.${language.locale}.md)`);
});

return languages.join(' | ');
}
}

0 comments on commit b861b92

Please sign in to comment.