| 13 | * @returns {string} - the path of generated xxx.d.ts |
| 14 | */ |
| 15 | export default function generateDTS({ |
| 16 | workDir, |
| 17 | dslType = 'react', |
| 18 | }: { |
| 19 | workDir: string; |
| 20 | dslType?: string; |
| 21 | }): { |
| 22 | originalTypePath: string; |
| 23 | newTypePath: string; |
| 24 | } { |
| 25 | const typeDir = path.join(workDir, 'node_modules', `@types/${dslType}`); |
| 26 | const typePath = path.join(typeDir, 'index.d.ts'); |
| 27 | const fileContent = loadFile(typePath); |
| 28 | // const materialParserTypeDir = path.join(workDir, `node_modules/material-parser-types/${type}`); |
| 29 | // ensureDirSync(materialParserTypeDir); |
| 30 | const materialParserTypeDir = typeDir; |
| 31 | const newTypePath = path.join(materialParserTypeDir, 'index.d.ts'); |
| 32 | // if (!pathExistsSync(newTypePath)) { |
| 33 | // copySync( |
| 34 | // path.join(typeDir, 'global.d.ts'), |
| 35 | // path.join(materialParserTypeDir, 'global.d.ts'), |
| 36 | // ); |
| 37 | let newContent = fileContent.replace( |
| 38 | /(?<=interface HTMLAttributes[^e]+)(extends[^}]+)/, |
| 39 | `{ |
| 40 | style?: CSSProperties; |
| 41 | className?: string; |
| 42 | `, |
| 43 | ); |
| 44 | newContent = newContent.replace(/(?<=interface IntrinsicElements {)([^}]+)/, ''); |
| 45 | newContent = newContent.replace(/type LibraryManagedAttributes[^;]+;/, ''); |
| 46 | writeFileSync(newTypePath, newContent); |
| 47 | log('generate dts', newTypePath); |
| 48 | // } else { |
| 49 | // log('found dts', newTypePath); |
| 50 | // } |
| 51 | |
| 52 | return { |
| 53 | originalTypePath: typePath, |
| 54 | newTypePath, |
| 55 | }; |
| 56 | } |