forked from thgh/wdcommunity-website
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgridsome.server.js
157 lines (142 loc) · 5.53 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Server API makes it possible to hook into various parts of Gridsome
// on server-side and add custom data to the GraphQL data layer.
// Learn more: https://gridsome.org/docs/server-api/
// Changes here require a server restart.
// To restart press CTRL + C in terminal and run `gridsome develop`
const axios = require('axios')
module.exports = function (api) {
api.loadSource(async (actions) => {
// Fetch data from APIs
// see https://bintray.com/docs/api/#_packages
const repoUrl = 'https://api.bintray.com/repos/tfl/wdpksrc'
const getPackages = async (start_pos = 0) => {
const query = `${repoUrl}/packages?start_pos=${start_pos}`
const response = await axios.get(query)
const data = response.data
if (data.length === 50) {
return data.concat(await getPackages(start_pos + 50))
} else {
return data
}
}
//const { data } = await getPackages()
const {data} = await axios.get(`${repoUrl}/packages`)
// This only has the package names, so now we fetch more info about each package
const packageUrl = 'https://api.bintray.com/packages/tfl/wdpksrc'
//let versions = await Promise.all(
// data.map(pkg => axios.get(`${packageUrl}/${pkg.name}/versions/_latest`).then(resp => resp.data))
//)
let packageData = await Promise.all(
data.map(pkg => axios.get(`${packageUrl}/${pkg.name}`).then(resp => resp.data))
)
//console.log(packageData[0])
let fileData = await Promise.all(
packageData
.filter(pkg => pkg.latest_version)
.map(pkg => axios.get(`${packageUrl}/${pkg.name}/versions/${pkg.latest_version}/files`)
.then(resp => resp.data))
)
//console.log(fileData[0])
// Create a new GraphQL Collection
const packages = actions.addCollection('BtPackage')
// Add data to the new collection
for (const pkg of packageData) {
packages.addNode({
id: pkg.name.toLowerCase(),
name: pkg.name,
repo: pkg.repo,
owner: pkg.owner,
desc: pkg.desc,
labels: pkg.labels,
licenses: pkg.licenses,
created: pkg.created,
website_url: pkg.website_url,
issue_tracker_url: pkg.issue_tracker_url,
linked_to_repos: pkg.linked_to_repos,
permissions: pkg.permissions,
latest_version: pkg.latest_version,
updated: pkg.updated,
rating_count: pkg.rating_count,
system_ids: pkg.system_ids,
vcs_url: pkg.vcs_url,
maturity: pkg.maturity,
icon: "" ,
})
}
const files = actions.addCollection('BtFile')
files.addReference('package', 'BtPackage')
// Add file data to the file collection
for (const versionFiles of fileData) {
versionFiles.map(file => files.addNode({
id: file.name,
name: file.name,
file_path: file.path,
platform: file.name.split('_').slice(-1)[0].split('.')[0],
repo: file.repo,
pkg: file.package,
version: file.version,
owner: file.owner,
created: file.created,
size: file.size,
sha1: file.sha1,
sha256: file.sha256
}))
}
console.info("Done loading Bintray data")
})
api.createPages(async ({graphql, createPage}) => {
const {data} = await graphql(`{
packages: allBtPackage {
edges {
node {
id
name
desc
latest_version
}
}
}
# (filter: {pkg: $id})
files: allBtFile {
edges{
node {
name
file_path
pkg
platform
}
}
}
}`)
data.packages.edges.forEach(({node}) => {
createPage({
path: `/pkg/${node.id}`,
component: './src/templates/Package.vue',
context: {
id: node.id,
name: node.name,
desc: node.desc,
latest_version: node.latest_version,
files: data.files.edges.map(edge => edge.node).filter(f =>
f.pkg === node.name
),
base_url: "https://dl.bintray.com/tfl/wdpksrc/",
}
})
})
//console.info(data.files.edges.map(edge => edge.node).filter(node => node.pkg === "Docker"))
})
api.createManagedPages(async ({createPage}) => {
// const {data} = await axios.get('https://api.example.com/posts')
// data.forEach(item => {
// createPage({
// path: `/pkg/${item.slug}`,
// component: './src/templates/Package.vue',
// context: {
// name: item.name,
// content: item.content
// }
// })
// })
})
}