Skip to content

Commit

Permalink
feat: add initial checks for outdated translations
Browse files Browse the repository at this point in the history
  • Loading branch information
aswanson-nr committed Jun 15, 2021
1 parent 6f12f41 commit 1e88761
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 2 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/validate-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,27 @@ jobs:

- name: Run Eslint
run: yarn lint

outdated-translations-removed:
name: Outdated Translations Removed
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v2

- name: Setup Node.js
uses: actions/setup-node@v2
with:
node-version: 12

- name: Cache dependencies
id: yarn-cache
uses: actions/cache@v2
with:
path: '**/node_modules'
key: ${{ runner.os }}-node-modules-${{ hashFiles('**/yarn.lock') }}

run: yarn install --frozen-lockfile

- name: check for outdated files
run: yarn check-for-outdated-translations
87 changes: 87 additions & 0 deletions scripts/actions/check-for-outdated-translations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
const fs = require('fs');
const path = require('path');
const fetch = require('node-fetch');
const frontmatter = require('@github-docs/frontmatter');

const checkArgs = require('./utils/check-args');
const { prop } = require('../utils/functional');
const { ADDITIONAL_LOCALES } = require('../utils/constants');
// console.log(
// `ACTION NEEDED: Translation without english version found ${filePath.replace(
// `${process.cwd()}/`,
// ''
// )}`
//
const checkForI18nFiles = (fileName, locales) => {
const i18nPrefix = path.join(process.cwd(), 'src/i18n/content');
const baseFileName = fileName.replace('src/content/', '');

return locales
.map((locale) => {
const filePath = path.join(i18nPrefix, locale, baseFileName);
const fileExists = fs.existsSync(filePath);

return fileExists ? filePath : null;
})
.filter(Boolean);
};

/**
* @param {string} url The API url that is used to fetch files.
*/
const checkOutdatedTranslations = async (url) => {
try {
const resp = await fetch(url);
const files = await resp.json();

const mdxFiles = files
? files.filter((file) => path.extname(file.filename) === '.mdx')
: [];

const mdxFilesContent = mdxFiles
.filter((file) => file.status !== 'removed')
.reduce((files, file) => {
const contents = fs.readFileSync(
path.join(process.cwd(), file.filename)
);
const { data } = frontmatter(contents);
return [...files, { ...file, locales: data.translate || [] }];
}, []);

const removedMdxFileNames = mdxFiles
.filter((f) => f.status === 'removed')
.map(prop('filename'));

// TODO: aggregate all files that need to be removed, print output, exit 1 or 0

// if a locale was removed from the translate frontmatter, we want to remove the translated version of that file.
mdxFilesContent.forEach((file) => {
const removedLocales = ADDITIONAL_LOCALES.filter(
(l) => !file.locales.includes(l)
);
if (removedLocales.length > 0) {
checkForI18nFiles(file, removedLocales);
}
});

removedMdxFileNames.forEach((name) =>
checkForI18nFiles(name, ADDITIONAL_LOCALES)
);
} catch (error) {
console.log(`[!] Unable to check for outdated translated files`);
console.log(error);
process.exit(1);
}
};

/** Entrypoint. */
const main = async () => {
checkArgs(3);
const url = process.argv[2];

await checkOutdatedTranslations(url);

process.exit(0);
};

main();
4 changes: 2 additions & 2 deletions scripts/utils/constants.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const path = require('path');

module.exports = {
CONTENT_DIR: 'src/content',
NAV_DIR: 'src/nav',
Expand All @@ -11,6 +9,8 @@ module.exports = {
DATA_DIR: 'src/data',
JP_DIR: 'src/i18n/content/jp',

ADDITIONAL_LOCALES: ['jp'],

INSTRUCTIONS: {
ADD: 'ADD',
MOVE: 'MOVE',
Expand Down

0 comments on commit 1e88761

Please sign in to comment.