Skip to content

Commit 2aa7b49

Browse files
committed
Feat: Reorganize spec folder to be easier to discover
1 parent f731043 commit 2aa7b49

27 files changed

+352
-385
lines changed

packages/specs/package.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77
"version": "0.18.0",
88
"sideEffects": false,
99
"type": "module",
10-
"main": "src/index.js",
11-
"module": "src/index.js",
10+
"main": "src/browser.js",
11+
"module": "src/browser.js",
1212
"exports": {
1313
".": {
14-
"node": "./src/node/spec-reader-node.js",
15-
"import": "./src/index.js",
16-
"browser": "./dist/bundle/index.min.js",
17-
"importmap": "./dist/cdn/index.min.js"
14+
"node": "./src/server.js",
15+
"browser": "./src/browser.js",
16+
"import": "./src/browser.js",
17+
"default": "./src/browser.js"
1818
}
1919
},
2020
"scripts": {

packages/specs/src/browser.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export * from './helpers.js';
2+
export { SpecReader } from './spec-reader.js';
3+
export * from './states/index.js';
4+
export * from './types/index.js';
5+
export * from './variations/index.js';

packages/specs/src/helpers.js

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import {
2+
ACTIVE_STATE,
3+
DISABLED_STATE,
4+
FOCUS_STATE,
5+
HOVER_STATE,
6+
LOADING_STATE,
7+
PRESSED_STATE,
8+
} from './states/index.js';
9+
10+
import {
11+
ATTACHED_VARIATION,
12+
CIRCULAR_VARIATION,
13+
COLORED_VARIATION,
14+
COMPACT_VARIATION,
15+
FLOATED_VARIATION,
16+
FLUID_VARIATION,
17+
HORIZONTAL_ALIGNED_VARIATION,
18+
PADDED_VARIATION,
19+
SIZE_VARIATION,
20+
VERTICAL_ALIGNED_VARIATION,
21+
} from './variations/index.js';
22+
23+
/*******************************
24+
Helpers
25+
*******************************/
26+
27+
/* Add Custom Examples to Shared Options */
28+
export const addOptionExamples = (options, customExamples = {}) => {
29+
return options.map(opt => ({
30+
...opt,
31+
exampleCode: customExamples[opt.value] || opt.exampleCode,
32+
}));
33+
};
34+
35+
/* Filter Options by Function or Array of Values */
36+
export const filterVariationOptions = (variation, filter) => {
37+
const filterFn = Array.isArray(filter)
38+
? (opt) => filter.includes(opt.value)
39+
: filter;
40+
41+
return {
42+
...variation,
43+
options: variation.options.filter(filterFn),
44+
};
45+
};
46+
47+
/* Get State Constants by Names */
48+
export const getStates = (stateNames) => {
49+
const stateMap = {
50+
hover: HOVER_STATE,
51+
focus: FOCUS_STATE,
52+
active: ACTIVE_STATE,
53+
loading: LOADING_STATE,
54+
pressed: PRESSED_STATE,
55+
disabled: DISABLED_STATE,
56+
};
57+
58+
return stateNames.map(name => stateMap[name]).filter(Boolean);
59+
};
60+
61+
/* Get Variation Constants by Names */
62+
export const getVariations = (variationNames) => {
63+
const variationMap = {
64+
size: SIZE_VARIATION,
65+
fluid: FLUID_VARIATION,
66+
compact: COMPACT_VARIATION,
67+
padded: PADDED_VARIATION,
68+
colored: COLORED_VARIATION,
69+
floated: FLOATED_VARIATION,
70+
attached: ATTACHED_VARIATION,
71+
'horizontal-aligned': HORIZONTAL_ALIGNED_VARIATION,
72+
'vertical-aligned': VERTICAL_ALIGNED_VARIATION,
73+
circular: CIRCULAR_VARIATION,
74+
};
75+
76+
return variationNames.map(name => variationMap[name]).filter(Boolean);
77+
};
78+
79+
/* Set Usage Level for Variation or Type */
80+
export const withUsageLevel = (item, usageLevel) => {
81+
return {
82+
...item,
83+
usageLevel,
84+
};
85+
};
86+
87+
/* Modify Arbitrary Properties of Variation, Type, or State */
88+
export const modifyVariation = (item, overrides) => {
89+
return {
90+
...item,
91+
...overrides,
92+
};
93+
};

packages/specs/src/index.js

Lines changed: 0 additions & 35 deletions
This file was deleted.

packages/specs/src/node/spec-reader-node.js

Lines changed: 0 additions & 50 deletions
This file was deleted.

packages/specs/src/server.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { mkdirSync, writeFileSync } from 'fs';
2+
import { dirname } from 'path';
3+
import { SpecReader as BaseSpecReader } from './spec-reader.js';
4+
5+
// Re-export all shared terms and helpers
6+
export * from './helpers.js';
7+
export * from './states/index.js';
8+
export * from './types/index.js';
9+
export * from './variations/index.js';
10+
11+
// Server-specific version of SpecReader with file system operations
12+
export class SpecReader extends BaseSpecReader {
13+
writeComponentSpec(path, options = {}) {
14+
const { plural = this.plural, banner = '' } = options;
15+
16+
mkdirSync(dirname(path), { recursive: true });
17+
18+
const componentSpec = this.getWebComponentSpec(this.spec, { plural });
19+
20+
let content = '';
21+
if (banner) {
22+
content += `${banner}\n`;
23+
}
24+
content += `// Auto-generated from spec\n`;
25+
content += `export default ${JSON.stringify(componentSpec, null, 2)};\n`;
26+
27+
writeFileSync(path, content);
28+
}
29+
30+
writeTestFromSpec(path, options = {}) {
31+
const spec = this.spec;
32+
const componentSpec = this.getTestFromSpec(spec);
33+
34+
mkdirSync(dirname(path), { recursive: true });
35+
36+
let content = `// Auto-generated test spec\n`;
37+
content += `export default ${JSON.stringify(componentSpec, null, 2)};\n`;
38+
39+
writeFileSync(path, content);
40+
}
41+
}

0 commit comments

Comments
 (0)