Skip to content

Commit

Permalink
🎨 Update code style according to new Prettier rules
Browse files Browse the repository at this point in the history
  • Loading branch information
leolabs committed Jul 18, 2021
1 parent a1c1420 commit 8b34852
Showing 1 changed file with 58 additions and 37 deletions.
95 changes: 58 additions & 37 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
evaluateFilePath,
FileType,
DirectoryStructure,
TranslatableFile
TranslatableFile,
} from './util/file-system';
import { matcherMap } from './matchers';

Expand Down Expand Up @@ -75,7 +75,7 @@ commander
)
.option(
'--decode-escapes',
'decodes escaped HTML entities like ' into normal UTF-8 characters'
'decodes escaped HTML entities like ' into normal UTF-8 characters',
)
.parse(process.argv);

Expand Down Expand Up @@ -116,13 +116,14 @@ const translate = async (

const translationService = serviceMap[service];

const templateFilePath = evaluateFilePath(workingDir, dirStructure, sourceLang);

const templateFiles = loadTranslations(
templateFilePath,
fileType,
const templateFilePath = evaluateFilePath(
workingDir,
dirStructure,
sourceLang,
);

const templateFiles = loadTranslations(templateFilePath, fileType);

if (templateFiles.length === 0) {
throw new Error(
`The source language ${sourceLang} doesn't contain any JSON files.`,
Expand All @@ -145,7 +146,11 @@ const translate = async (
console.log();

console.log(`✨ Initializing ${translationService.name}...`);
await translationService.initialize(config, matcherMap[matcher], decodeEscapes);
await translationService.initialize(
config,
matcherMap[matcher],
decodeEscapes,
);
console.log(chalk`└── {green.bold Done}`);
console.log();

Expand Down Expand Up @@ -262,7 +267,7 @@ const translate = async (
cacheDir,
workingDir,
dirStructure,
deleteUnusedStrings
deleteUnusedStrings,
);

switch (dirStructure) {
Expand All @@ -283,9 +288,17 @@ const translate = async (
chalk`├── {red.bold ${file.name} is no longer used and will be deleted.}`,
);

fs.unlinkSync(path.resolve(evaluateFilePath(workingDir, dirStructure, language), file.name));
fs.unlinkSync(
path.resolve(
evaluateFilePath(workingDir, dirStructure, language),
file.name,
),
);

const cacheFile = path.resolve(evaluateFilePath(workingDir, dirStructure, language), file.name);
const cacheFile = path.resolve(
evaluateFilePath(workingDir, dirStructure, language),
file.name,
);
if (fs.existsSync(cacheFile)) {
fs.unlinkSync(cacheFile);
}
Expand All @@ -296,21 +309,21 @@ const translate = async (
process.stdout.write(`├── Translating ${templateFile.name}`);

const [addedTranslations, removedTranslations] =
await translateContent(templateFile, existingFiles.find(
(f) => f.name === templateFile.name
));
await translateContent(
templateFile,
existingFiles.find((f) => f.name === templateFile.name),
);

totalAddedTranslations += addedTranslations;
totalRemovedTranslations += removedTranslations;
}
break;

case 'ngx-translate':
const [addedTranslations, removedTranslations] =
await translateContent(
templateFiles.find((f) => f.name === `${sourceLang}.json`),
templateFiles.find((f) => f.name === `${language}.json`)
);
const [addedTranslations, removedTranslations] = await translateContent(
templateFiles.find((f) => f.name === `${sourceLang}.json`),
templateFiles.find((f) => f.name === `${language}.json`),
);

totalAddedTranslations += addedTranslations;
totalRemovedTranslations += removedTranslations;
Expand All @@ -335,7 +348,9 @@ const translate = async (
}

console.log(
chalk.green.bold(`${totalAddedTranslations} new translations have been added!`),
chalk.green.bold(
`${totalAddedTranslations} new translations have been added!`,
),
);

if (totalRemovedTranslations > 0) {
Expand Down Expand Up @@ -370,7 +385,7 @@ translate(
commander.service,
commander.matcher,
commander.decodeEscapes,
commander.config,
commander.config,
).catch((e: Error) => {
console.log();
console.log(chalk.bgRed('An error has occurred:'));
Expand All @@ -388,14 +403,15 @@ function createTranslator(
cacheDir: string,
workingDir: string,
dirStructure: DirectoryStructure,
deleteUnusedStrings: boolean) {
deleteUnusedStrings: boolean,
) {
return async (
sourceFile: TranslatableFile,
destinationFile: TranslatableFile
destinationFile: TranslatableFile,
) => {
const cachePath = path.resolve(
evaluateFilePath(cacheDir, dirStructure, sourceLang),
sourceFile ? sourceFile.name : ''
sourceFile ? sourceFile.name : '',
);
let cacheDiff: string[] = [];
if (fs.existsSync(cachePath) && !fs.statSync(cachePath).isDirectory()) {
Expand All @@ -418,8 +434,7 @@ function createTranslator(
.filter((key) => !existingKeys.includes(key) || cacheDiff.includes(key))
.map((key) => ({
key,
value:
sourceFile.type === 'key-based' ? sourceFile.content[key] : key,
value: sourceFile.type === 'key-based' ? sourceFile.content[key] : key,
}));

const unusedStrings = existingKeys.filter(
Expand All @@ -438,13 +453,12 @@ function createTranslator(
);

if (service !== 'dry-run') {
const existingTranslations = destinationFile ? destinationFile.content : {};
const existingTranslations = destinationFile
? destinationFile.content
: {};

const translatedFile = {
...omit(
existingTranslations,
deleteUnusedStrings ? unusedStrings : [],
),
...omit(existingTranslations, deleteUnusedStrings ? unusedStrings : []),
...newKeys,
};

Expand All @@ -458,11 +472,18 @@ function createTranslator(
) + `\n`;

fs.writeFileSync(
path.resolve(evaluateFilePath(workingDir, dirStructure, targetLang), sourceFile.name),
path.resolve(
evaluateFilePath(workingDir, dirStructure, targetLang),
sourceFile.name,
),
newContent,
);

const languageCachePath = evaluateFilePath(cacheDir, dirStructure, targetLang);
const languageCachePath = evaluateFilePath(
cacheDir,
dirStructure,
targetLang,
);
if (!fs.existsSync(languageCachePath)) {
fs.mkdirSync(languageCachePath);
}
Expand All @@ -475,15 +496,15 @@ function createTranslator(
console.log(
deleteUnusedStrings && unusedStrings.length > 0
? chalk` ({green.bold +${String(
translatedStrings.length,
)}}/{red.bold -${String(unusedStrings.length)}})`
translatedStrings.length,
)}}/{red.bold -${String(unusedStrings.length)}})`
: chalk` ({green.bold +${String(translatedStrings.length)}})`,
);

// Added translations and removed translations
return [
translatedStrings.length,
deleteUnusedStrings ? unusedStrings.length : 0
deleteUnusedStrings ? unusedStrings.length : 0,
];
}
};
}

0 comments on commit 8b34852

Please sign in to comment.