-
Notifications
You must be signed in to change notification settings - Fork 26
/
generator.js
140 lines (121 loc) · 3.13 KB
/
generator.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
const {
graphql: { getSchemaMap, loadSchema, getDocumentLoaders },
} = require("@graphql-markdown/utils");
const { getGroups } = require("./group-info");
const Renderer = require("./renderer");
const NS_PER_SEC = 1e9;
const SEC_DECIMALS = 3;
const hasChanges = async (
schema,
tmpDir,
diffMethod,
diffModule = "@graphql-markdown/diff",
) => {
if (
typeof diffMethod === "undefined" ||
diffMethod == null ||
typeof diffModule === "undefined" ||
diffModule == null
) {
return true;
}
try {
const { checkSchemaChanges } = require(diffModule);
return await checkSchemaChanges(schema, tmpDir, diffMethod);
} catch (error) {
console.warn(
`Cannot find module '${diffModule}' from @graphql-markdown/core!`,
);
}
return true;
};
const getPrinter = (
schema,
baseURL,
linkRoot,
groups,
printTypeOptions,
printerModule,
skipDocDirective,
) => {
let Printer = undefined;
if (typeof printerModule !== "string") {
throw new Error(
'Invalid printer module name in "printTypeOptions" settings.',
);
}
try {
Printer = require(printerModule);
} catch (error) {
throw new Error(
`Cannot find module '${printerModule}' for @graphql-markdown/core in "printTypeOptions" settings.`,
);
}
Printer.init(schema, baseURL, linkRoot, {
groups,
printTypeOptions,
skipDocDirective,
});
return Printer;
};
const generateDocFromSchema = async ({
baseURL,
schemaLocation,
outputDir,
linkRoot,
homepageLocation,
diffMethod,
tmpDir,
loaders: loadersList,
groupByDirective,
prettify,
docOptions,
printTypeOptions,
printer: printerModule,
skipDocDirective,
}) => {
const start = process.hrtime.bigint();
const loaders = getDocumentLoaders(loadersList);
const schema = await loadSchema(schemaLocation, loaders);
const changed = await hasChanges(schema, tmpDir, diffMethod);
if (!changed) {
console.info(`No changes detected in schema "${schemaLocation}".`);
}
const rootTypes = getSchemaMap(schema);
const groups = new getGroups(rootTypes, groupByDirective);
const printer = getPrinter(
schema,
baseURL,
linkRoot,
groups,
printTypeOptions,
printerModule,
skipDocDirective,
);
const renderer = new Renderer(printer, outputDir, baseURL, groups, prettify, {
...docOptions,
deprecated: printTypeOptions.deprecated,
});
const pages = await Promise.all(
Object.keys(rootTypes).map((typeName) =>
renderer.renderRootTypes(typeName, rootTypes[typeName]),
),
);
await renderer.renderHomepage(homepageLocation);
const sidebarPath = await renderer.renderSidebar();
const duration = (
Number(process.hrtime.bigint() - start) / NS_PER_SEC
).toFixed(SEC_DECIMALS);
console.info(
`Documentation successfully generated in "${outputDir}" with base URL "${baseURL}".`,
);
console.log(
`${
pages.flat().length
} pages generated in ${duration}s from schema "${schemaLocation}".`,
);
console.info(
`Remember to update your Docusaurus site's sidebars with "${sidebarPath}".`,
);
};
module.exports = { getPrinter, hasChanges, generateDocFromSchema };