forked from cerebral/overmind
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
113 lines (99 loc) · 3.04 KB
/
index.js
File metadata and controls
113 lines (99 loc) · 3.04 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
const path = require('path')
const express = require('express')
const app = express()
const fs = require('fs')
const IS_PRODUCTION = process.env.NODE_ENV === 'production'
function getGuides() {
return fs.readdirSync(path.resolve('guides')).reduce((acc, type) => {
return acc.concat(
fs.readdirSync(path.resolve('guides', type)).reduce((acc, fileName) => {
const content = fs
.readFileSync(path.resolve('guides', type, fileName))
.toString()
return acc.concat({
title: content.split('\n')[0].replace('# ', ''),
type,
fileName,
})
}, [])
)
}, [])
}
function getVideos() {
return fs.readFileSync(path.resolve('videos.json')).toString()
}
function getApis() {
return fs.readdirSync(path.resolve('api')).reduce((acc, fileName) => {
const content = fs.readFileSync(path.resolve('api', fileName)).toString()
return acc.concat({
title: content.split('\n')[0].replace('# ', ''),
fileName,
})
}, [])
}
function getSearchData() {
return fs
.readdirSync(path.resolve('guides'))
.reduce((acc, type) => {
return acc.concat(
fs.readdirSync(path.resolve('guides', type)).reduce((acc, fileName) => {
const content = fs
.readFileSync(path.resolve('guides', type, fileName))
.toString()
return acc.concat({
type: 'guide',
title: content.split('\n')[0].replace('# ', ''),
content: content.toLowerCase(),
fileName,
})
}, [])
)
}, [])
.concat(
fs.readdirSync(path.resolve('api')).reduce((acc, fileName) => {
const content = fs
.readFileSync(path.resolve('api', fileName))
.toString()
return acc.concat({
type: 'api',
title: content.split('\n')[0].replace('# ', ''),
content: content.toLowerCase(),
fileName,
})
}, [])
)
}
const guides = getGuides()
const videos = getVideos()
const apis = getApis()
const searchData = getSearchData()
app.use(express.static('dist'))
app.use('/images', express.static('images'))
app.get('/backend/guides', (_, res) =>
res.send(IS_PRODUCTION ? guides : getGuides())
)
app.get('/backend/videos', (_, res) =>
res.send(IS_PRODUCTION ? videos : getVideos())
)
app.get('/backend/apis', (_, res) => res.send(IS_PRODUCTION ? apis : getApis()))
app.get('/backend/search', (req, res) => {
const query = req.query.query.toLowerCase()
const regexp = new RegExp(query, 'g')
const hits = searchData
.reduce((acc, data) => {
const matches = data.content.match(regexp)
if (matches) {
return acc.concat({
count: matches.length,
title: data.title,
type: data.type,
fileName: data.fileName,
})
}
return acc
}, [])
.sort((a, b) => a.count - b.count)
res.send(hits.slice(0, 5))
})
app.get('/*', (_, res) => res.sendFile(path.resolve('dist', 'index.html')))
app.listen(5000, () => console.log('Example app listening on port 5000!'))