astralis-cli 0.1.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 +61 -0
- package/package.json +44 -0
- package/src/commands/create.mjs +84 -0
- package/src/commands/init.mjs +109 -0
- package/src/commands/mcp.mjs +71 -0
- package/src/commands/theme.mjs +58 -0
- package/src/index.mjs +46 -0
- package/src/lib/detect.mjs +43 -0
- package/src/lib/edits.mjs +57 -0
- package/src/lib/ui.mjs +34 -0
package/README.md
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
# astralis-cli
|
|
2
|
+
|
|
3
|
+
Setup and tooling CLI for [Astralis UI](https://astralis-zeta.vercel.app).
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
npx astralis-cli <command>
|
|
7
|
+
# or: npm i -g astralis-cli && astralis <command>
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
## Commands
|
|
11
|
+
|
|
12
|
+
### `astralis create <name>`
|
|
13
|
+
|
|
14
|
+
Scaffolds a brand-new project and wires Astralis into it. The official
|
|
15
|
+
scaffolder runs with its own prompts — TypeScript, ESLint, and friends stay
|
|
16
|
+
your call — then `init` runs on the result. Extra args pass straight through
|
|
17
|
+
to the scaffolder.
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
npm create astralis my-app # interactive
|
|
21
|
+
npx astralis-cli create my-app --framework vite --template react-ts
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
### `astralis init`
|
|
25
|
+
|
|
26
|
+
Wires Astralis UI into a Next.js or Vite project: installs the package,
|
|
27
|
+
adds the stylesheet import, wraps your app in `AstralisProvider`, and (Next)
|
|
28
|
+
injects the anti-flash dark-mode script. Idempotent — safe to run twice.
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
npx astralis-cli init # do it
|
|
32
|
+
npx astralis-cli init --dry-run # show what would change
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
### `astralis theme <hex>`
|
|
36
|
+
|
|
37
|
+
Generates a static stylesheet with the full OKLCH-derived brand ramp and role
|
|
38
|
+
tokens for light and dark — the build-time alternative to
|
|
39
|
+
`<AstralisProvider tokens={{ brandColor }}>`, computed with the exact same
|
|
40
|
+
color math.
|
|
41
|
+
|
|
42
|
+
```bash
|
|
43
|
+
npx astralis-cli theme "#8b5cf6" --out astralis-theme.css
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
Import it **after** `astralis-ui/styles.css`.
|
|
47
|
+
|
|
48
|
+
### `astralis mcp`
|
|
49
|
+
|
|
50
|
+
Connects AI coding agents to the [astralis-mcp](https://www.npmjs.com/package/astralis-mcp)
|
|
51
|
+
docs server — prints ready-made configs for Claude Code, Cursor, and any MCP
|
|
52
|
+
client; `--write` adds it to Claude Desktop's config for you (with a backup).
|
|
53
|
+
|
|
54
|
+
```bash
|
|
55
|
+
npx astralis-cli mcp # print configs
|
|
56
|
+
npx astralis-cli mcp --write # configure Claude Desktop
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
## License
|
|
60
|
+
|
|
61
|
+
MIT
|
package/package.json
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "astralis-cli",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Setup and tooling CLI for Astralis UI — wire up a project, generate a static brand theme, connect the MCP server.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"author": "Paul Andrew",
|
|
8
|
+
"homepage": "https://astralis-zeta.vercel.app",
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+https://github.com/SekiroDev360/astralis-ui.git",
|
|
12
|
+
"directory": "packages/astralis-cli"
|
|
13
|
+
},
|
|
14
|
+
"bugs": {
|
|
15
|
+
"url": "https://github.com/SekiroDev360/astralis-ui/issues"
|
|
16
|
+
},
|
|
17
|
+
"keywords": [
|
|
18
|
+
"astralis-ui",
|
|
19
|
+
"cli",
|
|
20
|
+
"react",
|
|
21
|
+
"components",
|
|
22
|
+
"theming",
|
|
23
|
+
"setup",
|
|
24
|
+
"mcp"
|
|
25
|
+
],
|
|
26
|
+
"bin": {
|
|
27
|
+
"astralis": "src/index.mjs"
|
|
28
|
+
},
|
|
29
|
+
"exports": {
|
|
30
|
+
"./cli": "./src/index.mjs"
|
|
31
|
+
},
|
|
32
|
+
"files": [
|
|
33
|
+
"src"
|
|
34
|
+
],
|
|
35
|
+
"engines": {
|
|
36
|
+
"node": ">=18"
|
|
37
|
+
},
|
|
38
|
+
"dependencies": {
|
|
39
|
+
"astralis-ui": "^0.1.3"
|
|
40
|
+
},
|
|
41
|
+
"scripts": {
|
|
42
|
+
"test": "node --test"
|
|
43
|
+
}
|
|
44
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { existsSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { spawnSync } from "node:child_process";
|
|
4
|
+
import readline from "node:readline/promises";
|
|
5
|
+
import { ok, warn, info, fail, bold, cyan, dim } from "../lib/ui.mjs";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Scaffold a brand-new project and wire Astralis into it. We own NO project
|
|
9
|
+
* templates: the official scaffolder (create-next-app / create-vite) runs
|
|
10
|
+
* interactively with its own prompts — TS, ESLint, Tailwind stay the user's
|
|
11
|
+
* call, exactly as if they'd run it directly — and our `init` runs on the
|
|
12
|
+
* result. Any extra args are forwarded verbatim to the scaffolder, so
|
|
13
|
+
* non-interactive use works too:
|
|
14
|
+
*
|
|
15
|
+
* astralis create my-app --framework vite --template react
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
/** The package manager that invoked us — `pnpm create astralis` should scaffold with pnpm. */
|
|
19
|
+
export function invokingPackageManager(userAgent = process.env.npm_config_user_agent ?? "") {
|
|
20
|
+
const name = userAgent.split("/")[0];
|
|
21
|
+
return ["pnpm", "yarn", "bun"].includes(name) ? name : "npm";
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** One shell command string (spawned with shell:true — pm binaries are .cmd shims on Windows). */
|
|
25
|
+
export function buildScaffoldCommand(framework, name, forwarded, pm) {
|
|
26
|
+
// yarn classic's `create` chokes on @latest tags; everyone else wants them.
|
|
27
|
+
const tag = pm === "yarn" ? "" : "@latest";
|
|
28
|
+
const scaffolder = framework === "next" ? `next-app${tag}` : `vite${tag}`;
|
|
29
|
+
// npm swallows flags meant for the scaffolder unless they sit behind `--`.
|
|
30
|
+
const separator = pm === "npm" && forwarded.length > 0 ? ["--"] : [];
|
|
31
|
+
return [pm, "create", scaffolder, name, ...separator, ...forwarded].filter(Boolean).join(" ");
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
async function promptFramework() {
|
|
35
|
+
const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
|
|
36
|
+
const answer = await rl.question(
|
|
37
|
+
`${bold("Framework?")} ${cyan("1")} Next.js ${cyan("2")} Vite ${dim("(choose a React template when Vite asks)")} `,
|
|
38
|
+
);
|
|
39
|
+
rl.close();
|
|
40
|
+
const choice = answer.trim().toLowerCase();
|
|
41
|
+
if (choice === "1" || choice === "next" || choice === "next.js" || choice === "nextjs") return "next";
|
|
42
|
+
if (choice === "2" || choice === "vite") return "vite";
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export async function run(argv) {
|
|
47
|
+
// Manual parse: --framework is ours, first bare word is the name,
|
|
48
|
+
// EVERYTHING else is forwarded untouched to the scaffolder.
|
|
49
|
+
let framework;
|
|
50
|
+
let name;
|
|
51
|
+
const forwarded = [];
|
|
52
|
+
for (let i = 0; i < argv.length; i++) {
|
|
53
|
+
const arg = argv[i];
|
|
54
|
+
if (arg === "--framework") framework = argv[++i];
|
|
55
|
+
else if (!name && !arg.startsWith("-")) name = arg;
|
|
56
|
+
else forwarded.push(arg);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
if (!name) fail('Usage: astralis create <project-name> [--framework next|vite] [scaffolder args…]');
|
|
60
|
+
if (!/^[a-z0-9@][a-z0-9-_.]*$/i.test(name)) fail(`"${name}" isn't a safe project name.`);
|
|
61
|
+
if (existsSync(join(process.cwd(), name))) fail(`./${name} already exists.`);
|
|
62
|
+
|
|
63
|
+
framework ??= await promptFramework();
|
|
64
|
+
if (framework !== "next" && framework !== "vite") fail('Pick a framework: "next" or "vite".');
|
|
65
|
+
|
|
66
|
+
const pm = invokingPackageManager();
|
|
67
|
+
const command = buildScaffoldCommand(framework, name, forwarded, pm);
|
|
68
|
+
info(`${bold(command)} ${dim("(the scaffolder's own prompts — answer them as usual)")}`);
|
|
69
|
+
|
|
70
|
+
const scaffold = spawnSync(command, { stdio: "inherit", shell: true });
|
|
71
|
+
if (scaffold.status !== 0) fail("Scaffolding failed or was cancelled — nothing to wire up.");
|
|
72
|
+
if (!existsSync(join(process.cwd(), name, "package.json"))) {
|
|
73
|
+
fail(`Expected ./${name}/package.json after scaffolding — did the scaffolder use a different directory?`);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
ok(`project created in ./${name}`);
|
|
77
|
+
console.log();
|
|
78
|
+
info("wiring Astralis into it…");
|
|
79
|
+
process.chdir(name);
|
|
80
|
+
const init = await import("./init.mjs");
|
|
81
|
+
await init.run([]);
|
|
82
|
+
|
|
83
|
+
console.log(`\n${bold("Next steps:")}\n ${cyan(`cd ${name}`)}\n ${cyan(`${pm} run dev`)}`);
|
|
84
|
+
}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { parseArgs } from "node:util";
|
|
2
|
+
import { readFileSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { spawnSync } from "node:child_process";
|
|
4
|
+
import { readPackageJson, detectPackageManager, detectFramework, findEntryFile } from "../lib/detect.mjs";
|
|
5
|
+
import { addImports, wrapJsx, addTagAttribute, insertAfterBodyTag, addStatementAfterImports } from "../lib/edits.mjs";
|
|
6
|
+
import { ok, warn, info, fail, bold, dim, cyan } from "../lib/ui.mjs";
|
|
7
|
+
|
|
8
|
+
const PROVIDER_IMPORT = 'import { AstralisProvider } from "astralis-ui";';
|
|
9
|
+
const STYLES_IMPORT = 'import "astralis-ui/styles.css";';
|
|
10
|
+
|
|
11
|
+
// Applies `.astralis-dark` before first paint so a stored/system dark
|
|
12
|
+
// preference never flashes light (same script the Astralis docs site uses).
|
|
13
|
+
const THEME_INIT_CONST =
|
|
14
|
+
'const astralisThemeInit = `(function(){try{var t=localStorage.getItem("astralis-ui-theme");var d=t==="dark"||((!t||t==="system")&&window.matchMedia("(prefers-color-scheme: dark)").matches);if(d)document.documentElement.classList.add("astralis-dark");}catch(e){}})();`;';
|
|
15
|
+
const THEME_INIT_SCRIPT = '\n <script dangerouslySetInnerHTML={{ __html: astralisThemeInit }} />';
|
|
16
|
+
|
|
17
|
+
const MANUAL_SNIPPET = `
|
|
18
|
+
${bold("Manual setup:")}
|
|
19
|
+
1. ${PROVIDER_IMPORT}
|
|
20
|
+
2. ${STYLES_IMPORT} ${dim("(once, at your entry point)")}
|
|
21
|
+
3. Wrap your app: <AstralisProvider defaultTheme="system">…</AstralisProvider>
|
|
22
|
+
Full guide: ${cyan("https://astralis-zeta.vercel.app/docs/installation")}`;
|
|
23
|
+
|
|
24
|
+
export async function run(argv) {
|
|
25
|
+
const { values } = parseArgs({
|
|
26
|
+
args: argv,
|
|
27
|
+
options: { "dry-run": { type: "boolean", default: false } },
|
|
28
|
+
});
|
|
29
|
+
const dryRun = values["dry-run"];
|
|
30
|
+
const cwd = process.cwd();
|
|
31
|
+
|
|
32
|
+
const pkg = readPackageJson(cwd);
|
|
33
|
+
if (!pkg) fail("No package.json here — run `astralis init` from your project root.");
|
|
34
|
+
|
|
35
|
+
const framework = detectFramework(pkg);
|
|
36
|
+
if (!framework) {
|
|
37
|
+
warn("Couldn't detect Next.js or Vite in this project.");
|
|
38
|
+
console.log(MANUAL_SNIPPET);
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
const pm = detectPackageManager(cwd);
|
|
42
|
+
info(`${bold(framework === "next" ? "Next.js" : "Vite")} project, ${bold(pm)}${dryRun ? dim(" (dry run — nothing will be written)") : ""}`);
|
|
43
|
+
|
|
44
|
+
// 1. Dependency
|
|
45
|
+
const hasDep = Boolean(pkg.dependencies?.["astralis-ui"] ?? pkg.devDependencies?.["astralis-ui"]);
|
|
46
|
+
if (hasDep) {
|
|
47
|
+
ok("astralis-ui already in dependencies");
|
|
48
|
+
} else if (dryRun) {
|
|
49
|
+
info(`would run: ${pm} add astralis-ui`);
|
|
50
|
+
} else {
|
|
51
|
+
info(`installing astralis-ui with ${pm}…`);
|
|
52
|
+
// One command string: pm binaries are .cmd shims on Windows (need a shell),
|
|
53
|
+
// and shell+args-array triggers Node's unescaped-args deprecation.
|
|
54
|
+
const result = spawnSync(`${pm} add astralis-ui`, { stdio: "inherit", shell: true });
|
|
55
|
+
if (result.status !== 0) fail(`${pm} add astralis-ui failed — install it manually, then re-run init.`);
|
|
56
|
+
ok("astralis-ui installed");
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// 2. Entry file
|
|
60
|
+
const entry = findEntryFile(cwd, framework);
|
|
61
|
+
if (!entry) {
|
|
62
|
+
warn(`Couldn't find your ${framework === "next" ? "root layout" : "src/main.tsx"} — wire the provider manually:`);
|
|
63
|
+
console.log(MANUAL_SNIPPET);
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
let source = readFileSync(entry.path, "utf8");
|
|
68
|
+
const original = source;
|
|
69
|
+
const notes = [];
|
|
70
|
+
const apply = (result, label) => {
|
|
71
|
+
source = result.source;
|
|
72
|
+
if (result.changed) ok(`${label} ${dim(`(${entry.relative})`)}`);
|
|
73
|
+
else if (result.note) notes.push(`${label}: ${result.note}`);
|
|
74
|
+
else ok(`${label} — already present`);
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
apply(addImports(source, [PROVIDER_IMPORT, STYLES_IMPORT]), "imports added");
|
|
78
|
+
|
|
79
|
+
if (source.includes("<AstralisProvider")) {
|
|
80
|
+
ok("AstralisProvider already wraps this file");
|
|
81
|
+
} else if (framework === "next") {
|
|
82
|
+
apply(
|
|
83
|
+
wrapJsx(source, "{children}", '<AstralisProvider defaultTheme="system">', "</AstralisProvider>"),
|
|
84
|
+
"provider wrapped around {children}",
|
|
85
|
+
);
|
|
86
|
+
} else {
|
|
87
|
+
apply(
|
|
88
|
+
wrapJsx(source, "<App />", '<AstralisProvider defaultTheme="system">', "</AstralisProvider>"),
|
|
89
|
+
"provider wrapped around <App />",
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
if (framework === "next") {
|
|
94
|
+
apply(addStatementAfterImports(source, THEME_INIT_CONST), "anti-flash theme script const");
|
|
95
|
+
apply(insertAfterBodyTag(source, THEME_INIT_SCRIPT), "theme script injected after <body>");
|
|
96
|
+
apply(addTagAttribute(source, "html", "suppressHydrationWarning"), "suppressHydrationWarning on <html>");
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// 3. Write
|
|
100
|
+
if (source !== original) {
|
|
101
|
+
if (dryRun) info(`would update ${entry.relative}`);
|
|
102
|
+
else writeFileSync(entry.path, source);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
for (const note of notes) warn(note);
|
|
106
|
+
if (notes.length > 0) console.log(MANUAL_SNIPPET);
|
|
107
|
+
|
|
108
|
+
console.log(`\n${bold("Done.")} Try it:\n ${cyan('import { Button } from "astralis-ui";')}\n ${cyan('<Button colorScheme="teal">Hello</Button>')}\nDocs: ${cyan("https://astralis-zeta.vercel.app/docs")}`);
|
|
109
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { parseArgs } from "node:util";
|
|
2
|
+
import { existsSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { generateBrandTokens } from "astralis-ui/color-math";
|
|
4
|
+
import { ok, fail, cyan, dim } from "../lib/ui.mjs";
|
|
5
|
+
|
|
6
|
+
const HEX_PATTERN = /^#([0-9a-f]{3}|[0-9a-f]{6})$/i;
|
|
7
|
+
|
|
8
|
+
const serialize = (vars) =>
|
|
9
|
+
Object.entries(vars)
|
|
10
|
+
.map(([name, value]) => ` ${name}: ${value};`)
|
|
11
|
+
.join("\n");
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* The static equivalent of `<AstralisProvider tokens={{ brandColor }}>` —
|
|
15
|
+
* same OKLCH math (imported from the library, never duplicated), rendered as
|
|
16
|
+
* a stylesheet for people who prefer build-time theming over the runtime
|
|
17
|
+
* provider. Must be imported AFTER astralis-ui/styles.css so the cascade
|
|
18
|
+
* lets these :root declarations win.
|
|
19
|
+
*/
|
|
20
|
+
export function themeCss(hex) {
|
|
21
|
+
const light = generateBrandTokens(hex, "light");
|
|
22
|
+
const dark = generateBrandTokens(hex, "dark");
|
|
23
|
+
return [
|
|
24
|
+
`/* Generated by astralis-cli — brand ramp for ${hex}.`,
|
|
25
|
+
` * Import AFTER "astralis-ui/styles.css" so these overrides win. */`,
|
|
26
|
+
"",
|
|
27
|
+
":root {",
|
|
28
|
+
serialize(light),
|
|
29
|
+
"}",
|
|
30
|
+
"",
|
|
31
|
+
".astralis-dark {",
|
|
32
|
+
serialize(dark),
|
|
33
|
+
"}",
|
|
34
|
+
"",
|
|
35
|
+
].join("\n");
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export async function run(argv) {
|
|
39
|
+
const { values, positionals } = parseArgs({
|
|
40
|
+
args: argv,
|
|
41
|
+
options: {
|
|
42
|
+
out: { type: "string", default: "astralis-theme.css" },
|
|
43
|
+
force: { type: "boolean", default: false },
|
|
44
|
+
},
|
|
45
|
+
allowPositionals: true,
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
const hex = positionals[0];
|
|
49
|
+
if (!hex) fail('Usage: astralis theme "#8b5cf6" [--out astralis-theme.css] [--force]');
|
|
50
|
+
if (!HEX_PATTERN.test(hex)) fail(`"${hex}" is not a hex color — expected #rgb or #rrggbb.`);
|
|
51
|
+
if (existsSync(values.out) && !values.force) {
|
|
52
|
+
fail(`${values.out} already exists — pass --force to overwrite.`);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
writeFileSync(values.out, themeCss(hex.toLowerCase()));
|
|
56
|
+
ok(`wrote ${cyan(values.out)}`);
|
|
57
|
+
console.log(`\nImport it after the library stylesheet:\n ${cyan('import "astralis-ui/styles.css";')}\n ${cyan(`import "./${values.out}";`)}\n${dim("(Runtime alternative: <AstralisProvider tokens={{ brandColor }}> — same math.)")}`);
|
|
58
|
+
}
|
package/src/index.mjs
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { readFileSync } from "node:fs";
|
|
3
|
+
import { fileURLToPath } from "node:url";
|
|
4
|
+
import { join, dirname } from "node:path";
|
|
5
|
+
import { bold, cyan, dim, fail } from "./lib/ui.mjs";
|
|
6
|
+
|
|
7
|
+
const COMMANDS = {
|
|
8
|
+
create: () => import("./commands/create.mjs"),
|
|
9
|
+
init: () => import("./commands/init.mjs"),
|
|
10
|
+
theme: () => import("./commands/theme.mjs"),
|
|
11
|
+
mcp: () => import("./commands/mcp.mjs"),
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
const HELP = `
|
|
15
|
+
${bold("astralis")} — setup and tooling for Astralis UI
|
|
16
|
+
|
|
17
|
+
${bold("Usage")}
|
|
18
|
+
astralis <command> [options]
|
|
19
|
+
|
|
20
|
+
${bold("Commands")}
|
|
21
|
+
${cyan("create <name>")} scaffold a new project (official create-next-app /
|
|
22
|
+
create-vite prompts) and wire Astralis into it
|
|
23
|
+
${dim("--framework next|vite · extra args pass to the scaffolder")}
|
|
24
|
+
${cyan("init")} wire astralis-ui into this project (Next.js or Vite)
|
|
25
|
+
${dim("--dry-run show the changes without writing")}
|
|
26
|
+
${cyan("theme <hex>")} generate a static brand-theme stylesheet
|
|
27
|
+
${dim('--out <file> (default astralis-theme.css) · --force')}
|
|
28
|
+
${cyan("mcp")} connect AI coding agents to the Astralis docs server
|
|
29
|
+
${dim("--write add it to Claude Desktop's config")}
|
|
30
|
+
|
|
31
|
+
${bold("Docs")} ${cyan("https://astralis-zeta.vercel.app/docs")}
|
|
32
|
+
`;
|
|
33
|
+
|
|
34
|
+
const [command, ...rest] = process.argv.slice(2);
|
|
35
|
+
|
|
36
|
+
if (!command || command === "--help" || command === "-h" || command === "help") {
|
|
37
|
+
console.log(HELP);
|
|
38
|
+
} else if (command === "--version" || command === "-v") {
|
|
39
|
+
const pkg = JSON.parse(readFileSync(join(dirname(fileURLToPath(import.meta.url)), "..", "package.json"), "utf8"));
|
|
40
|
+
console.log(pkg.version);
|
|
41
|
+
} else if (COMMANDS[command]) {
|
|
42
|
+
const { run } = await COMMANDS[command]();
|
|
43
|
+
await run(rest);
|
|
44
|
+
} else {
|
|
45
|
+
fail(`Unknown command "${command}" — run ${cyan("astralis --help")}.`);
|
|
46
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
|
|
4
|
+
/** Project inspection — all pure lookups against a directory, no writes. */
|
|
5
|
+
|
|
6
|
+
export function readPackageJson(cwd) {
|
|
7
|
+
const path = join(cwd, "package.json");
|
|
8
|
+
if (!existsSync(path)) return null;
|
|
9
|
+
try {
|
|
10
|
+
return JSON.parse(readFileSync(path, "utf8"));
|
|
11
|
+
} catch {
|
|
12
|
+
return null;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** Lockfile tells the truth about which package manager a project uses. */
|
|
17
|
+
export function detectPackageManager(cwd) {
|
|
18
|
+
if (existsSync(join(cwd, "pnpm-lock.yaml"))) return "pnpm";
|
|
19
|
+
if (existsSync(join(cwd, "yarn.lock"))) return "yarn";
|
|
20
|
+
if (existsSync(join(cwd, "bun.lockb")) || existsSync(join(cwd, "bun.lock"))) return "bun";
|
|
21
|
+
return "npm";
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function detectFramework(pkg) {
|
|
25
|
+
const deps = { ...pkg?.dependencies, ...pkg?.devDependencies };
|
|
26
|
+
if (deps.next) return "next";
|
|
27
|
+
if (deps.vite && (deps.react || deps["@vitejs/plugin-react"])) return "vite";
|
|
28
|
+
return null;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const ENTRY_CANDIDATES = {
|
|
32
|
+
next: ["app/layout.tsx", "app/layout.jsx", "src/app/layout.tsx", "src/app/layout.jsx"],
|
|
33
|
+
vite: ["src/main.tsx", "src/main.jsx"],
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
/** The file init should wire the provider into, or null if none found. */
|
|
37
|
+
export function findEntryFile(cwd, framework) {
|
|
38
|
+
for (const candidate of ENTRY_CANDIDATES[framework] ?? []) {
|
|
39
|
+
const path = join(cwd, candidate);
|
|
40
|
+
if (existsSync(path)) return { path, relative: candidate };
|
|
41
|
+
}
|
|
42
|
+
return null;
|
|
43
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Idempotent string transforms for init's entry-file edits. Every function
|
|
3
|
+
* returns { source, changed, note? } and NEVER guesses: when a file's shape
|
|
4
|
+
* is ambiguous (zero or multiple wrap targets), it declines with a note so
|
|
5
|
+
* init can print manual instructions instead of corrupting code.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/** Add import lines that aren't already present, after the last existing import. */
|
|
9
|
+
export function addImports(source, lines) {
|
|
10
|
+
const missing = lines.filter((line) => !source.includes(line));
|
|
11
|
+
if (missing.length === 0) return { source, changed: false };
|
|
12
|
+
|
|
13
|
+
const block = missing.join("\n");
|
|
14
|
+
const imports = [...source.matchAll(/^import\s[^\n]*$/gm)];
|
|
15
|
+
if (imports.length > 0) {
|
|
16
|
+
const last = imports[imports.length - 1];
|
|
17
|
+
const insertAt = last.index + last[0].length;
|
|
18
|
+
return { source: source.slice(0, insertAt) + "\n" + block + source.slice(insertAt), changed: true };
|
|
19
|
+
}
|
|
20
|
+
// No imports at all — put them at the top, after a directive if present.
|
|
21
|
+
const directive = source.match(/^["']use \w+["'];?\s*\n/);
|
|
22
|
+
const insertAt = directive ? directive[0].length : 0;
|
|
23
|
+
return { source: source.slice(0, insertAt) + block + "\n" + source.slice(insertAt), changed: true };
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** Wrap a single unambiguous occurrence of `target` in open/close JSX tags. */
|
|
27
|
+
export function wrapJsx(source, target, open, close) {
|
|
28
|
+
const count = source.split(target).length - 1;
|
|
29
|
+
if (count === 0) return { source, changed: false, note: `no ${target} found to wrap` };
|
|
30
|
+
if (count > 1) return { source, changed: false, note: `${target} appears ${count} times — wrap it manually` };
|
|
31
|
+
return { source: source.replace(target, `${open}${target}${close}`), changed: true };
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** Add an attribute to a single unambiguous tag (e.g. suppressHydrationWarning on <html>). */
|
|
35
|
+
export function addTagAttribute(source, tag, attribute) {
|
|
36
|
+
if (source.includes(attribute)) return { source, changed: false };
|
|
37
|
+
const matches = [...source.matchAll(new RegExp(`<${tag}(?=[\\s>])`, "g"))];
|
|
38
|
+
if (matches.length !== 1) return { source, changed: false, note: `<${tag}> not found unambiguously` };
|
|
39
|
+
return { source: source.replace(matches[0][0], `<${tag} ${attribute}`), changed: true };
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** Insert markup right after the opening <body ...> tag. */
|
|
43
|
+
export function insertAfterBodyTag(source, insert) {
|
|
44
|
+
if (source.includes(insert.trim())) return { source, changed: false };
|
|
45
|
+
const matches = [...source.matchAll(/<body[^>]*>/g)];
|
|
46
|
+
if (matches.length !== 1) return { source, changed: false, note: "<body> tag not found unambiguously" };
|
|
47
|
+
const at = matches[0].index + matches[0][0].length;
|
|
48
|
+
return { source: source.slice(0, at) + insert + source.slice(at), changed: true };
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** Insert a top-level statement after the import block (e.g. the theme-init const). */
|
|
52
|
+
export function addStatementAfterImports(source, statement) {
|
|
53
|
+
if (source.includes(statement.slice(0, 40))) return { source, changed: false };
|
|
54
|
+
const imports = [...source.matchAll(/^import\s[^\n]*$/gm)];
|
|
55
|
+
const insertAt = imports.length > 0 ? imports[imports.length - 1].index + imports[imports.length - 1][0].length : 0;
|
|
56
|
+
return { source: source.slice(0, insertAt) + "\n\n" + statement + source.slice(insertAt), changed: true };
|
|
57
|
+
}
|
package/src/lib/ui.mjs
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import readline from "node:readline/promises";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Terminal output helpers — raw ANSI, no dependencies. Colors switch off for
|
|
5
|
+
* non-TTY output (piped) and when NO_COLOR is set, per convention.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
const useColor = process.stdout.isTTY && !process.env.NO_COLOR;
|
|
9
|
+
const wrap = (open, close) => (text) => (useColor ? `\x1b[${open}m${text}\x1b[${close}m` : String(text));
|
|
10
|
+
|
|
11
|
+
export const bold = wrap(1, 22);
|
|
12
|
+
export const dim = wrap(2, 22);
|
|
13
|
+
export const red = wrap(31, 39);
|
|
14
|
+
export const green = wrap(32, 39);
|
|
15
|
+
export const yellow = wrap(33, 39);
|
|
16
|
+
export const cyan = wrap(36, 39);
|
|
17
|
+
|
|
18
|
+
export const ok = (message) => console.log(`${green("✓")} ${message}`);
|
|
19
|
+
export const warn = (message) => console.log(`${yellow("!")} ${message}`);
|
|
20
|
+
export const info = (message) => console.log(`${cyan("→")} ${message}`);
|
|
21
|
+
|
|
22
|
+
/** Print the error and exit non-zero — the CLI's only failure path. */
|
|
23
|
+
export function fail(message) {
|
|
24
|
+
console.error(`${red("✗")} ${message}`);
|
|
25
|
+
process.exit(1);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** y/N prompt; default no. */
|
|
29
|
+
export async function confirm(question) {
|
|
30
|
+
const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
|
|
31
|
+
const answer = await rl.question(`${question} ${dim("(y/N)")} `);
|
|
32
|
+
rl.close();
|
|
33
|
+
return /^y(es)?$/i.test(answer.trim());
|
|
34
|
+
}
|