component-props-plugin.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /* eslint-env node */
  2. /* eslint import/no-nodejs-modules:off */
  3. import path from 'path';
  4. import * as docgen from 'react-docgen-typescript';
  5. import {glob} from 'glob';
  6. import SignedFileGenerator from './signed-file-generator';
  7. type GlobPattern = Parameters<typeof glob>[0];
  8. type Options = {
  9. cwd: string;
  10. output: string;
  11. pattern: GlobPattern;
  12. };
  13. class ComponentPropsPlugin extends SignedFileGenerator<string> {
  14. constructor(options: Options) {
  15. super('ComponentPropsPlugin', options);
  16. }
  17. generateData(files: string[]) {
  18. const parserOptions = {
  19. // skipPropsWithoutDoc: true,
  20. shouldExtractLiteralValuesFromEnum: true,
  21. propFilter: (prop: docgen.PropItem) => {
  22. if (prop.declarations !== undefined && prop.declarations.length > 0) {
  23. const hasPropAdditionalDescription = prop.declarations.find(declaration => {
  24. return !declaration.fileName.includes('node_modules');
  25. });
  26. return Boolean(hasPropAdditionalDescription);
  27. }
  28. return true;
  29. },
  30. };
  31. const tsConfigParser = docgen.withCustomConfig(
  32. path.join(__dirname, '..', 'tsconfig.json'),
  33. parserOptions
  34. );
  35. const staticPrefix = path.join(__dirname, '..', 'static');
  36. const filePaths = files.map(file => path.join(staticPrefix, file));
  37. const types = tsConfigParser.parse(filePaths);
  38. types.forEach(type => {
  39. // @ts-ignore
  40. delete type.description;
  41. delete type.tags;
  42. Object.keys(type.props).forEach(key => {
  43. delete type.props[key].declarations;
  44. // @ts-ignore
  45. delete type.props[key].description;
  46. });
  47. });
  48. return Promise.resolve(JSON.stringify(types));
  49. }
  50. sourceTemplate(data: string, signingToken: string) {
  51. return `
  52. // THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
  53. //
  54. // Generated by ${this.name} (using ${path.basename(__filename)})
  55. // ${signingToken}
  56. //
  57. // This script contains a list of components and their typescript props to be
  58. // imported by our ui component library. Used to generate Knobs.
  59. type IProps = {
  60. declarations: {
  61. fileName: string;
  62. name: string;
  63. }[];
  64. defaultValue: any;
  65. // description: string;
  66. name: string;
  67. required: boolean;
  68. type: {name: string};
  69. };
  70. type IModule = {
  71. // description: string,
  72. displayName: string;
  73. // tags: Record<string, unknown>,
  74. filePath: string;
  75. methods: unknown[];
  76. props: Record<string, IProps>;
  77. };
  78. const ComponentProps: IModule[] = ${data}
  79. export {ComponentProps}
  80. `;
  81. }
  82. }
  83. export default ComponentPropsPlugin;