-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
196 lines (173 loc) · 5.74 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/**
* Copyright Zendesk, Inc.
*
* Use of this source code is governed under the Apache License, Version 2.0
* found at http://www.apache.org/licenses/LICENSE-2.0.
*/
import { GlobTask, globby } from 'globby';
import { ParserOptions, withCustomConfig, withDefaultConfig } from 'react-docgen-typescript';
import {
handleErrorMessage,
handleSuccessMessage,
handleWarningMessage
} from '../../utils/index.js';
import { Command } from 'commander';
import { Ora } from 'ora';
import { parse as parseComment } from 'comment-parser';
import { resolve } from 'node:path';
import ts from 'typescript';
type TAGS = Record<string, string>;
type PROPS = Record<
string,
{
description: string;
defaultValue: string;
required: boolean;
type: string;
params: TAGS;
returns?: string;
}
>;
type RETVAL = {
name: string;
description: string;
extends: string;
props: PROPS;
file: string;
}[];
interface ICommandDocgenArgs {
paths: string[] | string;
extensions?: string[];
ignore?: string[];
spinner?: Ora;
}
const DEFAULT_EXTENSIONS = ['js', 'jsx', 'ts', 'tsx'];
const DEFAULT_IGNORE = ['**/*.spec.*', '**/dist/**', '**/node_modules/**'];
/**
* Execute the `cmd-docgen` command.
*
* @param {string[] | string} args.paths Component path globs.
* @param {string[]} [args.extensions] File extensions to consider.
* @param {string[]} [args.ignore] Paths to ignore.
* @param {Ora} [args.spinner] Terminal spinner.
*
* @returns {object} Generated component documentation information.
*/
export const execute = async (
args: ICommandDocgenArgs = {
paths: [],
extensions: DEFAULT_EXTENSIONS,
ignore: DEFAULT_IGNORE
}
): Promise<RETVAL | undefined> => {
let retVal: RETVAL | undefined;
try {
const parserOptions: ParserOptions = {
propFilter: props =>
!(props.description.includes('@ignore') || props.parent?.fileName.includes('node_modules')),
shouldRemoveUndefinedFromOptional: true
};
const globbyOptions: GlobTask['options'] = {
expandDirectories: {
extensions: args.extensions || DEFAULT_EXTENSIONS
},
ignore: args.ignore || DEFAULT_IGNORE
};
// eslint-disable-next-line @typescript-eslint/await-thenable
for await (const path of Array.isArray(args.paths) ? args.paths : [args.paths]) {
const resolvedPath = resolve(path);
/* eslint-disable-next-line @typescript-eslint/unbound-method */
const tsconfigPath = ts.findConfigFile(resolvedPath, ts.sys.fileExists);
const parser = tsconfigPath
? withCustomConfig(tsconfigPath, parserOptions)
: withDefaultConfig(parserOptions);
const paths = await globby(resolvedPath, globbyOptions);
const components = parser.parse(paths);
retVal = components.map(component => {
const props: PROPS = {};
Object.keys(component.props)
.sort(undefined)
.forEach(key => {
const prop = component.props[key];
const type = prop.type.name.replace(/"/gu, "'");
let defaultValue = prop.defaultValue?.value?.toString();
if (
(type === 'string' && defaultValue !== null && defaultValue !== undefined) ||
type.includes(`'${defaultValue}'`)
) {
// Surround default string literals with quotes.
defaultValue = `'${defaultValue}'`;
}
const params: TAGS = {};
let description;
let returns;
if (prop.description) {
description = parseComment(`/** ${prop.description} */`)[0];
description.tags
.filter(tag => tag.tag === 'param')
.forEach(param => {
params[param.name] = param.description;
});
returns = description.tags.find(tag => tag.tag.startsWith('return'));
}
props[key] = {
description: description ? description.description : '',
defaultValue,
required: prop.required,
type,
params,
returns: returns ? returns.description : undefined
};
});
return {
name: component.displayName,
description: component.description,
extends: component.tags ? (component.tags as TAGS).extends : '',
props,
file: component.filePath
};
});
}
} catch (error: unknown) {
handleErrorMessage(error, 'cmd-docgen', args.spinner);
throw error;
}
return retVal;
};
export default (spinner: Ora): Command => {
const command = new Command('cmd-docgen');
return command
.description('generate component documentation')
.argument('<paths...>', 'one or more component paths')
.option('-x --extensions <extensions...>', 'file extensions to consider', DEFAULT_EXTENSIONS)
.option('-i --ignore <ignore...>', 'paths to ignore', DEFAULT_IGNORE)
.option('--pretty', 'pretty-print JSON')
.action(async (paths: string[]) => {
try {
spinner.start();
const options = command.opts();
const result = await execute({
paths,
extensions: options.extensions,
ignore: options.ignore,
spinner
});
if (result) {
if (result.length > 0) {
const space = options.pretty ? 2 : undefined;
const message = JSON.stringify(result, undefined, space);
handleSuccessMessage(message, spinner);
} else {
handleWarningMessage('Component not found', spinner);
}
} else {
throw Error();
}
} catch {
spinner.fail('Unable to generate documentation');
process.exitCode = 1;
} finally {
spinner.stop();
}
});
};