From e2731ebb08e4f818294c314945058b25484c51ce Mon Sep 17 00:00:00 2001 From: Neil MacDougall Date: Wed, 30 Oct 2024 14:42:18 +0000 Subject: [PATCH] Fix documentation link check script (#12423) --- scripts/check-i18n-links | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/scripts/check-i18n-links b/scripts/check-i18n-links index 940b718e88e..7343092387a 100755 --- a/scripts/check-i18n-links +++ b/scripts/check-i18n-links @@ -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 = [ { @@ -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'); @@ -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 } } }); @@ -162,6 +180,11 @@ 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); @@ -169,6 +192,7 @@ 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 = []; @@ -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 @@ -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) {