-
Notifications
You must be signed in to change notification settings - Fork 128
/
helper.js
executable file
·83 lines (81 loc) · 2.4 KB
/
helper.js
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
import fs from 'fs'
import rimraf from 'rimraf'
import { chunk } from 'lodash'
import matter from 'gray-matter'
import { flattenResource } from '../helper'
export function compareDates(a, b) {
const aParsed = Date.parse(a.data.date)
const bParsed = Date.parse(b.data.date)
if (aParsed < bParsed) {
return -1
}
if (aParsed > bParsed) {
return 1
}
return 0
}
export function createPagination(numPages, items, dir) {
const paginationDir = dir
if (fs.existsSync(paginationDir)) {
rimraf.sync(paginationDir) // Delete all previous pagination endpoints
}
fs.mkdirSync(paginationDir)
const paginated = chunk(items, numPages)
let currentPage = 0
for (let i = 0; i < paginated.length; i++) {
currentPage = i + 1
const chunkWriteStream = fs.createWriteStream(
`${dir}/page-${currentPage}.json`,
'UTF-8'
)
chunkWriteStream.write(JSON.stringify(paginated[i]))
}
return paginated.length
}
export function createMeta(newMeta, file) {
let meta = {}
if (fs.existsSync(file)) {
meta = require(file)
}
const combined = Object.assign(meta, newMeta)
const chunkWriteStream = fs.createWriteStream(file, 'UTF-8')
chunkWriteStream.write(JSON.stringify(combined))
}
export function createAll(fromDir, toFile, apiDir) {
// Create api dir if doesn't exist
if (!fs.existsSync(apiDir)) {
fs.mkdirSync(apiDir)
}
// Create file if doesn't exist
if (!fs.existsSync(toFile)) {
fs.writeFileSync(toFile, '{}')
}
return new Promise((resolve, reject) => {
fs.readdir(fromDir, (err, files) => {
if (err) reject(err)
const index = []
const contents = {}
files.forEach((file) => {
contents[file] = ''
const readStream = fs.createReadStream(`${fromDir}/${file}`, 'UTF-8')
readStream.on('data', (data, err) => {
if (err) throw err
contents[file] += data
})
readStream.on('end', () => {
const parsed = matter(contents[file])
delete parsed.buffer
parsed.data.slug = file.replace(/.md$/, '')
index.push(parsed)
if (index.length === files.length) {
const writeStream = fs.createWriteStream(toFile, 'UTF-8')
let sorted = index.sort(compareDates).reverse()
sorted = flattenResource(sorted)
writeStream.write(JSON.stringify(sorted))
resolve(sorted)
}
})
})
})
})
}