-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph.js
More file actions
53 lines (47 loc) · 1.3 KB
/
Copy pathgraph.js
File metadata and controls
53 lines (47 loc) · 1.3 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
/* eslint no-console: 'off' */
const path = require('path')
const fs = require('fs').promises
const fm = require('front-matter')
const PATHS = {
CONTENT: path.join(__dirname, '..', 'content'),
STATIC: path.join(__dirname, '..', 'static')
}
async function assembleGraphData(inputPath) {
const files = await fs.readdir(inputPath, { withFileTypes: true })
const filesReducer = async (accumulatorPromise, file) => {
const accumulator = await accumulatorPromise
const fileNameWithoutExtension = path.basename(
file.name,
path.extname(file.name)
)
const fileStream = await fs.readFile(
path.join(inputPath, '/', file.name),
'utf8'
)
// Turn the raw .md content into a JSON object
const content = fm(fileStream)
// assign it to the main graph object
accumulator[fileNameWithoutExtension] = content
return accumulator
}
try {
return await files.reduce(filesReducer, Promise.resolve({}))
} catch (err) {
console.error(err)
}
}
async function writeGraphFile() {
const graph = {
posts: await assembleGraphData(PATHS.CONTENT)
}
try {
await fs.writeFile(
`${PATHS.STATIC}/graph.json`,
JSON.stringify(graph, null, 4)
)
console.log('Graph file generated!')
} catch (err) {
console.error(err)
}
}
writeGraphFile()