Skip to content

Commit 78a9500

Browse files
authored
enhance(build/spas): allow yarn dev without internet if DEV_MODE is enabled (#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
1 parent c207fcc commit 78a9500

File tree

3 files changed

+72
-28
lines changed

3 files changed

+72
-28
lines changed

Diff for: build/spas.ts

+59-28
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
CONTENT_TRANSLATED_ROOT,
2020
CONTRIBUTOR_SPOTLIGHT_ROOT,
2121
BUILD_OUT_ROOT,
22+
DEV_MODE,
2223
} from "../libs/env/index.js";
2324
import { isValidLocale } from "../libs/locale-utils/index.js";
2425
import { DocFrontmatter, NewsItem } from "../libs/types/document.js";
@@ -368,14 +369,25 @@ async function fetchGitHubPRs(repo, count = 5) {
368369
"sort:updated",
369370
].join("+");
370371
const pullRequestUrl = `https://api.github.com/search/issues?q=${pullRequestsQuery}&per_page=${count}`;
371-
const pullRequestsData = (await got(pullRequestUrl).json()) as {
372-
items: any[];
373-
};
374-
const prDataRepo = pullRequestsData.items.map((item) => ({
375-
...item,
376-
repo: { name: repo, url: `https://github.com/${repo}` },
377-
}));
378-
return prDataRepo;
372+
try {
373+
const pullRequestsData = (await got(pullRequestUrl).json()) as {
374+
items: any[];
375+
};
376+
const prDataRepo = pullRequestsData.items.map((item) => ({
377+
...item,
378+
repo: { name: repo, url: `https://github.com/${repo}` },
379+
}));
380+
return prDataRepo;
381+
} catch (e) {
382+
const msg = `Couldn't fetch recent GitHub contributions for repo ${repo}!`;
383+
if (!DEV_MODE) {
384+
console.error(`Error: ${msg}`);
385+
throw e;
386+
}
387+
388+
console.warn(`Warning: ${msg}`);
389+
return [];
390+
}
379391
}
380392

381393
async function fetchRecentContributions() {
@@ -403,10 +415,6 @@ async function fetchRecentContributions() {
403415
}
404416

405417
async function fetchLatestNews() {
406-
const xml = await got("https://hacks.mozilla.org/category/mdn/feed/").text();
407-
408-
const $ = cheerio.load(xml, { xmlMode: true });
409-
410418
const items: NewsItem[] = [];
411419

412420
items.push(
@@ -449,25 +457,48 @@ async function fetchLatestNews() {
449457
name: "developer.mozilla.org",
450458
url: `/${DEFAULT_LOCALE}/blog/`,
451459
},
452-
}
460+
},
461+
...(await fetchHacksNews())
453462
);
454463

455-
$("item").each((i, item) => {
456-
const $item = $(item);
457-
458-
items.push({
459-
title: $item.find("title").text(),
460-
url: $item.find("guid").text(),
461-
author: $item.find("dc\\:creator").text(),
462-
published_at: $item.find("pubDate").text(),
463-
source: {
464-
name: "hacks.mozilla.org",
465-
url: "https://hacks.mozilla.org/category/mdn/",
466-
},
467-
});
468-
});
469-
470464
return {
471465
items,
472466
};
473467
}
468+
469+
async function fetchHacksNews(): Promise<NewsItem[]> {
470+
try {
471+
const xml = await got(
472+
"https://hacks.mozilla.org/category/mdn/feed/"
473+
).text();
474+
475+
const $ = cheerio.load(xml, { xmlMode: true });
476+
477+
const items: NewsItem[] = [];
478+
$("item").each((i, item) => {
479+
const $item = $(item);
480+
481+
items.push({
482+
title: $item.find("title").text(),
483+
url: $item.find("guid").text(),
484+
author: $item.find("dc\\:creator").text(),
485+
published_at: $item.find("pubDate").text(),
486+
source: {
487+
name: "hacks.mozilla.org",
488+
url: "https://hacks.mozilla.org/category/mdn/",
489+
},
490+
});
491+
});
492+
493+
return items;
494+
} catch (e) {
495+
const msg = "Couldn't fetch hacks.mozilla.org feed!";
496+
if (!DEV_MODE) {
497+
console.error(`Error: ${msg}`);
498+
throw e;
499+
}
500+
501+
console.warn(`Warning: ${msg}`);
502+
return [];
503+
}
504+
}

Diff for: libs/env/index.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,4 @@ export const SENTRY_DSN_BUILD: string;
3333
export const OPENAI_KEY: string;
3434
export const PG_URI: string;
3535
export const SAMPLE_SIGN_KEY: Buffer;
36+
export const DEV_MODE: boolean;

Diff for: libs/env/index.js

+12
Original file line numberDiff line numberDiff line change
@@ -183,3 +183,15 @@ export const PG_URI = process.env.PG_URI || "";
183183
export const SAMPLE_SIGN_KEY = process.env.BUILD_SAMPLE_SIGN_KEY
184184
? Buffer.from(process.env.BUILD_SAMPLE_SIGN_KEY, "base64")
185185
: null;
186+
187+
const CRUD_MODE =
188+
process.env.REACT_APP_WRITER_MODE || process.env.REACT_APP_DEV_MODE
189+
? false
190+
: Boolean(
191+
JSON.parse(
192+
process.env.REACT_APP_CRUD_MODE ||
193+
JSON.stringify(process.env.NODE_ENV === "development")
194+
)
195+
);
196+
export const DEV_MODE =
197+
CRUD_MODE || Boolean(JSON.parse(process.env.REACT_APP_DEV_MODE || "false"));

0 commit comments

Comments
 (0)