-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.js
104 lines (85 loc) · 2.66 KB
/
gatsby-node.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
// @ts-check
const typescript = require("typescript");
const { Application, TypeDocReader, TSConfigReader } = require("typedoc");
const fs = require("fs");
exports.pluginOptionsSchema = ({ Joi }) =>
Joi.object({
src: Joi.array()
.items(Joi.string().required())
.required(),
id: Joi.string(),
typedoc: Joi.object().unknown(true),
}).unknown(true);
exports.sourceNodes = async (
{ actions, createNodeId, createContentDigest, cache, reporter },
configOptions
) => {
const { createNode } = actions;
delete configOptions.plugins;
const { src, id = "default", typedoc: typedocOptions = {} } = configOptions;
if (!src || !Array.isArray(src)) {
reporter.panicOnBuild(
"gatsby-source-typedoc requires a `src` array of TypeScript files to process"
);
}
function processTypeDoc(generated) {
const nodeId = createNodeId(`typedoc-${id}`);
const nodeContent = JSON.stringify(generated);
const nodeData = {
id: nodeId,
typedocId: id,
source: generated,
internal: {
type: "Typedoc",
content: nodeContent,
contentDigest: createContentDigest(generated),
},
};
return nodeData;
}
//
// Use existing cached data, if already processed
//
const existing = await cache.get(`gatsby-source-typedoc--generated-${id}`);
if (existing) {
const nodeData = processTypeDoc(existing);
createNode(nodeData);
reporter.verbose(
`Using generated TypeDoc from previous build with ID: ${id}`
);
return true;
}
const app = new Application();
app.options.addReader(new TypeDocReader());
app.options.addReader(new TSConfigReader());
// If specifying tsconfig file, use TS to find
// it so types are resolved relative to the source
// folder
if (typedocOptions.tsconfig) {
const tsconfig = typescript.findConfigFile(
typedocOptions.tsconfig,
fs.existsSync
);
if (tsconfig) {
typedocOptions.tsconfig = tsconfig;
}
}
app.bootstrap(Object.assign({ name: id }, typedocOptions));
try {
app.options.setValue("entryPoints", src);
const reflection = app.convert();
if (reflection) {
const serialized = app.serializer.toObject(reflection);
const nodeData = processTypeDoc(serialized);
// Store in Gatsby cache
await cache.set(`gatsby-source-typedoc--generated-${id}`, serialized);
createNode(nodeData);
reporter.verbose(`Generated TypeDoc and cached with ID: ${id}`);
} else {
reporter.warn("TypeDoc returned an empty project");
}
} catch (error) {
reporter.panicOnBuild("Encountered error when executing TypeDoc", error);
}
return Promise.resolve(true);
};