|
1 | 1 | import type * as CEM from 'custom-elements-manifest'; |
2 | | -import javascript from 'dedent'; |
| 2 | +import dedent from 'dedent'; |
3 | 3 | import { dirname, join, relative } from 'node:path'; |
4 | 4 | import { fileURLToPath } from 'node:url'; |
5 | 5 | import { readFile, writeFile, mkdir } from 'node:fs/promises'; |
@@ -40,26 +40,39 @@ async function writeReactWrapper( |
40 | 40 | if (!tagName) { |
41 | 41 | throw new NonCriticalError(`declaration does not have a tag name: ${decl.name}`); |
42 | 42 | } else { |
| 43 | + const javascript = dedent; |
| 44 | + const typescript = dedent; |
43 | 45 | const { name: Class } = ceExport; |
44 | 46 | const events = decl.events ?? []; |
45 | 47 | const outDirPath = |
46 | 48 | typeof outDirPathOrURL === 'string' ? outDirPathOrURL |
47 | 49 | : fileURLToPath(outDirPathOrURL); |
48 | 50 | const outPath = join(outDirPath, path); |
49 | 51 | await mkdir(dirname(outPath), { recursive: true }); |
| 52 | + const reactComponentName = getDeprefixedClassName(Class); |
| 53 | + const eventsMap = `{${events.map(event => ` |
| 54 | + ${getEventReactPropName(event)}: '${event.name}'`).join(',')}${events.length ? `, |
| 55 | + ` : ''}}`; |
| 56 | + const eventsInterface = eventsMap.replace(/\s+/g, ' ').replaceAll(',', ';').replace('; }', ' }'); |
50 | 57 | await writeFile(outPath, javascript`// ${path} |
51 | 58 | import { createComponent } from '@lit-labs/react'; |
52 | 59 | import react from 'react'; |
53 | 60 | import { ${Class} as elementClass } from '@patternfly/elements/${module.path}'; |
54 | | - export const ${getDeprefixedClassName(Class)} = createComponent({ |
| 61 | + export const ${reactComponentName} = createComponent({ |
55 | 62 | tagName: '${decl.tagName}', |
56 | 63 | elementClass, |
57 | 64 | react, |
58 | | - events: {${events.map(event => ` |
59 | | - ${getEventReactPropName(event)}: '${event.name}'`).join(',')}${events.length ? `, |
60 | | - ` : ''}}, |
| 65 | + events: ${eventsMap}, |
61 | 66 | }); |
62 | 67 |
|
| 68 | + `, 'utf8'); |
| 69 | + await writeFile(outPath.replace('.js', '.d.ts'), typescript`// ${path} |
| 70 | + declare module '@patternfly/elements/react/pf-button/pf-button.js' { |
| 71 | + import type { ReactWebComponent } from '@lit-labs/react'; |
| 72 | + import type { ${Class} } from '@patternfly/elements/${module.path}'; |
| 73 | + export const ${reactComponentName}: ReactWebComponent<${Class}, ${eventsInterface}>; |
| 74 | + } |
| 75 | +
|
63 | 76 | `, 'utf8'); |
64 | 77 | return { tagName, outPath }; |
65 | 78 | } |
|
0 commit comments