@blankways/ui 0.0.0-stage → 0.2.0-alpha.1

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.
Files changed (117) hide show
  1. package/README.md +246 -2
  2. package/dist/cli/cli.options.d.ts +5 -0
  3. package/dist/cli/cli.options.js +73 -0
  4. package/dist/cli/cli.registry.d.ts +6 -0
  5. package/dist/cli/cli.registry.js +37 -0
  6. package/dist/cli/cli.types.d.ts +33 -0
  7. package/dist/cli/cli.types.js +1 -0
  8. package/dist/cli/index.d.ts +3 -0
  9. package/dist/cli/index.js +3 -0
  10. package/dist/cli.d.ts +2 -0
  11. package/dist/cli.js +48 -0
  12. package/dist/commands/add.command.d.ts +4 -0
  13. package/dist/commands/add.command.js +49 -0
  14. package/dist/commands/dev.command.d.ts +3 -0
  15. package/dist/commands/dev.command.js +76 -0
  16. package/dist/commands/doctor.command.d.ts +3 -0
  17. package/dist/commands/doctor.command.js +103 -0
  18. package/dist/commands/index.d.ts +9 -0
  19. package/dist/commands/index.js +9 -0
  20. package/dist/commands/init.command.d.ts +2 -0
  21. package/dist/commands/init.command.js +43 -0
  22. package/dist/commands/install.command.d.ts +2 -0
  23. package/dist/commands/install.command.js +36 -0
  24. package/dist/commands/list.command.d.ts +2 -0
  25. package/dist/commands/list.command.js +59 -0
  26. package/dist/commands/meta.command.d.ts +1 -0
  27. package/dist/commands/meta.command.js +19 -0
  28. package/dist/commands/registry.command.d.ts +2 -0
  29. package/dist/commands/registry.command.js +69 -0
  30. package/dist/commands/styles.command.d.ts +5 -0
  31. package/dist/commands/styles.command.js +44 -0
  32. package/dist/config/config.helpers.d.ts +10 -0
  33. package/dist/config/config.helpers.js +116 -0
  34. package/dist/config/framework.helpers.d.ts +3 -0
  35. package/dist/config/framework.helpers.js +14 -0
  36. package/dist/config/index.d.ts +2 -0
  37. package/dist/config/index.js +2 -0
  38. package/dist/constants/cli.constants.d.ts +20 -0
  39. package/dist/constants/cli.constants.js +20 -0
  40. package/dist/constants/framework.constants.d.ts +3 -0
  41. package/dist/constants/framework.constants.js +2 -0
  42. package/dist/constants/index.d.ts +2 -0
  43. package/dist/constants/index.js +2 -0
  44. package/dist/doctor/checks/barrels.check.d.ts +1 -0
  45. package/dist/doctor/checks/barrels.check.js +60 -0
  46. package/dist/doctor/checks/contracts.check.d.ts +1 -0
  47. package/dist/doctor/checks/contracts.check.js +45 -0
  48. package/dist/doctor/checks/entries.check.d.ts +1 -0
  49. package/dist/doctor/checks/entries.check.js +21 -0
  50. package/dist/doctor/checks/icons.check.d.ts +1 -0
  51. package/dist/doctor/checks/icons.check.js +60 -0
  52. package/dist/doctor/checks/imports.check.d.ts +1 -0
  53. package/dist/doctor/checks/imports.check.js +45 -0
  54. package/dist/doctor/checks/index.d.ts +10 -0
  55. package/dist/doctor/checks/index.js +10 -0
  56. package/dist/doctor/checks/motion.check.d.ts +1 -0
  57. package/dist/doctor/checks/motion.check.js +66 -0
  58. package/dist/doctor/checks/namespaces.check.d.ts +1 -0
  59. package/dist/doctor/checks/namespaces.check.js +76 -0
  60. package/dist/doctor/checks/peers.check.d.ts +1 -0
  61. package/dist/doctor/checks/peers.check.js +55 -0
  62. package/dist/doctor/checks/styles.check.d.ts +1 -0
  63. package/dist/doctor/checks/styles.check.js +45 -0
  64. package/dist/doctor/checks/themes.check.d.ts +1 -0
  65. package/dist/doctor/checks/themes.check.js +42 -0
  66. package/dist/doctor/doctor.helpers.d.ts +6 -0
  67. package/dist/doctor/doctor.helpers.js +43 -0
  68. package/dist/doctor/doctor.registry.d.ts +10 -0
  69. package/dist/doctor/doctor.registry.js +24 -0
  70. package/dist/doctor/doctor.types.d.ts +31 -0
  71. package/dist/doctor/doctor.types.js +1 -0
  72. package/dist/doctor/index.d.ts +3 -0
  73. package/dist/doctor/index.js +3 -0
  74. package/dist/errors/cli-error.d.ts +3 -0
  75. package/dist/errors/cli-error.js +6 -0
  76. package/dist/errors/index.d.ts +1 -0
  77. package/dist/errors/index.js +1 -0
  78. package/dist/helpers/args.helpers.d.ts +6 -0
  79. package/dist/helpers/args.helpers.js +84 -0
  80. package/dist/helpers/barrels.helpers.d.ts +15 -0
  81. package/dist/helpers/barrels.helpers.js +157 -0
  82. package/dist/helpers/help.helpers.d.ts +1 -0
  83. package/dist/helpers/help.helpers.js +30 -0
  84. package/dist/helpers/index.d.ts +5 -0
  85. package/dist/helpers/index.js +5 -0
  86. package/dist/helpers/package.helpers.d.ts +2 -0
  87. package/dist/helpers/package.helpers.js +12 -0
  88. package/dist/helpers/path.helpers.d.ts +3 -0
  89. package/dist/helpers/path.helpers.js +15 -0
  90. package/dist/helpers/styles.helpers.d.ts +21 -0
  91. package/dist/helpers/styles.helpers.js +164 -0
  92. package/dist/index.d.ts +12 -0
  93. package/dist/index.js +11 -0
  94. package/dist/registry/index.d.ts +3 -0
  95. package/dist/registry/index.js +3 -0
  96. package/dist/registry/lockfile.d.ts +18 -0
  97. package/dist/registry/lockfile.js +32 -0
  98. package/dist/registry/registry.helpers.d.ts +12 -0
  99. package/dist/registry/registry.helpers.js +242 -0
  100. package/dist/registry/registry.providers.d.ts +13 -0
  101. package/dist/registry/registry.providers.js +159 -0
  102. package/dist/registry/registry.service.d.ts +14 -0
  103. package/dist/registry/registry.service.js +126 -0
  104. package/dist/types/cli.types.d.ts +6 -0
  105. package/dist/types/cli.types.js +1 -0
  106. package/dist/types/config.types.d.ts +70 -0
  107. package/dist/types/config.types.js +1 -0
  108. package/dist/types/index.d.ts +3 -0
  109. package/dist/types/index.js +3 -0
  110. package/dist/types/registry.types.d.ts +68 -0
  111. package/dist/types/registry.types.js +3 -0
  112. package/dist/ui-api/index.d.ts +16 -0
  113. package/dist/ui-api/index.js +30 -0
  114. package/dist/ui-api/ui-api.d.ts +18 -0
  115. package/dist/ui-api/ui-api.js +194 -0
  116. package/package.json +37 -3
  117. package/styles.css +16 -0
