-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(build): new script that generate extended component files for `w…
…ca` to be able to analyze #85
- Loading branch information
Showing
2 changed files
with
61 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
`; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |