@alauda/doom 0.11.2 → 0.11.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.
package/global.d.ts CHANGED
@@ -4,3 +4,19 @@ declare module '*.module.scss' {
4
4
  }
5
5
 
6
6
  type MDXProvidedComponents = typeof import('@alauda/doom/runtime')
7
+
8
+ declare module 'doom-@api-crdsMap' {
9
+ export default crdsMap
10
+ }
11
+
12
+ declare module 'doom-@api-openapisMap' {
13
+ export default openapisMap
14
+ }
15
+
16
+ declare module 'doom-@permission-functionResourcesMap' {
17
+ export default functionResourcesMap
18
+ }
19
+
20
+ declare module 'doom-@permission-roleTemplatesMap' {
21
+ export default roleTemplatesMap
22
+ }
@@ -1,26 +1,17 @@
1
- import path from 'node:path';
2
1
  import { openapiSchemaToJsonSchema } from '@openapi-contrib/openapi-schema-to-json-schema';
3
2
  import { convertObj } from 'swagger2openapi';
4
- import { glob } from 'tinyglobby';
5
- import { resolveStaticConfig } from '../../utils/index.js';
6
- export const apiPlugin = ({ localBasePath, crds, openapis, references, pathPrefix, }) => {
3
+ import { generateRuntimeModule } from '../../utils/index.js';
4
+ export const apiPlugin = ({ localBasePath, crds = [], openapis = [], references, pathPrefix, }) => {
7
5
  return {
8
6
  name: 'doom-api',
9
- async extendPageData(pageData) {
10
- const crdsMap = {};
11
- const openapisMap = {};
12
- if (crds) {
13
- for (const file of await glob(crds, {
14
- cwd: localBasePath,
15
- })) {
16
- crdsMap[file] = await resolveStaticConfig(path.resolve(localBasePath, file));
17
- }
18
- }
19
- if (openapis) {
20
- for (const file of await glob(openapis, {
21
- cwd: localBasePath,
22
- })) {
23
- let doc = await resolveStaticConfig(path.resolve(localBasePath, file));
7
+ extendPageData(pageData) {
8
+ pageData.references = references;
9
+ pageData.pathPrefix = pathPrefix;
10
+ },
11
+ async addRuntimeModules(_, isProd) {
12
+ return {
13
+ ...(await generateRuntimeModule(crds, 'api-crds', localBasePath, isProd)),
14
+ ...(await generateRuntimeModule(openapis, 'api-openapis', localBasePath, isProd, async (doc) => {
24
15
  // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
25
16
  doc.info ??= { title: 'Unknown', version: '0.0.0' };
26
17
  if ('swagger' in doc && doc.swagger === '2.0') {
@@ -30,13 +21,8 @@ export const apiPlugin = ({ localBasePath, crds, openapis, references, pathPrefi
30
21
  doc = openapiSchemaToJsonSchema(doc);
31
22
  doc.openapi = '3.1.0';
32
23
  }
33
- openapisMap[file] = doc;
34
- }
35
- }
36
- pageData.crdsMap = crdsMap;
37
- pageData.openapisMap = openapisMap;
38
- pageData.references = references;
39
- pageData.pathPrefix = pathPrefix;
24
+ })),
25
+ };
40
26
  },
41
27
  };
42
28
  };
@@ -1,3 +1,3 @@
1
1
  import { RspressPlugin } from '@rspress/core';
2
2
  import type { PermissionPluginOptions } from './types.js';
3
- export declare const permissionPlugin: ({ functionresources, localBasePath, roletemplates, }: PermissionPluginOptions) => RspressPlugin;
3
+ export declare const permissionPlugin: ({ localBasePath, functionresources, roletemplates, }: PermissionPluginOptions) => RspressPlugin;
@@ -1,28 +1,12 @@
1
- import path from 'node:path';
2
- import { glob } from 'tinyglobby';
3
- import { resolveStaticConfig } from '../../utils/index.js';
4
- export const permissionPlugin = ({ functionresources, localBasePath, roletemplates, }) => {
1
+ import { generateRuntimeModule } from '../../utils/index.js';
2
+ export const permissionPlugin = ({ localBasePath, functionresources = [], roletemplates = [], }) => {
5
3
  return {
6
- async extendPageData(pageData) {
7
- const functionResourcesMap = {};
8
- const roleTemplatesMap = {};
9
- if (functionresources) {
10
- for (const file of await glob(functionresources, {
11
- cwd: localBasePath,
12
- })) {
13
- functionResourcesMap[file] = (await resolveStaticConfig(path.resolve(localBasePath, file))).items;
14
- }
15
- }
16
- if (roletemplates) {
17
- for (const file of await glob(roletemplates, {
18
- cwd: localBasePath,
19
- })) {
20
- roleTemplatesMap[file] = (await resolveStaticConfig(path.resolve(localBasePath, file))).items;
21
- }
22
- }
23
- pageData.functionResourcesMap = functionResourcesMap;
24
- pageData.roleTemplatesMap = roleTemplatesMap;
25
- },
26
4
  name: 'doom-permission',
5
+ async addRuntimeModules(_, isProd) {
6
+ return {
7
+ ...(await generateRuntimeModule(functionresources, 'permission-functionResources', localBasePath, isProd, ({ items }) => items)),
8
+ ...(await generateRuntimeModule(roletemplates, 'permission-roleTemplates', localBasePath, isProd, ({ items }) => items)),
9
+ };
10
+ },
27
11
  };
28
12
  };
@@ -2,12 +2,12 @@ import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-run
2
2
  /**
3
3
  * modified based on @see https://github.com/crdsdev/doc/blob/main/template/doc.html
4
4
  */
5
- import { usePageData } from '@rspress/core/runtime';
6
5
  import { Badge, Button, getCustomMDXComponent } from '@rspress/core/theme';
7
6
  import { useCallback, useEffect, useMemo, useState } from 'react';
8
7
  import { useTranslation } from '../hooks/index.js';
9
8
  import { Directive } from './Directive.js';
10
9
  import { Markdown } from './_Markdown.js';
10
+ import crdsMap from 'doom-@api-crdsMap';
11
11
  export const K8sCrdSchemaPart = ({ name, parent, schema: property, expandAll, }) => {
12
12
  const [X] = useState(getCustomMDXComponent);
13
13
  const [open, setOpen] = useState(expandAll);
@@ -54,17 +54,16 @@ export const K8sCrdSchema = ({ schema, version, }) => {
54
54
  };
55
55
  export const K8sCrd = ({ name, crdPath }) => {
56
56
  const [X] = useState(getCustomMDXComponent);
57
- const { page } = usePageData();
58
- const [, crd] = useMemo(() => Object.entries(page.crdsMap || {}).find(([pathname, crd]) => {
57
+ const [, crd] = useMemo(() => Object.entries(crdsMap).find(([pathname, crd]) => {
59
58
  if (crdPath && pathname !== crdPath) {
60
59
  return false;
61
60
  }
62
61
  return crd.metadata.name === name;
63
- }) || [], [page]);
62
+ }) || [], []);
64
63
  if (!crd) {
65
64
  console.error(`No CustomResourceDefinition found for ${name}`);
66
65
  return null;
67
66
  }
68
- return (_jsxs(_Fragment, { children: [_jsxs(X.p, { children: [_jsx(X.code, { children: crd.spec.group }), _jsx(Badge, { children: "group" })] }), crd.spec.versions.map((version) => (_jsx(K8sCrdSchema, { schema: version.schema.openAPIV3Schema, version: version.name })))] }));
67
+ return (_jsxs(_Fragment, { children: [_jsxs(X.p, { children: [_jsx(X.code, { children: crd.spec.group }), _jsx(Badge, { children: "group" })] }), crd.spec.versions.map((version) => (_jsx(K8sCrdSchema, { schema: version.schema.openAPIV3Schema, version: version.name }, version.name)))] }));
69
68
  };
70
69
  export default K8sCrd;
@@ -1,9 +1,11 @@
1
1
  import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
- import { useLang, usePageData } from '@rspress/core/runtime';
2
+ import { useLang } from '@rspress/core/runtime';
3
3
  import { getCustomMDXComponent } from '@rspress/core/theme';
4
4
  import { intersection, sortBy } from 'es-toolkit';
5
5
  import { Fragment, useMemo, useState } from 'react';
6
6
  import { useTranslation } from '../hooks/index.js';
7
+ import functionResourcesMap from 'doom-@permission-functionResourcesMap';
8
+ import roleTemplatesMap from 'doom-@permission-roleTemplatesMap';
7
9
  export const VerbsMap = {
8
10
  view: ['get', 'list', 'watch'],
9
11
  create: ['create'],
@@ -48,11 +50,10 @@ const RolesPermission = ({ functionResource, roleTemplates, verb, }) => {
48
50
  };
49
51
  export const K8sPermissionTable = ({ functions }) => {
50
52
  const [X] = useState(getCustomMDXComponent);
51
- const { page } = usePageData();
52
- const functionResourcesMap = useMemo(() => Object.values(page.functionResourcesMap || {}).reduce((acc, curr) => Object.assign(acc, ...curr.map((fr) => ({ [fr.metadata.name]: fr }))), {}), [page.functionResourcesMap]);
53
+ const allFunctionResources = useMemo(() => Object.values(functionResourcesMap).reduce((acc, curr) => Object.assign(acc, ...curr.map((fr) => ({ [fr.metadata.name]: fr }))), {}), []);
53
54
  const functionResources = useMemo(() => {
54
55
  return functions.flatMap((name) => {
55
- const matched = functionResourcesMap[name];
56
+ const matched = allFunctionResources[name];
56
57
  if (!matched) {
57
58
  console.error(`FunctionResource \`${name}\` not found!\n`);
58
59
  return [];
@@ -60,7 +61,7 @@ export const K8sPermissionTable = ({ functions }) => {
60
61
  return matched;
61
62
  });
62
63
  }, [...functions]);
63
- const roleTemplates = useMemo(() => sortBy(Object.values(page.roleTemplatesMap || {}).flat(), [
64
+ const roleTemplates = useMemo(() => sortBy(Object.values(roleTemplatesMap).flat(), [
64
65
  (it) => it.metadata.annotations['auth.cpaas.io/role.id'],
65
66
  ]), []);
66
67
  const t = useTranslation();
@@ -6,9 +6,10 @@ import { OpenAPIV3 } from 'openapi-types';
6
6
  import { useMemo, useState } from 'react';
7
7
  import { DEFAULT_COMMON_REFS, omitRoutePathRefs, resolveRef } from '../utils.js';
8
8
  import { HeadingTitle } from './_HeadingTitle.js';
9
+ import { Markdown } from './_Markdown.js';
9
10
  import { RefLink } from './_RefLink.js';
10
11
  import OpenAPIRef from './OpenAPIRef.js';
11
- import { Markdown } from './_Markdown.js';
12
+ import openapisMap from 'doom-@api-openapisMap';
12
13
  export const OpenAPIParameters = ({ parameters, openapi, }) => {
13
14
  const [X] = useState(getCustomMDXComponent);
14
15
  return (_jsx(X.ul, { children: parameters.map((param, index) => {
@@ -79,7 +80,7 @@ export const OpenAPIPath = ({ path, openapiPath: openapiPath_, pathPrefix: pathP
79
80
  const pathPrefix = pathPrefix_ ?? (page.pathPrefix || '');
80
81
  const slugger = useMemo(() => new BananaSlug(), []);
81
82
  const [pathItem, openapi, openapiPath, refs] = useMemo(() => {
82
- for (const [pathname, openapi] of Object.entries(page.openapisMap || {})) {
83
+ for (const [pathname, openapi] of Object.entries(openapisMap)) {
83
84
  if (openapiPath_ && pathname !== openapiPath_) {
84
85
  continue;
85
86
  }
@@ -97,7 +98,7 @@ export const OpenAPIPath = ({ path, openapiPath: openapiPath_, pathPrefix: pathP
97
98
  }
98
99
  }
99
100
  return [];
100
- }, [page.openapiMap]);
101
+ }, []);
101
102
  if (!pathItem || !openapi) {
102
103
  console.error(`No OpenAPI path definition found for ${path}`);
103
104
  return null;
@@ -6,6 +6,7 @@ import { DEFAULT_COMMON_REFS, modelName, omitRoutePathRefs, resolveRef, } from '
6
6
  import { HeadingTitle } from './_HeadingTitle.js';
7
7
  import { Markdown } from './_Markdown.js';
8
8
  import { RefLink } from './_RefLink.js';
9
+ import openapisMap from 'doom-@api-openapisMap';
9
10
  export const OpenAPIProperties = ({ properties, openapi, }) => {
10
11
  const [X] = useState(getCustomMDXComponent);
11
12
  return (_jsx(X.ul, { children: Object.entries(properties).map(([name, prop], index) => {
@@ -68,7 +69,7 @@ const getRefsForSchema = (openapi, schema, knownRefs) => {
68
69
  export const OpenAPIRef = ({ schema, openapiPath: openapiPath_, collectRefs = true, }) => {
69
70
  const { page } = usePageData();
70
71
  const [schemaItem, openapi, openapiPath] = useMemo(() => {
71
- for (const [pathname, openapi] of Object.entries(page.openapisMap || {})) {
72
+ for (const [pathname, openapi] of Object.entries(openapisMap)) {
72
73
  if (openapiPath_ && pathname !== openapiPath_) {
73
74
  continue;
74
75
  }
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Get the value from the promise, or throw.
3
+ *
4
+ * This function add some symbol properties to the promise.
5
+ *
6
+ * @param promise
7
+ * @returns
8
+ * @see https://github.com/facebook/react/pull/25084
9
+ */
10
+ declare function usePromise<T>(promise: Promise<T>): T;
11
+ export declare const use: typeof usePromise;
12
+ export {};
@@ -0,0 +1,37 @@
1
+ import React from 'react';
2
+ const PROMISE_STATUS = Symbol('PROMISE_STATUS');
3
+ const PROMISE_VALUE = Symbol('PROMISE_VALUE');
4
+ const PROMISE_REASON = Symbol('PROMISE_REASON');
5
+ /**
6
+ * Get the value from the promise, or throw.
7
+ *
8
+ * This function add some symbol properties to the promise.
9
+ *
10
+ * @param promise
11
+ * @returns
12
+ * @see https://github.com/facebook/react/pull/25084
13
+ */
14
+ function usePromise(promise) {
15
+ const modified = promise;
16
+ switch (modified[PROMISE_STATUS]) {
17
+ case 'fulfilled':
18
+ return modified[PROMISE_VALUE];
19
+ case 'rejected':
20
+ throw modified[PROMISE_REASON];
21
+ case 'pending':
22
+ throw modified;
23
+ default: {
24
+ modified[PROMISE_STATUS] = 'pending';
25
+ throw modified.then((value) => {
26
+ modified[PROMISE_STATUS] = 'fulfilled';
27
+ modified[PROMISE_VALUE] = value;
28
+ }, (reason) => {
29
+ modified[PROMISE_STATUS] = 'rejected';
30
+ modified[PROMISE_REASON] = reason;
31
+ });
32
+ }
33
+ }
34
+ }
35
+ export const use =
36
+ // @ts-expect-error - unavailable in react <19
37
+ React.use || usePromise;
@@ -1,20 +1,13 @@
1
1
  import { PageData } from '@rspress/core';
2
- import { OpenAPIV3_1 } from 'openapi-types';
3
- import type { CustomResourceDefinition } from '../plugins/api/types.js';
4
2
  import type { DoomSite } from '../shared/types.js';
5
- import { FunctionResource, RoleTemplate } from '../plugins/permission/types.js';
6
3
  export interface SiteBrand {
7
4
  company: string;
8
5
  product: string;
9
6
  productShort: string;
10
7
  }
11
8
  export interface ExtendedPage {
12
- crdsMap?: Record<string, CustomResourceDefinition>;
13
- openapisMap?: Record<string, OpenAPIV3_1.Document>;
14
9
  references?: Record<string, string>;
15
10
  pathPrefix?: string;
16
- functionResourcesMap?: Record<string, FunctionResource[]>;
17
- roleTemplatesMap?: Record<string, RoleTemplate[]>;
18
11
  sites?: DoomSite[];
19
12
  v?: string;
20
13
  }
@@ -0,0 +1,3 @@
1
+ export declare const JSON_EXTENSION = ".json";
2
+ export declare const YAML_EXTENSIONS: readonly [".yaml", ".yml"];
3
+ export declare const STATIC_CONFIG_EXTENSIONS: readonly [".json", ".yaml", ".yml"];
@@ -0,0 +1,6 @@
1
+ export const JSON_EXTENSION = '.json';
2
+ export const YAML_EXTENSIONS = ['.yaml', '.yml'];
3
+ export const STATIC_CONFIG_EXTENSIONS = [
4
+ JSON_EXTENSION,
5
+ ...YAML_EXTENSIONS,
6
+ ];
@@ -1,3 +1,5 @@
1
+ import { StringMapper } from '../shared/types.js';
1
2
  export declare const baseResolve: (...paths: string[]) => string;
2
3
  export declare const pkgResolve: (...paths: string[]) => string;
3
4
  export declare const resolveStaticConfig: <T = unknown>(filepath: string) => Promise<T>;
5
+ export declare function generateRuntimeModule<T, R = T | void>(patterns: string[], kind: string, cwd: string, isProd: boolean, mapper?: (input: T) => R | Promise<R>): Promise<StringMapper>;
@@ -3,6 +3,7 @@ import path from 'node:path';
3
3
  import { parse } from 'yaml';
4
4
  import { BASE_DIR, PKG_DIR } from './constants.js';
5
5
  import { JSON_EXTENSION, YAML_EXTENSIONS } from '../cli/constants.js';
6
+ import { glob } from 'tinyglobby';
6
7
  export const baseResolve = (...paths) => path.resolve(BASE_DIR, ...paths);
7
8
  export const pkgResolve = (...paths) => path.resolve(PKG_DIR, ...paths);
8
9
  export const resolveStaticConfig = async (filepath) => {
@@ -19,3 +20,18 @@ export const resolveStaticConfig = async (filepath) => {
19
20
  }
20
21
  throw new Error(`Unsupported file extension: ${extname}`);
21
22
  };
23
+ export async function generateRuntimeModule(patterns, kind, cwd, isProd, mapper) {
24
+ const runtimeModules = {};
25
+ const files = await glob(patterns, { cwd });
26
+ for (const file of files) {
27
+ const result = await resolveStaticConfig(path.resolve(cwd, file));
28
+ runtimeModules[`doom-@${kind}/${file}.mjs`] =
29
+ `export default ${JSON.stringify((await mapper?.(result)) ?? result, null, isProd ? 0 : 2)}`;
30
+ }
31
+ runtimeModules[`doom-@${kind}Map`] =
32
+ files
33
+ .map((file, index) => `import _${index} from 'doom-@${kind}/${file}.mjs'`)
34
+ .join('\n') +
35
+ `\nexport default {${files.map((file, index) => `'${file}':_${index}`).join(',')}}`;
36
+ return runtimeModules;
37
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alauda/doom",
3
- "version": "0.11.2",
3
+ "version": "0.11.3",
4
4
  "type": "module",
5
5
  "description": "Doctor Doom making docs.",
6
6
  "repository": {
@@ -40,7 +40,7 @@
40
40
  "dependencies": {
41
41
  "@inquirer/prompts": "^7.3.2",
42
42
  "@openapi-contrib/openapi-schema-to-json-schema": "^5.1.0",
43
- "@rspress/core": "1.41.3",
43
+ "@rspress/core": "1.42.0",
44
44
  "@shikijs/transformers": "^3.0.0",
45
45
  "chokidar": "^4.0.3",
46
46
  "clsx": "^2.1.1",
@@ -66,11 +66,12 @@
66
66
  "devDependencies": {
67
67
  "@eslint/js": "^9.20.0",
68
68
  "@swc-node/register": "^1.10.9",
69
- "@swc/core": "1.10.17",
69
+ "@swc/core": "1.10.18",
70
70
  "@types/ejs": "^3.1.5",
71
71
  "@types/eslint__js": "^8.42.3",
72
72
  "@types/node": "^18.19.76",
73
73
  "@types/picomatch": "^3.0.2",
74
+ "@types/react": "^18.3.18",
74
75
  "@types/react-dom": "^18.3.5",
75
76
  "@types/swagger2openapi": "^7.0.4",
76
77
  "@unts/patch-package": "^8.0.0",