Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enhance(build/spas): allow yarn dev without internet if DEV_MODE is enabled #10533

Merged
merged 4 commits into from
Mar 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 59 additions & 28 deletions build/spas.ts
Original file line number Diff line number Diff line change
@@ -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<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
@@ -32,3 +32,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
@@ -173,3 +173,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"));