|
1 | | -// create some magic here! |
| 1 | +import fs from 'fs'; |
| 2 | +import path from 'path'; |
| 3 | + |
| 4 | +import globby from 'globby'; |
| 5 | +import { promisify } from 'util'; |
| 6 | + |
| 7 | +import Joi from '@hapi/joi'; |
| 8 | +import reactDocgen from 'react-docgen'; |
| 9 | + |
| 10 | +import { ParserOptions } from '@babel/parser'; |
| 11 | +import { Plugin, OptionValidationContext, DocusaurusContext } from '@docusaurus/types'; |
| 12 | +import { asyncMap } from './utils'; |
| 13 | + |
| 14 | +type Handler = (doc: any, definition: any, parser: { parse: (s: string) => {} }) => void; |
| 15 | + |
| 16 | +type Options = { |
| 17 | + src: string | string[]; |
| 18 | + docgen?: { |
| 19 | + /** |
| 20 | + * See react-docgen docs |
| 21 | + * @see https://github.com/reactjs/react-docgen#resolver |
| 22 | + */ |
| 23 | + resolver?: (ast: Record<any, any>) => any; |
| 24 | + |
| 25 | + /** |
| 26 | + * See react-docgen docs |
| 27 | + * @see https://github.com/reactjs/react-docgen#handlers |
| 28 | + */ |
| 29 | + handlers?: Handler[]; |
| 30 | + }; |
| 31 | + parserOptions?: ParserOptions; |
| 32 | + babel?: { |
| 33 | + babelrc?: boolean; |
| 34 | + babelrcRoots?: boolean | string | string[]; |
| 35 | + root?: string; |
| 36 | + rootMode?: 'root' | 'upward' | 'upward-optional'; |
| 37 | + envName?: string; |
| 38 | + configFile?: string | boolean; |
| 39 | + }; |
| 40 | +}; |
| 41 | + |
| 42 | +const readFile = promisify(fs.readFile); |
| 43 | + |
| 44 | +export default function plugin( |
| 45 | + context: DocusaurusContext, |
| 46 | + { src, docgen = {}, babel = {}, parserOptions }: Options |
| 47 | +): Plugin<{ file: string; docgen: Record<any, any> }[]> { |
| 48 | + return { |
| 49 | + name: 'docusaurus-plugin-react-docgen', |
| 50 | + async loadContent() { |
| 51 | + const fileOpts: { encoding: BufferEncoding } = { encoding: 'utf-8' }; |
| 52 | + const files = await globby(src); |
| 53 | + |
| 54 | + return ( |
| 55 | + await asyncMap(files, async file => { |
| 56 | + try { |
| 57 | + return { |
| 58 | + docgen: reactDocgen.parse( |
| 59 | + await readFile(file, fileOpts), |
| 60 | + docgen.resolver, |
| 61 | + docgen.handlers, |
| 62 | + { |
| 63 | + filename: file, |
| 64 | + parserOptions, |
| 65 | + ...babel, |
| 66 | + } |
| 67 | + ), |
| 68 | + file, |
| 69 | + }; |
| 70 | + } catch (e) { |
| 71 | + console.warn(e.message, file); |
| 72 | + } |
| 73 | + }) |
| 74 | + ).filter(Boolean); |
| 75 | + }, |
| 76 | + async contentLoaded({ content, actions }) { |
| 77 | + const re = /(?:\.d)?\.[jt]sx?$/gi; |
| 78 | + const { createData } = actions; |
| 79 | + |
| 80 | + for (let i = 0; i < content.length; i++) { |
| 81 | + await createData( |
| 82 | + path.basename(content[i].file.toLowerCase()).replace(re, '.json'), |
| 83 | + JSON.stringify(content[i].docgen) |
| 84 | + ); |
| 85 | + } |
| 86 | + }, |
| 87 | + }; |
| 88 | +} |
| 89 | + |
| 90 | +export const validateOptions = ({ options, validate }: OptionValidationContext<Options>) => { |
| 91 | + return validate( |
| 92 | + Joi.object({ |
| 93 | + src: Joi.alternatives(Joi.string(), Joi.array().min(1).items(Joi.string())).required(), |
| 94 | + docgen: Joi.object({ |
| 95 | + resolver: Joi.object().instance(Function), |
| 96 | + handlers: Joi.alternatives( |
| 97 | + Joi.object().instance(Function), |
| 98 | + Joi.array().min(1).items(Joi.object().instance(Function)) |
| 99 | + ), |
| 100 | + }), |
| 101 | + babel: Joi.object({ |
| 102 | + babelrc: Joi.boolean(), |
| 103 | + babelrcRoots: [Joi.boolean(), Joi.string(), Joi.array().items(Joi.string())], |
| 104 | + root: Joi.string(), |
| 105 | + rootMode: Joi.string().pattern(/root | upward | upward-optional/), |
| 106 | + envName: Joi.string(), |
| 107 | + configFile: [Joi.string(), Joi.boolean()], |
| 108 | + }), |
| 109 | + }), |
| 110 | + options |
| 111 | + ); |
| 112 | +}; |
0 commit comments