astralis-cli 0.4.0 → 0.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +179 -0
- package/LICENSE +21 -0
- package/README.md +179 -29
- package/package.json +29 -10
- package/src/commands/add.mjs +48 -71
- package/src/commands/connect-mcp.mjs +66 -29
- package/src/commands/create.mjs +73 -100
- package/src/commands/init.mjs +59 -22
- package/src/commands/report.mjs +257 -0
- package/src/commands/theme.mjs +77 -104
- package/src/commands/validate.mjs +101 -0
- package/src/index.mjs +26 -48
- package/src/lib/args.mjs +135 -0
- package/src/lib/aria.mjs +40 -0
- package/src/lib/children-rule.mjs +23 -0
- package/src/lib/ci-setup.mjs +78 -0
- package/src/lib/ci.mjs +155 -0
- package/src/lib/commands.mjs +214 -0
- package/src/lib/css-value.mjs +141 -0
- package/src/lib/devtools-setup.mjs +142 -0
- package/src/lib/edits.mjs +206 -26
- package/src/lib/github-annotations.mjs +26 -0
- package/src/lib/github.d.mts +72 -0
- package/src/lib/github.mjs +339 -0
- package/src/lib/library.mjs +40 -0
- package/src/lib/menu.mjs +59 -0
- package/src/lib/prompt-child.mjs +85 -39
- package/src/lib/registry.mjs +37 -4
- package/src/lib/report-tree.mjs +134 -0
- package/src/lib/run.mjs +39 -0
- package/src/lib/snapshot.d.mts +65 -0
- package/src/lib/snapshot.mjs +371 -0
- package/src/lib/source-files.mjs +29 -0
- package/src/lib/spec-load.mjs +32 -0
- package/src/lib/spec-resolve.mjs +20 -0
- package/src/lib/starter.mjs +11 -19
- package/src/lib/suggest.mjs +32 -0
- package/src/lib/ui.mjs +78 -36
- package/src/lib/validate-core.d.mts +48 -0
- package/src/lib/validate-core.mjs +655 -0
- package/src/lib/validate.d.mts +33 -0
- package/src/lib/validate.mjs +8 -0
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
import { parseArgs } from "node:util";
|
|
2
1
|
import { existsSync, readFileSync, writeFileSync, copyFileSync, mkdirSync } from "node:fs";
|
|
3
2
|
import { join, dirname } from "node:path";
|
|
4
3
|
import { homedir } from "node:os";
|
|
5
|
-
import { spawnSync } from "node:child_process";
|
|
6
4
|
import { ok, warn, info, fail, confirm, select, bold, cyan, dim } from "../lib/ui.mjs";
|
|
5
|
+
import { parseCommandArgs } from "../lib/args.mjs";
|
|
6
|
+
import { commandNamed } from "../lib/commands.mjs";
|
|
7
|
+
import { runCommand } from "../lib/run.mjs";
|
|
7
8
|
|
|
8
9
|
/**
|
|
9
|
-
* Connect one AI coding agent to the astralis-mcp docs server
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
* command, or writing its JSON config file (with a backup).
|
|
10
|
+
* Connect one AI coding agent to the astralis-mcp docs server: pick a client,
|
|
11
|
+
* see exactly what will happen, confirm, and it runs the client's own
|
|
12
|
+
* `mcp add` or writes its JSON config (backing the old one up).
|
|
13
13
|
*/
|
|
14
14
|
|
|
15
15
|
/** Windows can't spawn npx directly from a JSON command — it needs the cmd shim. */
|
|
@@ -35,39 +35,70 @@ export function antigravityConfigPath(home = homedir()) {
|
|
|
35
35
|
}
|
|
36
36
|
|
|
37
37
|
/**
|
|
38
|
-
* Every supported client, in menu order.
|
|
39
|
-
*
|
|
40
|
-
*
|
|
38
|
+
* Every supported client, in menu order. "command" clients own their config
|
|
39
|
+
* and we run their `mcp add`; "file" clients get an `mcpServers` entry written
|
|
40
|
+
* into their JSON config. `root` is the directory that exists once the client
|
|
41
|
+
* is installed.
|
|
41
42
|
*/
|
|
42
43
|
export const CLIENTS = {
|
|
43
44
|
"claude-code": { label: "Claude Code", kind: "command", bin: "claude" },
|
|
44
45
|
codex: { label: "Codex", kind: "command", bin: "codex" },
|
|
45
|
-
cursor: {
|
|
46
|
-
|
|
47
|
-
|
|
46
|
+
cursor: {
|
|
47
|
+
label: "Cursor",
|
|
48
|
+
kind: "file",
|
|
49
|
+
path: cursorConfigPath,
|
|
50
|
+
root: (home = homedir()) => join(home, ".cursor"),
|
|
51
|
+
reload: "Reload Cursor to pick it up.",
|
|
52
|
+
},
|
|
53
|
+
"claude-desktop": {
|
|
54
|
+
label: "Claude Desktop",
|
|
55
|
+
kind: "file",
|
|
56
|
+
path: claudeDesktopConfigPath,
|
|
57
|
+
root: () => dirname(claudeDesktopConfigPath()),
|
|
58
|
+
reload: "Restart Claude Desktop to pick it up.",
|
|
59
|
+
},
|
|
60
|
+
antigravity: {
|
|
61
|
+
label: "Antigravity",
|
|
62
|
+
kind: "file",
|
|
63
|
+
path: antigravityConfigPath,
|
|
64
|
+
root: (home = homedir()) => join(home, ".gemini"),
|
|
65
|
+
reload: "Reload Antigravity to pick it up.",
|
|
66
|
+
},
|
|
48
67
|
};
|
|
49
68
|
|
|
50
|
-
/** The `mcp add` argv
|
|
51
|
-
|
|
69
|
+
/** The `mcp add` argv for a command client, launching the server the way this platform needs. */
|
|
70
|
+
export function mcpAddArgs(platform = process.platform) {
|
|
71
|
+
const entry = mcpServerEntry(platform);
|
|
72
|
+
return ["mcp", "add", "astralis", "--", entry.command, ...entry.args];
|
|
73
|
+
}
|
|
52
74
|
|
|
53
75
|
/**
|
|
54
|
-
*
|
|
55
|
-
*
|
|
56
|
-
* error; genuinely malformed JSON throws for the caller to report.
|
|
76
|
+
* An existing config file's contents. Empty or whitespace-only counts as a
|
|
77
|
+
* fresh config; malformed JSON throws for the caller to report.
|
|
57
78
|
*/
|
|
58
79
|
export function parseConfigFile(raw) {
|
|
59
80
|
return raw.trim() === "" ? {} : JSON.parse(raw);
|
|
60
81
|
}
|
|
61
82
|
|
|
83
|
+
/** The first of `file.bak`, `file.bak.1`, `file.bak.2`… that doesn't exist yet, so no backup is overwritten. */
|
|
84
|
+
export function backupPath(file) {
|
|
85
|
+
if (!existsSync(`${file}.bak`)) return `${file}.bak`;
|
|
86
|
+
for (let n = 1; ; n++) {
|
|
87
|
+
if (!existsSync(`${file}.bak.${n}`)) return `${file}.bak.${n}`;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
62
91
|
export async function run(argv) {
|
|
63
|
-
const
|
|
92
|
+
const args = parseCommandArgs(commandNamed("connect-mcp"), argv);
|
|
93
|
+
if (!args) return;
|
|
64
94
|
|
|
65
|
-
let key = values.client;
|
|
95
|
+
let key = args.values.client;
|
|
66
96
|
if (key === undefined) {
|
|
67
97
|
console.log(`${bold("Connect a client")} to the Astralis MCP docs server.\n`);
|
|
68
98
|
key = await select(
|
|
69
99
|
"Which client do you want to connect?",
|
|
70
100
|
Object.entries(CLIENTS).map(([value, { label }]) => ({ value, label })),
|
|
101
|
+
{ hint: `Pass ${cyan("--client <name>")}.` },
|
|
71
102
|
);
|
|
72
103
|
if (key === null) {
|
|
73
104
|
warn("No client selected — nothing to do.");
|
|
@@ -87,7 +118,8 @@ export async function run(argv) {
|
|
|
87
118
|
|
|
88
119
|
/** Run the client's own `mcp add` command, falling back to a copy-paste line. */
|
|
89
120
|
async function connectViaCommand(client) {
|
|
90
|
-
const
|
|
121
|
+
const addArgs = mcpAddArgs();
|
|
122
|
+
const cmdline = [client.bin, ...addArgs].join(" ");
|
|
91
123
|
info(`${bold(client.label)} manages MCP servers with its own CLI.`);
|
|
92
124
|
console.log(` I'll run: ${cyan(cmdline)}`);
|
|
93
125
|
console.log(dim(` Needs the ${client.bin} CLI on your PATH.`));
|
|
@@ -97,9 +129,7 @@ async function connectViaCommand(client) {
|
|
|
97
129
|
return;
|
|
98
130
|
}
|
|
99
131
|
|
|
100
|
-
|
|
101
|
-
// one has never read stdin and is not holding the console.
|
|
102
|
-
const result = spawnSync(cmdline, { stdio: "inherit", shell: true });
|
|
132
|
+
const result = runCommand(client.bin, addArgs);
|
|
103
133
|
if (result.status === 0) {
|
|
104
134
|
ok(`Connected ${client.label} to the Astralis docs server.`);
|
|
105
135
|
} else {
|
|
@@ -111,9 +141,15 @@ async function connectViaCommand(client) {
|
|
|
111
141
|
async function connectViaFile(client) {
|
|
112
142
|
const entry = mcpServerEntry();
|
|
113
143
|
const configPath = client.path();
|
|
144
|
+
const snippet = `"astralis": ${JSON.stringify(entry)}`;
|
|
145
|
+
|
|
146
|
+
if (!existsSync(client.root())) {
|
|
147
|
+
fail(
|
|
148
|
+
`${client.label} doesn't look installed here (no ${client.root()}), so nothing was written.\n` +
|
|
149
|
+
` Install it, or add this under "mcpServers" in its config yourself:\n ${cyan(snippet)}`,
|
|
150
|
+
);
|
|
151
|
+
}
|
|
114
152
|
|
|
115
|
-
// Read the existing config first — nothing to describe or back up if the
|
|
116
|
-
// server is already there.
|
|
117
153
|
let config = {};
|
|
118
154
|
if (existsSync(configPath)) {
|
|
119
155
|
try {
|
|
@@ -129,20 +165,21 @@ async function connectViaFile(client) {
|
|
|
129
165
|
return;
|
|
130
166
|
}
|
|
131
167
|
|
|
168
|
+
const backup = existsSync(configPath) ? backupPath(configPath) : null;
|
|
132
169
|
info(`${bold(client.label)} reads MCP servers from a JSON config file.`);
|
|
133
170
|
console.log(` ${dim("file: ")} ${cyan(configPath)}`);
|
|
134
|
-
console.log(` ${dim("adds: ")} ${cyan(
|
|
135
|
-
if (
|
|
171
|
+
console.log(` ${dim("adds: ")} ${cyan(snippet)}`);
|
|
172
|
+
if (backup) console.log(dim(` Your current config is copied to ${backup} first.`));
|
|
136
173
|
|
|
137
174
|
if (!(await confirm("Proceed?"))) {
|
|
138
175
|
warn("Cancelled.");
|
|
139
176
|
return;
|
|
140
177
|
}
|
|
141
178
|
|
|
142
|
-
if (
|
|
179
|
+
if (backup) copyFileSync(configPath, backup);
|
|
143
180
|
else mkdirSync(dirname(configPath), { recursive: true });
|
|
144
181
|
config.mcpServers = { ...config.mcpServers, astralis: entry };
|
|
145
182
|
writeFileSync(configPath, JSON.stringify(config, null, 2) + "\n");
|
|
146
|
-
ok(`written${
|
|
183
|
+
ok(`written${backup ? dim(` (backup: ${backup})`) : ""}`);
|
|
147
184
|
info(client.reload);
|
|
148
185
|
}
|
package/src/commands/create.mjs
CHANGED
|
@@ -1,17 +1,18 @@
|
|
|
1
1
|
import { existsSync, writeFileSync, rmSync } from "node:fs";
|
|
2
2
|
import { join, relative } from "node:path";
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
3
|
+
import { ok, warn, info, fail, bold, cyan, dim, ask, isAssumeYes, menu, posix, printBanner } from "../lib/ui.mjs";
|
|
4
|
+
import { parseCommandArgs } from "../lib/args.mjs";
|
|
5
|
+
import { commandNamed } from "../lib/commands.mjs";
|
|
6
|
+
import { runCommand, UnsafeArgumentError } from "../lib/run.mjs";
|
|
5
7
|
import { findEntryFile } from "../lib/detect.mjs";
|
|
6
8
|
import { starterFile, starterCleanup } from "../lib/starter.mjs";
|
|
7
9
|
|
|
8
10
|
/**
|
|
9
11
|
* Scaffold a brand-new project and wire Astralis into it. We own NO project
|
|
10
|
-
* templates: the official scaffolder (create-next-app / create-vite) runs
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
* non-interactive use works too:
|
|
12
|
+
* templates: the official scaffolder (create-next-app / create-vite) runs with
|
|
13
|
+
* its own prompts, so TS, ESLint and Tailwind stay the user's call, and our
|
|
14
|
+
* `init` runs on the result. Unrecognised args are forwarded verbatim to the
|
|
15
|
+
* scaffolder:
|
|
15
16
|
*
|
|
16
17
|
* astralis create my-app --framework vite --template react
|
|
17
18
|
*/
|
|
@@ -28,27 +29,26 @@ const IMMEDIATE_FLAGS = new Set(["-i", "--immediate", "--no-immediate"]);
|
|
|
28
29
|
/** Did the caller pick their own template? */
|
|
29
30
|
const TEMPLATE_FLAGS = new Set(["-t", "--template"]);
|
|
30
31
|
|
|
32
|
+
/** Each scaffolder's own switch for "ask nothing", passed on under --yes. */
|
|
33
|
+
const NON_INTERACTIVE_FLAG = { next: "--yes", vite: "--no-interactive" };
|
|
34
|
+
|
|
31
35
|
/**
|
|
32
|
-
* Astralis is a React library
|
|
33
|
-
*
|
|
34
|
-
*
|
|
35
|
-
*
|
|
36
|
-
* steps later when wiring found no React.
|
|
37
|
-
*
|
|
38
|
-
* `--template` selects framework AND variant together, so naming one also
|
|
39
|
-
* skips create-vite's TypeScript/JavaScript question. That is a choice the
|
|
40
|
-
* caller still owns, so we ask it ourselves and map the answer here.
|
|
36
|
+
* Astralis is a React library, so the Vite path names a React template rather
|
|
37
|
+
* than letting create-vite offer Vue and friends (picking one scaffolded a
|
|
38
|
+
* project the library can't be used in). Naming the template also skips
|
|
39
|
+
* create-vite's TypeScript/JavaScript question, so we ask it and map it here.
|
|
41
40
|
*/
|
|
42
41
|
const VITE_TEMPLATES = { ts: "react-ts", js: "react" };
|
|
43
42
|
const DEFAULT_VITE_TEMPLATE = VITE_TEMPLATES.ts;
|
|
44
43
|
|
|
45
|
-
/**
|
|
44
|
+
/** The scaffolder's argv: [pm, "create", scaffolder, name, ...args]. */
|
|
46
45
|
export function buildScaffoldCommand(
|
|
47
46
|
framework,
|
|
48
47
|
name,
|
|
49
48
|
forwarded,
|
|
50
49
|
pm,
|
|
51
50
|
template = DEFAULT_VITE_TEMPLATE,
|
|
51
|
+
{ yes = false } = {},
|
|
52
52
|
) {
|
|
53
53
|
// yarn classic's `create` chokes on @latest tags; everyone else wants them.
|
|
54
54
|
const tag = pm === "yarn" ? "" : "@latest";
|
|
@@ -56,106 +56,88 @@ export function buildScaffoldCommand(
|
|
|
56
56
|
|
|
57
57
|
const args = [...forwarded];
|
|
58
58
|
|
|
59
|
-
// Name the template so create-vite never offers Vue, Svelte and friends.
|
|
60
|
-
// `template` carries the language the caller chose — see VITE_TEMPLATES.
|
|
61
59
|
if (framework === "vite" && !args.some((arg) => TEMPLATE_FLAGS.has(arg))) {
|
|
62
60
|
args.push("--template", template);
|
|
63
61
|
}
|
|
64
62
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
* so it would never run and the user would be handed a plain Vite app with no
|
|
69
|
-
* Astralis in it — which is exactly what happened. Opt out by default; a
|
|
70
|
-
* caller who passes -i/--immediate themselves has chosen to skip our wiring.
|
|
71
|
-
*/
|
|
63
|
+
// create-vite's "install and start now" runs a dev server that never exits,
|
|
64
|
+
// so our wiring step behind it would never run. Opt out unless the caller
|
|
65
|
+
// chose for themselves.
|
|
72
66
|
if (framework === "vite" && !args.some((arg) => IMMEDIATE_FLAGS.has(arg))) {
|
|
73
67
|
args.push("--no-immediate");
|
|
74
68
|
}
|
|
75
69
|
|
|
70
|
+
if (yes && !args.includes(NON_INTERACTIVE_FLAG[framework])) args.push(NON_INTERACTIVE_FLAG[framework]);
|
|
71
|
+
|
|
76
72
|
// npm swallows flags meant for the scaffolder unless they sit behind `--`.
|
|
77
73
|
const separator = pm === "npm" && args.length > 0 ? ["--"] : [];
|
|
78
|
-
return [pm, "create", scaffolder, name, ...separator, ...args]
|
|
74
|
+
return [pm, "create", scaffolder, name, ...separator, ...args];
|
|
79
75
|
}
|
|
80
76
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
);
|
|
87
|
-
const
|
|
77
|
+
function promptFramework() {
|
|
78
|
+
const chosen = menu("Which framework?", [
|
|
79
|
+
{ label: "Next.js", hint: "App Router; Server Components render with no library JavaScript" },
|
|
80
|
+
{ label: "React (Vite)", hint: "A client-rendered single-page app" },
|
|
81
|
+
]);
|
|
82
|
+
if (chosen !== null) return chosen === 0 ? "next" : "vite";
|
|
83
|
+
const answer = ask(`${bold("Framework?")} ${cyan("1")} Next.js ${cyan("2")} React ${dim("(Vite)")} `);
|
|
84
|
+
if (answer === null) fail(`No framework chosen and no input to read — pass ${cyan("--framework next|vite")}.`);
|
|
85
|
+
const choice = answer.toLowerCase();
|
|
88
86
|
if (choice === "1" || choice === "next" || choice === "next.js" || choice === "nextjs") return "next";
|
|
89
87
|
if (choice === "2" || choice === "vite" || choice === "react") return "vite";
|
|
90
88
|
return null;
|
|
91
89
|
}
|
|
92
90
|
|
|
93
|
-
/**
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
async function promptLanguage() {
|
|
100
|
-
const answer = await ask(
|
|
101
|
-
`${bold("Language?")} ${cyan("1")} TypeScript ${cyan("2")} JavaScript `,
|
|
102
|
-
);
|
|
103
|
-
const choice = answer.trim().toLowerCase();
|
|
104
|
-
if (choice === "2" || choice === "js" || choice === "javascript") return VITE_TEMPLATES.js;
|
|
105
|
-
return VITE_TEMPLATES.ts;
|
|
91
|
+
/** create-vite's own language question; anything unrecognised, or no answer, means TypeScript. */
|
|
92
|
+
function promptLanguage() {
|
|
93
|
+
const chosen = menu("Which language?", [{ label: "TypeScript", hint: "Recommended: the props are typed" }, { label: "JavaScript" }]);
|
|
94
|
+
if (chosen !== null) return chosen === 1 ? VITE_TEMPLATES.js : VITE_TEMPLATES.ts;
|
|
95
|
+
const answer = ask(`${bold("Language?")} ${cyan("1")} TypeScript ${cyan("2")} JavaScript `)?.toLowerCase();
|
|
96
|
+
return answer === "2" || answer === "js" || answer === "javascript" ? VITE_TEMPLATES.js : VITE_TEMPLATES.ts;
|
|
106
97
|
}
|
|
107
98
|
|
|
108
99
|
export async function run(argv) {
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
if (arg === "--framework") framework = argv[++i];
|
|
118
|
-
// Ours, not the scaffolder's — consume it so it isn't forwarded.
|
|
119
|
-
else if (arg === "--no-starter") starter = false;
|
|
120
|
-
else if (!name && !arg.startsWith("-")) name = arg;
|
|
121
|
-
else forwarded.push(arg);
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
if (!name) fail('Usage: astralis create <project-name> [--framework next|vite] [scaffolder args…]');
|
|
100
|
+
const args = parseCommandArgs(commandNamed("create"), argv);
|
|
101
|
+
if (!args) return;
|
|
102
|
+
const { values, forwarded } = args;
|
|
103
|
+
const [name] = args.positionals;
|
|
104
|
+
const yes = isAssumeYes();
|
|
105
|
+
|
|
106
|
+
if (!name) fail(`Name the project: ${cyan("astralis create <name>")}.`);
|
|
107
|
+
printBanner(`Creating ${name}`);
|
|
125
108
|
if (!/^[a-z0-9@][a-z0-9-_.]*$/i.test(name)) fail(`"${name}" isn't a safe project name.`);
|
|
126
109
|
if (existsSync(join(process.cwd(), name))) fail(`./${name} already exists.`);
|
|
127
110
|
|
|
128
|
-
framework
|
|
111
|
+
let framework = values.framework;
|
|
112
|
+
if (framework === undefined) {
|
|
113
|
+
if (yes) fail(`--yes can't choose the framework — pass ${cyan("--framework next|vite")}.`);
|
|
114
|
+
framework = promptFramework();
|
|
115
|
+
}
|
|
129
116
|
if (framework !== "next" && framework !== "vite") fail('Pick a framework: "next" or "vite".');
|
|
130
117
|
|
|
131
|
-
|
|
132
|
-
* Ask the language only when we are the ones naming the template, and only
|
|
133
|
-
* when there is somebody to answer: a caller who passed their own
|
|
134
|
-
* `--template` has already decided, and a non-interactive run (CI, piped
|
|
135
|
-
* input) must not block on a question. Both fall through to TypeScript.
|
|
136
|
-
*/
|
|
118
|
+
// Only ask the language when we name the template and someone can answer.
|
|
137
119
|
const namesOwnTemplate = forwarded.some((arg) => TEMPLATE_FLAGS.has(arg));
|
|
138
120
|
const template =
|
|
139
|
-
framework === "vite" && !namesOwnTemplate && process.stdin.isTTY
|
|
140
|
-
? await promptLanguage()
|
|
141
|
-
: DEFAULT_VITE_TEMPLATE;
|
|
121
|
+
framework === "vite" && !namesOwnTemplate && !yes && process.stdin.isTTY ? promptLanguage() : DEFAULT_VITE_TEMPLATE;
|
|
142
122
|
|
|
143
123
|
const pm = invokingPackageManager();
|
|
144
|
-
const
|
|
124
|
+
const [bin, ...scaffoldArgs] = buildScaffoldCommand(framework, name, forwarded, pm, template, { yes });
|
|
145
125
|
|
|
146
|
-
|
|
147
|
-
// process never gets to wire anything. Say so rather than appear to fail.
|
|
148
|
-
const startsDevServer = forwarded.some((arg) => arg === "-i" || arg === "--immediate");
|
|
149
|
-
if (startsDevServer) {
|
|
126
|
+
if (forwarded.some((arg) => arg === "-i" || arg === "--immediate")) {
|
|
150
127
|
warn("--immediate starts a dev server that doesn't exit, so Astralis can't be wired in afterwards.");
|
|
151
128
|
console.log(dim(` Run ${cyan("astralis init")} inside ./${name} once you've stopped it.`));
|
|
152
129
|
}
|
|
153
|
-
info(`${bold(
|
|
154
|
-
|
|
155
|
-
// The scaffolder's
|
|
156
|
-
//
|
|
157
|
-
|
|
158
|
-
|
|
130
|
+
info(`${bold([bin, ...scaffoldArgs].join(" "))} ${dim("(the scaffolder's own prompts — answer them as usual)")}`);
|
|
131
|
+
|
|
132
|
+
// The scaffolder's arrow-key menu needs the console to itself, which holds
|
|
133
|
+
// because this process has never read stdin (see lib/prompt-child.mjs).
|
|
134
|
+
let scaffold;
|
|
135
|
+
try {
|
|
136
|
+
scaffold = runCommand(bin, scaffoldArgs);
|
|
137
|
+
} catch (error) {
|
|
138
|
+
if (error instanceof UnsafeArgumentError) fail(`Couldn't forward that to the scaffolder: ${error.message}.`);
|
|
139
|
+
throw error;
|
|
140
|
+
}
|
|
159
141
|
if (scaffold.status !== 0) fail("Scaffolding failed or was cancelled — nothing to wire up.");
|
|
160
142
|
if (!existsSync(join(process.cwd(), name, "package.json"))) {
|
|
161
143
|
fail(`Expected ./${name}/package.json after scaffolding — did the scaffolder use a different directory?`);
|
|
@@ -166,29 +148,20 @@ export async function run(argv) {
|
|
|
166
148
|
info("wiring Astralis into it…");
|
|
167
149
|
process.chdir(name);
|
|
168
150
|
const init = await import("./init.mjs");
|
|
169
|
-
await init.run([], { nested: true });
|
|
151
|
+
await init.run(values["no-devtools"] ? [] : ["--devtools"], { nested: true, pm });
|
|
170
152
|
|
|
171
153
|
/*
|
|
172
|
-
* Replace the scaffolder's demo page with one built from Astralis.
|
|
173
|
-
*
|
|
174
|
-
*
|
|
175
|
-
* where overwriting someone's entry component would be vandalism. On a
|
|
176
|
-
* directory the scaffolder created seconds ago there is nothing to lose,
|
|
177
|
-
* and it turns "did init work?" from a package.json inspection into
|
|
178
|
-
* something visible on first `dev`.
|
|
154
|
+
* Replace the scaffolder's demo page with one built from Astralis. Only
|
|
155
|
+
* here, never in `init`: on a directory scaffolded seconds ago there is
|
|
156
|
+
* nothing to lose, and it makes "did init work?" visible on first `dev`.
|
|
179
157
|
*/
|
|
180
|
-
if (starter) {
|
|
158
|
+
if (!values["no-starter"]) {
|
|
181
159
|
const entry = findEntryFile(process.cwd(), framework);
|
|
182
160
|
if (entry) {
|
|
183
|
-
const shown = (p) => relative(process.cwd(), p).split("\\").join("/");
|
|
184
|
-
|
|
185
161
|
const { path, source } = starterFile(framework, entry.path);
|
|
186
162
|
writeFileSync(path, source);
|
|
187
163
|
|
|
188
|
-
// The template's demo CSS centres and re-colours everything it
|
|
189
|
-
// it has to go with the demo it was written for. Reported as one line:
|
|
190
|
-
// clearing the scaffolder's own leftovers is part of writing the screen,
|
|
191
|
-
// not three separate things the reader has to follow.
|
|
164
|
+
// The template's demo CSS centres and re-colours everything, so it goes with its demo.
|
|
192
165
|
const cleanup = starterCleanup(framework, entry.path);
|
|
193
166
|
for (const file of cleanup.rewrite) {
|
|
194
167
|
if (existsSync(file.path)) writeFileSync(file.path, file.source);
|
|
@@ -197,7 +170,7 @@ export async function run(argv) {
|
|
|
197
170
|
if (existsSync(orphan)) rmSync(orphan);
|
|
198
171
|
}
|
|
199
172
|
|
|
200
|
-
ok(`welcome screen written ${dim(`(${
|
|
173
|
+
ok(`welcome screen written ${dim(`(${posix(relative(process.cwd(), path))})`)}`);
|
|
201
174
|
}
|
|
202
175
|
}
|
|
203
176
|
|
package/src/commands/init.mjs
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
|
-
import { parseArgs } from "node:util";
|
|
2
1
|
import { readFileSync, writeFileSync } from "node:fs";
|
|
3
|
-
import { spawnSync } from "node:child_process";
|
|
4
2
|
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";
|
|
3
|
+
import { addImports, wrapJsx, addTagAttribute, insertAfterBodyTag, addStatementAfterImports, parses } from "../lib/edits.mjs";
|
|
4
|
+
import { ok, warn, info, fail, bold, dim, cyan, printBanner } from "../lib/ui.mjs";
|
|
5
|
+
import { parseCommandArgs } from "../lib/args.mjs";
|
|
6
|
+
import { commandNamed } from "../lib/commands.mjs";
|
|
7
|
+
import { runCommand } from "../lib/run.mjs";
|
|
8
|
+
import { setUpCi } from "../lib/ci-setup.mjs";
|
|
9
|
+
import { devtoolsLayoutEdits, setUpDevtools } from "../lib/devtools-setup.mjs";
|
|
7
10
|
|
|
8
11
|
const PROVIDER_IMPORT = 'import { AstralisProvider } from "astralis-ui";';
|
|
9
12
|
const STYLES_IMPORT = 'import "astralis-ui/styles.css";';
|
|
@@ -19,20 +22,23 @@ const MANUAL_SNIPPET = `
|
|
|
19
22
|
1. ${PROVIDER_IMPORT}
|
|
20
23
|
2. ${STYLES_IMPORT} ${dim("(once, at your entry point)")}
|
|
21
24
|
3. Wrap your app: <AstralisProvider defaultTheme="system">…</AstralisProvider>
|
|
22
|
-
Full guide: ${cyan("https://astralis-
|
|
25
|
+
Full guide: ${cyan("https://astralis-ui.com/docs/installation")}`;
|
|
23
26
|
|
|
24
27
|
/**
|
|
25
28
|
* `nested` is set when `create` calls this as its wiring step. It suppresses
|
|
26
29
|
* the sign-off, because create prints its own "Next steps" immediately after —
|
|
27
|
-
* two closing blocks in one run read as the command finishing twice.
|
|
30
|
+
* two closing blocks in one run read as the command finishing twice. `pm` is
|
|
31
|
+
* create's package manager: a fresh scaffold has no lockfile to detect it from.
|
|
28
32
|
*/
|
|
29
|
-
export async function run(argv, { nested = false } = {}) {
|
|
30
|
-
const
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
const
|
|
33
|
+
export async function run(argv, { nested = false, pm: givenPm } = {}) {
|
|
34
|
+
const args = parseCommandArgs(commandNamed("init"), argv);
|
|
35
|
+
if (!args) return;
|
|
36
|
+
const dryRun = args.values["dry-run"];
|
|
37
|
+
const ci = args.values.ci;
|
|
38
|
+
const devtools = args.values.devtools;
|
|
39
|
+
if (args.values.force && !ci) fail("--force only applies with --ci (it replaces an existing workflow).");
|
|
35
40
|
const cwd = process.cwd();
|
|
41
|
+
if (!nested) printBanner(ci ? "Setting up validation in CI" : devtools ? "Adding the DevTools" : "Adding Astralis UI to this project");
|
|
36
42
|
|
|
37
43
|
const pkg = readPackageJson(cwd);
|
|
38
44
|
if (!pkg) fail("No package.json here — run `astralis init` from your project root.");
|
|
@@ -54,7 +60,7 @@ export async function run(argv, { nested = false } = {}) {
|
|
|
54
60
|
console.log(MANUAL_SNIPPET);
|
|
55
61
|
return;
|
|
56
62
|
}
|
|
57
|
-
const pm = detectPackageManager(cwd);
|
|
63
|
+
const pm = givenPm ?? detectPackageManager(cwd);
|
|
58
64
|
|
|
59
65
|
/*
|
|
60
66
|
* Step output is buffered until we know whether anything actually changed.
|
|
@@ -66,6 +72,7 @@ export async function run(argv, { nested = false } = {}) {
|
|
|
66
72
|
* a package-manager install still streams in order.
|
|
67
73
|
*/
|
|
68
74
|
const steps = [];
|
|
75
|
+
const afterwards = [];
|
|
69
76
|
let printing = false;
|
|
70
77
|
let changed = false;
|
|
71
78
|
const step = (fn) => (printing ? fn() : steps.push(fn));
|
|
@@ -74,6 +81,7 @@ export async function run(argv, { nested = false } = {}) {
|
|
|
74
81
|
steps.length = 0;
|
|
75
82
|
printing = true;
|
|
76
83
|
};
|
|
84
|
+
const out = { step, flush, changed: () => (changed = true), after: (fn) => afterwards.push(fn) };
|
|
77
85
|
|
|
78
86
|
step(() =>
|
|
79
87
|
info(
|
|
@@ -92,9 +100,7 @@ export async function run(argv, { nested = false } = {}) {
|
|
|
92
100
|
changed = true;
|
|
93
101
|
flush();
|
|
94
102
|
info(`installing astralis-ui with ${pm}…`);
|
|
95
|
-
|
|
96
|
-
// and shell+args-array triggers Node's unescaped-args deprecation.
|
|
97
|
-
const result = spawnSync(`${pm} add astralis-ui`, { stdio: "inherit", shell: true });
|
|
103
|
+
const result = runCommand(pm, ["add", "astralis-ui"]);
|
|
98
104
|
if (result.status !== 0) fail(`${pm} add astralis-ui failed — install it manually, then re-run init.`);
|
|
99
105
|
ok("astralis-ui installed");
|
|
100
106
|
}
|
|
@@ -102,8 +108,15 @@ export async function run(argv, { nested = false } = {}) {
|
|
|
102
108
|
// 2. Entry file
|
|
103
109
|
const entry = findEntryFile(cwd, framework);
|
|
104
110
|
if (!entry) {
|
|
111
|
+
flush();
|
|
105
112
|
warn(`Couldn't find your ${framework === "next" ? "root layout" : "src/main.tsx"} — wire the provider manually:`);
|
|
106
113
|
console.log(MANUAL_SNIPPET);
|
|
114
|
+
const extra = [
|
|
115
|
+
...(devtools ? setUpDevtools({ cwd, pkg, pm, framework, entry: null, dryRun, out }) : []),
|
|
116
|
+
...(ci ? setUpCi({ cwd, pkg, pm, dryRun, force: args.values.force, out }) : []),
|
|
117
|
+
];
|
|
118
|
+
for (const note of extra) warn(note);
|
|
119
|
+
for (const fn of afterwards) fn();
|
|
107
120
|
return;
|
|
108
121
|
}
|
|
109
122
|
|
|
@@ -139,31 +152,55 @@ export async function run(argv, { nested = false } = {}) {
|
|
|
139
152
|
apply(addStatementAfterImports(source, THEME_INIT_CONST), "anti-flash theme script const");
|
|
140
153
|
apply(insertAfterBodyTag(source, THEME_INIT_SCRIPT), "theme script injected after <body>");
|
|
141
154
|
apply(addTagAttribute(source, "html", "suppressHydrationWarning"), "suppressHydrationWarning on <html>");
|
|
155
|
+
if (devtools) {
|
|
156
|
+
const [imported, placed] = devtoolsLayoutEdits(source);
|
|
157
|
+
apply(imported, "DevTools import added");
|
|
158
|
+
apply(placed, "<AstralisDevTools /> placed before </body>");
|
|
159
|
+
}
|
|
142
160
|
}
|
|
143
161
|
|
|
144
|
-
// 3. Write
|
|
162
|
+
// 3. Write — never a file that no longer parses; hand over to the manual steps instead.
|
|
163
|
+
if (source !== original && !parses(source)) {
|
|
164
|
+
source = original;
|
|
165
|
+
notes.push(`${entry.relative}: the edits would leave it unparseable, so nothing was written`);
|
|
166
|
+
}
|
|
145
167
|
if (source !== original) {
|
|
146
168
|
if (dryRun) step(() => info(`would update ${entry.relative}`));
|
|
147
169
|
else writeFileSync(entry.path, source);
|
|
148
170
|
}
|
|
149
171
|
|
|
172
|
+
// 4. DevTools and CI
|
|
173
|
+
const wiringNotes = notes.length;
|
|
174
|
+
if (devtools) notes.push(...setUpDevtools({ cwd, pkg, pm, framework, entry, dryRun, out }));
|
|
175
|
+
if (ci) notes.push(...setUpCi({ cwd, pkg, pm, dryRun, force: args.values.force, out }));
|
|
176
|
+
|
|
150
177
|
/*
|
|
151
178
|
* Nothing to do, and nothing that needed the reader's attention: say so in
|
|
152
179
|
* one line instead of listing the steps that were already satisfied.
|
|
153
180
|
*/
|
|
154
181
|
if (!changed && notes.length === 0) {
|
|
155
|
-
|
|
182
|
+
const extras = [devtools && "the DevTools", ci && "its CI check"].filter(Boolean);
|
|
183
|
+
ok(
|
|
184
|
+
extras.length
|
|
185
|
+
? `astralis-ui and ${extras.join(" and ")} are already set up in this project`
|
|
186
|
+
: "astralis-ui is already present in this project",
|
|
187
|
+
);
|
|
156
188
|
if (nested) return;
|
|
157
|
-
console.log(`Nothing to scaffold.\nDocs: ${cyan("https://astralis-
|
|
189
|
+
console.log(`Nothing to scaffold.\nDocs: ${cyan("https://astralis-ui.com/docs")}`);
|
|
158
190
|
return;
|
|
159
191
|
}
|
|
160
192
|
|
|
161
193
|
flush();
|
|
162
194
|
|
|
163
195
|
for (const note of notes) warn(note);
|
|
164
|
-
if (
|
|
196
|
+
if (wiringNotes > 0) console.log(MANUAL_SNIPPET);
|
|
197
|
+
for (const fn of afterwards) fn();
|
|
165
198
|
|
|
166
|
-
if (nested) return;
|
|
199
|
+
if (nested || !changed) return;
|
|
167
200
|
|
|
168
|
-
|
|
201
|
+
if (dryRun) {
|
|
202
|
+
console.log(`\n${bold("Dry run:")} nothing was written. Run ${cyan(["astralis init", devtools && "--devtools", ci && "--ci"].filter(Boolean).join(" "))} to apply it.`);
|
|
203
|
+
return;
|
|
204
|
+
}
|
|
205
|
+
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-ui.com/docs")}`);
|
|
169
206
|
}
|