@barqjs/ui-cli 0.0.0-stage → 0.2.0-next.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/LICENSE +21 -0
- package/README.md +195 -2
- package/dist/build-CcNYbuwH.js +137 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +1771 -0
- package/package.json +63 -4
- package/src/add.ts +124 -0
- package/src/build.ts +167 -0
- package/src/config.ts +76 -0
- package/src/diff.ts +111 -0
- package/src/files.ts +147 -0
- package/src/index.ts +989 -0
- package/src/project.ts +115 -0
- package/src/registry.ts +196 -0
- package/src/schema.ts +392 -0
- package/src/theme.ts +227 -0
- package/src/tty.ts +133 -0
package/src/project.ts
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What the project already has, and the package manager that gives it more.
|
|
3
|
+
*
|
|
4
|
+
* The manager is read from the LOCKFILE, nearest first, because that is the one
|
|
5
|
+
* fact a project states about itself: a pnpm workspace invoked through `npx`
|
|
6
|
+
* still wants `pnpm add`. What ran this process is the fallback, for a project
|
|
7
|
+
* that has not installed anything yet.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { spawnSync } from "node:child_process";
|
|
11
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
12
|
+
import { dirname, join, resolve } from "node:path";
|
|
13
|
+
|
|
14
|
+
export function installedPackages(root: string): Set<string> {
|
|
15
|
+
const path = join(root, "package.json");
|
|
16
|
+
if (!existsSync(path)) return new Set();
|
|
17
|
+
|
|
18
|
+
const manifest = JSON.parse(readFileSync(path, "utf8")) as Record<string, unknown>;
|
|
19
|
+
const out = new Set<string>();
|
|
20
|
+
for (const field of ["dependencies", "devDependencies", "peerDependencies"]) {
|
|
21
|
+
const entry = manifest[field];
|
|
22
|
+
if (typeof entry !== "object" || entry === null) continue;
|
|
23
|
+
for (const name of Object.keys(entry)) out.add(name);
|
|
24
|
+
}
|
|
25
|
+
return out;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export type PackageManager = "bun" | "pnpm" | "yarn" | "npm";
|
|
29
|
+
|
|
30
|
+
const LOCKFILES: readonly (readonly [string, PackageManager])[] = [
|
|
31
|
+
["bun.lock", "bun"],
|
|
32
|
+
["bun.lockb", "bun"],
|
|
33
|
+
["pnpm-lock.yaml", "pnpm"],
|
|
34
|
+
["yarn.lock", "yarn"],
|
|
35
|
+
["package-lock.json", "npm"],
|
|
36
|
+
];
|
|
37
|
+
|
|
38
|
+
/** `npm_config_user_agent` is set by all four, and names the one that ran us. */
|
|
39
|
+
function fromUserAgent(): PackageManager | undefined {
|
|
40
|
+
const name = process.env["npm_config_user_agent"]?.split(" ")[0]?.split("/")[0];
|
|
41
|
+
return name === "bun" || name === "pnpm" || name === "yarn" || name === "npm" ? name : undefined;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Walks up from `root`, because a workspace keeps its one lockfile at the top
|
|
46
|
+
* and the package a component is added to is somewhere under it.
|
|
47
|
+
*/
|
|
48
|
+
export function packageManager(root: string): PackageManager {
|
|
49
|
+
let directory = resolve(root);
|
|
50
|
+
for (;;) {
|
|
51
|
+
for (const [file, name] of LOCKFILES) {
|
|
52
|
+
if (existsSync(join(directory, file))) return name;
|
|
53
|
+
}
|
|
54
|
+
const parent = dirname(directory);
|
|
55
|
+
if (parent === directory) break;
|
|
56
|
+
directory = parent;
|
|
57
|
+
}
|
|
58
|
+
return fromUserAgent() ?? "npm";
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export interface InstallOptions {
|
|
62
|
+
readonly dev?: boolean;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** The command as its arguments, which is what runs it and what prints it. */
|
|
66
|
+
export function installArguments(
|
|
67
|
+
manager: PackageManager,
|
|
68
|
+
packages: readonly string[],
|
|
69
|
+
options: InstallOptions = {},
|
|
70
|
+
): string[] {
|
|
71
|
+
return [
|
|
72
|
+
manager,
|
|
73
|
+
manager === "npm" ? "install" : "add",
|
|
74
|
+
...(options.dev === true ? ["-D"] : []),
|
|
75
|
+
...packages,
|
|
76
|
+
];
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export function installCommand(
|
|
80
|
+
root: string,
|
|
81
|
+
packages: readonly string[],
|
|
82
|
+
options: InstallOptions = {},
|
|
83
|
+
): string {
|
|
84
|
+
return installArguments(packageManager(root), packages, options).join(" ");
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Runs the install in the project, with the manager's own output on the
|
|
89
|
+
* terminal: a resolution failure is that tool's to explain.
|
|
90
|
+
*/
|
|
91
|
+
export function install(
|
|
92
|
+
root: string,
|
|
93
|
+
packages: readonly string[],
|
|
94
|
+
options: InstallOptions = {},
|
|
95
|
+
): void {
|
|
96
|
+
const [command, ...args] = installArguments(packageManager(root), packages, options);
|
|
97
|
+
const result = spawnSync(command as string, args, {
|
|
98
|
+
cwd: root,
|
|
99
|
+
stdio: "inherit",
|
|
100
|
+
// Windows installs every one of them as a `.cmd`, which only a shell runs.
|
|
101
|
+
shell: process.platform === "win32",
|
|
102
|
+
});
|
|
103
|
+
if (result.error !== undefined) {
|
|
104
|
+
throw new Error(
|
|
105
|
+
`could not run ${command as string}: ${result.error.message}\n` +
|
|
106
|
+
`Run it yourself: ${[command, ...args].join(" ")}`,
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
if (result.status !== 0) {
|
|
110
|
+
throw new Error(
|
|
111
|
+
`${[command, ...args].join(" ")} exited with ${String(result.status)}.\n` +
|
|
112
|
+
"The files were written; the packages were not installed.",
|
|
113
|
+
);
|
|
114
|
+
}
|
|
115
|
+
}
|
package/src/registry.ts
ADDED
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Where an item comes from, and what else it needs.
|
|
3
|
+
*
|
|
4
|
+
* Every registry is a pattern with `{name}` in it, a URL or a path, serving one
|
|
5
|
+
* JSON file per item plus `index.json`. The project's own registry is
|
|
6
|
+
* `registry` in `components.json`; without one it is this CLI's own release of
|
|
7
|
+
* `@barqjs/ui`, read from the repository at the tag the CLI was published
|
|
8
|
+
* under, so a CLI and the components it writes always come from one commit.
|
|
9
|
+
*
|
|
10
|
+
* `registries` in `components.json` names more of them, and an item from one
|
|
11
|
+
* is addressed as `@scope/name`.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
15
|
+
import { resolve } from "node:path";
|
|
16
|
+
|
|
17
|
+
import {
|
|
18
|
+
parseIndex,
|
|
19
|
+
parseItem,
|
|
20
|
+
type Config,
|
|
21
|
+
type RegistryEndpoint,
|
|
22
|
+
type RegistryIndex,
|
|
23
|
+
type RegistryItem,
|
|
24
|
+
} from "./schema.ts";
|
|
25
|
+
|
|
26
|
+
export interface Source {
|
|
27
|
+
index(): Promise<RegistryIndex>;
|
|
28
|
+
item(name: string): Promise<RegistryItem>;
|
|
29
|
+
/** A file the registry serves beside its items, such as `themes`, unparsed. */
|
|
30
|
+
data(name: string): Promise<unknown>;
|
|
31
|
+
readonly describe: string;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** Replaces the default registry, for a fork, a mirror or a local build. */
|
|
35
|
+
export const REGISTRY_ENV = "BARQ_UI_REGISTRY";
|
|
36
|
+
|
|
37
|
+
function ownVersion(): string {
|
|
38
|
+
// `../package.json` from both `src/registry.ts` and the bundled `dist/index.js`.
|
|
39
|
+
const manifest = new URL("../package.json", import.meta.url);
|
|
40
|
+
return (JSON.parse(readFileSync(manifest, "utf8")) as { version: string }).version;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function defaultRegistry(): string {
|
|
44
|
+
const override = process.env[REGISTRY_ENV];
|
|
45
|
+
if (override !== undefined && override !== "") return override;
|
|
46
|
+
return (
|
|
47
|
+
"https://raw.githubusercontent.com/salamaashoush/barq/refs/tags/" +
|
|
48
|
+
`@barqjs/ui-cli@${ownVersion()}/packages/ui/registry/{name}.json`
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** `${NAME}` from the environment, so a token never has to be written down. */
|
|
53
|
+
function expand(value: string, where: string): string {
|
|
54
|
+
return value.replaceAll(/\$\{(\w+)\}/g, (_whole, name: string) => {
|
|
55
|
+
const found = process.env[name];
|
|
56
|
+
if (found === undefined) throw new Error(`${where} reads \${${name}}, which is not set`);
|
|
57
|
+
return found;
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* A registry behind a pattern with `{name}` in it: a URL, or a path.
|
|
63
|
+
*
|
|
64
|
+
* A path is what `build` leaves behind, so a registry can be used from the
|
|
65
|
+
* directory it was built into before anything serves it.
|
|
66
|
+
*/
|
|
67
|
+
function fromPattern(endpoint: RegistryEndpoint, cwd: string, where: string): Source {
|
|
68
|
+
const remote = /^https?:\/\//.test(endpoint.url);
|
|
69
|
+
const at = (name: string): string => {
|
|
70
|
+
const filled = expand(endpoint.url, where).replaceAll("{name}", name);
|
|
71
|
+
return remote ? filled : resolve(cwd, filled);
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
const get = async (name: string): Promise<unknown> => {
|
|
75
|
+
const location = at(name);
|
|
76
|
+
if (!remote) {
|
|
77
|
+
if (!existsSync(location)) throw new Error(`no item named "${name}" at ${location}`);
|
|
78
|
+
return JSON.parse(readFileSync(location, "utf8"));
|
|
79
|
+
}
|
|
80
|
+
const response = await fetch(location, {
|
|
81
|
+
headers: Object.fromEntries(
|
|
82
|
+
Object.entries(endpoint.headers).map(([key, value]) => [key, expand(value, where)]),
|
|
83
|
+
),
|
|
84
|
+
});
|
|
85
|
+
if (!response.ok) {
|
|
86
|
+
throw new Error(`${location} answered ${String(response.status)} ${response.statusText}`);
|
|
87
|
+
}
|
|
88
|
+
return await response.json();
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
return {
|
|
92
|
+
describe: endpoint.url,
|
|
93
|
+
index: async () => parseIndex(await get("index")),
|
|
94
|
+
item: async (name) => parseItem(await get(name), name),
|
|
95
|
+
data: get,
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export function sourceFor(config: Config, cwd: string): Source {
|
|
100
|
+
return config.registry === undefined
|
|
101
|
+
? fromPattern({ url: defaultRegistry(), headers: {} }, cwd, REGISTRY_ENV)
|
|
102
|
+
: fromPattern({ url: config.registry, headers: {} }, cwd, "components.json.registry");
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export interface Located {
|
|
106
|
+
readonly source: Source;
|
|
107
|
+
/** What the item is called where it lives, without the scope. */
|
|
108
|
+
readonly name: string;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export interface Registries {
|
|
112
|
+
/** The scopes `components.json` names, for a command that reads every registry. */
|
|
113
|
+
readonly scopes: readonly string[];
|
|
114
|
+
/** The project's own registry, or the one a scope names. */
|
|
115
|
+
source(scope?: string): Source;
|
|
116
|
+
/** `button` is the project's own registry; `@acme/button` is the one named `@acme`. */
|
|
117
|
+
locate(address: string): Located;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* The project's registry and the ones it names, each opened when first asked for,
|
|
122
|
+
* so a project that only ever adds `@acme/…` never reads the default one.
|
|
123
|
+
*/
|
|
124
|
+
export function registriesFor(config: Config, cwd: string): Registries {
|
|
125
|
+
const opened = new Map<string, Source>();
|
|
126
|
+
|
|
127
|
+
const source = (scope?: string): Source => {
|
|
128
|
+
const key = scope ?? "";
|
|
129
|
+
const found = opened.get(key);
|
|
130
|
+
if (found !== undefined) return found;
|
|
131
|
+
|
|
132
|
+
let made: Source;
|
|
133
|
+
if (scope === undefined) made = sourceFor(config, cwd);
|
|
134
|
+
else {
|
|
135
|
+
const endpoint = config.registries[scope];
|
|
136
|
+
if (endpoint === undefined) {
|
|
137
|
+
const known = Object.keys(config.registries).toSorted();
|
|
138
|
+
throw new Error(
|
|
139
|
+
`no registry named "${scope}" in components.json.` +
|
|
140
|
+
(known.length === 0 ? " Add it under `registries`." : ` It has: ${known.join(", ")}`),
|
|
141
|
+
);
|
|
142
|
+
}
|
|
143
|
+
made = fromPattern(endpoint, cwd, `components.json.registries.${scope}`);
|
|
144
|
+
}
|
|
145
|
+
opened.set(key, made);
|
|
146
|
+
return made;
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
return {
|
|
150
|
+
scopes: Object.keys(config.registries).toSorted(),
|
|
151
|
+
source,
|
|
152
|
+
locate(address) {
|
|
153
|
+
if (!address.startsWith("@")) return { source: source(), name: address };
|
|
154
|
+
|
|
155
|
+
const split = address.indexOf("/");
|
|
156
|
+
const scope = split < 0 ? address : address.slice(0, split);
|
|
157
|
+
const found = source(scope);
|
|
158
|
+
if (split < 0 || split === address.length - 1) {
|
|
159
|
+
throw new Error(`"${address}" names a registry and no item in it`);
|
|
160
|
+
}
|
|
161
|
+
return { source: found, name: address.slice(split + 1) };
|
|
162
|
+
},
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Every item these addresses need, dependencies first.
|
|
168
|
+
*
|
|
169
|
+
* Depth-first with a seen set, so a diamond — `dialog` and `sheet` both wanting
|
|
170
|
+
* `button` — resolves once, and a cycle terminates rather than recursing until
|
|
171
|
+
* the stack goes.
|
|
172
|
+
*
|
|
173
|
+
* An item from a named registry is recorded under its whole address, so
|
|
174
|
+
* `@acme/button` and the project's own `button` are two items rather than one
|
|
175
|
+
* overwriting the other. A bare dependency always means the project's own
|
|
176
|
+
* registry, whichever registry the item asking for it came from.
|
|
177
|
+
*/
|
|
178
|
+
export async function resolveItems(
|
|
179
|
+
registries: Registries,
|
|
180
|
+
addresses: readonly string[],
|
|
181
|
+
): Promise<RegistryItem[]> {
|
|
182
|
+
const seen = new Set<string>();
|
|
183
|
+
const out: RegistryItem[] = [];
|
|
184
|
+
|
|
185
|
+
const visit = async (address: string): Promise<void> => {
|
|
186
|
+
if (seen.has(address)) return;
|
|
187
|
+
seen.add(address);
|
|
188
|
+
const { source, name } = registries.locate(address);
|
|
189
|
+
const item = await source.item(name);
|
|
190
|
+
for (const dependency of item.registryDependencies) await visit(dependency);
|
|
191
|
+
out.push(item.name === address ? item : { ...item, name: address });
|
|
192
|
+
};
|
|
193
|
+
|
|
194
|
+
for (const address of addresses) await visit(address);
|
|
195
|
+
return out;
|
|
196
|
+
}
|
package/src/schema.ts
ADDED
|
@@ -0,0 +1,392 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What a registry serves and what a project records, as types and as checks.
|
|
3
|
+
*
|
|
4
|
+
* Hand-written rather than a schema library, for the reason the whole package
|
|
5
|
+
* has no dependencies: a validator here runs once per command over a file
|
|
6
|
+
* measured in kilobytes, and the error messages a hand-written one produces
|
|
7
|
+
* name the field a person has to fix.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* What an item, or one file of it, IS, which decides where it is written.
|
|
12
|
+
*
|
|
13
|
+
* `ui`, `lib`, `theme` and `hook` each have a directory of their own in
|
|
14
|
+
* `components.json`. `block`, `component` and `example` keep their path under
|
|
15
|
+
* the components directory, so a block made of several files lands as the
|
|
16
|
+
* tree it was written as. `page` and `file` belong to the application rather
|
|
17
|
+
* than to its components, so each names its own `target`.
|
|
18
|
+
*/
|
|
19
|
+
export type ItemType =
|
|
20
|
+
| "registry:ui"
|
|
21
|
+
| "registry:lib"
|
|
22
|
+
| "registry:theme"
|
|
23
|
+
| "registry:block"
|
|
24
|
+
| "registry:component"
|
|
25
|
+
| "registry:hook"
|
|
26
|
+
| "registry:page"
|
|
27
|
+
| "registry:file"
|
|
28
|
+
| "registry:example";
|
|
29
|
+
|
|
30
|
+
export interface RegistryFile {
|
|
31
|
+
/** Where it goes, relative to the directory its type resolves to. */
|
|
32
|
+
readonly path: string;
|
|
33
|
+
readonly type: ItemType;
|
|
34
|
+
readonly content: string;
|
|
35
|
+
/** Where it goes instead, relative to the project root. */
|
|
36
|
+
readonly target?: string;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** Custom properties an item adds to the project's theme, by mode. */
|
|
40
|
+
export interface CssVars {
|
|
41
|
+
/** Declared once on `:root`, whatever the mode. */
|
|
42
|
+
readonly theme?: Readonly<Record<string, string>>;
|
|
43
|
+
readonly light?: Readonly<Record<string, string>>;
|
|
44
|
+
readonly dark?: Readonly<Record<string, string>>;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Rules an item adds to the project's theme: a selector or an at-rule, and
|
|
49
|
+
* under it either declarations or more of the same.
|
|
50
|
+
*/
|
|
51
|
+
export interface CssRules {
|
|
52
|
+
readonly [selector: string]: string | CssRules;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export interface RegistryItem {
|
|
56
|
+
readonly name: string;
|
|
57
|
+
readonly type: ItemType;
|
|
58
|
+
readonly title: string;
|
|
59
|
+
readonly description?: string;
|
|
60
|
+
/** npm packages to install. */
|
|
61
|
+
readonly dependencies: readonly string[];
|
|
62
|
+
/** npm packages the project needs only to build or test. */
|
|
63
|
+
readonly devDependencies: readonly string[];
|
|
64
|
+
/**
|
|
65
|
+
* Other items to add first: a bare name from the project's own registry, or
|
|
66
|
+
* `@scope/name` from one of its `registries`.
|
|
67
|
+
*/
|
|
68
|
+
readonly registryDependencies: readonly string[];
|
|
69
|
+
readonly files: readonly RegistryFile[];
|
|
70
|
+
readonly categories: readonly string[];
|
|
71
|
+
/** Whatever the registry wants to say about the item. The CLI carries it and reads none of it. */
|
|
72
|
+
readonly meta?: Readonly<Record<string, unknown>>;
|
|
73
|
+
readonly cssVars?: CssVars;
|
|
74
|
+
readonly css?: CssRules;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export interface RegistryIndexEntry {
|
|
78
|
+
readonly name: string;
|
|
79
|
+
readonly type: ItemType;
|
|
80
|
+
readonly title: string;
|
|
81
|
+
readonly description?: string;
|
|
82
|
+
readonly dependencies: readonly string[];
|
|
83
|
+
readonly registryDependencies: readonly string[];
|
|
84
|
+
readonly files: readonly string[];
|
|
85
|
+
readonly categories: readonly string[];
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export interface RegistryIndex {
|
|
89
|
+
readonly name: string;
|
|
90
|
+
readonly homepage?: string;
|
|
91
|
+
readonly items: readonly RegistryIndexEntry[];
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export interface ThemeChoice {
|
|
95
|
+
/**
|
|
96
|
+
* One of shadcn's eight component styles: `vega`, `nova`, `maia`, `lyra`,
|
|
97
|
+
* `mira`, `luma`, `sera`, `rhea`. A style is a second opinion about how every
|
|
98
|
+
* component looks, so a project picks ONE and pays for one.
|
|
99
|
+
*/
|
|
100
|
+
readonly style?: string;
|
|
101
|
+
/** A name from the registry's base themes. */
|
|
102
|
+
readonly base: string;
|
|
103
|
+
/** A name from its accent themes, layered over the base. */
|
|
104
|
+
readonly accent?: string;
|
|
105
|
+
/** `--radius`. */
|
|
106
|
+
readonly radius?: string;
|
|
107
|
+
/** How dark mode is asked for: a selector, or `"media"`. */
|
|
108
|
+
readonly dark?: string;
|
|
109
|
+
/**
|
|
110
|
+
* What added items declared over the chosen theme.
|
|
111
|
+
*
|
|
112
|
+
* Recorded rather than only written, because `theme.ts` is regenerated when
|
|
113
|
+
* the theme changes and would otherwise lose every token an item brought.
|
|
114
|
+
*/
|
|
115
|
+
readonly cssVars?: CssVars;
|
|
116
|
+
readonly css?: CssRules;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export interface Paths {
|
|
120
|
+
readonly ui: string;
|
|
121
|
+
readonly lib: string;
|
|
122
|
+
readonly theme: string;
|
|
123
|
+
/** Blocks, components and examples, each under the path its registry gave it. */
|
|
124
|
+
readonly components: string;
|
|
125
|
+
readonly hooks: string;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* A registry other than the project's own, reached as `@scope/name`.
|
|
130
|
+
*
|
|
131
|
+
* `${NAME}` in the URL or in a header is read from the environment, so a token
|
|
132
|
+
* is never written into a file that is committed.
|
|
133
|
+
*/
|
|
134
|
+
export interface RegistryEndpoint {
|
|
135
|
+
/** A URL, or a path from the project root, with `{name}` in it. */
|
|
136
|
+
readonly url: string;
|
|
137
|
+
readonly headers: Readonly<Record<string, string>>;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
export interface Config {
|
|
141
|
+
readonly $schema?: string;
|
|
142
|
+
/**
|
|
143
|
+
* Where items without a scope come from: a URL or a path with `{name}` in it.
|
|
144
|
+
* Left out, it is the `@barqjs/ui` release this CLI was published with.
|
|
145
|
+
*/
|
|
146
|
+
readonly registry?: string;
|
|
147
|
+
/** More registries, by the `@scope` their items are addressed with. */
|
|
148
|
+
readonly registries: Readonly<Record<string, RegistryEndpoint>>;
|
|
149
|
+
readonly paths: Paths;
|
|
150
|
+
readonly theme: ThemeChoice;
|
|
151
|
+
/** Whether `init` wrote the reset. Recorded so `sync` knows what to keep. */
|
|
152
|
+
readonly reset: boolean;
|
|
153
|
+
/**
|
|
154
|
+
* What has been written, and the hash it was written with.
|
|
155
|
+
*
|
|
156
|
+
* This is what makes `sync` safe: a file whose hash still matches has not
|
|
157
|
+
* been touched and can be replaced without asking, and one that has been
|
|
158
|
+
* edited is shown as a diff instead. shadcn has no equivalent and cannot
|
|
159
|
+
* tell the two apart.
|
|
160
|
+
*/
|
|
161
|
+
readonly items: Record<string, Record<string, string>>;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
const TYPES = new Set<string>([
|
|
165
|
+
"registry:ui",
|
|
166
|
+
"registry:lib",
|
|
167
|
+
"registry:theme",
|
|
168
|
+
"registry:block",
|
|
169
|
+
"registry:component",
|
|
170
|
+
"registry:hook",
|
|
171
|
+
"registry:page",
|
|
172
|
+
"registry:file",
|
|
173
|
+
"registry:example",
|
|
174
|
+
]);
|
|
175
|
+
|
|
176
|
+
/** The two that are not components, so no directory in `paths` is theirs. */
|
|
177
|
+
const NEEDS_TARGET = new Set<string>(["registry:page", "registry:file"]);
|
|
178
|
+
|
|
179
|
+
function fail(message: string): never {
|
|
180
|
+
throw new Error(message);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
function record(value: unknown, where: string): Record<string, unknown> {
|
|
184
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) {
|
|
185
|
+
fail(`${where} must be an object`);
|
|
186
|
+
}
|
|
187
|
+
return value as Record<string, unknown>;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
function text(value: unknown, where: string): string {
|
|
191
|
+
if (typeof value !== "string") fail(`${where} must be a string`);
|
|
192
|
+
return value;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
function list(value: unknown, where: string): unknown[] {
|
|
196
|
+
if (!Array.isArray(value)) fail(`${where} must be an array`);
|
|
197
|
+
return value;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
function itemType(value: unknown, where: string): ItemType {
|
|
201
|
+
const name = text(value, where);
|
|
202
|
+
if (!TYPES.has(name)) fail(`${where} must be one of ${[...TYPES].join(", ")}, not "${name}"`);
|
|
203
|
+
return name as ItemType;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
function texts(value: unknown, where: string): string[] {
|
|
207
|
+
return list(value ?? [], where).map((one, index) => text(one, `${where}[${String(index)}]`));
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
function strings(value: unknown, where: string): Record<string, string> {
|
|
211
|
+
return Object.fromEntries(
|
|
212
|
+
Object.entries(record(value, where)).map(([key, one]) => [key, text(one, `${where}.${key}`)]),
|
|
213
|
+
);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
function cssVars(value: unknown, where: string): CssVars {
|
|
217
|
+
const entry = record(value, where);
|
|
218
|
+
const mode = (name: "theme" | "light" | "dark"): Partial<CssVars> =>
|
|
219
|
+
entry[name] === undefined ? {} : { [name]: strings(entry[name], `${where}.${name}`) };
|
|
220
|
+
return { ...mode("theme"), ...mode("light"), ...mode("dark") };
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
function cssRules(value: unknown, where: string): CssRules {
|
|
224
|
+
return Object.fromEntries(
|
|
225
|
+
Object.entries(record(value, where)).map(([key, one]) => [
|
|
226
|
+
key,
|
|
227
|
+
typeof one === "string" ? one : cssRules(one, `${where}.${key}`),
|
|
228
|
+
]),
|
|
229
|
+
);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
export function parseItem(value: unknown, where = "the item"): RegistryItem {
|
|
233
|
+
const entry = record(value, where);
|
|
234
|
+
const name = text(entry["name"], `${where}.name`);
|
|
235
|
+
return {
|
|
236
|
+
name,
|
|
237
|
+
type: itemType(entry["type"], `${where}.type`),
|
|
238
|
+
title: typeof entry["title"] === "string" ? entry["title"] : name,
|
|
239
|
+
...(typeof entry["description"] === "string" ? { description: entry["description"] } : {}),
|
|
240
|
+
dependencies: texts(entry["dependencies"], `${where}.dependencies`),
|
|
241
|
+
devDependencies: texts(entry["devDependencies"], `${where}.devDependencies`),
|
|
242
|
+
registryDependencies: texts(entry["registryDependencies"], `${where}.registryDependencies`),
|
|
243
|
+
files: list(entry["files"] ?? [], `${where}.files`).map((one, index) => {
|
|
244
|
+
const at = `${where}.files[${String(index)}]`;
|
|
245
|
+
const file = record(one, at);
|
|
246
|
+
const type = itemType(file["type"], `${at}.type`);
|
|
247
|
+
if (NEEDS_TARGET.has(type) && file["target"] === undefined) {
|
|
248
|
+
fail(`${at}.target is required for ${type}, which has no directory of its own`);
|
|
249
|
+
}
|
|
250
|
+
return {
|
|
251
|
+
path: text(file["path"], `${at}.path`),
|
|
252
|
+
type,
|
|
253
|
+
content: text(file["content"], `${at}.content`),
|
|
254
|
+
...(file["target"] === undefined ? {} : { target: text(file["target"], `${at}.target`) }),
|
|
255
|
+
};
|
|
256
|
+
}),
|
|
257
|
+
categories: texts(entry["categories"], `${where}.categories`),
|
|
258
|
+
...(entry["meta"] === undefined ? {} : { meta: record(entry["meta"], `${where}.meta`) }),
|
|
259
|
+
...(entry["cssVars"] === undefined
|
|
260
|
+
? {}
|
|
261
|
+
: { cssVars: cssVars(entry["cssVars"], `${where}.cssVars`) }),
|
|
262
|
+
...(entry["css"] === undefined ? {} : { css: cssRules(entry["css"], `${where}.css`) }),
|
|
263
|
+
};
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
export function parseIndex(value: unknown): RegistryIndex {
|
|
267
|
+
const entry = record(value, "the registry");
|
|
268
|
+
return {
|
|
269
|
+
name: text(entry["name"] ?? "registry", "the registry's name"),
|
|
270
|
+
...(typeof entry["homepage"] === "string" ? { homepage: entry["homepage"] } : {}),
|
|
271
|
+
items: list(entry["items"] ?? [], "the registry's items").map((one, index) => {
|
|
272
|
+
const item = record(one, `items[${String(index)}]`);
|
|
273
|
+
const name = text(item["name"], `items[${String(index)}].name`);
|
|
274
|
+
return {
|
|
275
|
+
name,
|
|
276
|
+
type: itemType(item["type"], `items[${String(index)}].type`),
|
|
277
|
+
title: typeof item["title"] === "string" ? item["title"] : name,
|
|
278
|
+
...(typeof item["description"] === "string" ? { description: item["description"] } : {}),
|
|
279
|
+
dependencies: list(item["dependencies"] ?? [], "dependencies").map((one_, at) =>
|
|
280
|
+
text(one_, `items[${String(index)}].dependencies[${String(at)}]`),
|
|
281
|
+
),
|
|
282
|
+
registryDependencies: list(item["registryDependencies"] ?? [], "registryDependencies").map(
|
|
283
|
+
(one_, at) => text(one_, `items[${String(index)}].registryDependencies[${String(at)}]`),
|
|
284
|
+
),
|
|
285
|
+
files: list(item["files"] ?? [], "files").map((one_, at) =>
|
|
286
|
+
text(one_, `items[${String(index)}].files[${String(at)}]`),
|
|
287
|
+
),
|
|
288
|
+
categories: texts(item["categories"], `items[${String(index)}].categories`),
|
|
289
|
+
};
|
|
290
|
+
}),
|
|
291
|
+
};
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
export const DEFAULT_PATHS: Paths = {
|
|
295
|
+
ui: "src/components/ui",
|
|
296
|
+
lib: "src/components/lib",
|
|
297
|
+
theme: "src/components/theme",
|
|
298
|
+
components: "src/components",
|
|
299
|
+
hooks: "src/hooks",
|
|
300
|
+
};
|
|
301
|
+
|
|
302
|
+
/**
|
|
303
|
+
* Where components go when only `ui` was chosen: the directory `ui` is in.
|
|
304
|
+
*
|
|
305
|
+
* `init` asks one question, and a project whose `ui` is `app/widgets/ui` wants
|
|
306
|
+
* its blocks in `app/widgets/blocks`, not in a `src` it does not have.
|
|
307
|
+
*/
|
|
308
|
+
function componentsBeside(ui: string): string {
|
|
309
|
+
const at = ui.lastIndexOf("/");
|
|
310
|
+
return ui.endsWith("/ui") && at > 0 ? ui.slice(0, at) : ui;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
function hooksBeside(components: string): string {
|
|
314
|
+
const at = components.lastIndexOf("/");
|
|
315
|
+
return at > 0 ? `${components.slice(0, at)}/hooks` : "hooks";
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
function endpoint(value: unknown, where: string): RegistryEndpoint {
|
|
319
|
+
const entry = typeof value === "string" ? { url: value } : record(value, where);
|
|
320
|
+
const url = text(entry["url"], `${where}.url`);
|
|
321
|
+
if (!url.includes("{name}")) fail(`${where} must contain {name}, which is where the item goes`);
|
|
322
|
+
return { url, headers: strings(entry["headers"] ?? {}, `${where}.headers`) };
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
function registries(value: unknown): Record<string, RegistryEndpoint> {
|
|
326
|
+
return Object.fromEntries(
|
|
327
|
+
Object.entries(record(value, "components.json.registries")).map(([scope, one]) => {
|
|
328
|
+
if (!/^@[\w-]+$/.test(scope)) {
|
|
329
|
+
fail(`components.json.registries: "${scope}" must be a scope, like @acme`);
|
|
330
|
+
}
|
|
331
|
+
return [scope, endpoint(one, `components.json.registries.${scope}`)];
|
|
332
|
+
}),
|
|
333
|
+
);
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
function pattern(value: unknown): string {
|
|
337
|
+
const url = text(value, "components.json.registry");
|
|
338
|
+
if (!url.includes("{name}")) {
|
|
339
|
+
fail(`components.json.registry must contain {name}, as in https://example.com/r/{name}.json`);
|
|
340
|
+
}
|
|
341
|
+
return url;
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
export function parseConfig(value: unknown): Config {
|
|
345
|
+
const entry = record(value, "components.json");
|
|
346
|
+
const paths = record(entry["paths"] ?? {}, "components.json.paths");
|
|
347
|
+
const theme = record(entry["theme"] ?? {}, "components.json.theme");
|
|
348
|
+
const items = record(entry["items"] ?? {}, "components.json.items");
|
|
349
|
+
const ui = text(paths["ui"] ?? DEFAULT_PATHS.ui, "components.json.paths.ui");
|
|
350
|
+
const components = text(
|
|
351
|
+
paths["components"] ?? componentsBeside(ui),
|
|
352
|
+
"components.json.paths.components",
|
|
353
|
+
);
|
|
354
|
+
|
|
355
|
+
return {
|
|
356
|
+
...(typeof entry["$schema"] === "string" ? { $schema: entry["$schema"] } : {}),
|
|
357
|
+
...(entry["registry"] === undefined ? {} : { registry: pattern(entry["registry"]) }),
|
|
358
|
+
registries: registries(entry["registries"] ?? {}),
|
|
359
|
+
paths: {
|
|
360
|
+
ui,
|
|
361
|
+
lib: text(paths["lib"] ?? DEFAULT_PATHS.lib, "components.json.paths.lib"),
|
|
362
|
+
theme: text(paths["theme"] ?? DEFAULT_PATHS.theme, "components.json.paths.theme"),
|
|
363
|
+
components,
|
|
364
|
+
hooks: text(paths["hooks"] ?? hooksBeside(components), "components.json.paths.hooks"),
|
|
365
|
+
},
|
|
366
|
+
theme: {
|
|
367
|
+
...(typeof theme["style"] === "string" ? { style: theme["style"] } : {}),
|
|
368
|
+
base: text(theme["base"] ?? "neutral", "components.json.theme.base"),
|
|
369
|
+
...(typeof theme["accent"] === "string" ? { accent: theme["accent"] } : {}),
|
|
370
|
+
...(typeof theme["radius"] === "string" ? { radius: theme["radius"] } : {}),
|
|
371
|
+
...(typeof theme["dark"] === "string" ? { dark: theme["dark"] } : {}),
|
|
372
|
+
...(theme["cssVars"] === undefined
|
|
373
|
+
? {}
|
|
374
|
+
: { cssVars: cssVars(theme["cssVars"], "components.json.theme.cssVars") }),
|
|
375
|
+
...(theme["css"] === undefined
|
|
376
|
+
? {}
|
|
377
|
+
: { css: cssRules(theme["css"], "components.json.theme.css") }),
|
|
378
|
+
},
|
|
379
|
+
reset: entry["reset"] !== false,
|
|
380
|
+
items: Object.fromEntries(
|
|
381
|
+
Object.entries(items).map(([name, files]) => [
|
|
382
|
+
name,
|
|
383
|
+
Object.fromEntries(
|
|
384
|
+
Object.entries(record(files, `components.json.items.${name}`)).map(([path, hash]) => [
|
|
385
|
+
path,
|
|
386
|
+
text(hash, `components.json.items.${name}.${path}`),
|
|
387
|
+
]),
|
|
388
|
+
),
|
|
389
|
+
]),
|
|
390
|
+
),
|
|
391
|
+
};
|
|
392
|
+
}
|