forked from gridsome/gridsome.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gridsome.server.js
98 lines (84 loc) · 2.6 KB
/
gridsome.server.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
const path = require('path')
const fs = require('fs-extra')
const execa = require('execa')
const yaml = require('js-yaml')
module.exports = function (api) {
api.loadSource(async store => {
let gridsomeVersion = ''
try {
const { stdout } = await execa('npm', ['show', 'gridsome', 'version'])
gridsomeVersion = stdout
} catch (err) {
console.warn('Failed to get gridsome version from npm.')
}
store.addMetaData('gridsomeVersion', gridsomeVersion)
// Fake plugin node. TODO: Will be replaced with client side routes
store
.addContentType({
typeName: 'Plugin',
route: '/plugins/:id*'
})
.addNode({ id: '1' })
// contributors
const authorsPath = path.join(__dirname, 'contributors/contributors.yaml')
const authorsRaw = await fs.readFile(authorsPath, 'utf8')
const authorsJson = yaml.safeLoad(authorsRaw)
const authors = store.addContentType({
typeName: 'Contributor',
route: '/contributor/:id'
})
authorsJson.forEach(({ id, name: title, ...fields }) => {
authors.addNode({
id,
title,
fields,
internal: {
origin: authorsPath
}
})
})
// Starters
const startersPath = path.join(__dirname, 'starters/starters.yaml')
const startersRaw = await fs.readFile(startersPath, 'utf8')
const startersJson = yaml.safeLoad(startersRaw)
const starters = store.addContentType({
typeName: 'Starter',
route: '/starters/:title'
})
// Connect author field to Contributors & Platforms
starters.addReference('author', 'Contributor')
starters.addReference('platforms', 'Platform')
startersJson.forEach((starter, index) => {
starters.addNode({
...starter,
index,
internal: {
origin: startersPath
}
})
})
// Platforms
const platformsPath = path.join(__dirname, 'platforms/platforms.yaml')
const platformsRaw = await fs.readFile(platformsPath, 'utf8')
const platformsJson = yaml.safeLoad(platformsRaw)
const platforms = store.addContentType({
typeName: 'Platform',
route: '/starters/platform/:id'
})
// Connect author field to Contributors
platformsJson.forEach((platform, index) => {
platforms.addNode({
...platform,
index,
internal: {
origin: platformsPath
}
})
})
})
api.afterBuild(async ({ config }) => {
const from = path.join(config.outDir, 'plugins/1/index.html')
const to = path.join(config.outDir, 'plugins/index.html')
await fs.copy(from, to)
})
}