diff --git a/scripts/build/extendedComponentTemplate.mjs b/scripts/build/extendedComponentTemplate.mjs new file mode 100644 index 0000000..5667884 --- /dev/null +++ b/scripts/build/extendedComponentTemplate.mjs @@ -0,0 +1,18 @@ +export default (code, sourcePath) => { + const defaultTag = (code.match(/static register\(name \= (.+)\)/) || code.match(/customElements.get\((.+?)\)/))[1]; + const className = code.match(/export class (.+) extends/)[1]; + + if (!defaultTag || !className) { + return code; + } + return ` + import { ${className} } from '${sourcePath}'; + + class ${className}WCA extends ${className} {} + + if (!customElements.get(${defaultTag})) { + customElements.define(${defaultTag}, ${className}WCA); + } + + `; +}; diff --git a/scripts/build/extendedComponentWriter.mjs b/scripts/build/extendedComponentWriter.mjs new file mode 100644 index 0000000..c1bdab6 --- /dev/null +++ b/scripts/build/extendedComponentWriter.mjs @@ -0,0 +1,43 @@ +import fs from 'fs'; +import path from 'path'; +import glob from 'glob'; +import util from 'util'; +import getTemplatedComponentCode from './extendedComponentTemplate.mjs'; + +const promisifiedGlob = util.promisify(glob); + +const WAC_DIR = path.resolve(process.cwd(), './docs/wca'); + +async function globPath(sources) { + try { + const fileArrays = await Promise.all( + sources.map(source => promisifiedGlob(source)) + ); + return fileArrays.flat(); + } catch (err) { + console.error('Error processing glob patterns:', err); + throw err; // Re-throw to handle failure at caller + } +} + +async function createExtendsFile(filePaths) { + if (!fs.existsSync(WAC_DIR)) { + await fs.promises.mkdir(WAC_DIR, { recursive: true }); + } + + for (const filePath of filePaths) { + const resolvedPath = path.resolve(process.cwd(), filePath); + const fileContent = await fs.promises.readFile(resolvedPath, 'utf-8',); + const newPath = path.resolve(WAC_DIR, `${path.basename(filePath)}`); + const newCode = getTemplatedComponentCode(fileContent, path.relative(WAC_DIR, filePath)); + fs.writeFileSync(newPath, newCode); + } +} + +async function main() { + // files to analyze + const filePaths = await globPath(process.argv.slice(2)); + await createExtendsFile(filePaths); +} + +main();