@xyd-js/plugin-docs 0.1.0-xyd.2 → 0.1.0-xyd.3

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.
@@ -0,0 +1,75 @@
1
+ import fs from 'fs/promises';
2
+ import path from 'node:path';
3
+
4
+ import { createServer } from 'vite';
5
+
6
+ import { Settings } from "@xyd-js/core";
7
+
8
+ const extensions = ['tsx', 'ts', 'json'];
9
+
10
+ /**
11
+ * Reads `xyd` settings from the current working directory.
12
+ *
13
+ * This function searches for a file named 'xyd' with one of the supported extensions
14
+ * (tsx, jsx, js, ts, json) in the current working directory. If found, it loads the
15
+ * settings from that file.
16
+ *
17
+ * For React-based settings files (tsx, jsx, js, ts), it uses Vite's SSR module loading
18
+ * to evaluate the file and extract the default export. For JSON files, it simply
19
+ * parses the JSON content.
20
+ *
21
+ * @returns A Promise that resolves to:
22
+ * - The Settings object if a valid settings file was found and loaded
23
+ * - A string if the settings file contains a string value
24
+ * - null if no settings file was found or an error occurred
25
+ *
26
+ * @throws May throw errors if file reading or parsing fails
27
+ */
28
+ export async function readSettings() {
29
+ const dirPath = process.cwd();
30
+ const baseFileName = 'docs';
31
+
32
+ let settingsFilePath = '';
33
+ let reactSettings = false;
34
+
35
+ try {
36
+ const files = await fs.readdir(dirPath);
37
+ const settingsFile = files.find(file => {
38
+ const ext = path.extname(file).slice(1);
39
+ return file.startsWith(baseFileName) && extensions.includes(ext);
40
+ });
41
+
42
+ if (settingsFile) {
43
+ settingsFilePath = path.join(dirPath, settingsFile);
44
+ reactSettings = path.extname(settingsFile) !== '.json';
45
+ } else {
46
+ console.error(`No settings file found.\nFile must be named 'docs' with one of the following extensions: ${extensions.join(', ')}`);
47
+ return null;
48
+ }
49
+ } catch (error) {
50
+ console.error(error);
51
+ return null;
52
+ }
53
+
54
+ if (reactSettings) {
55
+ const settingsPreview = await createServer({
56
+ optimizeDeps: {
57
+ include: ["react/jsx-runtime"],
58
+ },
59
+ });
60
+ const config = await settingsPreview.ssrLoadModule(settingsFilePath);
61
+ return config.default as Settings;
62
+ } else {
63
+ const rawJsonSettings = await fs.readFile(settingsFilePath, 'utf-8');
64
+ try {
65
+ let json = JSON.parse(rawJsonSettings) as Settings
66
+
67
+
68
+ return json
69
+ } catch (e) {
70
+ console.error(e)
71
+
72
+ return null
73
+ }
74
+ }
75
+ }
@@ -0,0 +1,69 @@
1
+ import { Settings } from "@xyd-js/core";
2
+ import { gqlSchemaToReferences } from "@xyd-js/gql";
3
+ import type { Reference } from "@xyd-js/uniform";
4
+
5
+ import { Preset } from "../../types"
6
+ import { UniformPreset } from "../uniform"
7
+
8
+ interface graphqlPluginOptions {
9
+ urlPrefix?: string
10
+ root?: string
11
+ disableFSWrite?: boolean
12
+ }
13
+
14
+ function preset(
15
+ settings: Settings,
16
+ options: graphqlPluginOptions
17
+ ) {
18
+ return GraphQLUniformPreset.new(settings, options)
19
+ }
20
+
21
+ export const graphqlPreset = preset satisfies Preset<unknown>
22
+
23
+ class GraphQLUniformPreset extends UniformPreset {
24
+ private constructor(
25
+ settings: Settings,
26
+ options: {
27
+ disableFSWrite?: boolean
28
+ }
29
+ ) {
30
+ super(
31
+ "graphql",
32
+ settings.api?.graphql || "",
33
+ settings?.navigation?.sidebar || [],
34
+ options.disableFSWrite
35
+ )
36
+
37
+ this.uniformRefResolver = this.uniformRefResolver.bind(this)
38
+ }
39
+
40
+ static new(
41
+ settings: Settings,
42
+ options: graphqlPluginOptions
43
+ ) {
44
+ return new GraphQLUniformPreset(settings, {
45
+ disableFSWrite: options.disableFSWrite
46
+ })
47
+ .urlPrefix(options.urlPrefix || "")
48
+ .newUniformPreset()(settings, "graphql")
49
+ }
50
+
51
+ protected override async uniformRefResolver(filePath: string): Promise<Reference[]> {
52
+ if (!filePath) {
53
+ return []
54
+ }
55
+
56
+ const resp = await gqlSchemaToReferences(filePath)
57
+
58
+ if ("__UNSAFE_route" in resp && typeof resp.__UNSAFE_route === "function") {
59
+ // If the route is a function, we need to call it to get the actual route
60
+ const route = resp.__UNSAFE_route();
61
+ if (route) {
62
+ this.fileRouting(filePath, route);
63
+ }
64
+ }
65
+
66
+ return resp
67
+ }
68
+ }
69
+
@@ -0,0 +1,66 @@
1
+ import {Settings} from "@xyd-js/core";
2
+ import {deferencedOpenAPI, oapSchemaToReferences, getXDocs} from "@xyd-js/openapi";
3
+ import type {Reference} from "@xyd-js/uniform";
4
+
5
+ import {Preset} from "../../types"
6
+ import {UniformPreset} from "../uniform"
7
+
8
+ export interface openapiPresetOptions {
9
+ urlPrefix?: string
10
+ root?: string
11
+ disableFSWrite?: boolean
12
+ }
13
+
14
+ function preset(
15
+ settings: Settings,
16
+ options: openapiPresetOptions
17
+ ) {
18
+ return OpenAPIUniformPreset.new(settings, options)
19
+ }
20
+
21
+ export const openapiPreset = preset satisfies Preset<unknown>
22
+
23
+ class OpenAPIUniformPreset extends UniformPreset {
24
+ private constructor(
25
+ settings: Settings,
26
+ options: {
27
+ disableFSWrite?: boolean
28
+ }
29
+ ) {
30
+ super(
31
+ "openapi",
32
+ settings.api?.openapi || "",
33
+ settings?.navigation?.sidebar || [],
34
+ options.disableFSWrite
35
+ )
36
+ this.uniformRefResolver = this.uniformRefResolver.bind(this)
37
+ }
38
+
39
+ static new(
40
+ settings: Settings,
41
+ options: openapiPresetOptions
42
+ ) {
43
+ return new OpenAPIUniformPreset(settings, {
44
+ disableFSWrite: options.disableFSWrite
45
+ })
46
+ .urlPrefix(options.urlPrefix || "")
47
+ .newUniformPreset()(settings, "openapi")
48
+ }
49
+
50
+ protected override async uniformRefResolver(filePath: string): Promise<Reference[]> {
51
+ if (!filePath) {
52
+ return []
53
+ }
54
+
55
+ const schema = await deferencedOpenAPI(filePath)
56
+ if (schema) {
57
+ const xdocs = getXDocs(schema)
58
+ if (xdocs?.route) {
59
+ this.fileRouting(filePath, xdocs.route)
60
+ }
61
+ }
62
+
63
+ return oapSchemaToReferences(schema)
64
+ }
65
+ }
66
+
@@ -0,0 +1,74 @@
1
+ import path from "path";
2
+ import fs from "fs/promises";
3
+
4
+ import type {Reference} from "@xyd-js/uniform";
5
+ import {Settings} from "@xyd-js/core";
6
+ import {sourcesToUniformV2} from "@xyd-js/sources/ts"
7
+
8
+ import {Preset} from "../../types"
9
+ import {UniformPreset} from "../uniform"
10
+
11
+ export interface sourcesPresetsOptions {
12
+ urlPrefix?: string
13
+ root?: string
14
+ disableFSWrite?: boolean
15
+ }
16
+
17
+ function preset(
18
+ settings: Settings,
19
+ options: sourcesPresetsOptions
20
+ ) {
21
+ return SourceUniformPreset.new(settings, options)
22
+ }
23
+
24
+ export const sourcesPreset = preset satisfies Preset<unknown>
25
+
26
+ class SourceUniformPreset extends UniformPreset {
27
+ private constructor(
28
+ settings: Settings,
29
+ options: {
30
+ disableFSWrite?: boolean
31
+ }
32
+ ) {
33
+ super(
34
+ "sources",
35
+ settings.api?.sources || "",
36
+ settings?.navigation?.sidebar || [],
37
+ options.disableFSWrite
38
+ )
39
+ }
40
+
41
+ static new(
42
+ settings: Settings,
43
+ options: sourcesPresetsOptions
44
+ ) {
45
+ return new SourceUniformPreset(settings, {
46
+ disableFSWrite: options.disableFSWrite
47
+ })
48
+ .urlPrefix(options.urlPrefix || "")
49
+ .sourceTheme(true)
50
+ .newUniformPreset()(settings, "sources")
51
+ }
52
+
53
+ // TODO: options to specify only specific packages?
54
+ protected override async uniformRefResolver(filePath: string): Promise<Reference[]> {
55
+ if (!filePath) {
56
+ return []
57
+ }
58
+
59
+ const packages = await fs.readdir(filePath)
60
+
61
+ const ref = await sourcesToUniformV2(
62
+ filePath,
63
+ packages.map(p => path.join(filePath, p))
64
+ )
65
+
66
+ if (!ref || !ref.references) {
67
+ console.error("Failed to generate documentation.", filePath)
68
+ return []
69
+ }
70
+
71
+ return ref.references
72
+ }
73
+ }
74
+