-
Notifications
You must be signed in to change notification settings - Fork 66
/
gridsome.server.js
286 lines (262 loc) · 8.32 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
// 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 fs = require("fs");
const path = require("path");
const yaml = require("js-yaml");
const marked = require("marked");
const buildJSONApi = require("./src/api/plugins");
const dataRoot = "_data/";
const defaultVariantData = yaml.load(
fs.readFileSync(path.join(dataRoot, "default_variants.yml"))
);
const pluginMetricsData = yaml.load(
fs.readFileSync(path.join(dataRoot, "variant_metrics.yml"))
).metrics;
const readMaintainers = yaml.load(
fs.readFileSync(path.join(dataRoot, "maintainers.yml"))
);
const readAirbyteMap = yaml.load(
fs.readFileSync(path.join(dataRoot, "singer_airbyte_map.yml"))
);
const pluginTypeSingulars = {
extractors: "extractor",
loaders: "loader",
mappers: "mapper",
transforms: "transform",
transformers: "transformer",
orchestrators: "orchestrator",
utilities: "utility",
files: "file",
};
function renderDefinition(definition) {
let rendered = marked.marked(definition);
rendered = rendered.replace(/<p>|<\/p>/gi, "");
return ` ${rendered.charAt(0).toLowerCase()}${rendered.slice(1)}`;
}
function renderNextSteps(nextSteps) {
const rendered = marked.marked(nextSteps);
return rendered.replace(
/<pre>/gi,
'<pre class="prose language-bash rounded-md"'
);
}
function renderMarkdownSections(pluginData) {
return {
...pluginData,
// Common
settings:
pluginData.settings &&
pluginData.settings.map((setting) => ({
...setting,
description_rendered:
setting.description && marked.marked(setting.description),
})),
// Rare
definition_rendered: pluginData.definition
? renderDefinition(pluginData.definition)
: undefined,
settings_preamble_rendered: pluginData.settings_preamble
? marked.marked(pluginData.settings_preamble)
: undefined,
usage_rendered: pluginData.usage
? marked.marked(pluginData.usage)
: undefined,
prereq_rendered: pluginData.prereq
? marked.marked(pluginData.prereq)
: undefined,
next_steps_rendered: pluginData.next_steps
? renderNextSteps(pluginData.next_steps)
: undefined,
};
}
function addLowercaseLabel(pluginData) {
return {
...pluginData,
label_lowercase: pluginData.label
? pluginData.label.toLowerCase()
: undefined,
};
}
function addCommandNames(pluginData) {
if (!pluginData.commands) {
return;
}
Object.keys(pluginData.commands).forEach((commandName) => {
const command = pluginData.commands[commandName];
command.name = commandName;
});
}
function buildData(dataPath, collections) {
const currentCollection = dataPath;
let collectionFolder = fs.readdirSync(dataPath);
collectionFolder = collectionFolder.filter(
(item) => !/(^|\/)\.[^/.]/g.test(item)
);
collectionFolder.forEach((folder) => {
const currentFolder = folder;
const subfolderPlugins = fs.readdirSync(`${currentCollection}/${folder}`);
subfolderPlugins.forEach((plugin) => {
const fileContents = fs.readFileSync(
`${currentCollection}/${currentFolder}/${plugin}`
);
let readPlugin = yaml.load(fileContents);
readPlugin = renderMarkdownSections(addLowercaseLabel(readPlugin));
addCommandNames(readPlugin);
readPlugin.isDefault =
defaultVariantData[path.basename(dataPath)][currentFolder] ===
readPlugin.variant;
readPlugin.pluginTypePlural = path.basename(dataPath);
readPlugin.pluginType = pluginTypeSingulars[readPlugin.pluginTypePlural];
// Need to stringify default setting values due to limited gql type system
readPlugin.settings =
readPlugin.settings &&
readPlugin.settings.map((setting) => ({
...setting,
value: (function parseValue(s) {
if (s.value === undefined) {
return undefined;
}
if (typeof s.value === "string") {
return s.value;
}
return JSON.stringify(s.value);
})(setting),
}));
// Include additional fields
readPlugin.metrics = pluginMetricsData[readPlugin.repo];
readPlugin.maintainer = readMaintainers[readPlugin.variant];
readPlugin.airbyte_name =
readAirbyteMap[path.basename(dataPath)]?.[currentFolder] ||
`source-${currentFolder.split("tap-")[1]}` ||
`source-${currentFolder.split("target-")[1]}`;
// If there are commands, turn them into a more graphql-friendly array
readPlugin.commands = readPlugin.commands
? Object.keys(readPlugin.commands).map((key) => ({
...readPlugin.commands[key],
name: key,
}))
: undefined;
// Make Meltano/Singer metadata a JSON string
readPlugin.metadata = readPlugin.metadata
? JSON.stringify(readPlugin.metadata)
: undefined;
collections.forEach((collection) => {
collection.addNode(readPlugin);
});
});
});
}
function buildMaintainers(datapath, collection) {
Object.keys(datapath).forEach((key) => {
collection.addNode({
name: datapath[key].name,
label: datapath[key].label,
url: datapath[key].url,
});
});
}
module.exports = function main(api) {
api.loadSource(async (actions) => {
const pluginsCollection = actions.addCollection({
typeName: "Plugins",
});
const extractorsCollection = actions.addCollection({
typeName: "Extractors",
});
const loadersCollection = actions.addCollection({
typeName: "Loaders",
});
const filesCollection = actions.addCollection({
typeName: "Files",
});
const orchestratorsCollection = actions.addCollection({
typeName: "Orchestrators",
});
const transformersCollection = actions.addCollection({
typeName: "Transformers",
});
const transformsCollection = actions.addCollection({
typeName: "Transforms",
});
const mappersCollection = actions.addCollection({
typeName: "Mappers",
});
const utilitiesCollection = actions.addCollection({
typeName: "Utilities",
});
const maintainersCollection = actions.addCollection({
typeName: "Maintainers",
});
buildData(path.join(dataRoot, "meltano/extractors"), [
extractorsCollection,
pluginsCollection,
]);
buildData(path.join(dataRoot, "meltano/loaders"), [
loadersCollection,
pluginsCollection,
]);
buildData(path.join(dataRoot, "meltano/files"), [
filesCollection,
pluginsCollection,
]);
buildData(path.join(dataRoot, "meltano/orchestrators"), [
orchestratorsCollection,
pluginsCollection,
]);
buildData(path.join(dataRoot, "meltano/transformers"), [
transformersCollection,
pluginsCollection,
]);
buildData(path.join(dataRoot, "meltano/transforms"), [
transformsCollection,
pluginsCollection,
]);
buildData(path.join(dataRoot, "meltano/mappers"), [
mappersCollection,
pluginsCollection,
]);
buildData(path.join(dataRoot, "meltano/utilities"), [
utilitiesCollection,
pluginsCollection,
]);
buildMaintainers(readMaintainers, maintainersCollection);
});
// Create default variant pages
api.createPages(async ({ createPage, graphql }) => {
const defaultPlugins = await graphql(`
{
allPlugins(filter: { isDefault: { eq: true } }) {
edges {
node {
id
path
name
pluginType
}
}
}
}
`);
Object.keys(defaultPlugins.data).forEach((query) => {
// For each default plugin we create an extra page one-level up
defaultPlugins.data[query].edges.forEach(({ node }) => {
createPage({
// Remove the variant part of the path
path: node.path.split("--")[0],
// And send it to the same rendering template
component: `./src/templates/Plugins.vue`,
context: {
id: node.id,
path: node.path,
name: node.name,
},
});
});
});
});
// Build JSON files to serve the static Meltano API
buildJSONApi(api);
};