Skip to content

Commit

Permalink
enhance(build/spas): allow yarn dev without internet if DEV_MODE is e…
Browse files Browse the repository at this point in the history
…nabled (#10533)

* chore(libs/env): copy DEV_MODE from /client

* enhance(build/spas): warn if GitHub fetch fails in DEV_MODE

* refactor(build/spas): extract fetchHacksNews()

* chore(build/spas): warn if Hacks fetch fails in DEV_MODE
  • Loading branch information
caugner authored Mar 1, 2024
1 parent c207fcc commit 78a9500
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 28 deletions.
87 changes: 59 additions & 28 deletions build/spas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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<NewsItem[]> {
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 [];
}
}
1 change: 1 addition & 0 deletions libs/env/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
12 changes: 12 additions & 0 deletions libs/env/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"));

0 comments on commit 78a9500

Please sign in to comment.