@takazudo/zudo-sg 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +35 -0
- package/LICENSE +21 -0
- package/README.md +99 -0
- package/bin/zudo-sg.js +6 -0
- package/dist/catalog/component-thumb.d.ts +78 -0
- package/dist/catalog/component-thumb.js +110 -0
- package/dist/catalog/index.d.ts +4 -0
- package/dist/catalog/index.js +40 -0
- package/dist/catalog/tile-size.d.ts +28 -0
- package/dist/catalog/tile-size.js +42 -0
- package/dist/chrome/_styleguide-layout.d.ts +59 -0
- package/dist/chrome/_styleguide-layout.js +69 -0
- package/dist/chrome/index.d.ts +4 -0
- package/dist/chrome/index.js +39 -0
- package/dist/chrome/panel-contract.d.ts +15 -0
- package/dist/chrome/panel-contract.js +65 -0
- package/dist/chrome/panel-scripts-source.d.ts +2 -0
- package/dist/chrome/panel-scripts-source.js +121 -0
- package/dist/chrome/panel-scripts.d.ts +5 -0
- package/dist/chrome/panel-scripts.js +12 -0
- package/dist/cli/bin.d.ts +1 -0
- package/dist/cli/bin.js +74 -0
- package/dist/cli/config.d.ts +54 -0
- package/dist/cli/config.js +14 -0
- package/dist/cli/registry/build-registry-source.d.ts +27 -0
- package/dist/cli/registry/build-registry-source.js +77 -0
- package/dist/cli/registry/discover-stories.d.ts +36 -0
- package/dist/cli/registry/discover-stories.js +61 -0
- package/dist/cli/registry/gen-registry.d.ts +23 -0
- package/dist/cli/registry/gen-registry.js +74 -0
- package/dist/cli/scaffold/component-scaffold.d.ts +64 -0
- package/dist/cli/scaffold/component-scaffold.js +233 -0
- package/dist/cli/scaffold/new-component.d.ts +17 -0
- package/dist/cli/scaffold/new-component.js +119 -0
- package/dist/cli/token-manifest/css-var-parser.d.ts +9 -0
- package/dist/cli/token-manifest/css-var-parser.js +23 -0
- package/dist/cli/token-manifest/gen-token-manifest.d.ts +16 -0
- package/dist/cli/token-manifest/gen-token-manifest.js +49 -0
- package/dist/cli/token-manifest/ui-token-manifest.d.ts +78 -0
- package/dist/cli/token-manifest/ui-token-manifest.js +340 -0
- package/dist/code-panel/code-panel.d.ts +18 -0
- package/dist/code-panel/code-panel.js +76 -0
- package/dist/code-panel/copy-button.d.ts +15 -0
- package/dist/code-panel/copy-button.js +92 -0
- package/dist/code-panel/css-injection.d.ts +6 -0
- package/dist/code-panel/css-injection.js +37 -0
- package/dist/code-panel/editor-setup.d.ts +10 -0
- package/dist/code-panel/editor-setup.js +33 -0
- package/dist/code-panel/index.d.ts +6 -0
- package/dist/code-panel/index.js +10 -0
- package/dist/code-panel/source-editor.d.ts +8 -0
- package/dist/code-panel/source-editor.js +39 -0
- package/dist/config/index.d.ts +73 -0
- package/dist/config/index.js +68 -0
- package/dist/host-paths.d.ts +19 -0
- package/dist/host-paths.js +41 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +18 -0
- package/dist/islands.d.ts +2 -0
- package/dist/islands.js +6 -0
- package/dist/plugins/preview-css.d.ts +53 -0
- package/dist/plugins/preview-css.js +183 -0
- package/dist/plugins/routes.d.ts +84 -0
- package/dist/plugins/routes.js +237 -0
- package/dist/plugins/zdtp-apply-proxy.d.ts +58 -0
- package/dist/plugins/zdtp-apply-proxy.js +171 -0
- package/dist/preview/detail-workbench.d.ts +22 -0
- package/dist/preview/detail-workbench.js +179 -0
- package/dist/preview/index.d.ts +8 -0
- package/dist/preview/index.js +21 -0
- package/dist/preview/messages.d.ts +34 -0
- package/dist/preview/messages.js +32 -0
- package/dist/preview/preview-app.d.ts +11 -0
- package/dist/preview/preview-app.js +87 -0
- package/dist/preview/route.d.ts +1 -0
- package/dist/preview/route.js +5 -0
- package/dist/preview/variant-frame.d.ts +42 -0
- package/dist/preview/variant-frame.js +289 -0
- package/dist/preview-css/compile.d.ts +40 -0
- package/dist/preview-css/compile.js +42 -0
- package/dist/preview-css/index.d.ts +2 -0
- package/dist/preview-css/index.js +10 -0
- package/dist/registry/component-docs.d.ts +38 -0
- package/dist/registry/component-docs.js +34 -0
- package/dist/registry/index.d.ts +6 -0
- package/dist/registry/index.js +28 -0
- package/dist/registry/nav-nodes.d.ts +22 -0
- package/dist/registry/nav-nodes.js +36 -0
- package/dist/registry/registry.d.ts +68 -0
- package/dist/registry/registry.js +91 -0
- package/dist/safelist.css +2 -0
- package/dist/search/catalog-filter.d.ts +12 -0
- package/dist/search/catalog-filter.js +175 -0
- package/dist/search/index.d.ts +2 -0
- package/dist/search/index.js +4 -0
- package/dist/sg-context.d.ts +24 -0
- package/dist/sg-context.js +6 -0
- package/dist/sg-routes.d.ts +12 -0
- package/dist/sg-routes.js +17 -0
- package/dist/stories/index.d.ts +2 -0
- package/dist/stories/index.js +4 -0
- package/dist/stories/types.d.ts +164 -0
- package/dist/stories/types.js +6 -0
- package/dist/token-dashboard/create-token-dashboards.d.ts +24 -0
- package/dist/token-dashboard/create-token-dashboards.js +57 -0
- package/dist/token-dashboard/dashboard-inventory.d.ts +11 -0
- package/dist/token-dashboard/dashboard-inventory.js +13 -0
- package/dist/token-dashboard/index.d.ts +5 -0
- package/dist/token-dashboard/index.js +20 -0
- package/dist/token-dashboard/ui-token-tabs.d.ts +53 -0
- package/dist/token-dashboard/ui-token-tabs.js +159 -0
- package/dist/token-tweak/bootstrap-preview-token-panel.d.ts +12 -0
- package/dist/token-tweak/bootstrap-preview-token-panel.js +18 -0
- package/dist/token-tweak/iframe-css-vars-bridge.d.ts +35 -0
- package/dist/token-tweak/iframe-css-vars-bridge.js +73 -0
- package/dist/token-tweak/index.d.ts +6 -0
- package/dist/token-tweak/index.js +36 -0
- package/dist/token-tweak/preview-iframe-registry.d.ts +16 -0
- package/dist/token-tweak/preview-iframe-registry.js +48 -0
- package/dist/token-tweak/preview-token-panel-bootstrap.d.ts +6 -0
- package/dist/token-tweak/preview-token-panel-bootstrap.js +17 -0
- package/dist/token-tweak/preview-token-panel-config.d.ts +42 -0
- package/dist/token-tweak/preview-token-panel-config.js +37 -0
- package/dist/token-tweak/preview-tokens-button.d.ts +5 -0
- package/dist/token-tweak/preview-tokens-button.js +17 -0
- package/dist/token-tweak/token-panel-native-bootstrap.d.ts +17 -0
- package/dist/token-tweak/token-panel-native-bootstrap.js +49 -0
- package/package.json +172 -0
- package/routes-src/_chrome.tsx +83 -0
- package/routes-src/_context.ts +15 -0
- package/routes-src/_preview-app.tsx +32 -0
- package/routes-src/_registry.ts +12 -0
- package/routes-src/components-index.tsx +95 -0
- package/routes-src/components-preview.tsx +55 -0
- package/routes-src/components-slug.tsx +145 -0
- package/routes-src/tokens.tsx +76 -0
- package/styles.css +612 -0
- package/virtual-modules.d.ts +25 -0
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/** Throws with a helpful message unless `name` is kebab-case (e.g. "demo-widget"). */
|
|
2
|
+
export declare function assertValidName(name: string | undefined): void;
|
|
3
|
+
/**
|
|
4
|
+
* Categories are open (any non-empty string) — see `StoryMeta.category` in
|
|
5
|
+
* `@takazudo/zudo-sg/stories`. This only throws for a malformed value; a
|
|
6
|
+
* category outside `validCategories` (the host's `categoryOrder`) is
|
|
7
|
+
* accepted, with a warning printed so the author notices they're
|
|
8
|
+
* introducing a new one (it sorts in alphabetically after the declared
|
|
9
|
+
* categories).
|
|
10
|
+
*/
|
|
11
|
+
export declare function assertValidCategory(category: string | undefined, validCategories: string[]): void;
|
|
12
|
+
/**
|
|
13
|
+
* `StoryMeta.category` display name → directory slug for the
|
|
14
|
+
* category-nested layout, e.g. "Data Display" → "data-display".
|
|
15
|
+
*/
|
|
16
|
+
export declare function categorySlug(category: string): string;
|
|
17
|
+
/** Throws if `name` already names a directory under the components root. */
|
|
18
|
+
export declare function assertUnusedName(name: string, existingNames: string[], componentsRootLabel: string): void;
|
|
19
|
+
/** Kebab-case ("demo-widget") → PascalCase ("DemoWidget"). */
|
|
20
|
+
export declare function toPascalCase(kebabName: string): string;
|
|
21
|
+
export interface ComponentTemplateArgs {
|
|
22
|
+
pascalName: string;
|
|
23
|
+
kebabName: string;
|
|
24
|
+
nested?: boolean;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* `<componentsRoot>/<name>/<name>.tsx` — typed-props skeleton following the
|
|
28
|
+
* house pattern. `nested` accounts for the extra directory level the
|
|
29
|
+
* category-nested layout inserts (one more `../` to reach `lib/cx`).
|
|
30
|
+
*/
|
|
31
|
+
export declare function componentTemplate({ pascalName, nested }: ComponentTemplateArgs): string;
|
|
32
|
+
export interface StoriesTemplateArgs {
|
|
33
|
+
pascalName: string;
|
|
34
|
+
kebabName: string;
|
|
35
|
+
category: string;
|
|
36
|
+
uiPackageName: string;
|
|
37
|
+
nested?: boolean;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* `<componentsRoot>/<name>/<name>.stories.tsx` — StoryMeta + a Playground
|
|
41
|
+
* variant in the typed `Story<Props>` shape.
|
|
42
|
+
*/
|
|
43
|
+
export declare function storiesTemplate({ pascalName, kebabName, category, uiPackageName, nested, }: StoriesTemplateArgs): string;
|
|
44
|
+
export interface TestTemplateArgs {
|
|
45
|
+
pascalName: string;
|
|
46
|
+
kebabName: string;
|
|
47
|
+
}
|
|
48
|
+
/** `<componentsRoot>/<name>/__tests__/<name>.test.tsx` — a starter suite. */
|
|
49
|
+
export declare function testTemplate({ pascalName, kebabName }: TestTemplateArgs): string;
|
|
50
|
+
export interface InsertBarrelExportArgs {
|
|
51
|
+
pascalName: string;
|
|
52
|
+
kebabName: string;
|
|
53
|
+
category: string;
|
|
54
|
+
nested?: boolean;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Appends the new component's barrel export into `indexSource`, inserted
|
|
58
|
+
* alphabetically by component name among the other exports already in that
|
|
59
|
+
* `category`'s "// ── <Category> ──" section. Returns the updated source.
|
|
60
|
+
*
|
|
61
|
+
* Throws if no section matches `category`, or if `pascalName` is already
|
|
62
|
+
* exported anywhere else in the barrel.
|
|
63
|
+
*/
|
|
64
|
+
export declare function insertBarrelExport(indexSource: string, { pascalName, kebabName, category, nested }: InsertBarrelExportArgs): string;
|
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
const KEBAB_NAME_RE = /^[a-z][a-z0-9]*(-[a-z0-9]+)*$/;
|
|
2
|
+
function assertValidName(name) {
|
|
3
|
+
if (!name || !KEBAB_NAME_RE.test(name)) {
|
|
4
|
+
throw new Error(
|
|
5
|
+
`"${name}" is not a valid component name \u2014 expected kebab-case like "demo-widget" (lowercase letters/digits, hyphen-separated, no leading digit or hyphen).`
|
|
6
|
+
);
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
function assertValidCategory(category, validCategories) {
|
|
10
|
+
if (!category || typeof category !== "string" || category.trim() === "") {
|
|
11
|
+
throw new Error(`"${category}" is not a valid --category \u2014 expected a non-empty string.`);
|
|
12
|
+
}
|
|
13
|
+
if (!validCategories.includes(category)) {
|
|
14
|
+
console.warn(
|
|
15
|
+
`new-component: "${category}" is a new category \u2014 not one of the host's declared categories (${validCategories.join(", ")}). It will appear in the sidebar after those, alphabetically among any other new categories.`
|
|
16
|
+
);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
function categorySlug(category) {
|
|
20
|
+
return category.toLowerCase().replace(/\s+/g, "-");
|
|
21
|
+
}
|
|
22
|
+
function assertUnusedName(name, existingNames, componentsRootLabel) {
|
|
23
|
+
if (existingNames.includes(name)) {
|
|
24
|
+
throw new Error(`${componentsRootLabel}/${name}/ already exists \u2014 pick a name that isn't in use.`);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
function toPascalCase(kebabName) {
|
|
28
|
+
return kebabName.split("-").map((segment) => segment.charAt(0).toUpperCase() + segment.slice(1)).join("");
|
|
29
|
+
}
|
|
30
|
+
function componentTemplate({ pascalName, nested = false }) {
|
|
31
|
+
const lines = [
|
|
32
|
+
`import type { ComponentChildren } from "preact";`,
|
|
33
|
+
`import { cx } from "${nested ? "../../lib/cx" : "../lib/cx"}";`,
|
|
34
|
+
``,
|
|
35
|
+
`export type ${pascalName}Variant = "primary" | "secondary";`,
|
|
36
|
+
``,
|
|
37
|
+
`export type ${pascalName}Props = {`,
|
|
38
|
+
` variant?: ${pascalName}Variant;`,
|
|
39
|
+
` class?: string;`,
|
|
40
|
+
` children?: ComponentChildren;`,
|
|
41
|
+
`};`,
|
|
42
|
+
``,
|
|
43
|
+
`const base =`,
|
|
44
|
+
` "inline-flex items-center gap-hsp-xs rounded-md outline-none transition-colors " +`,
|
|
45
|
+
` "focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-focus";`,
|
|
46
|
+
``,
|
|
47
|
+
`// TODO: replace with this component's real per-variant classes \u2014 these are`,
|
|
48
|
+
`// placeholders so the scaffold typechecks and passes lint:tokens as-is.`,
|
|
49
|
+
`const variants: Record<${pascalName}Variant, string> = {`,
|
|
50
|
+
` primary: "bg-accent text-on-accent",`,
|
|
51
|
+
` secondary: "bg-surface text-fg border border-border",`,
|
|
52
|
+
`};`,
|
|
53
|
+
``,
|
|
54
|
+
`/**`,
|
|
55
|
+
` * TODO: describe ${pascalName} \u2014 what it renders and when to use it.`,
|
|
56
|
+
` */`,
|
|
57
|
+
`export function ${pascalName}({ variant = "primary", class: cls, children }: ${pascalName}Props) {`,
|
|
58
|
+
` return <div class={cx(base, variants[variant], cls)}>{children}</div>;`,
|
|
59
|
+
`}`
|
|
60
|
+
];
|
|
61
|
+
return lines.join("\n") + "\n";
|
|
62
|
+
}
|
|
63
|
+
function storiesTemplate({
|
|
64
|
+
pascalName,
|
|
65
|
+
kebabName,
|
|
66
|
+
category,
|
|
67
|
+
uiPackageName,
|
|
68
|
+
nested = false
|
|
69
|
+
}) {
|
|
70
|
+
const lines = [
|
|
71
|
+
`import type { StoryMeta, Story } from "${nested ? "../../stories/types" : "../stories/types"}";`,
|
|
72
|
+
`import { ${pascalName}, type ${pascalName}Props } from "./${kebabName}";`,
|
|
73
|
+
``,
|
|
74
|
+
`const meta: StoryMeta = {`,
|
|
75
|
+
` title: "${pascalName}", // TODO: human-friendly display name, if different`,
|
|
76
|
+
` category: "${category}",`,
|
|
77
|
+
` description: "TODO: one-sentence description of ${pascalName}.",`,
|
|
78
|
+
` usage: \`import { ${pascalName} } from "${uiPackageName}";`,
|
|
79
|
+
``,
|
|
80
|
+
`<${pascalName}>Content</${pascalName}>\`,`,
|
|
81
|
+
`};`,
|
|
82
|
+
``,
|
|
83
|
+
`export default meta;`,
|
|
84
|
+
``,
|
|
85
|
+
`export const Playground: Story<${pascalName}Props> = {`,
|
|
86
|
+
` name: "Playground",`,
|
|
87
|
+
` source: \`<${pascalName} variant="primary">Content</${pascalName}>\`,`,
|
|
88
|
+
` controls: [`,
|
|
89
|
+
` {`,
|
|
90
|
+
` type: "select",`,
|
|
91
|
+
` prop: "variant",`,
|
|
92
|
+
` label: "Variant",`,
|
|
93
|
+
` options: ["primary", "secondary"],`,
|
|
94
|
+
` defaultValue: "primary",`,
|
|
95
|
+
` },`,
|
|
96
|
+
` {`,
|
|
97
|
+
` type: "text",`,
|
|
98
|
+
` prop: "children",`,
|
|
99
|
+
` label: "Content",`,
|
|
100
|
+
` defaultValue: "Content",`,
|
|
101
|
+
` },`,
|
|
102
|
+
` ],`,
|
|
103
|
+
` render: (args = {}) => (`,
|
|
104
|
+
` <${pascalName} variant={args.variant}>{args.children}</${pascalName}>`,
|
|
105
|
+
` ),`,
|
|
106
|
+
`};`
|
|
107
|
+
];
|
|
108
|
+
return lines.join("\n") + "\n";
|
|
109
|
+
}
|
|
110
|
+
function testTemplate({ pascalName, kebabName }) {
|
|
111
|
+
const lines = [
|
|
112
|
+
`import { render, screen } from "@testing-library/preact";`,
|
|
113
|
+
`import { describe, expect, it } from "vitest";`,
|
|
114
|
+
`import { ${pascalName} } from "../${kebabName}";`,
|
|
115
|
+
``,
|
|
116
|
+
`describe("${pascalName}", () => {`,
|
|
117
|
+
` it("renders its children", () => {`,
|
|
118
|
+
` render(<${pascalName}>Content</${pascalName}>);`,
|
|
119
|
+
` expect(screen.getByText("Content")).toBeInTheDocument();`,
|
|
120
|
+
` });`,
|
|
121
|
+
``,
|
|
122
|
+
` it("defaults to the primary variant", () => {`,
|
|
123
|
+
` render(<${pascalName}>Content</${pascalName}>);`,
|
|
124
|
+
` const el = screen.getByText("Content");`,
|
|
125
|
+
` expect(el.className).toContain("bg-accent");`,
|
|
126
|
+
` });`,
|
|
127
|
+
``,
|
|
128
|
+
` it("applies the secondary variant classes", () => {`,
|
|
129
|
+
` render(<${pascalName} variant="secondary">Content</${pascalName}>);`,
|
|
130
|
+
` const el = screen.getByText("Content");`,
|
|
131
|
+
` expect(el.className).toContain("border-border");`,
|
|
132
|
+
` });`,
|
|
133
|
+
`});`
|
|
134
|
+
];
|
|
135
|
+
return lines.join("\n") + "\n";
|
|
136
|
+
}
|
|
137
|
+
const HEADER_RE = /^\/\/ ── .+ ──+$/;
|
|
138
|
+
function getHeaderIndexes(lines) {
|
|
139
|
+
const headerIndexes = [];
|
|
140
|
+
lines.forEach((line, i) => {
|
|
141
|
+
if (HEADER_RE.test(line)) headerIndexes.push(i);
|
|
142
|
+
});
|
|
143
|
+
return headerIndexes;
|
|
144
|
+
}
|
|
145
|
+
function findCategorySection(lines, category) {
|
|
146
|
+
const headerIndexes = getHeaderIndexes(lines);
|
|
147
|
+
const needle = category.toLowerCase();
|
|
148
|
+
const startIdx = headerIndexes.find((i) => lines[i].toLowerCase().includes(needle));
|
|
149
|
+
if (startIdx === void 0) {
|
|
150
|
+
throw new Error(
|
|
151
|
+
`insertBarrelExport: no "// \u2500\u2500 \u2026 \u2500\u2500" section header matching category "${category}" found in the barrel index. Every category needs a barrel section \u2014 add one by hand once.`
|
|
152
|
+
);
|
|
153
|
+
}
|
|
154
|
+
const nextHeaderIdx = headerIndexes.find((i) => i > startIdx);
|
|
155
|
+
const endIdx = nextHeaderIdx === void 0 ? lines.length : nextHeaderIdx;
|
|
156
|
+
return { startIdx, endIdx };
|
|
157
|
+
}
|
|
158
|
+
function headerLabel(line) {
|
|
159
|
+
return line.replace(/^\/\/ ── /, "").replace(/ ──+\s*$/, "");
|
|
160
|
+
}
|
|
161
|
+
const VALUE_EXPORT_RE = /^export\s*\{([^}]*)\}\s*from\s*["']([^"']+)["'];?\s*$/;
|
|
162
|
+
function parseBoundNames(braceContents) {
|
|
163
|
+
return braceContents.split(",").map((spec) => spec.trim()).filter(Boolean).map((spec) => spec.includes(" as ") ? spec.split(" as ").pop().trim() : spec);
|
|
164
|
+
}
|
|
165
|
+
function findExistingPascalExport(lines, pascalName) {
|
|
166
|
+
const headerIndexes = getHeaderIndexes(lines);
|
|
167
|
+
for (let i = 0; i < lines.length; i++) {
|
|
168
|
+
const match = lines[i].match(VALUE_EXPORT_RE);
|
|
169
|
+
if (!match) continue;
|
|
170
|
+
const names = parseBoundNames(match[1]);
|
|
171
|
+
if (!names.includes(pascalName)) continue;
|
|
172
|
+
const headerIdx = [...headerIndexes].reverse().find((h) => h < i);
|
|
173
|
+
return {
|
|
174
|
+
path: match[2],
|
|
175
|
+
sectionLabel: headerIdx !== void 0 ? headerLabel(lines[headerIdx]) : "(unknown section)"
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
return void 0;
|
|
179
|
+
}
|
|
180
|
+
function parseBlocks(bodyLines) {
|
|
181
|
+
const blocks = [];
|
|
182
|
+
let current = [];
|
|
183
|
+
for (const line of bodyLines) {
|
|
184
|
+
if (line.trim() === "") {
|
|
185
|
+
if (current.length) {
|
|
186
|
+
blocks.push(current);
|
|
187
|
+
current = [];
|
|
188
|
+
}
|
|
189
|
+
} else {
|
|
190
|
+
current.push(line);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
if (current.length) blocks.push(current);
|
|
194
|
+
return blocks;
|
|
195
|
+
}
|
|
196
|
+
function blockSortKey(block) {
|
|
197
|
+
const match = block[0].match(VALUE_EXPORT_RE);
|
|
198
|
+
if (!match) return block[0];
|
|
199
|
+
return parseBoundNames(match[1])[0] ?? block[0];
|
|
200
|
+
}
|
|
201
|
+
function insertBarrelExport(indexSource, { pascalName, kebabName, category, nested = false }) {
|
|
202
|
+
const lines = indexSource.split("\n");
|
|
203
|
+
const collision = findExistingPascalExport(lines, pascalName);
|
|
204
|
+
if (collision) {
|
|
205
|
+
throw new Error(
|
|
206
|
+
`insertBarrelExport: "${pascalName}" is already exported from "${collision.path}" in the "${collision.sectionLabel}" section of the barrel index. The barrel can't hold two exports named "${pascalName}" \u2014 pass --skip-barrel and add this one to the barrel by hand under an alias (e.g. \`export { ${pascalName} as ${pascalName}FromWhichever } from "...";\`).`
|
|
207
|
+
);
|
|
208
|
+
}
|
|
209
|
+
const { startIdx, endIdx } = findCategorySection(lines, category);
|
|
210
|
+
const blocks = parseBlocks(lines.slice(startIdx + 1, endIdx));
|
|
211
|
+
const importPath = nested ? `./${categorySlug(category)}/${kebabName}/${kebabName}` : `./${kebabName}/${kebabName}`;
|
|
212
|
+
const newBlock = [
|
|
213
|
+
`export { ${pascalName} } from "${importPath}";`,
|
|
214
|
+
`export type { ${pascalName}Props, ${pascalName}Variant } from "${importPath}";`
|
|
215
|
+
];
|
|
216
|
+
let insertAt = blocks.findIndex((block) => blockSortKey(block) > pascalName);
|
|
217
|
+
if (insertAt === -1) insertAt = blocks.length;
|
|
218
|
+
blocks.splice(insertAt, 0, newBlock);
|
|
219
|
+
const newBodyLines = blocks.map((b) => b.join("\n")).join("\n\n").split("\n");
|
|
220
|
+
newBodyLines.push("");
|
|
221
|
+
return [...lines.slice(0, startIdx + 1), ...newBodyLines, ...lines.slice(endIdx)].join("\n");
|
|
222
|
+
}
|
|
223
|
+
export {
|
|
224
|
+
assertUnusedName,
|
|
225
|
+
assertValidCategory,
|
|
226
|
+
assertValidName,
|
|
227
|
+
categorySlug,
|
|
228
|
+
componentTemplate,
|
|
229
|
+
insertBarrelExport,
|
|
230
|
+
storiesTemplate,
|
|
231
|
+
testTemplate,
|
|
232
|
+
toPascalCase
|
|
233
|
+
};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { ZudoSgConfig } from "../config.js";
|
|
2
|
+
export interface ParsedNewComponentArgs {
|
|
3
|
+
name: string | undefined;
|
|
4
|
+
category: string | undefined;
|
|
5
|
+
skipBarrel: boolean;
|
|
6
|
+
nested: boolean;
|
|
7
|
+
}
|
|
8
|
+
export declare function parseArgs(argv: string[]): ParsedNewComponentArgs;
|
|
9
|
+
export declare function printNewComponentUsage(validCategories: string[], barrelIndex: string | null): void;
|
|
10
|
+
/**
|
|
11
|
+
* Runs `new-component` against `config`, resolved relative to
|
|
12
|
+
* `projectRoot`. Scaffolds into `config.componentsRoots[0]` — the first
|
|
13
|
+
* components root is the scaffold target when a host declares more than
|
|
14
|
+
* one. Returns 0 on success, 1 on a handled failure (already reported via
|
|
15
|
+
* console.error).
|
|
16
|
+
*/
|
|
17
|
+
export declare function runNewComponent(projectRoot: string, config: ZudoSgConfig, { name, category, skipBarrel, nested }: ParsedNewComponentArgs): number;
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import { existsSync, mkdirSync, readFileSync, readdirSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { resolve } from "node:path";
|
|
3
|
+
import { runGenRegistry } from "../registry/gen-registry.js";
|
|
4
|
+
import {
|
|
5
|
+
assertUnusedName,
|
|
6
|
+
assertValidCategory,
|
|
7
|
+
assertValidName,
|
|
8
|
+
categorySlug,
|
|
9
|
+
componentTemplate,
|
|
10
|
+
insertBarrelExport,
|
|
11
|
+
storiesTemplate,
|
|
12
|
+
testTemplate,
|
|
13
|
+
toPascalCase
|
|
14
|
+
} from "./component-scaffold.js";
|
|
15
|
+
function parseArgs(argv) {
|
|
16
|
+
const positional = [];
|
|
17
|
+
let category;
|
|
18
|
+
let skipBarrel = false;
|
|
19
|
+
let nested = false;
|
|
20
|
+
for (let i = 0; i < argv.length; i++) {
|
|
21
|
+
const arg = argv[i];
|
|
22
|
+
if (arg === "--category") {
|
|
23
|
+
category = argv[++i];
|
|
24
|
+
} else if (arg.startsWith("--category=")) {
|
|
25
|
+
category = arg.slice("--category=".length);
|
|
26
|
+
} else if (arg === "--skip-barrel") {
|
|
27
|
+
skipBarrel = true;
|
|
28
|
+
} else if (arg === "--nested") {
|
|
29
|
+
nested = true;
|
|
30
|
+
} else if (!arg.startsWith("--")) {
|
|
31
|
+
positional.push(arg);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return { name: positional[0], category, skipBarrel, nested };
|
|
35
|
+
}
|
|
36
|
+
function printNewComponentUsage(validCategories, barrelIndex) {
|
|
37
|
+
console.error(
|
|
38
|
+
`Usage: pnpm new:component <name> --category <Category> [--skip-barrel] [--nested]
|
|
39
|
+
<Category> is a free-form string. Declared categories: ${validCategories.join(", ")}
|
|
40
|
+
--skip-barrel skips inserting the export into ${barrelIndex ?? "the barrel file"}.
|
|
41
|
+
--nested scaffolds <componentsRoot>/<category-slug>/<name>/ \u2014 still inserts into the
|
|
42
|
+
barrel by default (import path adjusted for the nesting); --skip-barrel opts out.`
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
function runNewComponent(projectRoot, config, { name, category, skipBarrel, nested }) {
|
|
46
|
+
if (!name || !category) {
|
|
47
|
+
printNewComponentUsage(config.categoryOrder, config.barrelIndex);
|
|
48
|
+
return 1;
|
|
49
|
+
}
|
|
50
|
+
const componentsRoot = config.componentsRoots[0];
|
|
51
|
+
if (!componentsRoot) {
|
|
52
|
+
console.error("new-component: config.componentsRoots is empty \u2014 nowhere to scaffold into.");
|
|
53
|
+
return 1;
|
|
54
|
+
}
|
|
55
|
+
const uiSrcDir = resolve(projectRoot, componentsRoot.dir);
|
|
56
|
+
const indexPath = config.barrelIndex ? resolve(projectRoot, config.barrelIndex) : null;
|
|
57
|
+
const scanRoot = nested ? resolve(uiSrcDir, categorySlug(category)) : uiSrcDir;
|
|
58
|
+
const componentDir = nested ? resolve(uiSrcDir, categorySlug(category), name) : resolve(uiSrcDir, name);
|
|
59
|
+
const relComponentDir = nested ? `${componentsRoot.dir}/${categorySlug(category)}/${name}` : `${componentsRoot.dir}/${name}`;
|
|
60
|
+
const pascalName = toPascalCase(name);
|
|
61
|
+
const shouldInsertBarrel = indexPath !== null && !skipBarrel;
|
|
62
|
+
let newIndexSrc;
|
|
63
|
+
try {
|
|
64
|
+
assertValidName(name);
|
|
65
|
+
assertValidCategory(category, config.categoryOrder);
|
|
66
|
+
const existingNames = existsSync(scanRoot) ? readdirSync(scanRoot, { withFileTypes: true }).filter((entry) => entry.isDirectory()).map((entry) => entry.name) : [];
|
|
67
|
+
assertUnusedName(name, existingNames, componentsRoot.dir);
|
|
68
|
+
if (shouldInsertBarrel && indexPath) {
|
|
69
|
+
const indexSrc = readFileSync(indexPath, "utf8");
|
|
70
|
+
newIndexSrc = insertBarrelExport(indexSrc, { pascalName, kebabName: name, category, nested });
|
|
71
|
+
}
|
|
72
|
+
} catch (err) {
|
|
73
|
+
console.error(`new-component: ${err.message}`);
|
|
74
|
+
return 1;
|
|
75
|
+
}
|
|
76
|
+
const testsDir = resolve(componentDir, "__tests__");
|
|
77
|
+
mkdirSync(testsDir, { recursive: true });
|
|
78
|
+
writeFileSync(resolve(componentDir, `${name}.tsx`), componentTemplate({ pascalName, kebabName: name, nested }));
|
|
79
|
+
writeFileSync(
|
|
80
|
+
resolve(componentDir, `${name}.stories.tsx`),
|
|
81
|
+
storiesTemplate({ pascalName, kebabName: name, category, uiPackageName: config.uiPackageName, nested })
|
|
82
|
+
);
|
|
83
|
+
writeFileSync(resolve(testsDir, `${name}.test.tsx`), testTemplate({ pascalName, kebabName: name }));
|
|
84
|
+
if (shouldInsertBarrel && indexPath && newIndexSrc) {
|
|
85
|
+
writeFileSync(indexPath, newIndexSrc);
|
|
86
|
+
}
|
|
87
|
+
console.log(`Scaffolded ${pascalName} at ${relComponentDir}/`);
|
|
88
|
+
if (shouldInsertBarrel) {
|
|
89
|
+
console.log(`Added the barrel export to ${config.barrelIndex}.`);
|
|
90
|
+
} else if (indexPath === null) {
|
|
91
|
+
console.log("No barrelIndex configured \u2014 skipped the barrel-export step.");
|
|
92
|
+
} else {
|
|
93
|
+
console.log("Skipped the barrel-export step (--skip-barrel).");
|
|
94
|
+
}
|
|
95
|
+
try {
|
|
96
|
+
runGenRegistry(projectRoot, config);
|
|
97
|
+
} catch (err) {
|
|
98
|
+
console.error(
|
|
99
|
+
`new-component: files were scaffolded, but gen-registry failed \u2014 run \`pnpm gen:sg-registry\` by hand.
|
|
100
|
+
${err.message}`
|
|
101
|
+
);
|
|
102
|
+
return 1;
|
|
103
|
+
}
|
|
104
|
+
const steps = [`Fill in the TODOs in ${relComponentDir}/${name}.tsx and ${name}.stories.tsx.`];
|
|
105
|
+
if (!shouldInsertBarrel && indexPath !== null) {
|
|
106
|
+
steps.push(`Add the barrel export to ${config.barrelIndex} by hand (skipped via --skip-barrel).`);
|
|
107
|
+
}
|
|
108
|
+
steps.push(`Run \`pnpm lint:tokens\`, \`pnpm check\`, and \`pnpm test:unit\`.`);
|
|
109
|
+
steps.push(`\`pnpm build\`, then visit /components/${name} to confirm it renders.`);
|
|
110
|
+
console.log(`
|
|
111
|
+
Next steps:
|
|
112
|
+
${steps.map((step, i) => ` ${i + 1}. ${step}`).join("\n")}`);
|
|
113
|
+
return 0;
|
|
114
|
+
}
|
|
115
|
+
export {
|
|
116
|
+
parseArgs,
|
|
117
|
+
printNewComponentUsage,
|
|
118
|
+
runNewComponent
|
|
119
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Parse `cssText` and return every `--custom-property: value;` declaration
|
|
3
|
+
* found anywhere in the stylesheet, keyed by property name, value
|
|
4
|
+
* whitespace-normalized.
|
|
5
|
+
*
|
|
6
|
+
* Throws (via postcss) on malformed CSS. Throws explicitly if the same
|
|
7
|
+
* custom property is declared twice with two different values.
|
|
8
|
+
*/
|
|
9
|
+
export declare function parseCssCustomProperties(cssText: string): Map<string, string>;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { parse } from "postcss";
|
|
2
|
+
function normalizeValue(value) {
|
|
3
|
+
return value.replace(/\s+/g, " ").trim();
|
|
4
|
+
}
|
|
5
|
+
function parseCssCustomProperties(cssText) {
|
|
6
|
+
const root = parse(cssText);
|
|
7
|
+
const vars = /* @__PURE__ */ new Map();
|
|
8
|
+
root.walkDecls((decl) => {
|
|
9
|
+
if (!decl.prop.startsWith("--")) return;
|
|
10
|
+
const value = normalizeValue(decl.value);
|
|
11
|
+
const existing = vars.get(decl.prop);
|
|
12
|
+
if (existing !== void 0 && existing !== value) {
|
|
13
|
+
throw new Error(
|
|
14
|
+
`Custom property "${decl.prop}" is declared twice with different values ("${existing}" vs "${value}") \u2014 this parser assumes each token has one value.`
|
|
15
|
+
);
|
|
16
|
+
}
|
|
17
|
+
vars.set(decl.prop, value);
|
|
18
|
+
});
|
|
19
|
+
return vars;
|
|
20
|
+
}
|
|
21
|
+
export {
|
|
22
|
+
parseCssCustomProperties
|
|
23
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { ZudoSgConfig } from "../config.js";
|
|
2
|
+
export interface GenTokenManifestResult {
|
|
3
|
+
tokenCount: number;
|
|
4
|
+
changed: boolean;
|
|
5
|
+
manifestOut: string;
|
|
6
|
+
}
|
|
7
|
+
export interface GenTokenManifestOptions {
|
|
8
|
+
check?: boolean;
|
|
9
|
+
}
|
|
10
|
+
export declare class TokenManifestDriftError extends Error {
|
|
11
|
+
constructor(manifestOut: string);
|
|
12
|
+
}
|
|
13
|
+
export declare class TokensConfigMissingError extends Error {
|
|
14
|
+
constructor();
|
|
15
|
+
}
|
|
16
|
+
export declare function runGenTokenManifest(projectRoot: string, config: ZudoSgConfig, options?: GenTokenManifestOptions): GenTokenManifestResult;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { readFileSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { resolve } from "node:path";
|
|
3
|
+
import { buildUiTokenManifest, renderUiTokenManifestFile } from "./ui-token-manifest.js";
|
|
4
|
+
class TokenManifestDriftError extends Error {
|
|
5
|
+
constructor(manifestOut) {
|
|
6
|
+
super(
|
|
7
|
+
`Token manifest drift detected: ${manifestOut} is out of date.
|
|
8
|
+
Run \`pnpm gen:token-manifest\` and commit the result.`
|
|
9
|
+
);
|
|
10
|
+
this.name = "TokenManifestDriftError";
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
class TokensConfigMissingError extends Error {
|
|
14
|
+
constructor() {
|
|
15
|
+
super(
|
|
16
|
+
"[zudo-sg] gen-token-manifest needs a `tokens` entry in zudo-sg.config.mjs (`tokens: { cssFiles: [tokensCssPath, colorsCssPath], manifestOut }`)."
|
|
17
|
+
);
|
|
18
|
+
this.name = "TokensConfigMissingError";
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
function runGenTokenManifest(projectRoot, config, options = {}) {
|
|
22
|
+
const { tokens } = config;
|
|
23
|
+
if (!tokens) throw new TokensConfigMissingError();
|
|
24
|
+
const [tokensCssPath, colorsCssPath] = tokens.cssFiles;
|
|
25
|
+
const tokensCss = readFileSync(resolve(projectRoot, tokensCssPath), "utf8");
|
|
26
|
+
const colorsCss = readFileSync(resolve(projectRoot, colorsCssPath), "utf8");
|
|
27
|
+
const manifest = buildUiTokenManifest({ tokensCss, colorsCss });
|
|
28
|
+
const next = renderUiTokenManifestFile(manifest);
|
|
29
|
+
const tokenCount = manifest.paletteColors.length + manifest.colorTokens.length + manifest.spacingTokens.length + manifest.fontTokens.length + manifest.sizeTokens.length;
|
|
30
|
+
const manifestPath = resolve(projectRoot, tokens.manifestOut);
|
|
31
|
+
let current;
|
|
32
|
+
try {
|
|
33
|
+
current = readFileSync(manifestPath, "utf8");
|
|
34
|
+
} catch {
|
|
35
|
+
current = null;
|
|
36
|
+
}
|
|
37
|
+
if (options.check) {
|
|
38
|
+
if (current !== next) throw new TokenManifestDriftError(tokens.manifestOut);
|
|
39
|
+
return { tokenCount, changed: false, manifestOut: tokens.manifestOut };
|
|
40
|
+
}
|
|
41
|
+
if (current === next) return { tokenCount, changed: false, manifestOut: tokens.manifestOut };
|
|
42
|
+
writeFileSync(manifestPath, next);
|
|
43
|
+
return { tokenCount, changed: true, manifestOut: tokens.manifestOut };
|
|
44
|
+
}
|
|
45
|
+
export {
|
|
46
|
+
TokenManifestDriftError,
|
|
47
|
+
TokensConfigMissingError,
|
|
48
|
+
runGenTokenManifest
|
|
49
|
+
};
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
export interface TokenSpec {
|
|
2
|
+
cssVar: string;
|
|
3
|
+
group: string;
|
|
4
|
+
step?: number;
|
|
5
|
+
unit?: string;
|
|
6
|
+
control?: string;
|
|
7
|
+
options?: string[];
|
|
8
|
+
pill?: {
|
|
9
|
+
value: string;
|
|
10
|
+
customDefault: string;
|
|
11
|
+
};
|
|
12
|
+
note?: string;
|
|
13
|
+
}
|
|
14
|
+
export interface BuiltToken {
|
|
15
|
+
id: string;
|
|
16
|
+
cssVar: string;
|
|
17
|
+
label: string;
|
|
18
|
+
group: string;
|
|
19
|
+
default: string;
|
|
20
|
+
step?: number;
|
|
21
|
+
unit?: string;
|
|
22
|
+
control?: string;
|
|
23
|
+
options?: string[];
|
|
24
|
+
pill?: {
|
|
25
|
+
value: string;
|
|
26
|
+
customDefault: string;
|
|
27
|
+
};
|
|
28
|
+
note?: string;
|
|
29
|
+
}
|
|
30
|
+
export interface PaletteEntry {
|
|
31
|
+
name: string;
|
|
32
|
+
value: string;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Tier-1 raw palette — every `--palette-*` in the colors CSS file's `:root`
|
|
36
|
+
* block. Order matches the section order in that file (neutral, accent,
|
|
37
|
+
* state, line) purely for readability; has no functional effect.
|
|
38
|
+
*/
|
|
39
|
+
export declare const PALETTE_NAMES: string[];
|
|
40
|
+
/**
|
|
41
|
+
* Tier-2 canonical semantic color tokens (the grouped three-tier scheme).
|
|
42
|
+
* All render as free-text rows (`light-dark()` / `color-mix()` expressions
|
|
43
|
+
* can't drive a single-axis slider), so `control: "text"` is applied
|
|
44
|
+
* uniformly in buildColorTokens().
|
|
45
|
+
*/
|
|
46
|
+
export declare const COLOR_SPECS: TokenSpec[];
|
|
47
|
+
/** Horizontal (hsp) + vertical (vsp) spacing axes. */
|
|
48
|
+
export declare const SPACING_SPECS: TokenSpec[];
|
|
49
|
+
/** Font sizes, paired line-heights, weights, line-heights, families. */
|
|
50
|
+
export declare const FONT_SPECS: TokenSpec[];
|
|
51
|
+
/**
|
|
52
|
+
* Radius + shadow. Shadows are free-text rows: multi-layer `box-shadow`
|
|
53
|
+
* expressions can't be driven by a single-axis slider.
|
|
54
|
+
*/
|
|
55
|
+
export declare const SIZE_SPECS: TokenSpec[];
|
|
56
|
+
export declare function lookup(vars: Map<string, string>, cssVar: string, sourceLabel: string): string;
|
|
57
|
+
export declare function buildPaletteColors(colorVars: Map<string, string>): PaletteEntry[];
|
|
58
|
+
export declare function buildColorTokens(colorVars: Map<string, string>): BuiltToken[];
|
|
59
|
+
export declare function buildFromSpecs(specs: TokenSpec[], tokenVars: Map<string, string>, sourceLabel: string): BuiltToken[];
|
|
60
|
+
export interface UiTokenManifest {
|
|
61
|
+
paletteColors: PaletteEntry[];
|
|
62
|
+
colorTokens: BuiltToken[];
|
|
63
|
+
spacingTokens: BuiltToken[];
|
|
64
|
+
fontTokens: BuiltToken[];
|
|
65
|
+
sizeTokens: BuiltToken[];
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Parse `tokensCss` (the components root's main tokens file) and
|
|
69
|
+
* `colorsCss` (its colors file) and build the full manifest data — the same
|
|
70
|
+
* shape as the arrays exported by the generated manifest, minus the TS
|
|
71
|
+
* syntax.
|
|
72
|
+
*/
|
|
73
|
+
export declare function buildUiTokenManifest({ tokensCss, colorsCss }: {
|
|
74
|
+
tokensCss: string;
|
|
75
|
+
colorsCss: string;
|
|
76
|
+
}): UiTokenManifest;
|
|
77
|
+
/** Render the full generated manifest `.ts` source. */
|
|
78
|
+
export declare function renderUiTokenManifestFile(manifest: UiTokenManifest): string;
|