astralis-cli 0.2.0 → 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/README.md +12 -6
- package/package.json +2 -2
- package/src/commands/add.mjs +177 -0
- package/src/commands/connect-mcp.mjs +146 -0
- package/src/index.mjs +29 -14
- package/src/lib/blocks-dir.mjs +49 -0
- package/src/lib/registry.mjs +56 -0
- package/src/lib/ui.mjs +61 -5
- package/src/commands/mcp.mjs +0 -71
package/README.md
CHANGED
|
@@ -45,17 +45,23 @@ npx astralis-cli theme "#8b5cf6" --out astralis-theme.css
|
|
|
45
45
|
|
|
46
46
|
Import it **after** `astralis-ui/styles.css`.
|
|
47
47
|
|
|
48
|
-
### `astralis mcp`
|
|
48
|
+
### `astralis connect-mcp`
|
|
49
49
|
|
|
50
|
-
Connects
|
|
51
|
-
docs server
|
|
52
|
-
|
|
50
|
+
Connects a single client to the [astralis-mcp](https://www.npmjs.com/package/astralis-mcp)
|
|
51
|
+
docs server. It prompts for the client (Claude Code, Codex, Cursor, Claude
|
|
52
|
+
Desktop, or Antigravity), tells you exactly what it's about to do, asks you to
|
|
53
|
+
confirm, then does it — running the client's own `mcp add` command, or writing
|
|
54
|
+
its JSON config (with a backup). Pass `--client` to skip the prompt.
|
|
53
55
|
|
|
54
56
|
```bash
|
|
55
|
-
npx astralis-cli mcp
|
|
56
|
-
npx astralis-cli mcp --
|
|
57
|
+
npx astralis-cli connect-mcp # prompt for a client, then configure it
|
|
58
|
+
npx astralis-cli connect-mcp --client antigravity # go straight to Antigravity
|
|
59
|
+
npx astralis-cli connect-mcp --client codex # go straight to Codex
|
|
57
60
|
```
|
|
58
61
|
|
|
62
|
+
Claude Code and Codex are configured by running their own `mcp add` command;
|
|
63
|
+
Cursor, Claude Desktop, and Antigravity by writing their config files.
|
|
64
|
+
|
|
59
65
|
## License
|
|
60
66
|
|
|
61
67
|
MIT
|
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
|
+
}
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import { parseArgs } from "node:util";
|
|
2
|
+
import { existsSync, readFileSync, writeFileSync, copyFileSync, mkdirSync } from "node:fs";
|
|
3
|
+
import { join, dirname } from "node:path";
|
|
4
|
+
import { homedir } from "node:os";
|
|
5
|
+
import { spawnSync } from "node:child_process";
|
|
6
|
+
import { ok, warn, info, fail, confirm, select, bold, cyan, dim } from "../lib/ui.mjs";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Connect one AI coding agent to the astralis-mcp docs server. Pick a client
|
|
10
|
+
* (prompted, or via --client); the CLI explains exactly what it will do for
|
|
11
|
+
* that client, confirms, then does it — running the client's own `mcp add`
|
|
12
|
+
* command, or writing its JSON config file (with a backup).
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
/** Windows can't spawn npx directly from a JSON command — it needs the cmd shim. */
|
|
16
|
+
export function mcpServerEntry(platform = process.platform) {
|
|
17
|
+
return platform === "win32"
|
|
18
|
+
? { command: "cmd", args: ["/c", "npx", "-y", "astralis-mcp"] }
|
|
19
|
+
: { command: "npx", args: ["-y", "astralis-mcp"] };
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function claudeDesktopConfigPath(platform = process.platform, home = homedir()) {
|
|
23
|
+
if (platform === "win32") return join(process.env.APPDATA ?? join(home, "AppData", "Roaming"), "Claude", "claude_desktop_config.json");
|
|
24
|
+
if (platform === "darwin") return join(home, "Library", "Application Support", "Claude", "claude_desktop_config.json");
|
|
25
|
+
return join(home, ".config", "Claude", "claude_desktop_config.json");
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function cursorConfigPath(home = homedir()) {
|
|
29
|
+
return join(home, ".cursor", "mcp.json");
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** Antigravity shares one config across IDE and CLI — same path on every platform. */
|
|
33
|
+
export function antigravityConfigPath(home = homedir()) {
|
|
34
|
+
return join(home, ".gemini", "config", "mcp_config.json");
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Every supported client, in menu order. Two kinds:
|
|
39
|
+
* "command" — the client owns its config; we run its `mcp add` for you.
|
|
40
|
+
* "file" — we write an `mcpServers` entry into its JSON config (with backup).
|
|
41
|
+
*/
|
|
42
|
+
export const CLIENTS = {
|
|
43
|
+
"claude-code": { label: "Claude Code", kind: "command", bin: "claude" },
|
|
44
|
+
codex: { label: "Codex", kind: "command", bin: "codex" },
|
|
45
|
+
cursor: { label: "Cursor", kind: "file", path: cursorConfigPath, reload: "Reload Cursor to pick it up." },
|
|
46
|
+
"claude-desktop": { label: "Claude Desktop", kind: "file", path: claudeDesktopConfigPath, reload: "Restart Claude Desktop to pick it up." },
|
|
47
|
+
antigravity: { label: "Antigravity", kind: "file", path: antigravityConfigPath, reload: "Reload Antigravity to pick it up." },
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
/** The `mcp add` argv both command-based clients happen to share. */
|
|
51
|
+
const ADD_ARGS = ["mcp", "add", "astralis", "--", "npx", "-y", "astralis-mcp"];
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Parse an existing config file's contents. An empty or whitespace-only file
|
|
55
|
+
* (something else touched it into existence) counts as a fresh config, not an
|
|
56
|
+
* error; genuinely malformed JSON throws for the caller to report.
|
|
57
|
+
*/
|
|
58
|
+
export function parseConfigFile(raw) {
|
|
59
|
+
return raw.trim() === "" ? {} : JSON.parse(raw);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export async function run(argv) {
|
|
63
|
+
const { values } = parseArgs({ args: argv, options: { client: { type: "string" } } });
|
|
64
|
+
|
|
65
|
+
let key = values.client;
|
|
66
|
+
if (key === undefined) {
|
|
67
|
+
console.log(`${bold("Connect a client")} to the Astralis MCP docs server.\n`);
|
|
68
|
+
key = await select(
|
|
69
|
+
"Which client do you want to connect?",
|
|
70
|
+
Object.entries(CLIENTS).map(([value, { label }]) => ({ value, label })),
|
|
71
|
+
);
|
|
72
|
+
if (key === null) {
|
|
73
|
+
warn("No client selected — nothing to do.");
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const client = CLIENTS[key];
|
|
79
|
+
if (!client) {
|
|
80
|
+
fail(`Unknown --client "${key}" — expected one of: ${Object.keys(CLIENTS).join(", ")}.`);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
console.log();
|
|
84
|
+
if (client.kind === "command") await connectViaCommand(client);
|
|
85
|
+
else await connectViaFile(client);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** Run the client's own `mcp add` command, falling back to a copy-paste line. */
|
|
89
|
+
async function connectViaCommand(client) {
|
|
90
|
+
const cmdline = `${client.bin} ${ADD_ARGS.join(" ")}`;
|
|
91
|
+
info(`${bold(client.label)} manages MCP servers with its own CLI.`);
|
|
92
|
+
console.log(` I'll run: ${cyan(cmdline)}`);
|
|
93
|
+
console.log(dim(` Needs the ${client.bin} CLI on your PATH.`));
|
|
94
|
+
|
|
95
|
+
if (!(await confirm("Proceed?"))) {
|
|
96
|
+
warn("Cancelled.");
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const result = spawnSync(cmdline, { stdio: "inherit", shell: true });
|
|
101
|
+
if (result.status === 0) {
|
|
102
|
+
ok(`Connected ${client.label} to the Astralis docs server.`);
|
|
103
|
+
} else {
|
|
104
|
+
fail(`That didn't complete (is the ${cyan(client.bin)} CLI installed?). You can run it yourself:\n ${cyan(cmdline)}`);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** Merge the astralis entry into the client's JSON config, backing up first. */
|
|
109
|
+
async function connectViaFile(client) {
|
|
110
|
+
const entry = mcpServerEntry();
|
|
111
|
+
const configPath = client.path();
|
|
112
|
+
|
|
113
|
+
// Read the existing config first — nothing to describe or back up if the
|
|
114
|
+
// server is already there.
|
|
115
|
+
let config = {};
|
|
116
|
+
if (existsSync(configPath)) {
|
|
117
|
+
try {
|
|
118
|
+
config = parseConfigFile(readFileSync(configPath, "utf8"));
|
|
119
|
+
} catch {
|
|
120
|
+
fail(`${configPath} exists but isn't valid JSON — fix or remove it first.`);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
if (config.mcpServers?.astralis) {
|
|
125
|
+
ok(`${client.label} already has an "astralis" MCP server configured:`);
|
|
126
|
+
console.log(dim(JSON.stringify(config.mcpServers.astralis, null, 2)));
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
info(`${bold(client.label)} reads MCP servers from a JSON config file.`);
|
|
131
|
+
console.log(` ${dim("file: ")} ${cyan(configPath)}`);
|
|
132
|
+
console.log(` ${dim("adds: ")} ${cyan(`"astralis": ${JSON.stringify(entry)}`)}`);
|
|
133
|
+
if (existsSync(configPath)) console.log(dim(" Your current config will be backed up for ease of revision."));
|
|
134
|
+
|
|
135
|
+
if (!(await confirm("Proceed?"))) {
|
|
136
|
+
warn("Cancelled.");
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
if (existsSync(configPath)) copyFileSync(configPath, configPath + ".bak");
|
|
141
|
+
else mkdirSync(dirname(configPath), { recursive: true });
|
|
142
|
+
config.mcpServers = { ...config.mcpServers, astralis: entry };
|
|
143
|
+
writeFileSync(configPath, JSON.stringify(config, null, 2) + "\n");
|
|
144
|
+
ok(`written${existsSync(configPath + ".bak") ? dim(` (backup: ${configPath}.bak)`) : ""}`);
|
|
145
|
+
info(client.reload);
|
|
146
|
+
}
|
package/src/index.mjs
CHANGED
|
@@ -2,13 +2,14 @@
|
|
|
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 } 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
|
-
mcp: () => import("./commands/mcp.mjs"),
|
|
12
|
+
"connect-mcp": () => import("./commands/connect-mcp.mjs"),
|
|
12
13
|
};
|
|
13
14
|
|
|
14
15
|
const HELP = `
|
|
@@ -23,27 +24,41 @@ ${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>")}
|
|
29
34
|
${dim("--out <file> (default astralis-theme.css) · --force")}
|
|
30
35
|
${dim("run `astralis theme --help` for the full list")}
|
|
31
|
-
${cyan("mcp")}
|
|
32
|
-
${dim("
|
|
36
|
+
${cyan("connect-mcp")} connect a client to the Astralis docs server
|
|
37
|
+
${dim("prompts for the client, then configures it for you")}
|
|
38
|
+
${dim("--client claude-code|codex|cursor|claude-desktop|antigravity")}
|
|
39
|
+
${dim(" skip the prompt and pick the client directly")}
|
|
33
40
|
|
|
34
41
|
${bold("Docs")} ${cyan("https://astralis-zeta.vercel.app/docs")}
|
|
35
42
|
`;
|
|
36
43
|
|
|
37
44
|
const [command, ...rest] = process.argv.slice(2);
|
|
38
45
|
|
|
39
|
-
|
|
40
|
-
|
|
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]();
|
|
54
|
+
await run(rest);
|
|
55
|
+
} else {
|
|
56
|
+
fail(`Unknown command "${command}" — run ${cyan("astralis --help")}.`);
|
|
57
|
+
}
|
|
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();
|
|
49
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,16 +19,72 @@ 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);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/*
|
|
42
|
+
* One shared readline for the whole process, draining lines through a queue.
|
|
43
|
+
* readline reads ahead, so with piped input it emits the next line's `line`
|
|
44
|
+
* event in the gap between one prompt resolving and the next registering its
|
|
45
|
+
* listener (e.g. select → confirm) — a per-question `rl.question` loses it. A
|
|
46
|
+
* standing `line` listener queues those reads instead. Callers run
|
|
47
|
+
* closePrompts() when done so the open stream stops keeping the process alive.
|
|
48
|
+
*/
|
|
49
|
+
let sharedRl = null;
|
|
50
|
+
let lineQueue = [];
|
|
51
|
+
let lineWaiter = null;
|
|
52
|
+
|
|
53
|
+
function ask(promptText) {
|
|
54
|
+
if (!sharedRl) {
|
|
55
|
+
sharedRl = readline.createInterface({ input: process.stdin, output: process.stdout });
|
|
56
|
+
sharedRl.on("line", (line) => {
|
|
57
|
+
if (lineWaiter) { const resolve = lineWaiter; lineWaiter = null; resolve(line); }
|
|
58
|
+
else lineQueue.push(line);
|
|
59
|
+
});
|
|
60
|
+
// EOF (piped input exhausted): unblock a pending prompt with an empty answer.
|
|
61
|
+
sharedRl.on("close", () => { if (lineWaiter) { const resolve = lineWaiter; lineWaiter = null; resolve(""); } });
|
|
62
|
+
}
|
|
63
|
+
process.stdout.write(promptText);
|
|
64
|
+
if (lineQueue.length) return Promise.resolve(lineQueue.shift());
|
|
65
|
+
return new Promise((resolve) => { lineWaiter = resolve; });
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export function closePrompts() {
|
|
69
|
+
sharedRl?.close();
|
|
70
|
+
sharedRl = null;
|
|
71
|
+
lineQueue = [];
|
|
72
|
+
lineWaiter = null;
|
|
26
73
|
}
|
|
27
74
|
|
|
28
75
|
/** y/N prompt; default no. */
|
|
29
76
|
export async function confirm(question) {
|
|
30
|
-
const
|
|
31
|
-
const answer = await rl.question(`${question} ${dim("(y/N)")} `);
|
|
32
|
-
rl.close();
|
|
77
|
+
const answer = await ask(`${question} ${dim("(y/N)")} `);
|
|
33
78
|
return /^y(es)?$/i.test(answer.trim());
|
|
34
79
|
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Numbered single-choice prompt. `options` is [{ value, label }].
|
|
83
|
+
* Returns the chosen value, or null if the answer isn't a listed number.
|
|
84
|
+
*/
|
|
85
|
+
export async function select(question, options) {
|
|
86
|
+
console.log(question);
|
|
87
|
+
options.forEach((o, i) => console.log(` ${cyan(String(i + 1))} ${o.label}`));
|
|
88
|
+
const answer = await ask(`${dim("Enter a number:")} `);
|
|
89
|
+
return options[Number(answer.trim()) - 1]?.value ?? null;
|
|
90
|
+
}
|
package/src/commands/mcp.mjs
DELETED
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
import { parseArgs } from "node:util";
|
|
2
|
-
import { existsSync, readFileSync, writeFileSync, copyFileSync, mkdirSync } from "node:fs";
|
|
3
|
-
import { join, dirname } from "node:path";
|
|
4
|
-
import { homedir } from "node:os";
|
|
5
|
-
import { ok, warn, info, fail, confirm, bold, cyan, dim } from "../lib/ui.mjs";
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Connect AI coding agents to the astralis-mcp docs server. Prints the config
|
|
9
|
-
* for every client; --write merges it into Claude Desktop's config file
|
|
10
|
-
* (the only client without a CLI or UI for adding servers by hand).
|
|
11
|
-
*/
|
|
12
|
-
|
|
13
|
-
/** Windows can't spawn npx directly from a JSON command — it needs the cmd shim. */
|
|
14
|
-
export function mcpServerEntry(platform = process.platform) {
|
|
15
|
-
return platform === "win32"
|
|
16
|
-
? { command: "cmd", args: ["/c", "npx", "-y", "astralis-mcp"] }
|
|
17
|
-
: { command: "npx", args: ["-y", "astralis-mcp"] };
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export function claudeDesktopConfigPath(platform = process.platform, home = homedir()) {
|
|
21
|
-
if (platform === "win32") return join(process.env.APPDATA ?? join(home, "AppData", "Roaming"), "Claude", "claude_desktop_config.json");
|
|
22
|
-
if (platform === "darwin") return join(home, "Library", "Application Support", "Claude", "claude_desktop_config.json");
|
|
23
|
-
return join(home, ".config", "Claude", "claude_desktop_config.json");
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
export async function run(argv) {
|
|
27
|
-
const { values } = parseArgs({
|
|
28
|
-
args: argv,
|
|
29
|
-
options: { write: { type: "boolean", default: false } },
|
|
30
|
-
});
|
|
31
|
-
|
|
32
|
-
const entry = mcpServerEntry();
|
|
33
|
-
const snippet = JSON.stringify({ mcpServers: { astralis: entry } }, null, 2);
|
|
34
|
-
|
|
35
|
-
if (!values.write) {
|
|
36
|
-
console.log(`${bold("Astralis MCP server")} — live component docs for AI coding agents.\n`);
|
|
37
|
-
console.log(`${bold("Claude Code")}\n ${cyan("claude mcp add astralis -- npx -y astralis-mcp")}\n`);
|
|
38
|
-
console.log(`${bold("Cursor")} ${dim("(~/.cursor/mcp.json)")} ${bold("/ any MCP client")}\n${snippet.split("\n").map((l) => " " + l).join("\n")}\n`);
|
|
39
|
-
console.log(`${bold("Claude Desktop")}\n ${cyan("astralis mcp --write")} ${dim(`(edits ${claudeDesktopConfigPath()})`)}`);
|
|
40
|
-
return;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
const configPath = claudeDesktopConfigPath();
|
|
44
|
-
let config = {};
|
|
45
|
-
if (existsSync(configPath)) {
|
|
46
|
-
try {
|
|
47
|
-
config = JSON.parse(readFileSync(configPath, "utf8"));
|
|
48
|
-
} catch {
|
|
49
|
-
fail(`${configPath} exists but isn't valid JSON — fix or remove it first.`);
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
if (config.mcpServers?.astralis) {
|
|
54
|
-
ok("Claude Desktop already has an \"astralis\" MCP server configured:");
|
|
55
|
-
console.log(dim(JSON.stringify(config.mcpServers.astralis, null, 2)));
|
|
56
|
-
return;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
info(`This will add the "astralis" server to ${cyan(configPath)}`);
|
|
60
|
-
if (!(await confirm("Write it?"))) {
|
|
61
|
-
warn("Nothing written.");
|
|
62
|
-
return;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
if (existsSync(configPath)) copyFileSync(configPath, configPath + ".bak");
|
|
66
|
-
else mkdirSync(dirname(configPath), { recursive: true });
|
|
67
|
-
config.mcpServers = { ...config.mcpServers, astralis: entry };
|
|
68
|
-
writeFileSync(configPath, JSON.stringify(config, null, 2) + "\n");
|
|
69
|
-
ok(`written${existsSync(configPath + ".bak") ? dim(` (backup: ${configPath}.bak)`) : ""}`);
|
|
70
|
-
info("Restart Claude Desktop to pick it up.");
|
|
71
|
-
}
|