Files
webawesome/scripts/make-react.js

80 lines
2.5 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';
2022-07-27 16:17:23 -04:00
import { deleteSync } from 'del';
2021-11-04 07:27:18 -04:00
import prettier from 'prettier';
2023-10-12 15:03:38 -04:00
import { default as prettierConfig } from '../prettier.config.js';
2021-11-04 07:27:18 -04:00
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 = [];
2023-10-19 10:31:24 -04:00
for await (const component of components) {
2023-09-08 13:45:49 -04:00
const tagWithoutPrefix = component.tagName.replace(/^wa-/, '');
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 '../../events/events';`)
.join('\n');
const eventExports = (component.events || [])
.map(event => `export type { ${event.eventName} } from '../../events/events';`)
.join('\n');
2023-10-13 13:53:41 -04:00
const eventNameImport = (component.events || []).length > 0 ? `import { type EventName } from '@lit/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 || '';
2023-10-12 15:03:38 -04:00
const source = await prettier.format(
2021-11-04 07:27:18 -04:00
`
import * as React from 'react';
2023-10-16 12:39:21 -04:00
import { createComponent } from '@lit/react';
2021-11-04 07:27:18 -04:00
import Component from '../../${importPath}';
${eventNameImport}
${eventImports}
${eventExports}
const tagName = '${component.tagName}'
Component.define('${component.tagName}')
${jsDoc}
const reactWrapper = createComponent({
tagName,
elementClass: Component,
react: React,
events: {
2021-11-04 07:27:18 -04:00
${events}
},
displayName: "${component.name}"
})
export default reactWrapper
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');
2023-10-19 10:31:24 -04:00
}
2021-11-04 07:27:18 -04:00
// Generate the index file
2022-01-26 08:46:20 -05:00
fs.writeFileSync(path.join(reactDir, 'index.ts'), index.join('\n'), 'utf8');