|
| 1 | +import { mkdirSync, writeFileSync } from 'fs'; |
| 2 | +import { dirname } from 'path'; |
| 3 | +import { SpecReader as BaseSpecReader } from '../spec-reader.js'; |
| 4 | + |
| 5 | +export class SpecReader extends BaseSpecReader { |
| 6 | + writeComponentSpec(path, options = {}) { |
| 7 | + const { plural = this.plural, banner = '' } = options; |
| 8 | + |
| 9 | + mkdirSync(dirname(path), { recursive: true }); |
| 10 | + |
| 11 | + const componentSpec = this.getWebComponentSpec(this.spec, { plural }); |
| 12 | + |
| 13 | + let content = ''; |
| 14 | + if (banner) { |
| 15 | + content += `${banner}\n`; |
| 16 | + } |
| 17 | + content += `// Auto-generated from spec\n`; |
| 18 | + content += `export default ${JSON.stringify(componentSpec, null, 2)};\n`; |
| 19 | + |
| 20 | + writeFileSync(path, content); |
| 21 | + return this; |
| 22 | + } |
| 23 | + |
| 24 | + writeSpecJSON(path) { |
| 25 | + mkdirSync(dirname(path), { recursive: true }); |
| 26 | + |
| 27 | + const content = JSON.stringify(this.spec, null, 2) + '\n'; |
| 28 | + writeFileSync(path, content); |
| 29 | + return this; |
| 30 | + } |
| 31 | + |
| 32 | + writeAll(basePath, options = {}) { |
| 33 | + this.writeComponentSpec(`${basePath}.component.js`, options); |
| 34 | + this.writeSpecJSON(`${basePath}.spec.json`); |
| 35 | + |
| 36 | + // If spec supports plural, generate plural component spec |
| 37 | + if (this.spec.supportsPlural) { |
| 38 | + const pluralOptions = { ...options, plural: true }; |
| 39 | + const pluralName = this.spec.pluralTagName?.replace('ui-', '') || 'plural'; |
| 40 | + const pluralPath = dirname(basePath) + '/' + pluralName + '.component.js'; |
| 41 | + |
| 42 | + this.writeComponentSpec(pluralPath, pluralOptions); |
| 43 | + } |
| 44 | + |
| 45 | + return this; |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +// Re-export everything from base module for convenience |
| 50 | +export * from '../spec-reader.js'; |
0 commit comments