From d4fb1ed91ffe38402e4e722e91407755b6740189 Mon Sep 17 00:00:00 2001 From: Riley Brown <38029101+Riley-Brown@users.noreply.github.com> Date: Wed, 6 Sep 2023 08:36:59 -0400 Subject: [PATCH] fix(tools): use download-trending when offline (#51286) --- client/tools/download-trending.ts | 62 ++++++++++++++++++++++++------- 1 file changed, 48 insertions(+), 14 deletions(-) diff --git a/client/tools/download-trending.ts b/client/tools/download-trending.ts index 77b5709229c..26ae8bf3d7c 100644 --- a/client/tools/download-trending.ts +++ b/client/tools/download-trending.ts @@ -1,4 +1,4 @@ -import { writeFileSync } from 'fs'; +import { readFileSync, writeFileSync } from 'fs'; import path from 'path'; import fetch from 'node-fetch'; @@ -14,24 +14,58 @@ const createCdnUrl = (lang: string) => const download = async (clientLocale: string) => { const url = createCdnUrl(clientLocale); - const res = await fetch(url); - if (!res.ok) { - throw new Error( - ` - ---------------------------------------------------- - Error: The CDN is missing the trending YAML file. - ---------------------------------------------------- - Unable to fetch the ${clientLocale} footer: ${res.statusText} - ` - ); - } - const data = await res.text(); - const trendingJSON = JSON.stringify(yaml.load(data)); const trendingLocation = path.resolve( __dirname, `../i18n/locales/${clientLocale}/trending.json` ); + + const loadLocalTrendingJSON = () => { + const localTrendingJSON = readFileSync(trendingLocation, 'utf8'); + + if (!localTrendingJSON) { + throw new Error( + ` + ---------------------------------------------------- + Error: ${trendingLocation} is missing. + ---------------------------------------------------- + ` + ); + } + + return localTrendingJSON; + }; + + const loadTrendingJSON = async () => { + try { + const res = await fetch(url); + + if (!res.ok) { + throw new Error( + ` + ---------------------------------------------------- + Error: The CDN is missing the trending YAML file. + ---------------------------------------------------- + Unable to fetch the ${clientLocale} footer: ${res.statusText} + ` + ); + } + + const data = await res.text(); + const trendingJSON = JSON.stringify(yaml.load(data)); + + return trendingJSON; + } catch (error) { + if (process.env.FREECODECAMP_NODE_ENV === 'production') { + throw new Error((error as Error).message); + } + + return loadLocalTrendingJSON(); + } + }; + + const trendingJSON = await loadTrendingJSON(); + writeFileSync(trendingLocation, trendingJSON); const trendingObject = JSON.parse(trendingJSON) as Record;