Files
shoelace/scripts/make-react.js

104 lines
3.3 KiB
JavaScript
Raw Normal View History

2022-01-26 08:46:20 -05:00
import commandLineArgs from 'command-line-args';
2021-11-04 07:27:18 -04:00
import fs from 'fs';
import path from 'path';
import chalk from 'chalk';
2022-07-27 16:17:23 -04:00
import { deleteSync } from 'del';
2021-11-04 07:27:18 -04:00
import prettier from 'prettier';
import prettierConfig from '../prettier.config.cjs';
import { getAllComponents } from './shared.js';
2022-01-26 08:46:20 -05:00
const { outdir } = commandLineArgs({ name: 'outdir', type: String });
const reactDir = path.join('./src/react');
2021-11-04 07:27:18 -04:00
// Clear build directory
2022-07-27 16:17:23 -04:00
deleteSync(reactDir);
2022-01-26 08:46:20 -05:00
fs.mkdirSync(reactDir, { recursive: true });
2021-11-04 07:27:18 -04:00
// Fetch component metadata
2022-01-26 08:46:20 -05:00
const metadata = JSON.parse(fs.readFileSync(path.join(outdir, 'custom-elements.json'), 'utf8'));
2021-11-04 07:27:18 -04:00
const components = getAllComponents(metadata);
const index = [];
components.map(component => {
const tagWithoutPrefix = component.tagName.replace(/^sl-/, '');
2022-01-26 08:46:20 -05:00
const componentDir = path.join(reactDir, tagWithoutPrefix);
2021-11-04 07:27:18 -04:00
const componentFile = path.join(componentDir, 'index.ts');
const importPath = component.path.replace(/\.js$/, '.component.js');
const eventImports = (component.events || [])
.map(event => `import type { ${event.eventName} } from '../../../src/events/events';`)
.join('\n');
const eventExports = (component.events || [])
.map(event => `export type { ${event.eventName} } from '../../../src/events/events';`)
.join('\n');
const eventNameImport =
(component.events || []).length > 0 ? `import { type EventName } from '@lit-labs/react';` : ``;
const events = (component.events || [])
.map(event => `${event.reactName}: '${event.name}' as EventName<${event.eventName}>`)
.join(',\n');
2021-11-04 07:27:18 -04:00
fs.mkdirSync(componentDir, { recursive: true });
2021-11-04 07:27:18 -04:00
const jsDoc = component.jsDoc || '';
2021-11-04 07:27:18 -04:00
const source = prettier.format(
`
import * as React from 'react';
import { createComponent } from '@lit-labs/react';
import Component from '../../${importPath}';
${eventNameImport}
${eventImports}
${eventExports}
2023-08-17 16:55:07 -04:00
export type ForwardComponent<
Element extends HTMLElement,
ReactComponent extends React.ElementType
> = React.JSXElementConstructor<
React.ComponentPropsWithoutRef<ReactComponent> & {
ref?: React.ForwardedRef<Element>;
} &
// Adds { displayName?: string, propTypes?: {} } etc.
Omit<React.FC<ReactComponent>, 'ref'>
>;
const tagName = '${component.tagName}'
const component = createComponent({
tagName,
elementClass: Component,
react: React,
events: {
2021-11-04 07:27:18 -04:00
${events}
},
displayName: "${component.name}"
})
${jsDoc}
class SlComponent extends React.Component<Parameters<typeof component>[0]> {
constructor (...args: Parameters<typeof component>) {
super(...args)
Component.define(tagName)
2021-11-04 07:27:18 -04:00
}
render () {
const { children, ...props } = this.props
return React.createElement(component, props, children)
}
}
2023-08-17 16:55:07 -04:00
export default SlComponent as ForwardComponent<Component, typeof SlComponent>;
2021-11-04 07:27:18 -04:00
`,
Object.assign(prettierConfig, {
parser: 'babel-ts'
})
);
2023-06-22 10:56:24 -04:00
index.push(`export { default as ${component.name} } from './${tagWithoutPrefix}/index.js';`);
2021-11-04 07:27:18 -04:00
fs.writeFileSync(componentFile, source, 'utf8');
});
// Generate the index file
2022-01-26 08:46:20 -05:00
fs.writeFileSync(path.join(reactDir, 'index.ts'), index.join('\n'), 'utf8');