diff --git a/build/spas.ts b/build/spas.ts index b389e2bd1662..f64c294c2ed6 100644 --- a/build/spas.ts +++ b/build/spas.ts @@ -19,6 +19,7 @@ import { CONTENT_TRANSLATED_ROOT, CONTRIBUTOR_SPOTLIGHT_ROOT, BUILD_OUT_ROOT, + DEV_MODE, } from "../libs/env/index.js"; import { isValidLocale } from "../libs/locale-utils/index.js"; import { DocFrontmatter, NewsItem } from "../libs/types/document.js"; @@ -368,14 +369,25 @@ async function fetchGitHubPRs(repo, count = 5) { "sort:updated", ].join("+"); const pullRequestUrl = `https://api.github.com/search/issues?q=${pullRequestsQuery}&per_page=${count}`; - const pullRequestsData = (await got(pullRequestUrl).json()) as { - items: any[]; - }; - const prDataRepo = pullRequestsData.items.map((item) => ({ - ...item, - repo: { name: repo, url: `https://github.com/${repo}` }, - })); - return prDataRepo; + try { + const pullRequestsData = (await got(pullRequestUrl).json()) as { + items: any[]; + }; + const prDataRepo = pullRequestsData.items.map((item) => ({ + ...item, + repo: { name: repo, url: `https://github.com/${repo}` }, + })); + return prDataRepo; + } catch (e) { + const msg = `Couldn't fetch recent GitHub contributions for repo ${repo}!`; + if (!DEV_MODE) { + console.error(`Error: ${msg}`); + throw e; + } + + console.warn(`Warning: ${msg}`); + return []; + } } async function fetchRecentContributions() { @@ -403,10 +415,6 @@ async function fetchRecentContributions() { } async function fetchLatestNews() { - const xml = await got("https://hacks.mozilla.org/category/mdn/feed/").text(); - - const $ = cheerio.load(xml, { xmlMode: true }); - const items: NewsItem[] = []; items.push( @@ -449,25 +457,48 @@ async function fetchLatestNews() { name: "developer.mozilla.org", url: `/${DEFAULT_LOCALE}/blog/`, }, - } + }, + ...(await fetchHacksNews()) ); - $("item").each((i, item) => { - const $item = $(item); - - items.push({ - title: $item.find("title").text(), - url: $item.find("guid").text(), - author: $item.find("dc\\:creator").text(), - published_at: $item.find("pubDate").text(), - source: { - name: "hacks.mozilla.org", - url: "https://hacks.mozilla.org/category/mdn/", - }, - }); - }); - return { items, }; } + +async function fetchHacksNews(): Promise { + try { + const xml = await got( + "https://hacks.mozilla.org/category/mdn/feed/" + ).text(); + + const $ = cheerio.load(xml, { xmlMode: true }); + + const items: NewsItem[] = []; + $("item").each((i, item) => { + const $item = $(item); + + items.push({ + title: $item.find("title").text(), + url: $item.find("guid").text(), + author: $item.find("dc\\:creator").text(), + published_at: $item.find("pubDate").text(), + source: { + name: "hacks.mozilla.org", + url: "https://hacks.mozilla.org/category/mdn/", + }, + }); + }); + + return items; + } catch (e) { + const msg = "Couldn't fetch hacks.mozilla.org feed!"; + if (!DEV_MODE) { + console.error(`Error: ${msg}`); + throw e; + } + + console.warn(`Warning: ${msg}`); + return []; + } +} diff --git a/libs/env/index.d.ts b/libs/env/index.d.ts index 100b39e80c7f..df2c2dfc9a66 100644 --- a/libs/env/index.d.ts +++ b/libs/env/index.d.ts @@ -33,3 +33,4 @@ export const SENTRY_DSN_BUILD: string; export const OPENAI_KEY: string; export const PG_URI: string; export const SAMPLE_SIGN_KEY: Buffer; +export const DEV_MODE: boolean; diff --git a/libs/env/index.js b/libs/env/index.js index 09751f77d069..de2e3cbc275c 100644 --- a/libs/env/index.js +++ b/libs/env/index.js @@ -183,3 +183,15 @@ export const PG_URI = process.env.PG_URI || ""; export const SAMPLE_SIGN_KEY = process.env.BUILD_SAMPLE_SIGN_KEY ? Buffer.from(process.env.BUILD_SAMPLE_SIGN_KEY, "base64") : null; + +const CRUD_MODE = + process.env.REACT_APP_WRITER_MODE || process.env.REACT_APP_DEV_MODE + ? false + : Boolean( + JSON.parse( + process.env.REACT_APP_CRUD_MODE || + JSON.stringify(process.env.NODE_ENV === "development") + ) + ); +export const DEV_MODE = + CRUD_MODE || Boolean(JSON.parse(process.env.REACT_APP_DEV_MODE || "false"));