Skip to content

Commit

Permalink
Fix documentation link check script (rancher#12423)
Browse files Browse the repository at this point in the history
  • Loading branch information
nwmac authored Oct 30, 2024
1 parent 190f486 commit e2731eb
Showing 1 changed file with 28 additions and 3 deletions.
31 changes: 28 additions & 3 deletions scripts/check-i18n-links
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ const cyan = `\x1b[96m`;
const yellow = `\x1b[33m`;
const bold = `\x1b[1m`;

const DOCS_BASE_REGEX = /export const DOCS_BASE = '([^']*)';/;
const DOCS_BASE_REGEX = /export const DOCS_BASE = `([^']*v)\${ CURRENT_RANCHER_VERSION }`/;
const VERSION_REGEX = /export const CURRENT_RANCHER_VERSION = '([0-9]\.[0-9]+)';/;

const CATEGORIES = [
{
Expand Down Expand Up @@ -65,6 +66,21 @@ if (docsBaseFileMatches && docsBaseFileMatches.length === 2) {
docsBaseUrl = docsBaseFileMatches[1];
}

if (docsBaseUrl.length === 0) {
console.log('Could not parse documentation base URL'); // eslint-disable-line no-console
process.exit(1);
}

const versionFile = fs.readFileSync(path.join(srcFolder, 'config', 'version.js'), 'utf8');
const versionFileMatches = versionFile.match(VERSION_REGEX);

if (versionFileMatches && versionFileMatches.length === 2) {
docsBaseUrl = `${ docsBaseUrl }${ versionFileMatches[1] }`;
} else {
console.log('Could not parse version number from the version file'); // eslint-disable-line no-console
process.exit(1);
}

function readAndParseTranslations(filePath) {
const data = fs.readFileSync(filePath, 'utf8');

Expand Down Expand Up @@ -120,6 +136,8 @@ function parseLinks(str) {

if (link.startsWith('http')) {
links.push(link);
} else if (!(link.startsWith('{') && link.endsWith('}'))) {
console.log(`${ yellow }${bold}Skipping link: ${ link }${ reset }`); // eslint-disable-line no-console
}
}
});
Expand Down Expand Up @@ -162,13 +180,19 @@ console.log(`${ cyan }Checking source files for i18n strings${ reset }`); // esl
console.log('======================================'); // eslint-disable-line no-console

console.log(''); // eslint-disable-line no-console

console.log(`Using documentation base URL: ${ cyan }${ docsBaseUrl }${ reset }`); // eslint-disable-line no-console
console.log(''); // eslint-disable-line no-console


console.log('Reading translation files:'); // eslint-disable-line no-console

let i18n = loadI18nFiles(srcFolder);

i18n = { ...i18n, ...loadI18nFiles(pkgFolder) };

console.log(`Read ${ cyan }${ Object.keys(i18n).length }${ reset } translations`); // eslint-disable-line no-console
console.log(''); // eslint-disable-line no-console

const links = [];

Expand All @@ -180,6 +204,7 @@ Object.keys(i18n).forEach((str) => {
});

console.log(`Discovered ${ cyan }${ links.length }${ reset } links`); // eslint-disable-line no-console
console.log(''); // eslint-disable-line no-console

console.log(`${ cyan }Links:${ reset }`); // eslint-disable-line no-console
console.log(`${ cyan }======${ reset }`); // eslint-disable-line no-console
Expand Down Expand Up @@ -242,8 +267,8 @@ async function check(links) {
statusCode = r.status;
statusMessage = r.statusText;
} catch (e) {
statusCode = e.status;
statusMessage = e.statusText;
statusCode = e.response ? e.response.status : e.status;
statusMessage = e.response ? e.response.statusText : e.statusText;
}

if (statusCode !== 200) {
Expand Down

0 comments on commit e2731eb

Please sign in to comment.