-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
aaa8385
commit b861b92
Showing
4 changed files
with
159 additions
and
86 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(' | '); | ||
} | ||
} |