Skip to content

Commit b861b92

Browse files
committed
Refactor writer
1 parent aaa8385 commit b861b92

File tree

4 files changed

+159
-86
lines changed

4 files changed

+159
-86
lines changed

dist/index.js

Lines changed: 78 additions & 36 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/translator.ts

Lines changed: 11 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,15 @@ import {Config, CredentialsConfig} from './config';
44
import {Logger} from './logger';
55
import BuildRequest = TranslationsModel.BuildRequest;
66
import {baseName, downloadZipAndUnzip} from './utils';
7-
import {toPlatformPath} from '@actions/core';
7+
import {Writer} from './writer';
88

99
export class Translator {
10-
private placeholderStart = '<!-- README-TRANSLATE-LANGUAGES-START -->';
11-
private placeholderEnd = '<!-- README-TRANSLATE-LANGUAGES-END -->';
12-
1310
private credentials: CredentialsConfig;
1411
private config: Config;
1512
private logger: Logger;
13+
1614
private crowdin: crowdin;
15+
private writer: Writer;
1716

1817
constructor(credentials: CredentialsConfig, config: Config, logger: Logger) {
1918
this.credentials = credentials;
@@ -24,21 +23,24 @@ export class Translator {
2423
token: this.credentials.token,
2524
organization: this.credentials.organization
2625
});
26+
27+
this.writer = new Writer(credentials, config, logger);
2728
}
2829

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

32-
const switcher = await this.renderSwitcher();
33+
const project = await this.crowdin.projectsGroupsApi.getProject(this.credentials.projectId);
34+
this.writer.setProjectLanguages(project.data.targetLanguages);
3335

34-
await this.addLanguageSwitcher(this.config.file, switcher);
36+
await this.writer.addLanguageSwitcher(this.config.file);
3537
await this.uploadSources();
3638

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

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

4446
this.logger.log('info', 'Done!');
@@ -96,46 +98,6 @@ export class Translator {
9698
}
9799
}
98100

99-
private async addLanguageSwitcher(file: string, switcher: string): Promise<void> {
100-
if (!this.config.languageSwitcher) {
101-
return;
102-
}
103-
104-
this.logger.log('info', `Adding language switcher to ${file}...`);
105-
106-
let fileContents = fs.readFileSync(toPlatformPath(file)).toString();
107-
108-
if (!fileContents.includes(this.placeholderStart) || !fileContents.includes(this.placeholderEnd)) {
109-
this.logger.log(
110-
'warning',
111-
`Skipped! Please add ${this.placeholderStart} and ${this.placeholderEnd} to your README.md`
112-
);
113-
114-
return;
115-
}
116-
117-
const sliceFrom = fileContents.indexOf(this.placeholderStart) + this.placeholderStart.length;
118-
const sliceTo = fileContents.indexOf(this.placeholderEnd);
119-
120-
fileContents = `${fileContents.slice(0, sliceFrom)}\n${switcher}\n${fileContents.slice(sliceTo)}`;
121-
122-
this.logger.log('debug', fileContents);
123-
124-
fs.writeFileSync(toPlatformPath(file), fileContents);
125-
}
126-
127-
private async renderSwitcher(): Promise<string> {
128-
const project = await this.crowdin.projectsGroupsApi.getProject(this.credentials.projectId);
129-
130-
let languages: string[] = [];
131-
132-
project.data.targetLanguages.map(language => {
133-
languages.push(`[${language.name}](${this.config.destination}/README.${language.locale}.md)`);
134-
});
135-
136-
return languages.join(' | ');
137-
}
138-
139101
private async downloadTranslations(): Promise<string> {
140102
this.logger.log('info', 'Downloading translations...');
141103

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

175-
const sourceFile = files.data.find(file => file.data.path === this.getFilePath());
137+
const sourceFile = files.data.find(file => file.data.path === this.getCrowdinFilePath());
176138

177139
return sourceFile?.data;
178140
}
@@ -185,7 +147,7 @@ export class Translator {
185147
return crowdinBranch?.data;
186148
}
187149

188-
private getFilePath(): string {
150+
private getCrowdinFilePath(): string {
189151
const fileName = baseName(this.config.file);
190152

191153
if (this.config.branch) {

src/writer.ts

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import fs from 'fs';
2+
import {Logger} from './logger';
3+
import {Config, CredentialsConfig} from './config';
4+
import {toPlatformPath} from '@actions/core';
5+
import {LanguagesModel} from '@crowdin/crowdin-api-client/out/languages';
6+
7+
export class Writer {
8+
private placeholderStart = '<!-- README-TRANSLATE-LANGUAGES-START -->';
9+
private placeholderEnd = '<!-- README-TRANSLATE-LANGUAGES-END -->';
10+
11+
private credentials: CredentialsConfig;
12+
private config: Config;
13+
private logger: Logger;
14+
15+
private projectLanguages: LanguagesModel.Language[] = [];
16+
private switcher = '';
17+
18+
constructor(credentials: CredentialsConfig, config: Config, logger: Logger) {
19+
this.credentials = credentials;
20+
this.config = config;
21+
this.logger = logger;
22+
}
23+
24+
public setProjectLanguages(projectLanguages: LanguagesModel.Language[]): void {
25+
this.projectLanguages = projectLanguages;
26+
}
27+
28+
public async addLanguageSwitcher(file: string): Promise<void> {
29+
if (!this.config.languageSwitcher) {
30+
return;
31+
}
32+
33+
if (this.switcher.length === 0) {
34+
this.switcher = await this.renderSwitcher();
35+
}
36+
37+
this.logger.log('info', `Adding language switcher to ${file}...`);
38+
39+
let fileContents = fs.readFileSync(toPlatformPath(file)).toString();
40+
41+
if (!fileContents.includes(this.placeholderStart) || !fileContents.includes(this.placeholderEnd)) {
42+
this.logger.log(
43+
'warning',
44+
`Skipped! Please add ${this.placeholderStart} and ${this.placeholderEnd} to your README.md`
45+
);
46+
47+
return;
48+
}
49+
50+
const sliceFrom = fileContents.indexOf(this.placeholderStart) + this.placeholderStart.length;
51+
const sliceTo = fileContents.indexOf(this.placeholderEnd);
52+
53+
fileContents = `${fileContents.slice(0, sliceFrom)}\n${this.switcher}\n${fileContents.slice(sliceTo)}`;
54+
55+
this.logger.log('debug', fileContents);
56+
57+
fs.writeFileSync(toPlatformPath(file), fileContents);
58+
}
59+
60+
private async renderSwitcher(): Promise<string> {
61+
let languages: string[] = [];
62+
63+
this.projectLanguages.map(language => {
64+
languages.push(`[${language.name}](${this.config.destination}/README.${language.locale}.md)`);
65+
});
66+
67+
return languages.join(' | ');
68+
}
69+
}

0 commit comments

Comments
 (0)