Skip to content

Commit

Permalink
Relative links
Browse files Browse the repository at this point in the history
  • Loading branch information
andrii-bodnar committed Jul 21, 2023
1 parent b861b92 commit 59921e0
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 19 deletions.
9 changes: 8 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ jobs:

- name: List files
run: |
cd ./__tests__/files/docs
cd ./__tests__/files
echo "Source file:"
cat README.md
cd docs
echo "Translated files:"
ls -la
cat README.fr-FR.md
25 changes: 17 additions & 8 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.

7 changes: 6 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as fs from 'fs';
import * as path from 'path';
import AdmZip from 'adm-zip';
import axios from 'axios';
import {toPlatformPath} from '@actions/core';
Expand All @@ -19,8 +20,12 @@ const downloadZipAndUnzip = async (zipUrl: string, destination: string): Promise
return entries;
};

const generateRelativeFileLink = (currentFilePath: string, destinationFilePath: string): string => {
return path.relative(path.dirname(currentFilePath), destinationFilePath);
};

const baseName = (filePath: string): string => {
return filePath.split('/').pop() || filePath;
};

export {downloadZipAndUnzip, baseName};
export {downloadZipAndUnzip, generateRelativeFileLink, baseName};
19 changes: 11 additions & 8 deletions src/writer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {Logger} from './logger';
import {Config, CredentialsConfig} from './config';
import {toPlatformPath} from '@actions/core';
import {LanguagesModel} from '@crowdin/crowdin-api-client/out/languages';
import {baseName, generateRelativeFileLink} from './utils';

export class Writer {
private placeholderStart = '<!-- README-TRANSLATE-LANGUAGES-START -->';
Expand All @@ -13,7 +14,6 @@ export class Writer {
private logger: Logger;

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

constructor(credentials: CredentialsConfig, config: Config, logger: Logger) {
this.credentials = credentials;
Expand All @@ -30,10 +30,6 @@ export class Writer {
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();
Expand All @@ -49,19 +45,26 @@ export class Writer {

const sliceFrom = fileContents.indexOf(this.placeholderStart) + this.placeholderStart.length;
const sliceTo = fileContents.indexOf(this.placeholderEnd);
const switcher = await this.renderSwitcher(file);

fileContents = `${fileContents.slice(0, sliceFrom)}\n${this.switcher}\n${fileContents.slice(sliceTo)}`;
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> {
private async renderSwitcher(file: string): Promise<string> {
let languages: string[] = [];

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

if (baseName(link) === baseName(file)) {
languages.push(`**${language.name}**`);
} else {
languages.push(`[${language.name}](${generateRelativeFileLink(file, link)})`);
}
});

return languages.join(' | ');
Expand Down

0 comments on commit 59921e0

Please sign in to comment.