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.
Files changed (42) hide show
  1. package/CHANGELOG.md +179 -0
  2. package/LICENSE +21 -0
  3. package/README.md +179 -29
  4. package/package.json +29 -10
  5. package/src/commands/add.mjs +48 -71
  6. package/src/commands/connect-mcp.mjs +66 -29
  7. package/src/commands/create.mjs +73 -100
  8. package/src/commands/init.mjs +59 -22
  9. package/src/commands/report.mjs +257 -0
  10. package/src/commands/theme.mjs +77 -104
  11. package/src/commands/validate.mjs +101 -0
  12. package/src/index.mjs +26 -48
  13. package/src/lib/args.mjs +135 -0
  14. package/src/lib/aria.mjs +40 -0
  15. package/src/lib/children-rule.mjs +23 -0
  16. package/src/lib/ci-setup.mjs +78 -0
  17. package/src/lib/ci.mjs +155 -0
  18. package/src/lib/commands.mjs +214 -0
  19. package/src/lib/css-value.mjs +141 -0
  20. package/src/lib/devtools-setup.mjs +142 -0
  21. package/src/lib/edits.mjs +206 -26
  22. package/src/lib/github-annotations.mjs +26 -0
  23. package/src/lib/github.d.mts +72 -0
  24. package/src/lib/github.mjs +339 -0
  25. package/src/lib/library.mjs +40 -0
  26. package/src/lib/menu.mjs +59 -0
  27. package/src/lib/prompt-child.mjs +85 -39
  28. package/src/lib/registry.mjs +37 -4
  29. package/src/lib/report-tree.mjs +134 -0
  30. package/src/lib/run.mjs +39 -0
  31. package/src/lib/snapshot.d.mts +65 -0
  32. package/src/lib/snapshot.mjs +371 -0
  33. package/src/lib/source-files.mjs +29 -0
  34. package/src/lib/spec-load.mjs +32 -0
  35. package/src/lib/spec-resolve.mjs +20 -0
  36. package/src/lib/starter.mjs +11 -19
  37. package/src/lib/suggest.mjs +32 -0
  38. package/src/lib/ui.mjs +78 -36
  39. package/src/lib/validate-core.d.mts +48 -0
  40. package/src/lib/validate-core.mjs +655 -0
  41. package/src/lib/validate.d.mts +33 -0
  42. package/src/lib/validate.mjs +8 -0
