Skip to content

Commit

Permalink
feat(build): new script that generate extended component files for `w…
Browse files Browse the repository at this point in the history
…ca` to be able to analyze #85
  • Loading branch information
sun-mota committed Nov 4, 2024
1 parent 2d7f453 commit 33eb3ea
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
18 changes: 18 additions & 0 deletions scripts/build/extendedComponentTemplate.mjs
Original file line number Diff line number Diff line change
@@ -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);
}
`;
};
43 changes: 43 additions & 0 deletions scripts/build/extendedComponentWriter.mjs
Original file line number Diff line number Diff line change
@@ -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();

0 comments on commit 33eb3ea

Please sign in to comment.