@@ -0,0 +1,32 @@
1
+ import { createHash } from 'node:crypto';
2
+ import { readFile, writeFile } from 'node:fs/promises';
3
+ import { join } from 'node:path';
4
+ import { LOCK_FILE_NAME } from '../constants/index.js';
5
+ export function createEmptyLock(exposure = 'full') {
6
+ return { version: 1, exposure, registries: {}, entries: {} };
7
+ }
8
+ export async function readLockfile(cwd) {
9
+ try {
10
+ const content = await readFile(join(cwd, LOCK_FILE_NAME), 'utf8');
11
+ const parsed = JSON.parse(content);
12
+ if (parsed.version !== 1 || typeof parsed.entries !== 'object') {
13
+ throw new Error('unsupported version');
14
+ }
15
+ return parsed;
16
+ }
17
+ catch (error) {
18
+ if (error.code === 'ENOENT')
19
+ return createEmptyLock();
20
+ const message = error instanceof Error ? error.message : String(error);
21
+ throw new Error(`Invalid ${LOCK_FILE_NAME}: ${message}`);
22
+ }
23
+ }
24
+ export async function writeLockfile(cwd, lock) {
25
+ await writeFile(join(cwd, LOCK_FILE_NAME), `${JSON.stringify(lock, null, 2)}\n`, 'utf8');
26
+ }
27
+ export function hashContent(content) {
28
+ return `sha256:${createHash('sha256').update(content).digest('hex').slice(0, 16)}`;
29
+ }
30
+ export async function hashFile(path) {
31
+ return hashContent(await readFile(path, 'utf8'));
32
+ }
@@ -0,0 +1,12 @@
1
+ import { type SupportedFramework } from '../constants/index.js';
2
+ import { type CopyResult, type RegistryItem, type RegistryKind } from '../types/registry.types.js';
3
+ export declare function isValidComponentName(name: string): boolean;
4
+ export declare function assertValidComponentName(name: string): void;
5
+ export declare function assertValidRegistryKind(value: string | undefined): RegistryKind | undefined;
6
+ /** Reads every entry of a registry directory, independent of where it lives. */
7
+ export declare function listItemsFromDirectory(root: string, framework: SupportedFramework, providerId: string, registryId?: string): Promise<RegistryItem[]>;
8
+ export declare function copyRegistryItem(item: RegistryItem, componentsDir: string, options: {
9
+ cwd: string;
10
+ force?: boolean;
11
+ dryRun?: boolean;
12
+ }): Promise<CopyResult>;
@@ -0,0 +1,242 @@
1
+ import { copyFile, mkdir, readFile, readdir, stat } from 'node:fs/promises';
2
+ import { basename, dirname, isAbsolute, join, normalize, relative } from 'node:path';
3
+ import { COMPONENT_NAME_PATTERN, COMPONENTS_SUBDIR, MANIFEST_FILE_NAME } from '../constants/index.js';
4
+ import { assertFramework } from '../config/framework.helpers.js';
5
+ import { CliError } from '../errors/index.js';
6
+ import { assertSafeRelativePath, toDisplayPath } from '../helpers/path.helpers.js';
7
+ import { REGISTRY_KINDS, REGISTRY_ROOTS } from '../types/registry.types.js';
8
+ const COMPONENT_EXTENSIONS = ['.tsx', '.ts', '.jsx', '.js'];
9
+ const IGNORED_DIRECTORIES = new Set(['node_modules']);
10
+ const IGNORED_FILE_NAMES = new Set([MANIFEST_FILE_NAME, 'README.md']);
11
+ async function pathExists(path) {
12
+ try {
13
+ await stat(path);
14
+ return true;
15
+ }
16
+ catch {
17
+ return false;
18
+ }
19
+ }
20
+ async function isDirectory(path) {
21
+ try {
22
+ return (await stat(path)).isDirectory();
23
+ }
24
+ catch {
25
+ return false;
26
+ }
27
+ }
28
+ async function isFile(path) {
29
+ try {
30
+ return (await stat(path)).isFile();
31
+ }
32
+ catch {
33
+ return false;
34
+ }
35
+ }
36
+ export function isValidComponentName(name) {
37
+ return COMPONENT_NAME_PATTERN.test(name) && name !== '.' && name !== '..';
38
+ }
39
+ export function assertValidComponentName(name) {
40
+ if (!isValidComponentName(name)) {
41
+ throw new CliError(`Invalid component name "${name}". Use letters, numbers, dots, dashes or underscores.`);
42
+ }
43
+ }
44
+ export function assertValidRegistryKind(value) {
45
+ if (value === undefined)
46
+ return undefined;
47
+ if (!REGISTRY_KINDS.includes(value)) {
48
+ throw new CliError(`Unknown kind "${value}". Use ${REGISTRY_KINDS.join(', ')}.`);
49
+ }
50
+ return value;
51
+ }
52
+ function inferKind(name) {
53
+ if (name.startsWith('icons'))
54
+ return 'icon';
55
+ if (name.startsWith('styles'))
56
+ return 'style';
57
+ if (name.startsWith('theme'))
58
+ return 'theme';
59
+ if (name === 'cn' || name.endsWith('-styles'))
60
+ return 'utility';
61
+ return 'component';
62
+ }
63
+ function getFileComponentName(fileName) {
64
+ for (const extension of COMPONENT_EXTENSIONS) {
65
+ if (fileName.endsWith(extension)) {
66
+ return fileName.slice(0, -extension.length);
67
+ }
68
+ }
69
+ return undefined;
70
+ }
71
+ async function walkDirectory(directory) {
72
+ const files = [];
73
+ const entries = await readdir(directory, { withFileTypes: true });
74
+ for (const entry of entries) {
75
+ if (entry.name.startsWith('.') || IGNORED_DIRECTORIES.has(entry.name))
76
+ continue;
77
+ const fullPath = join(directory, entry.name);
78
+ if (entry.isDirectory()) {
79
+ files.push(...(await walkDirectory(fullPath)));
80
+ }
81
+ else if (entry.isFile() && !IGNORED_FILE_NAMES.has(entry.name)) {
82
+ files.push(fullPath);
83
+ }
84
+ }
85
+ return files;
86
+ }
87
+ function toStringArray(value, field) {
88
+ if (value === undefined)
89
+ return [];
90
+ if (!Array.isArray(value) || value.some(item => typeof item !== 'string')) {
91
+ throw new CliError(`${MANIFEST_FILE_NAME}: "${field}" must be an array of strings.`);
92
+ }
93
+ return value;
94
+ }
95
+ function normalizeTarget(target, kind) {
96
+ assertSafeRelativePath(target);
97
+ const normalized = normalize(target).replace(/\\/g, '/');
98
+ const isKnownRoot = REGISTRY_ROOTS.some(root => normalized.startsWith(`${root}/`));
99
+ if (kind !== 'component' || isKnownRoot)
100
+ return normalized;
101
+ return `${COMPONENTS_SUBDIR}/${normalized}`;
102
+ }
103
+ function resolveSource(root, name, source) {
104
+ if (isAbsolute(source))
105
+ return source;
106
+ assertSafeRelativePath(source);
107
+ return join(root, name, normalize(source));
108
+ }
109
+ function normalizeFiles(root, name, files, kind) {
110
+ if (files === undefined)
111
+ return [];
112
+ if (!Array.isArray(files)) {
113
+ throw new CliError(`${MANIFEST_FILE_NAME}: "files" must be an array.`);
114
+ }
115
+ return files.map((file, index) => {
116
+ if (typeof file !== 'object' || file === null) {
117
+ throw new CliError(`${MANIFEST_FILE_NAME}: files[${index}] must be an object.`);
118
+ }
119
+ const record = file;
120
+ if (typeof record.source !== 'string' || typeof record.target !== 'string') {
121
+ throw new CliError(`${MANIFEST_FILE_NAME}: files[${index}] needs string "source" and "target".`);
122
+ }
123
+ return {
124
+ source: resolveSource(root, name, record.source),
125
+ target: normalizeTarget(record.target, kind)
126
+ };
127
+ });
128
+ }
129
+ async function readManifest(path) {
130
+ let parsed;
131
+ try {
132
+ parsed = JSON.parse(await readFile(path, 'utf8'));
133
+ }
134
+ catch (error) {
135
+ const message = error instanceof Error ? error.message : String(error);
136
+ throw new CliError(`Invalid ${MANIFEST_FILE_NAME}: ${message}`);
137
+ }
138
+ if (typeof parsed !== 'object' || parsed === null) {
139
+ throw new CliError(`${MANIFEST_FILE_NAME} must contain a JSON object.`);
140
+ }
141
+ return parsed;
142
+ }
143
+ async function createItem(root, providerId, registryId, name, manifest, fallbackFiles, framework = 'react') {
144
+ assertValidComponentName(name);
145
+ if (manifest?.name !== undefined && manifest.name !== name) {
146
+ throw new CliError(`${MANIFEST_FILE_NAME} in "${name}" declares name "${manifest.name}".`);
147
+ }
148
+ const declaredKind = manifest?.kind;
149
+ if (declaredKind !== undefined &&
150
+ !REGISTRY_KINDS.includes(declaredKind)) {
151
+ throw new CliError(`${MANIFEST_FILE_NAME} in "${name}" has invalid kind "${String(declaredKind)}".`);
152
+ }
153
+ const kind = declaredKind ?? inferKind(name);
154
+ const files = manifest?.files !== undefined
155
+ ? normalizeFiles(root, name, manifest.files, kind)
156
+ : fallbackFiles.map(file => {
157
+ const directoryRoot = join(root, name);
158
+ const target = isAbsolute(file) && file.startsWith(directoryRoot)
159
+ ? join(name, relative(directoryRoot, file))
160
+ : basename(file);
161
+ return { source: file, target: normalizeTarget(target, kind) };
162
+ });
163
+ if (files.length === 0)
164
+ return null;
165
+ return {
166
+ registry: providerId,
167
+ id: registryId,
168
+ name,
169
+ kind,
170
+ framework: assertFramework(manifest?.framework ?? framework),
171
+ description: manifest?.description ?? `Registry entry "${name}".`,
172
+ dependencies: toStringArray(manifest?.dependencies, 'dependencies'),
173
+ requires: toStringArray(manifest?.requires, 'requires'),
174
+ themes: toStringArray(manifest?.themes, 'themes'),
175
+ revision: typeof manifest?.revision === 'number' ? manifest.revision : 1,
176
+ peers: toStringArray(manifest?.peers, 'peers'),
177
+ suggestions: toStringArray(manifest?.suggestions, 'suggestions'),
178
+ files,
179
+ hidden: manifest?.hidden === true
180
+ };
181
+ }
182
+ async function loadDirectoryItem(root, providerId, registryId, name, directory, framework) {
183
+ const manifestPath = join(directory, MANIFEST_FILE_NAME);
184
+ // A directory is an entry only when it declares a manifest; helpers such as
185
+ // scripts/ or docs/ are package infrastructure, not registry entries.
186
+ if (!(await isFile(manifestPath)))
187
+ return null;
188
+ const manifest = await readManifest(manifestPath);
189
+ return createItem(root, providerId, registryId, name, manifest, [], framework);
190
+ }
191
+ /** Reads every entry of a registry directory, independent of where it lives. */
192
+ export async function listItemsFromDirectory(root, framework, providerId, registryId = providerId) {
193
+ if (!(await isDirectory(root)))
194
+ return [];
195
+ const entries = await readdir(root, { withFileTypes: true });
196
+ const items = [];
197
+ for (const entry of entries) {
198
+ if (entry.name.startsWith('.') || IGNORED_DIRECTORIES.has(entry.name))
199
+ continue;
200
+ if (entry.isDirectory() && isValidComponentName(entry.name)) {
201
+ const item = await loadDirectoryItem(root, providerId, registryId, entry.name, join(root, entry.name), framework);
202
+ if (item?.framework === framework)
203
+ items.push(item);
204
+ continue;
205
+ }
206
+ if (entry.isFile() && !IGNORED_FILE_NAMES.has(entry.name)) {
207
+ const name = getFileComponentName(entry.name);
208
+ if (name !== undefined && isValidComponentName(name)) {
209
+ const item = await createItem(root, providerId, registryId, name, null, [join(root, entry.name)], framework);
210
+ if (item?.framework === framework)
211
+ items.push(item);
212
+ }
213
+ }
214
+ }
215
+ return items.sort((a, b) => a.name.localeCompare(b.name));
216
+ }
217
+ export async function copyRegistryItem(item, componentsDir, options) {
218
+ const result = { copied: [], skipped: [], planned: [] };
219
+ for (const entry of item.files) {
220
+ assertSafeRelativePath(entry.target);
221
+ const target = join(componentsDir, entry.target);
222
+ const displayPath = toDisplayPath(options.cwd, target);
223
+ if (options.dryRun) {
224
+ result.planned.push(displayPath);
225
+ continue;
226
+ }
227
+ if (!options.force && (await pathExists(target))) {
228
+ result.skipped.push(displayPath);
229
+ continue;
230
+ }
231
+ await mkdir(dirname(target), { recursive: true });
232
+ try {
233
+ await copyFile(entry.source, target);
234
+ }
235
+ catch (error) {
236
+ const message = error instanceof Error ? error.message : String(error);
237
+ throw new CliError(`Could not copy "${entry.source}": ${message}`);
238
+ }
239
+ result.copied.push(displayPath);
240
+ }
241
+ return result;
242
+ }
@@ -0,0 +1,13 @@
1
+ import { type SupportedFramework } from '../constants/index.js';
2
+ import type { RegistryProvider } from '../types/registry.types.js';
3
+ /** Resolves the entries directory declared by an external registry package. */
4
+ export declare function resolveRegistryDirectory(cwd: string, specifier: string): Promise<{
5
+ packageRoot: string;
6
+ root: string;
7
+ id: string;
8
+ version: string;
9
+ uiRequires: string[];
10
+ devRegistries: string[];
11
+ }>;
12
+ export declare function loadRegistryProvider(cwd: string, specifier: string, framework: SupportedFramework): Promise<RegistryProvider>;
13
+ export declare function loadRegistryProviders(cwd: string, specifiers: readonly string[], framework: SupportedFramework): Promise<RegistryProvider[]>;
@@ -0,0 +1,159 @@
1
+ import { readFile, stat } from 'node:fs/promises';
2
+ import { createRequire } from 'node:module';
3
+ import { dirname, isAbsolute, join, resolve } from 'node:path';
4
+ import { UI_REGISTRY_FIELD, UI_DEV_REGISTRIES_FIELD, UI_REGISTRY_ID_FIELD, UI_REQUIRES_FIELD } from '../constants/index.js';
5
+ import { CliError } from '../errors/index.js';
6
+ import { assertSafeRelativePath, resolveProjectPath } from '../helpers/path.helpers.js';
7
+ import { listItemsFromDirectory } from './registry.helpers.js';
8
+ const PACKAGE_JSON = 'package.json';
9
+ const REGISTRY_ID_PATTERN = /^[a-z0-9][a-z0-9._-]*$/;
10
+ function readStringArray(value, field) {
11
+ if (value === undefined)
12
+ return [];
13
+ if (!Array.isArray(value) || value.some(entry => typeof entry !== 'string')) {
14
+ throw new CliError(`${PACKAGE_JSON}: "${field}" must be an array of strings.`);
15
+ }
16
+ return value;
17
+ }
18
+ async function isDirectory(path) {
19
+ try {
20
+ return (await stat(path)).isDirectory();
21
+ }
22
+ catch {
23
+ return false;
24
+ }
25
+ }
26
+ async function isFile(path) {
27
+ try {
28
+ return (await stat(path)).isFile();
29
+ }
30
+ catch {
31
+ return false;
32
+ }
33
+ }
34
+ async function readPackageJson(path) {
35
+ let parsed;
36
+ try {
37
+ parsed = JSON.parse((await readFile(path, 'utf8')).replace(/^\uFEFF/, ''));
38
+ }
39
+ catch (error) {
40
+ const message = error instanceof Error ? error.message : String(error);
41
+ throw new CliError(`Invalid ${PACKAGE_JSON} at ${path}: ${message}`);
42
+ }
43
+ if (typeof parsed !== 'object' || parsed === null) {
44
+ throw new CliError(`${PACKAGE_JSON} at ${path} must contain an object.`);
45
+ }
46
+ return parsed;
47
+ }
48
+ async function findPackageRoot(entry, specifier, entryIsManifest) {
49
+ if (entryIsManifest) {
50
+ const manifest = await readPackageJson(entry);
51
+ return { directory: dirname(entry), manifest };
52
+ }
53
+ let directory = dirname(entry);
54
+ while (true) {
55
+ const manifestPath = join(directory, PACKAGE_JSON);
56
+ if (await isFile(manifestPath)) {
57
+ try {
58
+ const manifest = await readPackageJson(manifestPath);
59
+ if (manifest.name === specifier || manifest[UI_REGISTRY_FIELD] !== undefined) {
60
+ return { directory, manifest };
61
+ }
62
+ }
63
+ catch {
64
+ // Keep walking: a nested, malformed manifest is not the package root.
65
+ }
66
+ }
67
+ const parent = dirname(directory);
68
+ if (parent === directory)
69
+ break;
70
+ directory = parent;
71
+ }
72
+ throw new CliError(`Could not find the root of registry package "${specifier}" from ${entry}.`);
73
+ }
74
+ async function resolvePackageRoot(cwd, specifier) {
75
+ if (specifier.startsWith('.') || isAbsolute(specifier)) {
76
+ const directory = resolveProjectPath(cwd, specifier);
77
+ if (!(await isDirectory(directory))) {
78
+ throw new CliError(`Registry package directory not found: ${specifier}`);
79
+ }
80
+ return directory;
81
+ }
82
+ const requireFromCwd = createRequire(join(cwd, '__ui_registry__.js'));
83
+ let entry;
84
+ let entryIsManifest = false;
85
+ try {
86
+ entry = requireFromCwd.resolve(`${specifier}/${PACKAGE_JSON}`);
87
+ entryIsManifest = true;
88
+ }
89
+ catch {
90
+ try {
91
+ entry = requireFromCwd.resolve(specifier);
92
+ }
93
+ catch {
94
+ throw new CliError(`Registry package "${specifier}" could not be resolved from ${cwd}. Install it or use a relative path.`);
95
+ }
96
+ }
97
+ return (await findPackageRoot(entry, specifier, entryIsManifest)).directory;
98
+ }
99
+ /** Resolves the entries directory declared by an external registry package. */
100
+ export async function resolveRegistryDirectory(cwd, specifier) {
101
+ if (typeof specifier !== 'string' || specifier.trim() === '') {
102
+ throw new CliError('Registry specifiers must be non-empty strings.');
103
+ }
104
+ const packageRoot = await resolvePackageRoot(cwd, specifier);
105
+ const manifest = await readPackageJson(join(packageRoot, PACKAGE_JSON));
106
+ const declared = manifest[UI_REGISTRY_FIELD];
107
+ if (declared === undefined) {
108
+ throw new CliError(`Package "${specifier}" does not declare "${UI_REGISTRY_FIELD}" in its ${PACKAGE_JSON}.`);
109
+ }
110
+ const directoryValue = declared === true ? '.' : declared;
111
+ if (typeof directoryValue !== 'string' || directoryValue.trim() === '') {
112
+ throw new CliError(`${PACKAGE_JSON} of "${specifier}" must declare "${UI_REGISTRY_FIELD}" as a directory path.`);
113
+ }
114
+ const id = manifest[UI_REGISTRY_ID_FIELD];
115
+ if (typeof id !== 'string' || !REGISTRY_ID_PATTERN.test(id)) {
116
+ throw new CliError(`${PACKAGE_JSON} of "${specifier}" must declare "${UI_REGISTRY_ID_FIELD}" as a lowercase slug.`);
117
+ }
118
+ if (directoryValue !== '.')
119
+ assertSafeRelativePath(directoryValue);
120
+ const root = resolve(packageRoot, directoryValue);
121
+ if (!(await isDirectory(root))) {
122
+ throw new CliError(`Registry directory "${directoryValue}" declared by "${specifier}" does not exist.`);
123
+ }
124
+ return {
125
+ packageRoot,
126
+ root,
127
+ id,
128
+ version: typeof manifest.version === 'string' ? manifest.version : '0.0.0',
129
+ uiRequires: readStringArray(manifest[UI_REQUIRES_FIELD], UI_REQUIRES_FIELD),
130
+ devRegistries: readStringArray(manifest[UI_DEV_REGISTRIES_FIELD], UI_DEV_REGISTRIES_FIELD)
131
+ };
132
+ }
133
+ export async function loadRegistryProvider(cwd, specifier, framework) {
134
+ const { packageRoot, root, id, version, uiRequires, devRegistries } = await resolveRegistryDirectory(cwd, specifier);
135
+ return {
136
+ specifier,
137
+ packageRoot,
138
+ root,
139
+ id,
140
+ version,
141
+ uiRequires,
142
+ devRegistries,
143
+ items: await listItemsFromDirectory(root, framework, specifier, id)
144
+ };
145
+ }
146
+ export async function loadRegistryProviders(cwd, specifiers, framework) {
147
+ const providers = [];
148
+ const ids = new Map();
149
+ for (const specifier of Array.from(new Set(specifiers))) {
150
+ const provider = await loadRegistryProvider(cwd, specifier, framework);
151
+ const owner = ids.get(provider.id);
152
+ if (owner) {
153
+ throw new CliError(`Duplicate registry id "${provider.id}" declared by "${owner}" and "${specifier}".`);
154
+ }
155
+ ids.set(provider.id, specifier);
156
+ providers.push(provider);
157
+ }
158
+ return providers;
159
+ }
@@ -0,0 +1,14 @@
1
+ import { type SupportedFramework } from '../constants/index.js';
2
+ import type { RegistryItem, ResolvedRegistryItem } from '../types/registry.types.js';
3
+ export type ListRegistryOptions = {
4
+ cwd?: string;
5
+ framework?: SupportedFramework;
6
+ registries?: readonly string[];
7
+ };
8
+ export type ResolveOptions = ListRegistryOptions & {
9
+ kind?: string;
10
+ noDeps?: boolean;
11
+ };
12
+ /** Lists entries from every registry configured for the project. */
13
+ export declare function listRegistryItems(options?: ListRegistryOptions): Promise<RegistryItem[]>;
14
+ export declare function resolveSelectedComponents(names: readonly string[], all: boolean, options?: ResolveOptions): Promise<ResolvedRegistryItem[]>;
@@ -0,0 +1,126 @@
1
+ import { resolve } from 'node:path';
2
+ import { assertFramework } from '../config/framework.helpers.js';
3
+ import { DEFAULT_FRAMEWORK } from '../constants/index.js';
4
+ import { CliError } from '../errors/index.js';
5
+ import { assertValidComponentName, assertValidRegistryKind } from './registry.helpers.js';
6
+ import { loadRegistryProviders } from './registry.providers.js';
7
+ function getContext(options) {
8
+ return {
9
+ cwd: options.cwd ? resolve(options.cwd) : process.cwd(),
10
+ framework: assertFramework(options.framework ?? DEFAULT_FRAMEWORK),
11
+ registries: options.registries ?? []
12
+ };
13
+ }
14
+ function qualifiedName(item) {
15
+ return `${item.id}:${item.name}`;
16
+ }
17
+ function specifierName(item) {
18
+ return `${item.registry}:${item.name}`;
19
+ }
20
+ /** Lists entries from every registry configured for the project. */
21
+ export async function listRegistryItems(options = {}) {
22
+ const { cwd, framework, registries } = getContext(options);
23
+ if (registries.length === 0)
24
+ return [];
25
+ const providers = await loadRegistryProviders(cwd, registries, framework);
26
+ return providers.flatMap(provider => provider.items);
27
+ }
28
+ export async function resolveSelectedComponents(names, all, options = {}) {
29
+ const kind = assertValidRegistryKind(options.kind);
30
+ const { cwd, framework, registries } = getContext(options);
31
+ const items = await listRegistryItems({ cwd, framework, registries });
32
+ if (items.length === 0) {
33
+ throw new CliError('No registries configured. Add one with "ui registry add <package>" or pass --registry.');
34
+ }
35
+ const byQualified = new Map();
36
+ for (const item of items) {
37
+ byQualified.set(qualifiedName(item), item);
38
+ byQualified.set(specifierName(item), item);
39
+ }
40
+ const byPlain = new Map();
41
+ for (const item of items) {
42
+ const existing = byPlain.get(item.name) ?? [];
43
+ existing.push(item);
44
+ byPlain.set(item.name, existing);
45
+ }
46
+ const describe = (item) => qualifiedName(item);
47
+ const resolveName = (name) => {
48
+ const exact = byQualified.get(name);
49
+ if (exact)
50
+ return exact;
51
+ const candidates = byPlain.get(name) ?? [];
52
+ if (candidates.length === 1)
53
+ return candidates[0];
54
+ if (candidates.length > 1) {
55
+ throw new CliError(`Ambiguous entry "${name}". Use one of: ${candidates.map(describe).join(', ')}.`);
56
+ }
57
+ if (name.includes(':')) {
58
+ const id = name.slice(0, name.indexOf(':'));
59
+ const ids = Array.from(new Set(items.map(item => item.id))).sort();
60
+ throw new CliError(`Unknown registry entry "${name}". No configured registry declares id "${id}". Available ids: ${ids.join(', ') || 'none'}.`);
61
+ }
62
+ const available = items
63
+ .filter(item => !item.hidden)
64
+ .map(describe)
65
+ .join(', ') || 'none';
66
+ throw new CliError(`Unknown registry entry/entries: ${name}. Available: ${available}.`);
67
+ };
68
+ const requestedNames = all
69
+ ? items
70
+ .filter(item => !item.hidden && (kind === undefined || item.kind === kind))
71
+ .map(qualifiedName)
72
+ : Array.from(new Set(names));
73
+ if (requestedNames.length === 0) {
74
+ throw new CliError('No entries specified. Use <name...>, --all or --kind.');
75
+ }
76
+ for (const name of requestedNames) {
77
+ if (!name.includes(':') && !byPlain.has(name))
78
+ assertValidComponentName(name);
79
+ }
80
+ const selected = requestedNames.map(resolveName);
81
+ const hidden = selected.filter(item => item.hidden);
82
+ if (hidden.length > 0) {
83
+ throw new CliError(`Internal registry entries cannot be added directly: ${hidden.map(describe).join(', ')}. They are installed automatically as dependencies.`);
84
+ }
85
+ if (kind !== undefined) {
86
+ const wrongKind = selected.filter(item => item.kind !== kind);
87
+ if (wrongKind.length > 0) {
88
+ throw new CliError(`Expected kind "${kind}" but got: ${wrongKind.map(describe).join(', ')}.`);
89
+ }
90
+ }
91
+ const resolveDependency = (owner, dependency) => {
92
+ if (dependency.includes(':'))
93
+ return resolveName(dependency);
94
+ const local = items.find(item => item.id === owner.id && item.name === dependency);
95
+ if (local)
96
+ return local;
97
+ return resolveName(dependency);
98
+ };
99
+ const ordered = [];
100
+ const requested = new Set();
101
+ const resolved = new Set();
102
+ const visiting = [];
103
+ const visit = (item, isRequested) => {
104
+ const key = qualifiedName(item);
105
+ if (isRequested)
106
+ requested.add(key);
107
+ if (resolved.has(key))
108
+ return;
109
+ const cycleIndex = visiting.indexOf(key);
110
+ if (cycleIndex !== -1) {
111
+ throw new CliError(`Registry dependency cycle: ${[...visiting.slice(cycleIndex), key].join(' -> ')}`);
112
+ }
113
+ visiting.push(key);
114
+ if (!options.noDeps) {
115
+ for (const dependency of [...item.dependencies, ...item.requires]) {
116
+ visit(resolveDependency(item, dependency), false);
117
+ }
118
+ }
119
+ visiting.pop();
120
+ resolved.add(key);
121
+ ordered.push({ ...item, requested: requested.has(key) });
122
+ };
123
+ for (const item of selected)
124
+ visit(item, true);
125
+ return ordered;
126
+ }
@@ -0,0 +1,6 @@
1
+ export type FlagValue = string | boolean;
2
+ export type CliArgs = {
3
+ command: string | undefined;
4
+ names: string[];
5
+ flags: Record<string, FlagValue>;
6
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,70 @@
1
+ import type { SupportedFramework } from '../constants/index.js';
2
+ import type { ExposureMode } from './registry.types.js';
3
+ export type ComponentsConfig = {
4
+ componentsDir: string;
5
+ framework: SupportedFramework;
6
+ overwrite: boolean;
7
+ /** Package specifiers that provide registry entries. */
8
+ registries: string[];
9
+ /** `full` installs every visible entry; `incremental` installs on demand. */
10
+ exposure: ExposureMode;
11
+ /** Entries owned by an incremental project; the reproducible source for CI. */
12
+ entries?: string[];
13
+ /** Optional deterministic CSS order across peers. */
14
+ themeOrder?: string[];
15
+ };
16
+ export type InitOptions = {
17
+ cwd?: string;
18
+ componentsDir?: string;
19
+ framework?: string;
20
+ variant?: string;
21
+ registries?: string[];
22
+ force?: boolean;
23
+ };
24
+ export type AddOptions = {
25
+ cwd?: string;
26
+ componentsDir?: string;
27
+ names: string[];
28
+ registries?: string[];
29
+ force?: boolean;
30
+ dryRun?: boolean;
31
+ all?: boolean;
32
+ kind?: string;
33
+ noDeps?: boolean;
34
+ noImport?: boolean;
35
+ globalCss?: string;
36
+ };
37
+ export type ListOptions = {
38
+ cwd?: string;
39
+ kind?: string;
40
+ framework?: SupportedFramework;
41
+ registries?: string[];
42
+ };
43
+ export type RegistryOptions = {
44
+ cwd?: string;
45
+ names?: string[];
46
+ };
47
+ export type StylesOptions = {
48
+ cwd?: string;
49
+ componentsDir?: string;
50
+ globalCss?: string;
51
+ dryRun?: boolean;
52
+ };
53
+ export type DoctorOptions = {
54
+ cwd?: string;
55
+ componentsDir?: string;
56
+ registries?: string[];
57
+ globalCss?: string;
58
+ };
59
+ export type InstallOptions = {
60
+ cwd?: string;
61
+ componentsDir?: string;
62
+ registries?: string[];
63
+ selectors?: string[];
64
+ exposure?: ExposureMode;
65
+ force?: boolean;
66
+ dryRun?: boolean;
67
+ frozen?: boolean;
68
+ installPeers?: boolean;
69
+ globalCss?: string;
70
+ };
@@ -0,0 +1 @@
1
+ export {};