Skip to content

Commit 7da9ed7

Browse files
committed
Feat: Add write to file support to specs
1 parent c80c28c commit 7da9ed7

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

packages/specs/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"module": "src/index.js",
1212
"exports": {
1313
".": {
14+
"node": "./src/node/spec-reader-node.js",
1415
"import": "./src/index.js",
1516
"browser": "./dist/bundle/index.min.js",
1617
"importmap": "./dist/cdn/index.min.js"
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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

Comments
 (0)