@@ -0,0 +1,20 @@
1
+ import { statSync } from "node:fs";
2
+ import { join, dirname, resolve } from "node:path";
3
+
4
+ /**
5
+ * Walk upward for node_modules/astralis-ui/<file>: the CONSUMER's copy, the
6
+ * version their code must conform to, not this tool's own dependency.
7
+ * (require.resolve can't be used: the library's exports carry no "require"
8
+ * condition.) Returns the file's path, or null.
9
+ */
10
+ export function findInstalledLibrary(startDir, file = "package.json") {
11
+ for (let dir = resolve(startDir); ; dir = dirname(dir)) {
12
+ const candidate = join(dir, "node_modules", "astralis-ui", file);
13
+ if (statSync(candidate, { throwIfNoEntry: false })) return candidate;
14
+ if (dirname(dir) === dir) return null;
15
+ }
16
+ }
17
+
18
+ export function findInstalledSpec(startDir) {
19
+ return findInstalledLibrary(startDir, join("dist", "system-spec.json"));
20
+ }
@@ -53,26 +53,18 @@ function starterSource() {
53
53
  * It is an ordinary file in your project. Edit it, or delete it and start over.
54
54
  */
55
55
  /**
56
- * The Astralis mark — an eight-point compass rose. Painted with the brand
57
- * token rather than fixed hex, so it re-colours with the rest of the theme.
56
+ * The Astralis mark — four blocks, the last still being placed. Painted with
57
+ * the brand token rather than fixed hex, so it re-colours with the theme.
58
58
  */
59
- const ROTATIONS = [0, 90, 180, 270];
60
-
61
59
  function AstralisMark() {
62
60
  return (
63
- <svg viewBox="0 0 32 32" width="28" height="28" aria-hidden="true">
64
- {ROTATIONS.map((deg) => (
65
- <g key={\`d\${deg}\`} transform={\`rotate(\${deg} 16 16)\`}>
66
- <path d="M22.8 9.2 L17.7 11.1 L16 16 Z" fill="var(--astralis-color-brand-500)" />
67
- <path d="M22.8 9.2 L20.9 14.3 L16 16 Z" fill="var(--astralis-color-brand-300)" />
68
- </g>
69
- ))}
70
- {ROTATIONS.map((deg) => (
71
- <g key={\`c\${deg}\`} transform={\`rotate(\${deg} 16 16)\`}>
72
- <path d="M16 0.8 L14.15 12.4 L16 16 Z" fill="var(--astralis-color-brand-200)" />
73
- <path d="M16 0.8 L17.85 12.4 L16 16 Z" fill="var(--astralis-color-brand-500)" />
74
- </g>
75
- ))}
61
+ <svg viewBox="0 0 32 32" width="28" height="28" aria-hidden="true" style={{ overflow: "visible" }}>
62
+ <g fill="var(--astralis-color-brand-500)">
63
+ <rect x="3" y="17" width="12" height="12" rx="3" />
64
+ <rect x="17" y="17" width="12" height="12" rx="3" />
65
+ <rect x="3" y="3" width="12" height="12" rx="3" />
66
+ <rect x="19" y="1" width="12" height="12" rx="3" opacity="0.6" transform="rotate(14 25 7)" />
67
+ </g>
76
68
  </svg>
77
69
  );
78
70
  }
@@ -107,7 +99,7 @@ export default function Welcome() {
107
99
  <Flex gap="3" wrap="wrap">
108
100
  <Button
109
101
  as="a"
110
- href="https://astralis-zeta.vercel.app/docs/quick-start"
102
+ href="https://astralis-ui.com/docs/quick-start"
111
103
  target="_blank"
112
104
  rel="noreferrer"
113
105
  size="lg"
@@ -117,7 +109,7 @@ export default function Welcome() {
117
109
  </Button>
118
110
  <Button
119
111
  as="a"
120
- href="https://astralis-zeta.vercel.app/docs"
112
+ href="https://astralis-ui.com/docs"
121
113
  target="_blank"
122
114
  rel="noreferrer"
123
115
  size="lg"
@@ -0,0 +1,32 @@
1
+ /* Pure and dependency-free: validate-core runs in the browser too. */
2
+
3
+ /** Levenshtein distance, giving up (returning `cap`) once it can't come in under it. */
4
+ export function editDistance(a, b, cap = Infinity) {
5
+ let prev = Array.from({ length: b.length + 1 }, (_, i) => i);
6
+ for (let i = 1; i <= a.length; i++) {
7
+ const row = [i];
8
+ let rowMin = i;
9
+ for (let j = 1; j <= b.length; j++) {
10
+ row[j] = Math.min(prev[j] + 1, row[j - 1] + 1, prev[j - 1] + (a[i - 1] === b[j - 1] ? 0 : 1));
11
+ if (row[j] < rowMin) rowMin = row[j];
12
+ }
13
+ if (rowMin >= cap) return cap;
14
+ prev = row;
15
+ }
16
+ return Math.min(prev[b.length], cap);
17
+ }
18
+
19
+ /** The nearest candidate within edit distance 2, for "did you mean" hints; null if none. */
20
+ export function suggest(value, candidates) {
21
+ let best = null;
22
+ let bestDistance = 3;
23
+ for (const candidate of candidates) {
24
+ if (Math.abs(candidate.length - value.length) >= bestDistance) continue;
25
+ const distance = editDistance(value, candidate, bestDistance);
26
+ if (distance < bestDistance) {
27
+ bestDistance = distance;
28
+ best = candidate;
29
+ }
30
+ }
31
+ return best;
32
+ }
package/src/lib/ui.mjs CHANGED
@@ -1,10 +1,9 @@
1
1
  import { spawnSync } from "node:child_process";
2
+ import { readFileSync } from "node:fs";
2
3
  import { fileURLToPath } from "node:url";
4
+ import { banner } from "./menu.mjs";
3
5
 
4
- /**
5
- * Terminal output helpers — raw ANSI, no dependencies. Colors switch off for
6
- * non-TTY output (piped) and when NO_COLOR is set, per convention.
7
- */
6
+ /** Terminal output helpers: raw ANSI, off when piped or when NO_COLOR is set. */
8
7
 
9
8
  const useColor = process.stdout.isTTY && !process.env.NO_COLOR;
10
9
  const wrap = (open, close) => (text) => (useColor ? `\x1b[${open}m${text}\x1b[${close}m` : String(text));
@@ -20,17 +19,26 @@ export const ok = (message) => console.log(`${green("✓")} ${message}`);
20
19
  export const warn = (message) => console.log(`${yellow("!")} ${message}`);
21
20
  export const info = (message) => console.log(`${cyan("→")} ${message}`);
22
21
 
23
- /** Thrown by fail() once the message is printed — index.mjs stops quietly on it. */
22
+ /**
23
+ * One line: the CLI's name and version, and what this command is
24
+ * about to do. In a terminal only: piped output and CI logs stay as they were.
25
+ */
26
+ export function printBanner(subtitle) {
27
+ if (!process.stdout.isTTY) return;
28
+ const { version } = JSON.parse(readFileSync(new URL("../../package.json", import.meta.url), "utf8"));
29
+ console.log(banner({ version, subtitle, color: useColor }));
30
+ }
31
+
32
+ /** Forward slashes on every platform: shown paths and import specifiers both want them. */
33
+ export const posix = (path) => path.split("\\").join("/");
34
+
35
+ /** Thrown by fail() once the message is printed; index.mjs stops quietly on it. */
24
36
  export class CliExit extends Error {}
25
37
 
26
38
  /**
27
- * Print the error and stop — the CLI's only failure path.
28
- *
29
- * Sets an exit code and unwinds rather than calling process.exit(): on Windows,
30
- * process.exit() after any fetch() trips a libuv assertion
31
- * (`!(handle->flags & UV_HANDLE_CLOSING)`) and reports exit 127 instead of 1.
32
- * Verified in isolation — fetch alone is clean, exit alone is clean, only the
33
- * pair crashes. Unwinding lets the event loop drain, so finally blocks run.
39
+ * Print the error and stop: the CLI's only failure path. It unwinds instead of
40
+ * calling process.exit(), which on Windows trips a libuv assertion (exit 127)
41
+ * when a fetch() handle is still open.
34
42
  */
35
43
  export function fail(message) {
36
44
  console.error(`${red("✗")} ${message}`);
@@ -38,45 +46,79 @@ export function fail(message) {
38
46
  throw new CliExit(message);
39
47
  }
40
48
 
49
+ /* ---- prompts ------------------------------------------------------------------ */
50
+
51
+ let assumeYes = false;
52
+
53
+ /** Set by the global -y/--yes: no prompt is shown and each takes its default. */
54
+ export function setAssumeYes(value) {
55
+ assumeYes = value;
56
+ }
57
+
58
+ export const isAssumeYes = () => assumeYes;
59
+
41
60
  const PROMPT_CHILD = fileURLToPath(new URL("./prompt-child.mjs", import.meta.url));
42
61
 
43
62
  /**
44
- * Read one line from the user, in a child process that exits immediately.
45
- *
46
- * This CLI must never read its own stdin. On Windows libuv services a TTY on a
47
- * dedicated thread blocked in ReadConsoleInputW, and once started that thread
48
- * cannot be stopped — not by readline.close(), not by pause(), not by dropping
49
- * listeners. A process that has read stdin even once keeps consuming console
50
- * input records for the rest of its life, starving any interactive child it
51
- * later spawns. `astralis create` asked which framework, then handed over to
52
- * create-vite, whose arrow-key menu drew perfectly and never received a
53
- * keystroke; Ctrl+C did not reach it either.
54
- *
55
- * Isolated in a child, the read thread dies when the child exits and the
56
- * terminal is clean for the scaffolder. Piped input still works: the child
57
- * inherits the same stdin, and each prompt consumes one line.
63
+ * Read one line from the user, or null when there is no one to answer (stdin
64
+ * at EOF, as in CI). Reads in a child process so this one never touches stdin;
65
+ * prompt-child.mjs explains why.
58
66
  */
59
67
  export function ask(promptText) {
60
68
  const result = spawnSync(process.execPath, [PROMPT_CHILD, promptText], {
61
69
  stdio: ["inherit", "pipe", "inherit"],
62
70
  encoding: "utf8",
63
71
  });
64
- return (result.stdout ?? "").trim();
72
+ if (result.status !== 0) {
73
+ if (result.status === 3) process.stderr.write("\n");
74
+ return null;
75
+ }
76
+ return result.stdout.trim();
77
+ }
78
+
79
+ const noAnswer = (question, hint) =>
80
+ fail(`No answer to "${question}": there is no input to read.${hint ? ` ${hint}` : ""}`);
81
+
82
+ /**
83
+ * An arrow-key menu in a terminal, run in the prompt child like ask(). The
84
+ * chosen index; null when there is no terminal to draw it in. Cancelling
85
+ * stops the command.
86
+ */
87
+ export function menu(question, options, index = 0) {
88
+ if (!process.stdin.isTTY || !process.stdout.isTTY) return null;
89
+ const spec = JSON.stringify({ question, options: options.map(({ label, hint }) => ({ label, hint })), index, color: useColor });
90
+ const result = spawnSync(process.execPath, [PROMPT_CHILD, "--menu", spec], {
91
+ stdio: ["inherit", "pipe", "inherit"],
92
+ encoding: "utf8",
93
+ });
94
+ if (result.status === 130) fail("Cancelled.");
95
+ if (result.status !== 0) return null;
96
+ return Number(result.stdout.trim());
65
97
  }
66
98
 
67
- /** y/N prompt; default no. */
68
- export async function confirm(question) {
69
- const answer = await ask(`${question} ${dim("(y/N)")} `);
70
- return /^y(es)?$/i.test(answer.trim());
99
+ /** Yes or no; default no. Always yes under --yes. `hint` says how to answer non-interactively. */
100
+ export async function confirm(question, { hint = "Pass --yes to proceed without prompts." } = {}) {
101
+ if (assumeYes) return true;
102
+ const chosen = menu(question, [{ label: "Yes" }, { label: "No" }], 1);
103
+ if (chosen !== null) return chosen === 0;
104
+ const answer = ask(`${question} ${dim("(y/N)")} `);
105
+ if (answer === null) noAnswer(question, hint);
106
+ return /^y(es)?$/i.test(answer);
71
107
  }
72
108
 
73
109
  /**
74
- * Numbered single-choice prompt. `options` is [{ value, label }].
75
- * Returns the chosen value, or null if the answer isn't a listed number.
110
+ * Single-choice prompt. `options` is [{ value, label, hint? }]. An arrow-key
111
+ * menu in a terminal; a numbered prompt otherwise, which returns null if the
112
+ * answer isn't a listed number. Under --yes, or with no input, it stops with
113
+ * `hint`: a choice has no safe default.
76
114
  */
77
- export async function select(question, options) {
115
+ export async function select(question, options, { hint } = {}) {
116
+ if (assumeYes) fail(`"${question}" needs an answer, and --yes can't pick one.${hint ? ` ${hint}` : ""}`);
117
+ const chosen = menu(question, options);
118
+ if (chosen !== null) return options[chosen]?.value ?? null;
78
119
  console.log(question);
79
120
  options.forEach((o, i) => console.log(` ${cyan(String(i + 1))} ${o.label}`));
80
- const answer = await ask(`${dim("Enter a number:")} `);
81
- return options[Number(answer.trim()) - 1]?.value ?? null;
121
+ const answer = ask(`${dim("Enter a number:")} `);
122
+ if (answer === null) noAnswer(question, hint);
123
+ return options[Number(answer) - 1]?.value ?? null;
82
124
  }
@@ -0,0 +1,48 @@
1
+ /** One finding. `line` is 1-based, `column` 1-based; both are 0 when a position is unknown. */
2
+ export interface Finding {
3
+ /** Stable rule id, e.g. "unknown-import", "invalid-keyword-value", "missing-alt". */
4
+ code: string;
5
+ file: string;
6
+ line: number;
7
+ column: number;
8
+ message: string;
9
+ /** The astralis-ui component the finding is on, as the page names it ("Card.Body"); absent for imports and plain elements. */
10
+ component?: string;
11
+ /** The attribute the finding is on, when it is on one. */
12
+ prop?: string;
13
+ /** That attribute's literal value, when it is a literal. */
14
+ value?: string | number;
15
+ }
16
+
17
+ export interface ValidationResult {
18
+ /** Mistakes that render wrong or not at all. Sorted by position. */
19
+ errors: Finding[];
20
+ /** Doctrine nudges and claims file-local analysis can't prove. Sorted by position. */
21
+ warnings: Finding[];
22
+ }
23
+
24
+ export interface ValidateOptions {
25
+ /** Also warn on valid colours that bypass the tokens (the drift check). Default false. */
26
+ strictTokens?: boolean;
27
+ }
28
+
29
+ declare const prepared: unique symbol;
30
+
31
+ /** Lookup tables built from one spec by prepareSpec(); reuse it across files. */
32
+ export interface PreparedSpec {
33
+ readonly [prepared]: true;
34
+ }
35
+
36
+ /**
37
+ * Build the lookup tables for one parsed astralis-ui `dist/system-spec.json`.
38
+ * Takes `unknown` because the spec arrives from disk or the network as JSON.
39
+ */
40
+ export function prepareSpec(spec: unknown): PreparedSpec;
41
+
42
+ /** Validate one TSX/JSX source. Pure, and safe to run in a browser. */
43
+ export function validateSource(
44
+ source: string,
45
+ prepared: PreparedSpec,
46
+ filePath?: string,
47
+ opts?: ValidateOptions,
48
+ ): ValidationResult;