Skip to content

Commit

Permalink
chore: initial work for pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
aswanson-nr committed Jun 15, 2021
1 parent 1e88761 commit 02f05b7
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 16 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/validate-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,9 @@ jobs:
path: '**/node_modules'
key: ${{ runner.os }}-node-modules-${{ hashFiles('**/yarn.lock') }}

- name: Install dependencies
if: steps.yarn-cache.outputs.cache-hit != 'true'
run: yarn install --frozen-lockfile

- name: check for outdated files
- name: check for outdated localized files
run: yarn check-for-outdated-translations
67 changes: 52 additions & 15 deletions scripts/actions/check-for-outdated-translations.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ const { ADDITIONAL_LOCALES } = require('../utils/constants');
// ''
// )}`
//
const checkForI18nFiles = (fileName, locales) => {
//

const doI18nFilesExist = (fileName, locales) => {
const i18nPrefix = path.join(process.cwd(), 'src/i18n/content');
const baseFileName = fileName.replace('src/content/', '');

Expand All @@ -26,11 +28,34 @@ const checkForI18nFiles = (fileName, locales) => {
.filter(Boolean);
};

const fetchFilesFromGH = (url) => {
const files = [];
let nextLink = url;

while (nextLink) {
const resp = await fetch(nextLink);
const chunk = await resp.json();
files = [...files, ...chunk];
nextLink = parseLinkHeader(resp.headers.get('Link'));
}

return files;
}

const parseLink = (entry) => {

};

const parseLinkHeader = (linkHeader) => {
const links = link.split(',');
const links2 = links.map(l => l.split(';'));
}
/**
* @param {string} url The API url that is used to fetch files.
*/
const checkOutdatedTranslations = async (url) => {
try {
// TODO: DEAL WITH PAGINATION
const resp = await fetch(url);
const files = await resp.json();

Expand All @@ -45,28 +70,40 @@ const checkOutdatedTranslations = async (url) => {
path.join(process.cwd(), file.filename)
);
const { data } = frontmatter(contents);
return [...files, { ...file, locales: data.translate || [] }];
return [
...files,
{ path: file.filename, 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)

const modifiedFiles = mdxFilesContent
.map((file) => {
const unsetLocales = ADDITIONAL_LOCALES.filter(
(l) => !file.locales.includes(l)
);
return doI18nFilesExist(file.path, unsetLocales);
})
.flat();

const removedFiles = removedMdxFileNames
.map((name) => doI18nFilesExist(name, ADDITIONAL_LOCALES))
.flat();

const orphanedI18nFiles = [...modifiedFiles, ...removedFiles];

if (orphanedI18nFiles.length > 0) {
orphanedI18nFiles.forEach((f) =>
// TODO: improve output
console.log(`${f.replace(`${process.cwd()}/`, '')}`)
);
if (removedLocales.length > 0) {
checkForI18nFiles(file, removedLocales);
}
});

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

0 comments on commit 02f05b7

Please sign in to comment.