astralis-cli 0.2.1 → 0.3.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/package.json +2 -2
- package/src/commands/add.mjs +177 -0
- package/src/index.mjs +22 -13
- package/src/lib/blocks-dir.mjs +49 -0
- package/src/lib/registry.mjs +56 -0
- package/src/lib/ui.mjs +15 -2
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "astralis-cli",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "Setup and tooling CLI for Astralis UI — wire up a project, generate a static brand theme, connect the MCP server.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -36,7 +36,7 @@
|
|
|
36
36
|
"node": ">=18"
|
|
37
37
|
},
|
|
38
38
|
"dependencies": {
|
|
39
|
-
"astralis-ui": "^0.
|
|
39
|
+
"astralis-ui": "^0.4.0"
|
|
40
40
|
},
|
|
41
41
|
"scripts": {
|
|
42
42
|
"test": "node --test"
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import { parseArgs } from "node:util";
|
|
2
|
+
import { existsSync, mkdirSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { dirname, join, relative } from "node:path";
|
|
4
|
+
import { ok, warn, info, fail, cyan, dim, bold, confirm } from "../lib/ui.mjs";
|
|
5
|
+
import { readPackageJson, detectPackageManager } from "../lib/detect.mjs";
|
|
6
|
+
import { resolveBlocksDir } from "../lib/blocks-dir.mjs";
|
|
7
|
+
import { registryBase, fetchIndex, fetchBlock } from "../lib/registry.mjs";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Copies a block's source into the project.
|
|
11
|
+
*
|
|
12
|
+
* This is deliberately small compared with the equivalent in other libraries,
|
|
13
|
+
* and the reason is architectural: a block imports only astralis-ui, react, or
|
|
14
|
+
* a sibling file — the registry builder rejects anything else — and the
|
|
15
|
+
* stylesheet is precompiled. So there is no import rewriting to consumer
|
|
16
|
+
* aliases, no dependency graph between blocks, and no Tailwind config to
|
|
17
|
+
* merge. Fetch text, write files, check one dependency.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
const USAGE = `Usage: astralis add <block>... [options]
|
|
21
|
+
|
|
22
|
+
Copies a block's source into your project. The files are yours after that —
|
|
23
|
+
no dependency to configure around.
|
|
24
|
+
|
|
25
|
+
Options
|
|
26
|
+
--list print every available block and exit
|
|
27
|
+
--dir <path> where to write (default: [src/]components/blocks)
|
|
28
|
+
--overwrite replace existing files without asking
|
|
29
|
+
--dry-run show what would be written, write nothing
|
|
30
|
+
--registry <url> read from a different registry host
|
|
31
|
+
-h, --help this message
|
|
32
|
+
|
|
33
|
+
Examples
|
|
34
|
+
astralis add hero-01
|
|
35
|
+
astralis add login-01 dashboard-02 --dir src/ui/blocks
|
|
36
|
+
astralis add --list
|
|
37
|
+
`;
|
|
38
|
+
|
|
39
|
+
/** Group ids by category for a readable --list. */
|
|
40
|
+
function printCatalogue(blocks) {
|
|
41
|
+
const byCategory = new Map();
|
|
42
|
+
for (const block of blocks) {
|
|
43
|
+
if (!byCategory.has(block.category)) byCategory.set(block.category, []);
|
|
44
|
+
byCategory.get(block.category).push(block);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
console.log(`\n${bold("Available blocks")} ${dim(`(${blocks.length})`)}\n`);
|
|
48
|
+
for (const [category, items] of [...byCategory].sort()) {
|
|
49
|
+
console.log(` ${bold(category)}`);
|
|
50
|
+
for (const item of items.sort((a, b) => a.id.localeCompare(b.id))) {
|
|
51
|
+
console.log(` ${cyan(item.id.padEnd(22))} ${dim(item.name)}`);
|
|
52
|
+
}
|
|
53
|
+
console.log("");
|
|
54
|
+
}
|
|
55
|
+
console.log(` ${dim(`Add one with ${cyan("astralis add <id>")}`)}\n`);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export async function run(argv) {
|
|
59
|
+
let parsed;
|
|
60
|
+
try {
|
|
61
|
+
parsed = parseArgs({
|
|
62
|
+
args: argv,
|
|
63
|
+
allowPositionals: true,
|
|
64
|
+
options: {
|
|
65
|
+
list: { type: "boolean", default: false },
|
|
66
|
+
dir: { type: "string" },
|
|
67
|
+
overwrite: { type: "boolean", default: false },
|
|
68
|
+
"dry-run": { type: "boolean", default: false },
|
|
69
|
+
registry: { type: "string" },
|
|
70
|
+
help: { type: "boolean", short: "h", default: false },
|
|
71
|
+
},
|
|
72
|
+
});
|
|
73
|
+
} catch (error) {
|
|
74
|
+
fail(`${error.message}\n\n${USAGE}`);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const { values, positionals } = parsed;
|
|
78
|
+
if (values.help) return console.log(USAGE);
|
|
79
|
+
|
|
80
|
+
const cwd = process.cwd();
|
|
81
|
+
const base = registryBase(values.registry);
|
|
82
|
+
|
|
83
|
+
if (values.list) return printCatalogue((await fetchIndex(base)).blocks);
|
|
84
|
+
|
|
85
|
+
if (positionals.length === 0) {
|
|
86
|
+
fail(`Name at least one block — try ${cyan("astralis add --list")}.\n\n${USAGE}`);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/* --- resolve every id before writing anything -------------------------- */
|
|
90
|
+
const items = [];
|
|
91
|
+
for (const id of positionals) {
|
|
92
|
+
const block = await fetchBlock(base, id);
|
|
93
|
+
if (!block) {
|
|
94
|
+
fail(
|
|
95
|
+
`No block called ${cyan(id)}.\n` +
|
|
96
|
+
` Run ${cyan("astralis add --list")} to see what exists.`,
|
|
97
|
+
);
|
|
98
|
+
}
|
|
99
|
+
items.push(block);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const { relative: dirRelative, source } = resolveBlocksDir(cwd, values.dir);
|
|
103
|
+
const target = join(cwd, dirRelative);
|
|
104
|
+
const dryRun = values["dry-run"];
|
|
105
|
+
|
|
106
|
+
// Paths are shown forward-slashed on every platform: they end up in an
|
|
107
|
+
// import statement, where a Windows separator would be wrong anyway.
|
|
108
|
+
const posix = (path) => path.split("\\").join("/");
|
|
109
|
+
|
|
110
|
+
console.log("");
|
|
111
|
+
info(
|
|
112
|
+
`Writing to ${cyan(posix(dirRelative))}` +
|
|
113
|
+
(source === "default" ? "" : dim(` (from ${source})`)),
|
|
114
|
+
);
|
|
115
|
+
|
|
116
|
+
/* --- write ------------------------------------------------------------- */
|
|
117
|
+
let written = 0;
|
|
118
|
+
let skipped = 0;
|
|
119
|
+
|
|
120
|
+
for (const block of items) {
|
|
121
|
+
for (const file of block.contents) {
|
|
122
|
+
const path = join(target, file.path);
|
|
123
|
+
const shown = posix(relative(cwd, path));
|
|
124
|
+
|
|
125
|
+
if (existsSync(path) && !values.overwrite) {
|
|
126
|
+
if (dryRun) {
|
|
127
|
+
warn(`${shown} ${dim("exists — would ask before replacing")}`);
|
|
128
|
+
skipped += 1;
|
|
129
|
+
continue;
|
|
130
|
+
}
|
|
131
|
+
const replace = await confirm(`${shown} already exists. Replace it?`);
|
|
132
|
+
if (!replace) {
|
|
133
|
+
warn(`skipped ${shown}`);
|
|
134
|
+
skipped += 1;
|
|
135
|
+
continue;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
if (dryRun) {
|
|
140
|
+
ok(`${shown} ${dim(`(${Buffer.byteLength(file.content)} bytes)`)}`);
|
|
141
|
+
} else {
|
|
142
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
143
|
+
writeFileSync(path, file.content);
|
|
144
|
+
ok(shown);
|
|
145
|
+
}
|
|
146
|
+
written += 1;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/* --- the one dependency a block actually has --------------------------- */
|
|
151
|
+
const pkg = readPackageJson(cwd);
|
|
152
|
+
const deps = { ...pkg?.dependencies, ...pkg?.devDependencies };
|
|
153
|
+
if (pkg && !deps["astralis-ui"]) {
|
|
154
|
+
const pm = detectPackageManager(cwd);
|
|
155
|
+
const install = pm === "npm" ? "npm install" : `${pm} add`;
|
|
156
|
+
console.log("");
|
|
157
|
+
warn(
|
|
158
|
+
`astralis-ui is not in this project's dependencies — the block will not compile.\n` +
|
|
159
|
+
` Run ${cyan(`${install} astralis-ui`)}, or ${cyan("astralis init")} to wire it up fully.`,
|
|
160
|
+
);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
console.log("");
|
|
164
|
+
const verb = dryRun ? "would write" : "wrote";
|
|
165
|
+
console.log(
|
|
166
|
+
`${verb} ${bold(String(written))} file(s)` +
|
|
167
|
+
(skipped ? dim(`, skipped ${skipped}`) : "") +
|
|
168
|
+
(dryRun ? dim(" — dry run, nothing changed") : ""),
|
|
169
|
+
);
|
|
170
|
+
if (!dryRun && written) {
|
|
171
|
+
const first = items[0];
|
|
172
|
+
const alias = posix(dirRelative.replace(/^src[\\/]/, ""));
|
|
173
|
+
const file = first.contents[0].path.replace(/\.tsx?$/, "");
|
|
174
|
+
console.log(dim(` import { ${first.component} } from "@/${alias}/${file}";`));
|
|
175
|
+
}
|
|
176
|
+
console.log("");
|
|
177
|
+
}
|
package/src/index.mjs
CHANGED
|
@@ -2,11 +2,12 @@
|
|
|
2
2
|
import { readFileSync } from "node:fs";
|
|
3
3
|
import { fileURLToPath } from "node:url";
|
|
4
4
|
import { join, dirname } from "node:path";
|
|
5
|
-
import { bold, cyan, dim, fail, closePrompts } from "./lib/ui.mjs";
|
|
5
|
+
import { bold, cyan, dim, fail, closePrompts, CliExit } from "./lib/ui.mjs";
|
|
6
6
|
|
|
7
7
|
const COMMANDS = {
|
|
8
8
|
create: () => import("./commands/create.mjs"),
|
|
9
9
|
init: () => import("./commands/init.mjs"),
|
|
10
|
+
add: () => import("./commands/add.mjs"),
|
|
10
11
|
theme: () => import("./commands/theme.mjs"),
|
|
11
12
|
"connect-mcp": () => import("./commands/connect-mcp.mjs"),
|
|
12
13
|
};
|
|
@@ -23,6 +24,10 @@ ${bold("Commands")}
|
|
|
23
24
|
${dim("--framework next|vite · extra args pass to the scaffolder")}
|
|
24
25
|
${cyan("init")} wire astralis-ui into this project (Next.js or Vite)
|
|
25
26
|
${dim("--dry-run show the changes without writing")}
|
|
27
|
+
${cyan("add <block>...")} copy a block's source into this project
|
|
28
|
+
${dim("--list every available block, by category")}
|
|
29
|
+
${dim("--dir <path> where to write (default [src/]components/blocks)")}
|
|
30
|
+
${dim("--overwrite · --dry-run · --registry <url>")}
|
|
26
31
|
${cyan("theme")} generate a static theme stylesheet from a seed
|
|
27
32
|
${dim("--brand/--gray <hex> · --font-heading/-body/-mono <stack>")}
|
|
28
33
|
${dim("--radius/--spacing/--font-scale/--motion <n>")}
|
|
@@ -38,18 +43,22 @@ ${bold("Docs")} ${cyan("https://astralis-zeta.vercel.app/docs")}
|
|
|
38
43
|
|
|
39
44
|
const [command, ...rest] = process.argv.slice(2);
|
|
40
45
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
46
|
+
try {
|
|
47
|
+
if (!command || command === "--help" || command === "-h" || command === "help") {
|
|
48
|
+
console.log(HELP);
|
|
49
|
+
} else if (command === "--version" || command === "-v") {
|
|
50
|
+
const pkg = JSON.parse(readFileSync(join(dirname(fileURLToPath(import.meta.url)), "..", "package.json"), "utf8"));
|
|
51
|
+
console.log(pkg.version);
|
|
52
|
+
} else if (COMMANDS[command]) {
|
|
53
|
+
const { run } = await COMMANDS[command]();
|
|
49
54
|
await run(rest);
|
|
50
|
-
}
|
|
51
|
-
|
|
55
|
+
} else {
|
|
56
|
+
fail(`Unknown command "${command}" — run ${cyan("astralis --help")}.`);
|
|
52
57
|
}
|
|
53
|
-
}
|
|
54
|
-
fail(
|
|
58
|
+
} catch (error) {
|
|
59
|
+
// fail() has already printed and set the exit code; anything else is a bug
|
|
60
|
+
// and deserves its stack.
|
|
61
|
+
if (!(error instanceof CliExit)) throw error;
|
|
62
|
+
} finally {
|
|
63
|
+
closePrompts();
|
|
55
64
|
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Where a block's files get written.
|
|
6
|
+
*
|
|
7
|
+
* `astralis init` writes no config file — the whole point of the precompiled
|
|
8
|
+
* stylesheet is that a project needs no Astralis-shaped setup — so this is
|
|
9
|
+
* convention first, with two escape hatches for people whose layout differs:
|
|
10
|
+
*
|
|
11
|
+
* 1. --dir on the command line, for a one-off
|
|
12
|
+
* 2. "blocksDir" in astralis.json, for a project that always differs
|
|
13
|
+
* 3. otherwise src/components/blocks, or components/blocks without a src/
|
|
14
|
+
*
|
|
15
|
+
* Never the project root: a block is a component, and dropping .tsx files
|
|
16
|
+
* beside package.json would be nobody's idea of where they live.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
export const DEFAULT_SUBPATH = join("components", "blocks");
|
|
20
|
+
|
|
21
|
+
/** `astralis.json`, if the project has one. Malformed is treated as absent. */
|
|
22
|
+
export function readConfig(cwd) {
|
|
23
|
+
const path = join(cwd, "astralis.json");
|
|
24
|
+
if (!existsSync(path)) return null;
|
|
25
|
+
try {
|
|
26
|
+
return JSON.parse(readFileSync(path, "utf8"));
|
|
27
|
+
} catch {
|
|
28
|
+
return null;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Resolve the destination directory, relative to cwd.
|
|
34
|
+
* Returns { relative, source } — source names which rule won, for the log line.
|
|
35
|
+
*/
|
|
36
|
+
export function resolveBlocksDir(cwd, flagDir) {
|
|
37
|
+
if (flagDir) return { relative: flagDir, source: "--dir" };
|
|
38
|
+
|
|
39
|
+
const configured = readConfig(cwd)?.blocksDir;
|
|
40
|
+
if (typeof configured === "string" && configured.trim()) {
|
|
41
|
+
return { relative: configured.trim(), source: "astralis.json" };
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const hasSrc = existsSync(join(cwd, "src"));
|
|
45
|
+
return {
|
|
46
|
+
relative: hasSrc ? join("src", DEFAULT_SUBPATH) : DEFAULT_SUBPATH,
|
|
47
|
+
source: "default",
|
|
48
|
+
};
|
|
49
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { fail } from "./ui.mjs";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The block registry lives on the docs site as static JSON — see
|
|
5
|
+
* astralis-docs/scripts/gen-block-registry.mjs. Fetching it rather than
|
|
6
|
+
* bundling it into this package keeps the two on separate clocks: adding a
|
|
7
|
+
* block is a docs deploy, not a CLI release.
|
|
8
|
+
*
|
|
9
|
+
* --registry (or ASTRALIS_REGISTRY) points at a different host, which is how
|
|
10
|
+
* the registry gets tested against a local docs server before it ships.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
const DEFAULT_REGISTRY = "https://astralis-zeta.vercel.app/r";
|
|
14
|
+
|
|
15
|
+
export function registryBase(flag) {
|
|
16
|
+
const base = flag ?? process.env.ASTRALIS_REGISTRY ?? DEFAULT_REGISTRY;
|
|
17
|
+
return base.replace(/\/+$/, "");
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
async function getJson(url) {
|
|
21
|
+
let response;
|
|
22
|
+
try {
|
|
23
|
+
response = await fetch(url);
|
|
24
|
+
} catch (error) {
|
|
25
|
+
fail(`Could not reach the block registry at ${url}\n ${error.message}`);
|
|
26
|
+
}
|
|
27
|
+
// Cancel the body on any path that will not read it. An undici response left
|
|
28
|
+
// unconsumed keeps its socket handle open, and a process.exit() while that
|
|
29
|
+
// handle is live trips a libuv assertion on Windows — the CLI printed a
|
|
30
|
+
// correct error and then crashed underneath it.
|
|
31
|
+
if (response.status === 404) {
|
|
32
|
+
await response.body?.cancel();
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
35
|
+
if (!response.ok) {
|
|
36
|
+
await response.body?.cancel();
|
|
37
|
+
fail(`Registry responded ${response.status} for ${url}`);
|
|
38
|
+
}
|
|
39
|
+
try {
|
|
40
|
+
return await response.json();
|
|
41
|
+
} catch {
|
|
42
|
+
fail(`Registry returned invalid JSON for ${url}`);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** The catalogue: every block, without source. */
|
|
47
|
+
export async function fetchIndex(base) {
|
|
48
|
+
const data = await getJson(`${base}/index.json`);
|
|
49
|
+
if (!data) fail(`No block catalogue at ${base}/index.json`);
|
|
50
|
+
return data;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** One block, with the full text of its files. Null when the id is unknown. */
|
|
54
|
+
export function fetchBlock(base, id) {
|
|
55
|
+
return getJson(`${base}/${id}.json`);
|
|
56
|
+
}
|
package/src/lib/ui.mjs
CHANGED
|
@@ -19,10 +19,23 @@ export const ok = (message) => console.log(`${green("✓")} ${message}`);
|
|
|
19
19
|
export const warn = (message) => console.log(`${yellow("!")} ${message}`);
|
|
20
20
|
export const info = (message) => console.log(`${cyan("→")} ${message}`);
|
|
21
21
|
|
|
22
|
-
/**
|
|
22
|
+
/** Thrown by fail() once the message is printed — index.mjs stops quietly on it. */
|
|
23
|
+
export class CliExit extends Error {}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Print the error and stop — the CLI's only failure path.
|
|
27
|
+
*
|
|
28
|
+
* Sets an exit code and unwinds rather than calling process.exit(): on Windows,
|
|
29
|
+
* process.exit() after any fetch() trips a libuv assertion
|
|
30
|
+
* (`!(handle->flags & UV_HANDLE_CLOSING)`) and reports exit 127 instead of 1.
|
|
31
|
+
* Verified in isolation — fetch alone is clean, exit alone is clean, only the
|
|
32
|
+
* pair crashes. Unwinding lets the event loop drain, which also means finally
|
|
33
|
+
* blocks and closePrompts() actually run.
|
|
34
|
+
*/
|
|
23
35
|
export function fail(message) {
|
|
24
36
|
console.error(`${red("✗")} ${message}`);
|
|
25
|
-
process.
|
|
37
|
+
process.exitCode = 1;
|
|
38
|
+
throw new CliExit(message);
|
|
26
39
|
}
|
|
27
40
|
|
|
28
41
|
/*
|