-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetch-mdx-files.mjs
64 lines (54 loc) · 1.65 KB
/
fetch-mdx-files.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import fetch from "node-fetch";
import fs from "fs";
import path from "path";
import dotenv from "dotenv";
function loadEnvConfig() {
const envFiles = [
".env",
".env.local",
".env.development",
".env.production",
];
envFiles.forEach((file) => {
const filePath = path.resolve(file);
if (fs.existsSync(filePath)) {
dotenv.config({ path: filePath });
}
});
// Load environment-specific file based on NODE_ENV if exists
if (process.env.NODE_ENV) {
const envSpecificFile = `.env.${process.env.NODE_ENV}`;
const envSpecificPath = path.resolve(envSpecificFile);
if (fs.existsSync(envSpecificPath)) {
dotenv.config({ path: envSpecificPath });
}
}
}
// Load available environment config
loadEnvConfig();
const REPO_URL = `https://api.github.com/repos/${process.env.GITHUB_USERNAME}/${process.env.GITHUB_PROJECTS_REPO}/contents/projects`;
const OUTPUT_DIR = path.resolve("temp-mdx-files");
async function fetchMdxFiles() {
const response = await fetch(REPO_URL, {
method: "GET",
headers: {
Authorization: `token ${process.env.GITHUB_TOKEN}`,
Accept: "application/vnd.github.v3+json",
},
});
if (!response.ok) {
throw new Error(`Failed to fetch .mdx files: ${response.statusText}`);
}
const files = await response.json();
if (!fs.existsSync(OUTPUT_DIR)) {
fs.mkdirSync(OUTPUT_DIR);
}
for (const file of files) {
if (file.name.endsWith(".mdx")) {
const fileResponse = await fetch(file.download_url);
const content = await fileResponse.text();
fs.writeFileSync(path.join(OUTPUT_DIR, file.name), content);
}
}
}
fetchMdxFiles().catch(console.error);