-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathgatsby-node.js
More file actions
113 lines (111 loc) · 2.93 KB
/
Copy pathgatsby-node.js
File metadata and controls
113 lines (111 loc) · 2.93 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 fs = require("fs")
const yaml = require("js-yaml")
exports.createPages = async ({ actions, graphql }) => {
const { createPage } = actions
const { data } = await graphql(`
query HomePageQuery {
apps: allFile(
filter: {
sourceInstanceName: { eq: "sample-apps" }
extension: { in: ["yml", "yaml"] }
relativeDirectory: { regex: "/apps/" }
}
sort: { fields: modifiedTime, order: DESC }
) {
edges {
node {
extension
dir
name
modifiedTime
birthTime
}
}
}
pics: allFile(
filter: {
sourceInstanceName: { eq: "sample-apps" }
extension: { in: ["png", "PNG", "jpg", "jpeg", "JPG", "JPEG"] }
relativeDirectory: { regex: "/logos/" }
}
) {
edges {
node {
extension
name
dir
modifiedTime
publicURL
}
}
}
}
`)
const apps = {}
data.apps.edges.forEach(edge => {
const ymlDoc = yaml.load(
fs.readFileSync(
`${edge.node.dir}/${edge.node.name}.${edge.node.extension}`,
"utf-8"
)
)
if (ymlDoc.caproverOneClickApp.displayName === "") {
ymlDoc.caproverOneClickApp.displayName = edge.node.name
}
apps[edge.node.name] = {
// edge,
data: ymlDoc,
modified: edge.node.modifiedTime,
created: edge.node.birthTime,
slug: edge.node.name,
friendlyName: ymlDoc.caproverOneClickApp.displayName,
}
})
data.pics.edges.forEach(edge => {
apps[edge.node.name] = {
...apps[edge.node.name],
cover: edge.node.publicURL,
}
})
const appsArray = Object.keys(apps).map(app => apps[app])
createPage({
path: "/",
component: require.resolve("./src/templates/index.tsx"),
context: {
apps: appsArray,
},
})
function escapeRegExp(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&") // $& means the whole matched string
}
function replaceAll(str, match, replacement) {
return str.replace(new RegExp(escapeRegExp(match), "g"), () => replacement)
}
appsArray.forEach(element => {
createPage({
path: `/app/${element.slug}`,
component: require.resolve("./src/templates/app.tsx"),
context: {
appData: JSON.parse(
replaceAll(
replaceAll(JSON.stringify(element), "$$cap_appname", element.slug),
"$$cap_root_domain",
"captain.yourdomain.com"
)
),
similar: appsArray
.filter(
current =>
current.slug !== element.slug &&
(current.slug.startsWith(element.slug) ||
element.slug.startsWith(current.slug))
)
.map(app => ({
cover: app.cover,
name: app.friendlyName,
slug: app.slug,
})),
},
})
})
}