shoelace/scripts/make-react.js

80 wiersze
2.5 KiB
JavaScript
Czysty Zwykły widok Historia

2022-01-26 13:46:20 +00:00
import commandLineArgs from 'command-line-args';
2021-11-04 11:27:18 +00:00
import fs from 'fs';
import path from 'path';
2022-07-27 20:17:23 +00:00
import { deleteSync } from 'del';
2021-11-04 11:27:18 +00:00
import prettier from 'prettier';
2023-10-12 17:56:11 +00:00
import { default as prettierConfig } from '../prettier.config.js';
2021-11-04 11:27:18 +00:00
import { getAllComponents } from './shared.js';
2022-01-26 13:46:20 +00:00
const { outdir } = commandLineArgs({ name: 'outdir', type: String });
const reactDir = path.join('./src/react');
2021-11-04 11:27:18 +00:00
// Clear build directory
2022-07-27 20:17:23 +00:00
deleteSync(reactDir);
2022-01-26 13:46:20 +00:00
fs.mkdirSync(reactDir, { recursive: true });
2021-11-04 11:27:18 +00:00
// Fetch component metadata
2022-01-26 13:46:20 +00:00
const metadata = JSON.parse(fs.readFileSync(path.join(outdir, 'custom-elements.json'), 'utf8'));
2021-11-04 11:27:18 +00:00
const components = getAllComponents(metadata);
const index = [];
for await (const component of components) {
2021-11-04 11:27:18 +00:00
const tagWithoutPrefix = component.tagName.replace(/^sl-/, '');
2022-01-26 13:46:20 +00:00
const componentDir = path.join(reactDir, tagWithoutPrefix);
2021-11-04 11:27:18 +00: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 17:53:12 +00: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 11:27:18 +00:00
fs.mkdirSync(componentDir, { recursive: true });
2021-11-04 11:27:18 +00:00
const jsDoc = component.jsDoc || '';
2023-10-12 17:56:11 +00:00
const source = await prettier.format(
2021-11-04 11:27:18 +00:00
`
import * as React from 'react';
import { createComponent } from '@lit/react';
2021-11-04 11:27:18 +00: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 11:27:18 +00:00
${events}
},
displayName: "${component.name}"
})
export default reactWrapper
2021-11-04 11:27:18 +00:00
`,
Object.assign(prettierConfig, {
parser: 'babel-ts'
})
);
2023-06-22 14:56:24 +00:00
index.push(`export { default as ${component.name} } from './${tagWithoutPrefix}/index.js';`);
2021-11-04 11:27:18 +00:00
fs.writeFileSync(componentFile, source, 'utf8');
}
2021-11-04 11:27:18 +00:00
// Generate the index file
2022-01-26 13:46:20 +00:00
fs.writeFileSync(path.join(reactDir, 'index.ts'), index.join('\n'), 'utf8');