Skip to content

Commit

Permalink
chore: Add pagination logic
Browse files Browse the repository at this point in the history
  • Loading branch information
LizBaker committed Jun 15, 2021
1 parent 4f9b064 commit 2e5a2ea
Showing 1 changed file with 27 additions and 30 deletions.
57 changes: 27 additions & 30 deletions scripts/actions/check-for-outdated-translations.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,11 @@ const fs = require('fs');
const path = require('path');
const fetch = require('node-fetch');
const frontmatter = require('@github-docs/frontmatter');
const parseLinkHeader = require('parse-link-header');

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 doI18nFilesExist = (fileName, locales) => {
const i18nPrefix = path.join(process.cwd(), 'src/i18n/content');
Expand All @@ -22,43 +16,41 @@ const doI18nFilesExist = (fileName, locales) => {
.map((locale) => {
const filePath = path.join(i18nPrefix, locale, baseFileName);
const fileExists = fs.existsSync(filePath);

return fileExists ? filePath : null;
})
.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'));
const fetchFilesFromGH = async (url) => {
let files = [];
let nextPageLink = url;

while (nextPageLink) {
const resp = await fetch(nextPageLink, {
headers: { authorization: `token ${process.env.GITHUB_TOKEN}` },
});
const page = await resp.json();
nextPageLink = getNextLink(resp.headers.get('Link'));
files = [...files, ...page];
}

return files;
}

const parseLink = (entry) => {
return files;
};

const getNextLink = (linkHeader) => {
const parsedLinkHeader = parseLinkHeader(linkHeader);
if (parsedLinkHeader && parsedLinkHeader.next) {
return parsedLinkHeader.next.url || null;
}
return null;
};

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();

const files = await fetchFilesFromGH(url);
const mdxFiles = files
? files.filter((file) => path.extname(file.filename) === '.mdx')
: [];
Expand Down Expand Up @@ -100,7 +92,12 @@ const checkOutdatedTranslations = async (url) => {
if (orphanedI18nFiles.length > 0) {
orphanedI18nFiles.forEach((f) =>
// TODO: improve output
console.log(`${f.replace(`${process.cwd()}/`, '')}`)
console.log(
`ACTION NEEDED: Translation without english version found-- ${f.replace(
`${process.cwd()}/`,
''
)}`
)
);
process.exit(1);
}
Expand Down

0 comments on commit 2e5a2ea

Please sign in to comment.