@usalyt/cli 0.0.0-stage → 0.0.2

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.
@@ -0,0 +1,120 @@
1
+ import { existsSync, readFileSync } from "node:fs";
2
+ import { createRequire } from "node:module";
3
+ import path from "node:path";
4
+ import { fileURLToPath } from "node:url";
5
+
6
+ export interface RegistryFile {
7
+ path: string;
8
+ type: string;
9
+ content: string;
10
+ }
11
+
12
+ export interface RegistryItem {
13
+ name: string;
14
+ type: string;
15
+ title: string;
16
+ description: string;
17
+ license: string;
18
+ dependencies: string[];
19
+ devDependencies: string[];
20
+ registryDependencies: string[];
21
+ dependencyVersions: Record<string, string>;
22
+ files: RegistryFile[];
23
+ }
24
+
25
+ export interface Registry {
26
+ name: string;
27
+ version: string;
28
+ items: RegistryItem[];
29
+ }
30
+
31
+ export class RegistryLoadError extends Error {
32
+ constructor(message: string) {
33
+ super(message);
34
+ this.name = "RegistryLoadError";
35
+ }
36
+ }
37
+
38
+ const require = createRequire(import.meta.url);
39
+
40
+ /**
41
+ * Resolve the registry through Node package resolution so the CLI works from
42
+ * any working directory once `@usalyt/registry` is installed. The relative
43
+ * candidates are a development-only fallback for the monorepo layout and are
44
+ * never based on the current working directory.
45
+ */
46
+ export function resolveRegistryPath(): string {
47
+ try {
48
+ return require.resolve("@usalyt/registry/registry.json");
49
+ } catch {
50
+ const here = path.dirname(fileURLToPath(import.meta.url));
51
+ const candidates = [
52
+ path.resolve(here, "../../registry/registry.json"),
53
+ path.resolve(here, "../registry/registry.json"),
54
+ ];
55
+ for (const candidate of candidates) {
56
+ if (existsSync(candidate)) {
57
+ return candidate;
58
+ }
59
+ }
60
+ throw new RegistryLoadError(
61
+ "Could not locate the packaged @usalyt/registry/registry.json. " +
62
+ "Reinstall @usalyt/cli so its @usalyt/registry dependency is present, then retry."
63
+ );
64
+ }
65
+ }
66
+
67
+ function isRecord(value: unknown): value is Record<string, unknown> {
68
+ return typeof value === "object" && value !== null && !Array.isArray(value);
69
+ }
70
+
71
+ function validateRegistry(value: unknown, registryPath: string): Registry {
72
+ if (!isRecord(value)) {
73
+ throw new RegistryLoadError(`Registry at ${registryPath} is not a JSON object.`);
74
+ }
75
+ if (typeof value.name !== "string" || typeof value.version !== "string") {
76
+ throw new RegistryLoadError(`Registry at ${registryPath} is missing "name" or "version".`);
77
+ }
78
+ if (!Array.isArray(value.items) || value.items.length === 0) {
79
+ throw new RegistryLoadError(`Registry at ${registryPath} contains no items.`);
80
+ }
81
+
82
+ for (const item of value.items) {
83
+ if (!isRecord(item) || typeof item.name !== "string" || typeof item.type !== "string") {
84
+ throw new RegistryLoadError(`Registry at ${registryPath} contains an invalid item.`);
85
+ }
86
+ if (!Array.isArray(item.files) || item.files.length === 0) {
87
+ throw new RegistryLoadError(`Registry item "${item.name}" contains no files.`);
88
+ }
89
+ for (const file of item.files) {
90
+ if (!isRecord(file) || typeof file.path !== "string" || typeof file.content !== "string") {
91
+ throw new RegistryLoadError(`Registry item "${item.name}" contains an invalid file entry.`);
92
+ }
93
+ }
94
+ }
95
+
96
+ return value as unknown as Registry;
97
+ }
98
+
99
+ export function loadRegistry(): { registry: Registry; registryPath: string } {
100
+ const registryPath = resolveRegistryPath();
101
+ let raw: string;
102
+ try {
103
+ raw = readFileSync(registryPath, "utf8");
104
+ } catch (error) {
105
+ throw new RegistryLoadError(
106
+ `Could not read the packaged registry at ${registryPath}: ${(error as Error).message}`
107
+ );
108
+ }
109
+
110
+ let parsed: unknown;
111
+ try {
112
+ parsed = JSON.parse(raw);
113
+ } catch (error) {
114
+ throw new RegistryLoadError(
115
+ `The packaged registry at ${registryPath} is not valid JSON: ${(error as Error).message}`
116
+ );
117
+ }
118
+
119
+ return { registry: validateRegistry(parsed, registryPath), registryPath };
120
+ }
@@ -0,0 +1,161 @@
1
+ import {
2
+ applyThemePresetToCss,
3
+ decodeThemePreset,
4
+ DEFAULT_THEME_CONFIG,
5
+ encodeThemePreset,
6
+ isDefaultThemeConfig,
7
+ isValidHexColor,
8
+ NAMED_THEME_PRESETS,
9
+ THEME_ACCENTS,
10
+ THEME_BASES,
11
+ THEME_DENSITIES,
12
+ THEME_FONTS,
13
+ THEME_RADII,
14
+ type ThemeAccentId,
15
+ type ThemeBaseId,
16
+ type ThemeDensityId,
17
+ type ThemeFontId,
18
+ type ThemePolarity,
19
+ type ThemePresetConfig,
20
+ type ThemeRadiusId,
21
+ } from "../../../registry/src/theme-presets";
22
+ import type { UsalytThemeConfig } from "./config";
23
+
24
+ export {
25
+ applyThemePresetToCss,
26
+ DEFAULT_THEME_CONFIG,
27
+ encodeThemePreset,
28
+ isDefaultThemeConfig,
29
+ NAMED_THEME_PRESETS,
30
+ THEME_ACCENTS,
31
+ THEME_BASES,
32
+ THEME_DENSITIES,
33
+ THEME_FONTS,
34
+ THEME_RADII,
35
+ type ThemePresetConfig,
36
+ };
37
+
38
+ export interface ThemeFlagInput {
39
+ preset?: string;
40
+ base?: string;
41
+ accent?: string;
42
+ radius?: string;
43
+ density?: string;
44
+ font?: string;
45
+ mode?: string;
46
+ }
47
+
48
+ export class ThemeOptionError extends Error {
49
+ constructor(message: string) {
50
+ super(message);
51
+ this.name = "ThemeOptionError";
52
+ }
53
+ }
54
+
55
+ export function hasThemeFlags(flags: ThemeFlagInput): boolean {
56
+ return Boolean(
57
+ flags.preset ||
58
+ flags.base ||
59
+ flags.accent ||
60
+ flags.radius ||
61
+ flags.density ||
62
+ flags.font ||
63
+ flags.mode
64
+ );
65
+ }
66
+
67
+ export function resolveThemeFromFlags(
68
+ flags: ThemeFlagInput,
69
+ startingConfig: ThemePresetConfig = DEFAULT_THEME_CONFIG
70
+ ): ThemePresetConfig {
71
+ let resolved: ThemePresetConfig = { ...startingConfig };
72
+
73
+ if (flags.preset) {
74
+ const trimmed = flags.preset.trim();
75
+ const isQueryOrUrl = trimmed.includes("=") || trimmed.includes("?");
76
+ const isKnownNamed = NAMED_THEME_PRESETS.some(
77
+ (p) => p.id.toLowerCase() === trimmed.toLowerCase()
78
+ );
79
+ if (!isQueryOrUrl && !isKnownNamed) {
80
+ const available = NAMED_THEME_PRESETS.map((p) => p.id).join(", ");
81
+ throw new ThemeOptionError(
82
+ `Unknown theme preset "${trimmed}". Available presets: ${available} (or pass a query/URL from the Theme Studio).`
83
+ );
84
+ }
85
+ resolved = decodeThemePreset(trimmed);
86
+ }
87
+
88
+ if (flags.base) {
89
+ if (!(flags.base in THEME_BASES)) {
90
+ throw new ThemeOptionError(
91
+ `Invalid --base "${flags.base}". Choose one of: ${Object.keys(THEME_BASES).join(", ")}.`
92
+ );
93
+ }
94
+ resolved.base = flags.base as ThemeBaseId;
95
+ }
96
+
97
+ if (flags.accent) {
98
+ const raw = flags.accent.trim();
99
+ if (raw in THEME_ACCENTS) {
100
+ resolved.accent = raw as ThemeAccentId;
101
+ } else if (isValidHexColor(raw)) {
102
+ resolved.accent = raw.toLowerCase();
103
+ } else if (isValidHexColor(`#${raw}`)) {
104
+ resolved.accent = `#${raw.toLowerCase()}`;
105
+ } else {
106
+ throw new ThemeOptionError(
107
+ `Invalid --accent "${raw}". Use one of (${Object.keys(THEME_ACCENTS).join(", ")}) or a 6-digit hex color like #5e6ad2.`
108
+ );
109
+ }
110
+ }
111
+
112
+ if (flags.radius) {
113
+ if (!(flags.radius in THEME_RADII)) {
114
+ throw new ThemeOptionError(
115
+ `Invalid --radius "${flags.radius}". Choose one of: ${Object.keys(THEME_RADII).join(", ")}.`
116
+ );
117
+ }
118
+ resolved.radius = flags.radius as ThemeRadiusId;
119
+ }
120
+
121
+ if (flags.density) {
122
+ if (!(flags.density in THEME_DENSITIES)) {
123
+ throw new ThemeOptionError(
124
+ `Invalid --density "${flags.density}". Choose one of: ${Object.keys(THEME_DENSITIES).join(", ")}.`
125
+ );
126
+ }
127
+ resolved.density = flags.density as ThemeDensityId;
128
+ }
129
+
130
+ if (flags.font) {
131
+ if (!(flags.font in THEME_FONTS)) {
132
+ throw new ThemeOptionError(
133
+ `Invalid --font "${flags.font}". Choose one of: ${Object.keys(THEME_FONTS).join(", ")}.`
134
+ );
135
+ }
136
+ resolved.font = flags.font as ThemeFontId;
137
+ }
138
+
139
+ if (flags.mode) {
140
+ if (flags.mode !== "dark" && flags.mode !== "light") {
141
+ throw new ThemeOptionError(
142
+ `Invalid --mode "${flags.mode}". Choose "dark" or "light".`
143
+ );
144
+ }
145
+ resolved.defaultMode = flags.mode as ThemePolarity;
146
+ }
147
+
148
+ return resolved;
149
+ }
150
+
151
+ export function toUsalytThemeConfig(config: ThemePresetConfig): UsalytThemeConfig {
152
+ return {
153
+ preset: encodeThemePreset(config),
154
+ base: config.base,
155
+ accent: config.accent,
156
+ radius: config.radius,
157
+ density: config.density,
158
+ font: config.font,
159
+ defaultMode: config.defaultMode,
160
+ };
161
+ }
@@ -0,0 +1,9 @@
1
+ import { defineConfig } from "tsdown";
2
+
3
+ export default defineConfig({
4
+ entry: ["src/index.ts"],
5
+ format: ["esm"],
6
+ dts: true,
7
+ clean: true,
8
+ outDir: "dist"
9
+ });