@graphql-markdown/core 1.6.4 → 1.7.0-next.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/config.d.ts +26 -0
- package/dist/config.js +172 -0
- package/dist/diff.d.ts +2 -0
- package/dist/diff.js +42 -0
- package/dist/generator.d.ts +2 -0
- package/dist/generator.js +58 -0
- package/dist/graphql-config.d.ts +16 -0
- package/dist/graphql-config.js +96 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +8 -0
- package/dist/printer.d.ts +2 -0
- package/dist/printer.js +44 -0
- package/dist/renderer.d.ts +20 -0
- package/dist/renderer.js +132 -0
- package/package.json +61 -11
- package/assets/sidebar.json +0 -8
- package/src/config.js +0 -194
- package/src/diff.js +0 -30
- package/src/generator.js +0 -99
- package/src/graphql-config.js +0 -61
- package/src/index.js +0 -6
- package/src/printer.js +0 -24
- package/src/renderer.js +0 -193
package/dist/config.d.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { CliOptions, ConfigDocOptions, ConfigOptions, ConfigPrintTypeOptions, CustomDirective, DirectiveName, GroupByDirectiveOptions, LoaderOption, Maybe, Options, TypeDiffMethod } from "@graphql-markdown/types";
|
|
2
|
+
export declare enum DiffMethod {
|
|
3
|
+
NONE = "NONE",
|
|
4
|
+
FORCE = "FORCE"
|
|
5
|
+
}
|
|
6
|
+
export declare enum DeprecatedOption {
|
|
7
|
+
DEFAULT = "default",
|
|
8
|
+
GROUP = "group",
|
|
9
|
+
SKIP = "skip"
|
|
10
|
+
}
|
|
11
|
+
export declare const DOCS_URL: "https://graphql-markdown.github.io/docs";
|
|
12
|
+
export declare const PACKAGE_NAME: "@graphql-markdown/docusaurus";
|
|
13
|
+
export declare const ASSET_HOMEPAGE_LOCATION: string;
|
|
14
|
+
export declare const DEFAULT_OPTIONS: Required<Omit<ConfigOptions, "customDirective" | "groupByDirective" | "loaders">> & {
|
|
15
|
+
groupByDirective: Maybe<GroupByDirectiveOptions>;
|
|
16
|
+
customDirective: Maybe<CustomDirective>;
|
|
17
|
+
loaders: Maybe<LoaderOption>;
|
|
18
|
+
};
|
|
19
|
+
export declare function buildConfig(configFileOpts: Maybe<ConfigOptions>, cliOpts: Maybe<CliOptions>, id?: Maybe<string>): Promise<Options>;
|
|
20
|
+
export declare function getCustomDirectives(customDirectiveOptions: Maybe<CustomDirective>, skipDocDirective?: Maybe<DirectiveName[]>): Maybe<CustomDirective>;
|
|
21
|
+
export declare function getDiffMethod(diff: TypeDiffMethod, force?: boolean): TypeDiffMethod;
|
|
22
|
+
export declare function getDocOptions(cliOpts: Maybe<CliOptions>, configOptions: Maybe<ConfigDocOptions>): Required<ConfigDocOptions>;
|
|
23
|
+
export declare function getPrintTypeOptions(cliOpts: Maybe<CliOptions>, configOptions: Maybe<ConfigPrintTypeOptions>): Required<ConfigPrintTypeOptions>;
|
|
24
|
+
export declare function getSkipDocDirectives(cliOpts: Maybe<CliOptions>, configFileOpts: Maybe<Pick<ConfigOptions, "printTypeOptions" | "skipDocDirective">>): DirectiveName[];
|
|
25
|
+
export declare function getSkipDocDirective(name: Maybe<DirectiveName>): DirectiveName;
|
|
26
|
+
export declare function parseGroupByOption(groupOptions: unknown): Maybe<GroupByDirectiveOptions>;
|
package/dist/config.js
ADDED
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.parseGroupByOption = exports.getSkipDocDirective = exports.getSkipDocDirectives = exports.getPrintTypeOptions = exports.getDocOptions = exports.getDiffMethod = exports.getCustomDirectives = exports.buildConfig = exports.DEFAULT_OPTIONS = exports.ASSET_HOMEPAGE_LOCATION = exports.PACKAGE_NAME = exports.DOCS_URL = exports.DeprecatedOption = exports.DiffMethod = void 0;
|
|
4
|
+
const node_path_1 = require("node:path");
|
|
5
|
+
const node_os_1 = require("node:os");
|
|
6
|
+
const graphql_config_1 = require("./graphql-config");
|
|
7
|
+
var DiffMethod;
|
|
8
|
+
(function (DiffMethod) {
|
|
9
|
+
DiffMethod["NONE"] = "NONE";
|
|
10
|
+
DiffMethod["FORCE"] = "FORCE";
|
|
11
|
+
})(DiffMethod || (exports.DiffMethod = DiffMethod = {}));
|
|
12
|
+
var DeprecatedOption;
|
|
13
|
+
(function (DeprecatedOption) {
|
|
14
|
+
DeprecatedOption["DEFAULT"] = "default";
|
|
15
|
+
DeprecatedOption["GROUP"] = "group";
|
|
16
|
+
DeprecatedOption["SKIP"] = "skip";
|
|
17
|
+
})(DeprecatedOption || (exports.DeprecatedOption = DeprecatedOption = {}));
|
|
18
|
+
exports.DOCS_URL = "https://graphql-markdown.github.io/docs";
|
|
19
|
+
exports.PACKAGE_NAME = "@graphql-markdown/docusaurus";
|
|
20
|
+
exports.ASSET_HOMEPAGE_LOCATION = (0, node_path_1.join)(__dirname, "..", "assets", "generated.md");
|
|
21
|
+
exports.DEFAULT_OPTIONS = {
|
|
22
|
+
id: "default",
|
|
23
|
+
baseURL: "schema",
|
|
24
|
+
customDirective: undefined,
|
|
25
|
+
diffMethod: DiffMethod.NONE,
|
|
26
|
+
docOptions: {
|
|
27
|
+
index: false,
|
|
28
|
+
pagination: true,
|
|
29
|
+
toc: true,
|
|
30
|
+
},
|
|
31
|
+
groupByDirective: undefined,
|
|
32
|
+
homepage: exports.ASSET_HOMEPAGE_LOCATION,
|
|
33
|
+
linkRoot: "/",
|
|
34
|
+
loaders: undefined,
|
|
35
|
+
pretty: false,
|
|
36
|
+
printer: "@graphql-markdown/printer-legacy",
|
|
37
|
+
printTypeOptions: {
|
|
38
|
+
codeSection: true,
|
|
39
|
+
deprecated: DeprecatedOption.DEFAULT,
|
|
40
|
+
parentTypePrefix: true,
|
|
41
|
+
relatedTypeSection: true,
|
|
42
|
+
typeBadges: true,
|
|
43
|
+
},
|
|
44
|
+
rootPath: "./docs",
|
|
45
|
+
schema: "./schema.graphql",
|
|
46
|
+
tmpDir: (0, node_path_1.join)((0, node_os_1.tmpdir)(), exports.PACKAGE_NAME),
|
|
47
|
+
skipDocDirective: [],
|
|
48
|
+
};
|
|
49
|
+
async function buildConfig(configFileOpts, cliOpts, id = "default") {
|
|
50
|
+
if (!cliOpts) {
|
|
51
|
+
cliOpts = {};
|
|
52
|
+
}
|
|
53
|
+
const graphqlConfig = await (0, graphql_config_1.loadConfiguration)(id);
|
|
54
|
+
const config = {
|
|
55
|
+
...exports.DEFAULT_OPTIONS,
|
|
56
|
+
...graphqlConfig,
|
|
57
|
+
...configFileOpts,
|
|
58
|
+
};
|
|
59
|
+
const baseURL = cliOpts.base ?? config.baseURL;
|
|
60
|
+
const skipDocDirective = getSkipDocDirectives(cliOpts, config);
|
|
61
|
+
return {
|
|
62
|
+
baseURL,
|
|
63
|
+
customDirective: getCustomDirectives(config.customDirective, skipDocDirective),
|
|
64
|
+
diffMethod: getDiffMethod(cliOpts.diff ?? config.diffMethod, cliOpts.force),
|
|
65
|
+
docOptions: getDocOptions(cliOpts, config.docOptions),
|
|
66
|
+
groupByDirective: parseGroupByOption(cliOpts.groupByDirective) ?? config.groupByDirective,
|
|
67
|
+
homepageLocation: cliOpts.homepage ?? config.homepage ?? exports.DEFAULT_OPTIONS.homepage,
|
|
68
|
+
id: id ?? exports.DEFAULT_OPTIONS.id,
|
|
69
|
+
linkRoot: cliOpts.link ?? config.linkRoot ?? exports.DEFAULT_OPTIONS.linkRoot,
|
|
70
|
+
loaders: config.loaders,
|
|
71
|
+
outputDir: (0, node_path_1.join)(cliOpts.root ?? config.rootPath, baseURL),
|
|
72
|
+
prettify: cliOpts.pretty ?? config.pretty ?? exports.DEFAULT_OPTIONS.pretty,
|
|
73
|
+
printer: (config.printer ?? exports.DEFAULT_OPTIONS.printer),
|
|
74
|
+
printTypeOptions: getPrintTypeOptions(cliOpts, config.printTypeOptions),
|
|
75
|
+
schemaLocation: cliOpts.schema ?? config.schema ?? exports.DEFAULT_OPTIONS.schema,
|
|
76
|
+
skipDocDirective,
|
|
77
|
+
tmpDir: cliOpts.tmp ?? config.tmpDir ?? exports.DEFAULT_OPTIONS.tmpDir,
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
exports.buildConfig = buildConfig;
|
|
81
|
+
function getCustomDirectives(customDirectiveOptions, skipDocDirective) {
|
|
82
|
+
if (typeof customDirectiveOptions === "undefined" ||
|
|
83
|
+
customDirectiveOptions === null ||
|
|
84
|
+
Object.keys(customDirectiveOptions).length === 0) {
|
|
85
|
+
return undefined;
|
|
86
|
+
}
|
|
87
|
+
for (const [name, option] of Object.entries(customDirectiveOptions)) {
|
|
88
|
+
if (Array.isArray(skipDocDirective) &&
|
|
89
|
+
skipDocDirective.includes(name)) {
|
|
90
|
+
delete customDirectiveOptions[name];
|
|
91
|
+
}
|
|
92
|
+
else if (("descriptor" in option && typeof option.descriptor !== "function") ||
|
|
93
|
+
("tag" in option && typeof option.tag !== "function") ||
|
|
94
|
+
!("tag" in option || "descriptor" in option)) {
|
|
95
|
+
throw new Error(`Wrong format for plugin custom directive "${name}".\nPlease refer to ${exports.DOCS_URL}/advanced/custom-directive`);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return Object.keys(customDirectiveOptions).length === 0
|
|
99
|
+
? undefined
|
|
100
|
+
: customDirectiveOptions;
|
|
101
|
+
}
|
|
102
|
+
exports.getCustomDirectives = getCustomDirectives;
|
|
103
|
+
function getDiffMethod(diff, force = false) {
|
|
104
|
+
return force
|
|
105
|
+
? DiffMethod.FORCE
|
|
106
|
+
: diff.toLocaleUpperCase();
|
|
107
|
+
}
|
|
108
|
+
exports.getDiffMethod = getDiffMethod;
|
|
109
|
+
function getDocOptions(cliOpts, configOptions) {
|
|
110
|
+
return {
|
|
111
|
+
pagination: (!cliOpts?.noPagination && configOptions?.pagination) ??
|
|
112
|
+
exports.DEFAULT_OPTIONS.docOptions.pagination,
|
|
113
|
+
toc: (!cliOpts?.noToc && configOptions?.toc) ??
|
|
114
|
+
exports.DEFAULT_OPTIONS.docOptions.toc,
|
|
115
|
+
index: (cliOpts?.index || configOptions?.index) ??
|
|
116
|
+
exports.DEFAULT_OPTIONS.docOptions.index,
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
exports.getDocOptions = getDocOptions;
|
|
120
|
+
function getPrintTypeOptions(cliOpts, configOptions) {
|
|
121
|
+
return {
|
|
122
|
+
codeSection: (!cliOpts?.noCode && configOptions?.codeSection) ??
|
|
123
|
+
exports.DEFAULT_OPTIONS.printTypeOptions.codeSection,
|
|
124
|
+
deprecated: (cliOpts?.deprecated ??
|
|
125
|
+
configOptions?.deprecated ??
|
|
126
|
+
exports.DEFAULT_OPTIONS.printTypeOptions.deprecated).toLocaleLowerCase(),
|
|
127
|
+
parentTypePrefix: (!cliOpts?.noParentType && configOptions?.parentTypePrefix) ??
|
|
128
|
+
exports.DEFAULT_OPTIONS.printTypeOptions.parentTypePrefix,
|
|
129
|
+
relatedTypeSection: (!cliOpts?.noRelatedType && configOptions?.relatedTypeSection) ??
|
|
130
|
+
exports.DEFAULT_OPTIONS.printTypeOptions.relatedTypeSection,
|
|
131
|
+
typeBadges: (!cliOpts?.noTypeBadges && configOptions?.typeBadges) ??
|
|
132
|
+
exports.DEFAULT_OPTIONS.printTypeOptions.typeBadges,
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
exports.getPrintTypeOptions = getPrintTypeOptions;
|
|
136
|
+
function getSkipDocDirectives(cliOpts, configFileOpts) {
|
|
137
|
+
const directiveList = [].concat((cliOpts?.skip ?? []), (configFileOpts?.skipDocDirective ?? []));
|
|
138
|
+
const skipDirectives = directiveList.map((option) => getSkipDocDirective(option));
|
|
139
|
+
if ((configFileOpts &&
|
|
140
|
+
configFileOpts.printTypeOptions?.deprecated === DeprecatedOption.SKIP) ||
|
|
141
|
+
(cliOpts && cliOpts.deprecated === DeprecatedOption.SKIP)) {
|
|
142
|
+
skipDirectives.push("deprecated");
|
|
143
|
+
}
|
|
144
|
+
return skipDirectives;
|
|
145
|
+
}
|
|
146
|
+
exports.getSkipDocDirectives = getSkipDocDirectives;
|
|
147
|
+
function getSkipDocDirective(name) {
|
|
148
|
+
const OPTION_REGEX = /^@(?<directive>\w+)$/;
|
|
149
|
+
if (typeof name !== "string" || !OPTION_REGEX.test(name)) {
|
|
150
|
+
throw new Error(`Invalid "${name}"`);
|
|
151
|
+
}
|
|
152
|
+
const { groups: { directive }, } = OPTION_REGEX.exec(name);
|
|
153
|
+
return directive;
|
|
154
|
+
}
|
|
155
|
+
exports.getSkipDocDirective = getSkipDocDirective;
|
|
156
|
+
function parseGroupByOption(groupOptions) {
|
|
157
|
+
const DEFAULT_GROUP = "Miscellaneous";
|
|
158
|
+
const OPTION_REGEX = /^@(?<directive>\w+)\((?<field>\w+)(?:\|=(?<fallback>\w+))?\)/;
|
|
159
|
+
if (typeof groupOptions !== "string") {
|
|
160
|
+
return undefined;
|
|
161
|
+
}
|
|
162
|
+
const parsedOptions = OPTION_REGEX.exec(groupOptions);
|
|
163
|
+
if (typeof parsedOptions === "undefined" || parsedOptions == null) {
|
|
164
|
+
throw new Error(`Invalid "${groupOptions}"`);
|
|
165
|
+
}
|
|
166
|
+
if (!("groups" in parsedOptions)) {
|
|
167
|
+
return undefined;
|
|
168
|
+
}
|
|
169
|
+
const { directive, field, fallback = DEFAULT_GROUP, } = parsedOptions.groups;
|
|
170
|
+
return { directive, field, fallback };
|
|
171
|
+
}
|
|
172
|
+
exports.parseGroupByOption = parseGroupByOption;
|
package/dist/diff.d.ts
ADDED
package/dist/diff.js
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
exports.hasChanges = void 0;
|
|
27
|
+
const logger_1 = require("@graphql-markdown/logger");
|
|
28
|
+
const logger = (0, logger_1.Logger)();
|
|
29
|
+
const hasChanges = async (schema, tmpDir, diffMethod, diffModule = "@graphql-markdown/diff") => {
|
|
30
|
+
if (typeof diffMethod !== "string" || typeof diffModule !== "string") {
|
|
31
|
+
return true;
|
|
32
|
+
}
|
|
33
|
+
try {
|
|
34
|
+
const { checkSchemaChanges, } = await Promise.resolve(`${diffModule}`).then(s => __importStar(require(s)));
|
|
35
|
+
return await checkSchemaChanges(schema, tmpDir, diffMethod);
|
|
36
|
+
}
|
|
37
|
+
catch (error) {
|
|
38
|
+
logger.warn(`Cannot find module '${diffModule}' from @graphql-markdown/core!`);
|
|
39
|
+
return true;
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
exports.hasChanges = hasChanges;
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import type { GeneratorOptions } from "@graphql-markdown/types";
|
|
2
|
+
export declare const generateDocFromSchema: ({ baseURL, customDirective, diffMethod, docOptions, groupByDirective, homepageLocation, linkRoot, loaders: loadersList, loggerModule, outputDir, prettify, printer: printerModule, printTypeOptions, schemaLocation, skipDocDirective, tmpDir, }: GeneratorOptions) => Promise<void>;
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.generateDocFromSchema = void 0;
|
|
4
|
+
const logger_1 = require("@graphql-markdown/logger");
|
|
5
|
+
const graphql_1 = require("@graphql-markdown/graphql");
|
|
6
|
+
const renderer_1 = require("./renderer");
|
|
7
|
+
const diff_1 = require("./diff");
|
|
8
|
+
const printer_1 = require("./printer");
|
|
9
|
+
const config_1 = require("./config");
|
|
10
|
+
const NS_PER_SEC = 1e9;
|
|
11
|
+
const SEC_DECIMALS = 3;
|
|
12
|
+
const generateDocFromSchema = async ({ baseURL, customDirective, diffMethod, docOptions, groupByDirective, homepageLocation, linkRoot, loaders: loadersList, loggerModule, outputDir, prettify, printer: printerModule, printTypeOptions, schemaLocation, skipDocDirective, tmpDir, }) => {
|
|
13
|
+
const start = process.hrtime.bigint();
|
|
14
|
+
const logger = (0, logger_1.Logger)(loggerModule);
|
|
15
|
+
const loaders = await (0, graphql_1.getDocumentLoaders)(loadersList);
|
|
16
|
+
if (!loaders) {
|
|
17
|
+
logger.error(`An error occurred while loading GraphQL loader.\nCheck your dependencies and configuration.`);
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
const schema = await (0, graphql_1.loadSchema)(schemaLocation, loaders);
|
|
21
|
+
if (diffMethod !== config_1.DiffMethod.NONE) {
|
|
22
|
+
const changed = await (0, diff_1.hasChanges)(schema, tmpDir, diffMethod);
|
|
23
|
+
if (!changed) {
|
|
24
|
+
logger.info(`No changes detected in schema "${String(schemaLocation)}".`);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
const rootTypes = (0, graphql_1.getSchemaMap)(schema);
|
|
28
|
+
const customDirectives = (0, graphql_1.getCustomDirectives)(rootTypes, customDirective);
|
|
29
|
+
const groups = (0, graphql_1.getGroups)(rootTypes, groupByDirective);
|
|
30
|
+
const printer = await (0, printer_1.getPrinter)(
|
|
31
|
+
// module mandatory
|
|
32
|
+
printerModule,
|
|
33
|
+
// config mandatory
|
|
34
|
+
{
|
|
35
|
+
baseURL,
|
|
36
|
+
linkRoot,
|
|
37
|
+
schema,
|
|
38
|
+
},
|
|
39
|
+
// options
|
|
40
|
+
{
|
|
41
|
+
customDirectives,
|
|
42
|
+
groups,
|
|
43
|
+
printTypeOptions,
|
|
44
|
+
skipDocDirective,
|
|
45
|
+
});
|
|
46
|
+
const renderer = new renderer_1.Renderer(printer, outputDir, baseURL, groups, prettify, {
|
|
47
|
+
...docOptions,
|
|
48
|
+
deprecated: printTypeOptions.deprecated,
|
|
49
|
+
});
|
|
50
|
+
const pages = await Promise.all(Object.keys(rootTypes).map(async (typeName) => renderer.renderRootTypes(typeName, rootTypes[typeName])));
|
|
51
|
+
await renderer.renderHomepage(homepageLocation);
|
|
52
|
+
const sidebarPath = await renderer.renderSidebar();
|
|
53
|
+
const duration = (Number(process.hrtime.bigint() - start) / NS_PER_SEC).toFixed(SEC_DECIMALS);
|
|
54
|
+
logger.success(`Documentation successfully generated in "${outputDir}" with base URL "${baseURL}".`);
|
|
55
|
+
logger.info(`${pages.flat().length} pages generated in ${duration}s from schema "${String(schemaLocation)}".`);
|
|
56
|
+
logger.info(`Remember to update your Docusaurus site's sidebars with "${sidebarPath}".`);
|
|
57
|
+
};
|
|
58
|
+
exports.generateDocFromSchema = generateDocFromSchema;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { ConfigOptions, GraphQLProjectConfig, LoaderOption, Maybe, PackageOptionsConfig } from "@graphql-markdown/types";
|
|
2
|
+
export declare const EXTENSION_NAME: "graphql-markdown";
|
|
3
|
+
export declare const GraphQLConfigExtension: () => {
|
|
4
|
+
readonly name: "graphql-markdown";
|
|
5
|
+
};
|
|
6
|
+
type ThrowOptions = {
|
|
7
|
+
throwOnMissing: boolean;
|
|
8
|
+
throwOnEmpty: boolean;
|
|
9
|
+
};
|
|
10
|
+
type Writeable<T> = {
|
|
11
|
+
-readonly [P in keyof T]: T[P];
|
|
12
|
+
};
|
|
13
|
+
export type ExtensionProjectConfig = Omit<ConfigOptions, "schema"> & Writeable<GraphQLProjectConfig>;
|
|
14
|
+
export declare const setLoaderOptions: (loaders: LoaderOption, options: PackageOptionsConfig) => LoaderOption;
|
|
15
|
+
export declare const loadConfiguration: (id: Maybe<string>, options?: Maybe<PackageOptionsConfig>, { throwOnMissing, throwOnEmpty }?: ThrowOptions) => Promise<Maybe<Readonly<ExtensionProjectConfig>>>;
|
|
16
|
+
export {};
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
exports.loadConfiguration = exports.setLoaderOptions = exports.GraphQLConfigExtension = exports.EXTENSION_NAME = void 0;
|
|
27
|
+
const logger_1 = require("@graphql-markdown/logger");
|
|
28
|
+
const logger = (0, logger_1.Logger)();
|
|
29
|
+
exports.EXTENSION_NAME = "graphql-markdown";
|
|
30
|
+
const GraphQLConfigExtension = () => ({ name: exports.EXTENSION_NAME });
|
|
31
|
+
exports.GraphQLConfigExtension = GraphQLConfigExtension;
|
|
32
|
+
const setLoaderOptions = (loaders, options) => {
|
|
33
|
+
for (const loader in loaders) {
|
|
34
|
+
if (typeof loaders[loader] === "string") {
|
|
35
|
+
loaders[loader] = {
|
|
36
|
+
module: loaders[loader],
|
|
37
|
+
options,
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
loaders[loader].options = {
|
|
42
|
+
...options,
|
|
43
|
+
...loaders[loader].options,
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return loaders;
|
|
48
|
+
};
|
|
49
|
+
exports.setLoaderOptions = setLoaderOptions;
|
|
50
|
+
const loadConfiguration = async (id, options, { throwOnMissing, throwOnEmpty } = {
|
|
51
|
+
throwOnMissing: false,
|
|
52
|
+
throwOnEmpty: false,
|
|
53
|
+
}) => {
|
|
54
|
+
let GraphQLConfig;
|
|
55
|
+
if (typeof id !== "string") {
|
|
56
|
+
return undefined;
|
|
57
|
+
}
|
|
58
|
+
try {
|
|
59
|
+
GraphQLConfig = await Promise.resolve().then(() => __importStar(require("graphql-config")));
|
|
60
|
+
}
|
|
61
|
+
catch (error) {
|
|
62
|
+
logger.log("Cannot find module 'graphql-config'!");
|
|
63
|
+
return undefined;
|
|
64
|
+
}
|
|
65
|
+
const config = await GraphQLConfig.loadConfig({
|
|
66
|
+
...options,
|
|
67
|
+
extensions: [exports.GraphQLConfigExtension],
|
|
68
|
+
throwOnMissing,
|
|
69
|
+
throwOnEmpty,
|
|
70
|
+
});
|
|
71
|
+
if (!config) {
|
|
72
|
+
return undefined;
|
|
73
|
+
}
|
|
74
|
+
try {
|
|
75
|
+
const projectConfig = config
|
|
76
|
+
.getProject(id)
|
|
77
|
+
.extension(exports.EXTENSION_NAME);
|
|
78
|
+
if (Array.isArray(projectConfig.schema)) {
|
|
79
|
+
const schema = projectConfig.schema[0];
|
|
80
|
+
if (typeof schema === "string") {
|
|
81
|
+
projectConfig.schema = schema;
|
|
82
|
+
}
|
|
83
|
+
if (typeof projectConfig.schema === "object") {
|
|
84
|
+
projectConfig.schema = Object.keys(schema)[0];
|
|
85
|
+
if (typeof projectConfig.loaders !== "undefined") {
|
|
86
|
+
projectConfig.loaders = (0, exports.setLoaderOptions)(projectConfig.loaders, Object.values(schema)[0]);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
return projectConfig;
|
|
91
|
+
}
|
|
92
|
+
catch (error) {
|
|
93
|
+
return undefined;
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
exports.loadConfiguration = loadConfiguration;
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/* istanbul ignore file */
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.buildConfig = exports.generateDocFromSchema = void 0;
|
|
5
|
+
var generator_1 = require("./generator");
|
|
6
|
+
Object.defineProperty(exports, "generateDocFromSchema", { enumerable: true, get: function () { return generator_1.generateDocFromSchema; } });
|
|
7
|
+
var config_1 = require("./config");
|
|
8
|
+
Object.defineProperty(exports, "buildConfig", { enumerable: true, get: function () { return config_1.buildConfig; } });
|
package/dist/printer.js
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
exports.getPrinter = void 0;
|
|
27
|
+
const getPrinter = async (printerModule, config, options) => {
|
|
28
|
+
if (typeof printerModule !== "string") {
|
|
29
|
+
throw new Error('Invalid printer module name in "printTypeOptions" settings.');
|
|
30
|
+
}
|
|
31
|
+
if (!config) {
|
|
32
|
+
throw new Error('Invalid printer config in "printTypeOptions" settings.');
|
|
33
|
+
}
|
|
34
|
+
try {
|
|
35
|
+
const { Printer } = await Promise.resolve(`${printerModule}`).then(s => __importStar(require(s)));
|
|
36
|
+
const { schema, baseURL, linkRoot } = config;
|
|
37
|
+
Printer.init(schema, baseURL, linkRoot, { ...options });
|
|
38
|
+
return Printer;
|
|
39
|
+
}
|
|
40
|
+
catch (error) {
|
|
41
|
+
throw new Error(`Cannot find module '${printerModule}' defined in "printTypeOptions" settings.`);
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
exports.getPrinter = getPrinter;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { Category, ConfigDocOptions, Maybe, Printer, SchemaEntitiesGroupMap, SchemaEntity, TypeDeprecatedOption } from "@graphql-markdown/types";
|
|
2
|
+
export declare class Renderer {
|
|
3
|
+
private printer;
|
|
4
|
+
group: Maybe<SchemaEntitiesGroupMap>;
|
|
5
|
+
outputDir: string;
|
|
6
|
+
baseURL: string;
|
|
7
|
+
prettify: boolean;
|
|
8
|
+
options: Maybe<ConfigDocOptions & {
|
|
9
|
+
deprecated: TypeDeprecatedOption;
|
|
10
|
+
}>;
|
|
11
|
+
constructor(printer: Printer, outputDir: string, baseURL: string, group: Maybe<SchemaEntitiesGroupMap>, prettify: boolean, docOptions: Maybe<ConfigDocOptions & {
|
|
12
|
+
deprecated: TypeDeprecatedOption;
|
|
13
|
+
}>);
|
|
14
|
+
generateCategoryMetafile(category: string, dirPath: string, sidebarPosition?: number, styleClass?: string): Promise<void>;
|
|
15
|
+
generateCategoryMetafileType(type: unknown, name: string, rootTypeName: SchemaEntity): Promise<string>;
|
|
16
|
+
renderRootTypes(rootTypeName: SchemaEntity, type: unknown): Promise<Maybe<Maybe<Category>[]>>;
|
|
17
|
+
renderTypeEntities(dirPath: string, name: string, type: unknown): Promise<Maybe<Category>>;
|
|
18
|
+
renderSidebar(): Promise<string>;
|
|
19
|
+
renderHomepage(homepageLocation: string): Promise<void>;
|
|
20
|
+
}
|
package/dist/renderer.js
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Renderer = void 0;
|
|
4
|
+
const node_path_1 = require("node:path");
|
|
5
|
+
const graphql_1 = require("@graphql-markdown/graphql");
|
|
6
|
+
const utils_1 = require("@graphql-markdown/utils");
|
|
7
|
+
const logger_1 = require("@graphql-markdown/logger");
|
|
8
|
+
const logger = (0, logger_1.Logger)();
|
|
9
|
+
const SIDEBAR = "sidebar-schema.js";
|
|
10
|
+
const CATEGORY_YAML = "_category_.yml";
|
|
11
|
+
var SIDEBAR_POSITION;
|
|
12
|
+
(function (SIDEBAR_POSITION) {
|
|
13
|
+
SIDEBAR_POSITION[SIDEBAR_POSITION["FIRST"] = 1] = "FIRST";
|
|
14
|
+
SIDEBAR_POSITION[SIDEBAR_POSITION["LAST"] = 999] = "LAST";
|
|
15
|
+
})(SIDEBAR_POSITION || (SIDEBAR_POSITION = {}));
|
|
16
|
+
class Renderer {
|
|
17
|
+
printer;
|
|
18
|
+
group;
|
|
19
|
+
outputDir;
|
|
20
|
+
baseURL;
|
|
21
|
+
prettify;
|
|
22
|
+
options;
|
|
23
|
+
constructor(printer, outputDir, baseURL, group, prettify, docOptions) {
|
|
24
|
+
this.printer = printer;
|
|
25
|
+
this.group = group;
|
|
26
|
+
this.outputDir = outputDir;
|
|
27
|
+
this.baseURL = baseURL;
|
|
28
|
+
this.prettify = prettify;
|
|
29
|
+
this.options = docOptions;
|
|
30
|
+
}
|
|
31
|
+
async generateCategoryMetafile(category, dirPath, sidebarPosition = SIDEBAR_POSITION.FIRST, styleClass) {
|
|
32
|
+
const filePath = (0, node_path_1.join)(dirPath, CATEGORY_YAML);
|
|
33
|
+
if (await (0, utils_1.fileExists)(filePath)) {
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
await (0, utils_1.ensureDir)(dirPath);
|
|
37
|
+
const label = (0, utils_1.startCase)(category);
|
|
38
|
+
const link = this.options && this.options.index !== true
|
|
39
|
+
? "null"
|
|
40
|
+
: `\n type: generated-index\n title: '${label} overview'`;
|
|
41
|
+
const className = typeof styleClass === "string" ? `className: ${styleClass}\n` : "";
|
|
42
|
+
await (0, utils_1.saveFile)(filePath, `label: ${label}\nposition: ${sidebarPosition}\n${className}link: ${link}\n`);
|
|
43
|
+
}
|
|
44
|
+
async generateCategoryMetafileType(type, name, rootTypeName) {
|
|
45
|
+
let dirPath = this.outputDir;
|
|
46
|
+
if (this.options &&
|
|
47
|
+
this.options.deprecated === "group" &&
|
|
48
|
+
(0, graphql_1.isDeprecated)(type)) {
|
|
49
|
+
dirPath = (0, node_path_1.join)(dirPath, (0, utils_1.slugify)("deprecated"));
|
|
50
|
+
await this.generateCategoryMetafile("deprecated", dirPath, SIDEBAR_POSITION.LAST, "deprecated");
|
|
51
|
+
}
|
|
52
|
+
if (this.group &&
|
|
53
|
+
rootTypeName in this.group &&
|
|
54
|
+
name in this.group[rootTypeName]) {
|
|
55
|
+
dirPath = (0, node_path_1.join)(dirPath, (0, utils_1.slugify)(this.group[rootTypeName][name] ?? ""));
|
|
56
|
+
await this.generateCategoryMetafile(this.group[rootTypeName][name] ?? "", dirPath);
|
|
57
|
+
}
|
|
58
|
+
dirPath = (0, node_path_1.join)(dirPath, (0, utils_1.slugify)(rootTypeName));
|
|
59
|
+
await this.generateCategoryMetafile(rootTypeName, dirPath);
|
|
60
|
+
return dirPath;
|
|
61
|
+
}
|
|
62
|
+
async renderRootTypes(rootTypeName, type) {
|
|
63
|
+
if (typeof type !== "object" || type === null) {
|
|
64
|
+
return undefined;
|
|
65
|
+
}
|
|
66
|
+
return Promise.all(Object.keys(type)
|
|
67
|
+
.map(async (name) => {
|
|
68
|
+
const dirPath = await this.generateCategoryMetafileType(type[name], name, rootTypeName);
|
|
69
|
+
return this.renderTypeEntities(dirPath, name, type[name]);
|
|
70
|
+
})
|
|
71
|
+
.filter((res) => typeof res !== "undefined"));
|
|
72
|
+
}
|
|
73
|
+
async renderTypeEntities(dirPath, name, type) {
|
|
74
|
+
const PageRegex = /(?<category>[A-Za-z0-9-]+)[\\/]+(?<pageId>[A-Za-z0-9-]+).mdx$/;
|
|
75
|
+
const fileName = (0, utils_1.slugify)(name);
|
|
76
|
+
const filePath = (0, node_path_1.join)((0, node_path_1.normalize)(dirPath), `${fileName}.mdx`);
|
|
77
|
+
let content;
|
|
78
|
+
try {
|
|
79
|
+
content = this.printer.printType(fileName, type, this.options);
|
|
80
|
+
if (typeof content !== "string") {
|
|
81
|
+
return undefined;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
catch (error) {
|
|
85
|
+
logger.warn(`An error occurred while processing "${type}"`);
|
|
86
|
+
return undefined;
|
|
87
|
+
}
|
|
88
|
+
await (0, utils_1.saveFile)(filePath, content, this.prettify ? utils_1.prettifyMarkdown : undefined);
|
|
89
|
+
const pagePath = (0, node_path_1.relative)(this.outputDir, filePath);
|
|
90
|
+
const page = PageRegex.exec(pagePath);
|
|
91
|
+
if (!page?.groups) {
|
|
92
|
+
logger.warn(`An error occurred while processing file ${filePath} for type "${type}"`);
|
|
93
|
+
return undefined;
|
|
94
|
+
}
|
|
95
|
+
const slug = utils_1.pathUrl.join(page.groups.category, page.groups.pageId);
|
|
96
|
+
return {
|
|
97
|
+
category: (0, utils_1.startCase)(page.groups.category),
|
|
98
|
+
slug: slug,
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
async renderSidebar() {
|
|
102
|
+
const sidebar = {
|
|
103
|
+
schemaSidebar: [
|
|
104
|
+
{
|
|
105
|
+
type: "autogenerated",
|
|
106
|
+
dirName: this.baseURL,
|
|
107
|
+
},
|
|
108
|
+
],
|
|
109
|
+
};
|
|
110
|
+
const jsonSidebar = `
|
|
111
|
+
/** @type {import('@docusaurus/plugin-content-docs').SidebarsConfig} */
|
|
112
|
+
|
|
113
|
+
module.exports = ${JSON.stringify(sidebar, null, 2)};
|
|
114
|
+
`;
|
|
115
|
+
const filePath = (0, node_path_1.join)(this.outputDir, SIDEBAR);
|
|
116
|
+
await (0, utils_1.saveFile)(filePath, jsonSidebar, this.prettify ? utils_1.prettifyJavascript : undefined);
|
|
117
|
+
return filePath;
|
|
118
|
+
}
|
|
119
|
+
async renderHomepage(homepageLocation) {
|
|
120
|
+
const homePage = (0, node_path_1.basename)(homepageLocation);
|
|
121
|
+
const destLocation = (0, node_path_1.join)(this.outputDir, homePage);
|
|
122
|
+
const slug = utils_1.pathUrl.resolve("/", this.baseURL);
|
|
123
|
+
await (0, utils_1.copyFile)(homepageLocation, destLocation);
|
|
124
|
+
const template = await (0, utils_1.readFile)(destLocation);
|
|
125
|
+
const data = template
|
|
126
|
+
.toString()
|
|
127
|
+
.replace(/##baseURL##/gm, slug)
|
|
128
|
+
.replace(/##generated-date-time##/gm, new Date().toLocaleString());
|
|
129
|
+
await (0, utils_1.saveFile)(destLocation, data);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
exports.Renderer = Renderer;
|