1 import path from "path"
2 import readFiles from "../utils/read-files.js"
3 import { builtinHandlers, parse } from "react-docgen"
4 import { existsSync, mkdirSync, rmSync, writeFileSync } from "fs"
5 import TypedocManager from "../classes/typedoc-manager.js"
6 import chalk from "chalk"
7 import CustomResolver from "../resolvers/custom-resolver.js"
8 import argsPropHandler from "../handlers/argsPropHandler.js"
9
10 type GenerateOptions = {
11 src: string
12 output: string
13 clean?: boolean
14 tsconfigPath: string
15 disableTypedoc: boolean
16 verboseTypedoc: boolean
17 }
18
19 export default async function ({
20 src,
21 output,
22 clean,
23 tsconfigPath,
24 disableTypedoc,
25 verboseTypedoc,
26 }: GenerateOptions) {
27 const fileContents = readFiles(src)
28
29 // optionally use typedoc to add missing props, descriptions
30 // or types.
31 const typedocManager = new TypedocManager({
32 tsconfigPath,
33 disable: disableTypedoc,
34 verbose: verboseTypedoc,
35 })
36
37 // set up typedoc before the output directory is cleaned, so that a failing
38 // setup doesn't leave the existing specs deleted.
39 await typedocManager.setup(src)
40
41 let outputExists = existsSync(output)
42
43 if (clean && outputExists) {
44 // remove the directory which will be created in the next condition block
45 rmSync(output, { recursive: true, force: true })
46 outputExists = false
47 }
48
49 // create output directory if it doesn't exist
50 if (!outputExists) {
51 mkdirSync(output)
52 }
53
54 for (const [filePath, fileContent] of fileContents) {
55 try {
56 const relativePath = path.resolve(filePath)
57 // retrieve the specs of a file
58 const specs = parse(fileContent, {
59 filename: relativePath,
60 resolver: new CustomResolver(typedocManager),
61 handlers: [
62 // Built-in handlers
63 builtinHandlers.childContextTypeHandler,
64 builtinHandlers.codeTypeHandler,
65 builtinHandlers.componentDocblockHandler,
66 builtinHandlers.componentMethodsHandler,
67 builtinHandlers.contextTypeHandler,
68 builtinHandlers.defaultPropsHandler,
69 builtinHandlers.displayNameHandler,
70 builtinHandlers.propDocblockHandler,
71 builtinHandlers.propTypeCompositionHandler,
72 builtinHandlers.propTypeHandler,
73 // Custom handlers
74 (documentation, componentPath) =>
75 argsPropHandler(documentation, componentPath, typedocManager),
76 ],
77 babelOptions: {
78 ast: true,
79 },
80 })
81
82 // write each of the specs into output directory
83 specs.forEach((spec) => {
84 if (!spec.displayName) {
85 return
86 }
87 const specNameSplit = spec.displayName.split(".")
88 let filePath = output
89
90 if (spec.description) {
91 spec.description = typedocManager.normalizeDescription(
92 spec.description
93 )
94 }
95
96 // if typedoc isn't disabled, this method will try to fill
97 // missing descriptions and types, and add missing props.
98 spec = typedocManager.tryFillWithTypedocData(spec, specNameSplit)
99
100 // put the spec in a sub-directory
101 filePath = path.join(filePath, specNameSplit[0])
102 if (!existsSync(filePath)) {
103 mkdirSync(filePath)
104 }
105
106 // write spec to output path
107 writeFileSync(
108 path.join(filePath, `${spec.displayName}.json`),
109 JSON.stringify(spec, null, 2)
110 )
111
112 console.log(chalk.green(`Created spec file for ${spec.displayName}.`))
113 })
114 } catch (e) {
115 console.error(chalk.red(`Failed to parse ${filePath}: ${e}`))
116 }
117 }
118 }