-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetchBlogSource.js
More file actions
88 lines (79 loc) · 2.45 KB
/
Copy pathfetchBlogSource.js
File metadata and controls
88 lines (79 loc) · 2.45 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import fs from "fs"
import path from "path"
import stream from "stream"
import url from "url"
import util from "util"
const __filename = url.fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
const asyncPipeline = util.promisify(stream.pipeline)
const CONFIG = {
posts: {
url: `${process.env.BLOG_SRC_URL}/posts`,
dirname: "/src/content/blog",
isPost: true,
},
images: {
url: `${process.env.BLOG_SRC_URL}/assets/images`,
dirname: "/public/images/blog",
},
}
/** @param {string} url */
async function fetchFromGitHub(url) {
try {
const res = await fetch(url, {
headers: { Authorization: `token ${process.env.GITHUB_TOKEN}` },
})
if (res.ok) return res
throw new Error(`Failed to fetch from ${url} with status ${res.status}`)
} catch (e) {
console.error("fetchFromGitHub", e)
throw e
}
}
/**
* @param {Response} res
* @param {string} dirPath
* @param {string} filename
*/
async function writeBlogPost(res, dirPath, filename) {
const [date, updatedFilename] = filename.split("_")
const filePath = path.join(dirPath, updatedFilename)
const pubDate = `${date.slice(0, 4)}-${date.slice(4, 6)}-${date.slice(6)}`
try {
const text = await res.text()
const [firstLine, ...lines] = text.split("\n")
const pubDateLine = `pubDate: "${pubDate}Z-06:00"` // use central time zone
const updatedText = [firstLine, pubDateLine, ...lines].join("\n")
fs.promises.writeFile(filePath, updatedText)
} catch (e) {
console.error("writeBlogPost", e)
}
}
/**
* @param {Response} res
* @param {string} dirPath
* @param {string} filename
*/
async function writeBlogFile(res, dirPath, filename) {
const filePath = path.join(dirPath, filename)
asyncPipeline(res.body, fs.createWriteStream(filePath))
}
/** @param {{ url: string, dirname: string }} config */
async function fetchBlogSource(config) {
const dirPath = path.join(__dirname, config.dirname)
if (!fs.existsSync(dirPath)) fs.mkdirSync(dirPath, { recursive: true })
try {
const res = await fetchFromGitHub(config.url)
const files = await res.json()
for (const file of files) {
const res = await fetchFromGitHub(file.download_url)
if (config.isPost) writeBlogPost(res, dirPath, file.name)
else writeBlogFile(res, dirPath, file.name)
}
} catch (e) {
console.error("fetchBlogSource", e)
process.exit(1)
}
}
console.log("Fetch blog source...")
Object.values(CONFIG).map(fetchBlogSource)