@collectui/cli 0.2.0 → 0.2.2
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 +18 -12
- package/package.json +1 -1
- package/src/args.mjs +1 -0
- package/src/banner.mjs +62 -0
- package/src/cli.mjs +6 -4
- package/src/commands/login.mjs +1 -0
- package/src/commands/setup.mjs +6 -5
- package/src/login.mjs +70 -71
package/README.md
CHANGED
|
@@ -29,18 +29,20 @@ collectui --version
|
|
|
29
29
|
collectui login
|
|
30
30
|
```
|
|
31
31
|
|
|
32
|
-
|
|
32
|
+
**OAuth 式浏览器授权**:CLI 起一个本机回调监听并打开 Dash 的 `/authorize` 授权页,
|
|
33
|
+
登录态下点一次「授权」——Dash 自动创建一条 `collectui-cli` PAT 并把明文回传本机
|
|
34
|
+
(`127.0.0.1` 回环,全程无需复制粘贴)。PAT 保存到 `~/.collectui/config.json`(`chmod 600`)。
|
|
33
35
|
|
|
34
|
-
|
|
36
|
+
无浏览器场景(CI / ssh / 远程机):
|
|
35
37
|
|
|
36
38
|
```bash
|
|
37
|
-
|
|
38
|
-
#
|
|
39
|
-
collectui login --token dpat_... --json
|
|
39
|
+
collectui login --pat # 终端粘贴 PAT(隐藏输入)
|
|
40
|
+
COLLECTUI_TOKEN=dpat_... collectui whoami --json # 或完全 headless
|
|
41
|
+
collectui login --token dpat_... --json # 或显式传入
|
|
40
42
|
```
|
|
41
43
|
|
|
42
|
-
>
|
|
43
|
-
>
|
|
44
|
+
> 授权页与 PAT 创建都在 Dash 完成(服务端只把明文回传给 `127.0.0.1`/`localhost`
|
|
45
|
+
> 回调,CLI 侧带随机 secret 路径 + state 校验);本机回环即用即弃。
|
|
44
46
|
|
|
45
47
|
## 看账户与库
|
|
46
48
|
|
|
@@ -84,17 +86,21 @@ collectui configure . --libs all --dry-run
|
|
|
84
86
|
| 官方 shadcn 占位(`ui.shadcn.com` / `npx shadcn mcp`) | 替换为该库的专属 MCP |
|
|
85
87
|
| 自定义 URL/命令 | **保留**,并明确报告;只有 `--force` 才替换 |
|
|
86
88
|
|
|
87
|
-
##
|
|
89
|
+
## 从零初始化一个 shadcn 项目
|
|
90
|
+
|
|
91
|
+
**默认就在系统临时目录建一个验证项目**,不碰当前目录——`collectui init` 什么都不带
|
|
92
|
+
就是一条「从零验证」命令:
|
|
88
93
|
|
|
89
94
|
```bash
|
|
90
|
-
collectui init /tmp/
|
|
91
|
-
|
|
92
|
-
collectui init
|
|
95
|
+
collectui init --libs all # /tmp/collectui-app(OS temp dir)
|
|
96
|
+
collectui init --name demo --libs reui,arc # <temp>/demo
|
|
97
|
+
collectui init /path/to/app --libs all # 指定任意目录
|
|
98
|
+
collectui init --libs all --dry-run # 只看计划
|
|
93
99
|
```
|
|
94
100
|
|
|
95
101
|
默认模板:Vite + React + TypeScript + Tailwind v4 + shadcn 兼容
|
|
96
102
|
`components.json`,并执行 `git init`。`--no-install` 跳过依赖安装;
|
|
97
|
-
`--template none`
|
|
103
|
+
`--template none` 只配置、不搭脚手架(需已有 components.json)。
|
|
98
104
|
|
|
99
105
|
## 装组件
|
|
100
106
|
|
package/package.json
CHANGED
package/src/args.mjs
CHANGED
package/src/banner.mjs
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { stdout } from "node:process";
|
|
2
|
+
import { CLI_VERSION } from "./version.mjs";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* 彩色 ASCII 品牌 banner(login / init 这类仪式感命令的开机画面)。
|
|
6
|
+
* - 支持 truecolor 的终端逐行渐变(青 → 紫);仅 256 色/其他 → 256 色近似;
|
|
7
|
+
* - NO_COLOR、非 TTY、headless 一律降级为无色纯文本(Agent/CI 永远干净)。
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
const GLYPH = [
|
|
11
|
+
" ██████╗ ██╗ ██╗██╗",
|
|
12
|
+
"██╔════╝ ██║ ██║██║",
|
|
13
|
+
"██║ ███╗██║ ██║██║",
|
|
14
|
+
"██║ ██║██║ ██║██║",
|
|
15
|
+
"╚██████╔╝╚██████╔╝██║",
|
|
16
|
+
" ╚═════╝ ╚═════╝ ╚═╝",
|
|
17
|
+
];
|
|
18
|
+
|
|
19
|
+
const TAGLINE = "One CLI for every shadcn library";
|
|
20
|
+
|
|
21
|
+
// 青 → 紫(Tailwind cyan-400 → violet-400)
|
|
22
|
+
const FROM = [34, 211, 238];
|
|
23
|
+
const TO = [167, 139, 250];
|
|
24
|
+
|
|
25
|
+
const GRAY = "\x1b[90m";
|
|
26
|
+
const RESET = "\x1b[0m";
|
|
27
|
+
|
|
28
|
+
function colorSupport() {
|
|
29
|
+
if (process.env.NO_COLOR) return "none";
|
|
30
|
+
if (!stdout.isTTY) return "none";
|
|
31
|
+
const ct = String(process.env.COLORTERM ?? "");
|
|
32
|
+
if (ct.includes("truecolor") || ct.includes("24bit")) return "truecolor";
|
|
33
|
+
// iTerm2/Terminal.app/VSCode/Windows Terminal 等现代终端默认按 256 起步
|
|
34
|
+
return "256";
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function lerp(a, b, t) {
|
|
38
|
+
return Math.round(a + (b - a) * t);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function rgbColor(at, text) {
|
|
42
|
+
const t = GLYPH.length <= 1 ? 0 : at / (GLYPH.length - 1);
|
|
43
|
+
return `\x1b[38;2;${lerp(FROM[0], TO[0], t)};${lerp(FROM[1], TO[1], t)};${lerp(FROM[2], TO[2], t)}m${text}${RESET}`;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function ansi256(at, text) {
|
|
47
|
+
const codes = [44, 44, 45, 99, 134, 140];
|
|
48
|
+
return `\x1b[38;5;${codes[Math.min(at, codes.length - 1)]}m${text}${RESET}`;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function showBanner(reporter) {
|
|
52
|
+
if (reporter?.headless) return; // --json / 非 TTY:输出保持机器可读,不开机画面
|
|
53
|
+
const support = colorSupport();
|
|
54
|
+
const paint = support === "truecolor" ? rgbColor : support === "256" ? ansi256 : null;
|
|
55
|
+
for (let i = 0; i < GLYPH.length; i += 1) {
|
|
56
|
+
const line = paint ? paint(i, GLYPH[i]) : GLYPH[i];
|
|
57
|
+
console.log(line);
|
|
58
|
+
}
|
|
59
|
+
const tag = support === "none" ? `${TAGLINE} · v${CLI_VERSION}` : `${GRAY}${TAGLINE} · v${CLI_VERSION}${RESET}`;
|
|
60
|
+
console.log(tag);
|
|
61
|
+
console.log();
|
|
62
|
+
}
|
package/src/cli.mjs
CHANGED
|
@@ -31,11 +31,11 @@ Usage:
|
|
|
31
31
|
collectui <command> [args] [flags]
|
|
32
32
|
|
|
33
33
|
Commands:
|
|
34
|
-
login
|
|
34
|
+
login Authorize CollectUI CLI in your browser (OAuth-style)
|
|
35
35
|
logout Remove the saved PAT
|
|
36
36
|
whoami, me Show account, libraries, bound keys and repos
|
|
37
37
|
libs List every library, ownership and key status
|
|
38
|
-
init [dir] Scaffold a project and configure
|
|
38
|
+
init [dir] Scaffold a fresh shadcn project and configure libraries (no dir: OS temp dir)
|
|
39
39
|
configure [dir] Configure an existing shadcn project
|
|
40
40
|
add <lib> <comp...> Install components with the library's channel
|
|
41
41
|
mcp <sub> Show/init MCP config; call Dash MCP tools
|
|
@@ -51,6 +51,7 @@ Global options:
|
|
|
51
51
|
--dry-run Print the full plan; write nothing
|
|
52
52
|
--verbose, -v Show HTTP requests, diffs and per-entry decisions
|
|
53
53
|
--no-open Do not open a browser during login
|
|
54
|
+
--pat login: paste a PAT instead of browser authorization (no-browser hosts)
|
|
54
55
|
--no-install init: skip dependency installation
|
|
55
56
|
--no-mcp init/configure: skip MCP configuration
|
|
56
57
|
--no-dash-mcp Do not add the collectui-dash MCP server
|
|
@@ -58,7 +59,7 @@ Global options:
|
|
|
58
59
|
--libs <list> Comma list or "all" (default: all owned)
|
|
59
60
|
--clients <list> claude,cursor,vscode,codex or all (default: claude,cursor,vscode)
|
|
60
61
|
--cwd <dir> Project directory (default: current directory)
|
|
61
|
-
--tmp
|
|
62
|
+
--tmp Force the OS temp dir as target (init defaults there anyway)
|
|
62
63
|
--name <name> Project/package name
|
|
63
64
|
--template <t> vite (default) or none
|
|
64
65
|
--pm <pm> npm | pnpm | yarn | bun
|
|
@@ -73,7 +74,8 @@ Global options:
|
|
|
73
74
|
Examples:
|
|
74
75
|
collectui login
|
|
75
76
|
collectui libs --json
|
|
76
|
-
collectui init
|
|
77
|
+
collectui init --libs reui,arc --yes --no-install # fresh verify project under the OS temp dir
|
|
78
|
+
collectui init /tmp/my-app --libs all --yes --no-install
|
|
77
79
|
collectui configure . --libs all --dry-run
|
|
78
80
|
collectui add reui data-grid
|
|
79
81
|
collectui mcp show --json
|
package/src/commands/login.mjs
CHANGED
package/src/commands/setup.mjs
CHANGED
|
@@ -10,6 +10,7 @@ import { fetchMe } from "../dash.mjs";
|
|
|
10
10
|
import { planMcpConfig, describeEntries } from "../mcp.mjs";
|
|
11
11
|
import { detectPackageManager, planProjectConfig } from "../project.mjs";
|
|
12
12
|
import { planManifest, planScaffold } from "../scaffold.mjs";
|
|
13
|
+
import { showBanner } from "../banner.mjs";
|
|
13
14
|
import { selectLibs } from "./common.mjs";
|
|
14
15
|
|
|
15
16
|
const DEFAULT_CLIENTS = ["claude", "cursor", "vscode"];
|
|
@@ -37,11 +38,10 @@ export function resolveTarget({ flags, rest, mode }) {
|
|
|
37
38
|
const explicit = flags.cwd || rest[0];
|
|
38
39
|
if (explicit) return resolve(String(explicit));
|
|
39
40
|
const safeName = String(flags.name ?? "collectui-app").replace(/[\\/]+/g, "-");
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
return resolve(safeName);
|
|
41
|
+
// init 的定位就是「从零开一个临时验证项目」:无显式目标时默认落 OS 临时目录,
|
|
42
|
+
// 绝不弄脏当前目录;configure 才默认当前项目。
|
|
43
|
+
if (flags.tmp || mode === "init") return join(tmpdir(), safeName);
|
|
44
|
+
return process.cwd();
|
|
45
45
|
}
|
|
46
46
|
|
|
47
47
|
function isNonEmptyDir(dir) {
|
|
@@ -61,6 +61,7 @@ function isNonEmptyDir(dir) {
|
|
|
61
61
|
*/
|
|
62
62
|
export async function runSetup({ flags, rest = [], reporter, mode }) {
|
|
63
63
|
reporter.intro(`CollectUI CLI · ${mode}`);
|
|
64
|
+
if (mode === "init") showBanner(reporter);
|
|
64
65
|
|
|
65
66
|
let token = resolveToken(flags);
|
|
66
67
|
if (!token) {
|
package/src/login.mjs
CHANGED
|
@@ -2,6 +2,7 @@ import { spawn } from "node:child_process";
|
|
|
2
2
|
import { randomBytes } from "node:crypto";
|
|
3
3
|
import { createServer } from "node:http";
|
|
4
4
|
import * as p from "@clack/prompts";
|
|
5
|
+
import { showBanner } from "./banner.mjs";
|
|
5
6
|
import { clearConfig, configFile, dashUrl, writeConfig } from "./config.mjs";
|
|
6
7
|
import { authError } from "./errors.mjs";
|
|
7
8
|
import { jsonRequest } from "./http.mjs";
|
|
@@ -33,10 +34,12 @@ export function saveSession(token, me, { reporter } = {}) {
|
|
|
33
34
|
}
|
|
34
35
|
|
|
35
36
|
/**
|
|
36
|
-
*
|
|
37
|
-
* 1.
|
|
38
|
-
* 2.
|
|
39
|
-
*
|
|
37
|
+
* 登录(OAuth 浏览器授权为主通道):
|
|
38
|
+
* 1. 本地起 127.0.0.1 回调监听(带随机 secret 路径 + state 校验)
|
|
39
|
+
* 2. 打开 Dash 的 /authorize 授权页(登录态下点一次「授权」,Dash 建好 PAT
|
|
40
|
+
* 并把明文 redirect 回本机回调,全程无需复制粘贴)
|
|
41
|
+
* 3. 无浏览器场景(CI / 远程 / ssh)用 `collectui login --pat` 粘贴 PAT,
|
|
42
|
+
* 或 headless 下 --token / COLLECTUI_TOKEN / stdin
|
|
40
43
|
*
|
|
41
44
|
* 无头模式(--json / 非 TTY):从 --token / 环境变量 / stdin 取 PAT,绝不弹交互。
|
|
42
45
|
*/
|
|
@@ -46,8 +49,10 @@ export async function login({
|
|
|
46
49
|
timeout = 180_000,
|
|
47
50
|
reporter,
|
|
48
51
|
skipVerify = false,
|
|
52
|
+
pat = false,
|
|
49
53
|
}) {
|
|
50
54
|
reporter.intro("CollectUI CLI · login");
|
|
55
|
+
showBanner(reporter);
|
|
51
56
|
|
|
52
57
|
let token = providedToken || process.env.COLLECTUI_TOKEN || process.env.DASH_PAT || "";
|
|
53
58
|
token = token.trim();
|
|
@@ -64,60 +69,59 @@ export async function login({
|
|
|
64
69
|
`${dashUrl()} (top-right 「API 密钥」).`,
|
|
65
70
|
});
|
|
66
71
|
}
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
72
|
+
if (!token && pat) {
|
|
73
|
+
// --pat:无浏览器场景(CI / ssh / 远程),单次隐藏输入,等用户贴完或 Ctrl+C
|
|
74
|
+
const pasted = await promptToken();
|
|
75
|
+
if (pasted === null) throw authError("Login cancelled.");
|
|
76
|
+
token = tokenOrThrow(pasted);
|
|
77
|
+
}
|
|
71
78
|
if (!token) {
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
const
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
reporter.info("
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
const remaining = timeout - (Date.now() - started);
|
|
88
|
-
if (remaining <= 0) throw authError("Login timed out.", { hint: "Re-run `collectui login`." });
|
|
89
|
-
answer = await Promise.race([
|
|
90
|
-
callback.waitForToken(remaining),
|
|
91
|
-
promptToken(Math.min(remaining, 120_000)),
|
|
92
|
-
]);
|
|
93
|
-
if (!answer) {
|
|
94
|
-
// 用户回车但没贴内容:继续等回调/再次提示,直到超时
|
|
95
|
-
if (!callback.pending) break;
|
|
79
|
+
// OAuth 主通道:浏览器授权后 Dash 直接回传,不用复制粘贴
|
|
80
|
+
const callback = startCallbackServer();
|
|
81
|
+
try {
|
|
82
|
+
const state = randomBytes(8).toString("hex");
|
|
83
|
+
const cbUrl = await callback.url();
|
|
84
|
+
const authUrl =
|
|
85
|
+
`${dashUrl()}/authorize?state=${state}&callback=${encodeURIComponent(cbUrl)}`;
|
|
86
|
+
reporter.info("Authorize CollectUI CLI in your browser:");
|
|
87
|
+
reporter.info(" " + authUrl);
|
|
88
|
+
if (!noOpen) {
|
|
89
|
+
const opened = openBrowser(authUrl);
|
|
90
|
+
if (opened) reporter.info("Opened the authorize page in your browser.");
|
|
91
|
+
else reporter.warn("Could not open a browser automatically; open the URL above manually.");
|
|
92
|
+
} else {
|
|
93
|
+
reporter.info("--no-open: open the URL above yourself.");
|
|
96
94
|
}
|
|
95
|
+
reporter.info(
|
|
96
|
+
`Waiting for the browser to come back (timeout ${Math.round(timeout / 1000)}s)… ` +
|
|
97
|
+
`No browser here? Re-run with --pat to paste a PAT.`
|
|
98
|
+
);
|
|
99
|
+
|
|
100
|
+
const answer = await callback.waitForToken(timeout);
|
|
101
|
+
token = tokenOrThrow(answer);
|
|
102
|
+
} finally {
|
|
103
|
+
callback.close();
|
|
97
104
|
}
|
|
98
|
-
token = tokenOrThrow(answer);
|
|
99
105
|
}
|
|
106
|
+
}
|
|
100
107
|
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
}
|
|
112
|
-
} else {
|
|
113
|
-
reporter.warn("--no-verify: token saved without checking it. Run `collectui whoami` to verify.");
|
|
108
|
+
let me = null;
|
|
109
|
+
if (!skipVerify) {
|
|
110
|
+
try {
|
|
111
|
+
me = await verifyToken(token, { reporter });
|
|
112
|
+
} catch (error) {
|
|
113
|
+
throw authError(`PAT verification failed: ${error.message}`, {
|
|
114
|
+
hint:
|
|
115
|
+
"Make sure you copied the full dpat_ token and that it is still active in " +
|
|
116
|
+
`${dashUrl()} → API 密钥.`,
|
|
117
|
+
});
|
|
114
118
|
}
|
|
115
|
-
|
|
116
|
-
reporter.
|
|
117
|
-
return { token, me };
|
|
118
|
-
} finally {
|
|
119
|
-
callback.close();
|
|
119
|
+
} else {
|
|
120
|
+
reporter.warn("--no-verify: token saved without checking it. Run `collectui whoami` to verify.");
|
|
120
121
|
}
|
|
122
|
+
saveSession(token, me, { reporter });
|
|
123
|
+
reporter.outro("Login complete. Try `collectui libs`.");
|
|
124
|
+
return { token, me };
|
|
121
125
|
}
|
|
122
126
|
|
|
123
127
|
function tokenOrThrow(value) {
|
|
@@ -131,22 +135,18 @@ function tokenOrThrow(value) {
|
|
|
131
135
|
return token;
|
|
132
136
|
}
|
|
133
137
|
|
|
134
|
-
async function promptToken(
|
|
135
|
-
if (!process.stdin.isTTY || !process.stdout.isTTY) return
|
|
136
|
-
const
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
timeout,
|
|
143
|
-
]);
|
|
144
|
-
if (answer === "") return "";
|
|
145
|
-
return answer;
|
|
138
|
+
async function promptToken() {
|
|
139
|
+
if (!process.stdin.isTTY || !process.stdout.isTTY) return null;
|
|
140
|
+
const v = await p.password({
|
|
141
|
+
message: "Paste your Dash PAT (dpat_…, hidden input)",
|
|
142
|
+
mask: "*",
|
|
143
|
+
});
|
|
144
|
+
if (p.isCancel(v)) return null;
|
|
145
|
+
return String(v ?? "");
|
|
146
146
|
}
|
|
147
147
|
|
|
148
|
-
/**
|
|
149
|
-
export function startCallbackServer() {
|
|
148
|
+
/** 本地回调监听:随机 secret 路径 + 可选 state 回查(Dash /authorize 会把 state 原样带回)。 */
|
|
149
|
+
export function startCallbackServer({ state } = {}) {
|
|
150
150
|
const secret = randomBytes(12).toString("hex");
|
|
151
151
|
let resolveToken;
|
|
152
152
|
const tokenPromise = new Promise((resolve) => {
|
|
@@ -156,18 +156,17 @@ export function startCallbackServer() {
|
|
|
156
156
|
|
|
157
157
|
const server = createServer((req, res) => {
|
|
158
158
|
const url = new URL(req.url ?? "/", "http://127.0.0.1");
|
|
159
|
-
|
|
159
|
+
const token = url.searchParams.get("token") ?? "";
|
|
160
|
+
const gotState = url.searchParams.get("state") ?? "";
|
|
161
|
+
if (url.pathname !== `/${secret}` || (state && gotState !== state) || !token) {
|
|
160
162
|
res.writeHead(404, { "Content-Type": "text/plain; charset=utf-8" });
|
|
161
163
|
res.end("Not found");
|
|
162
164
|
return;
|
|
163
165
|
}
|
|
164
|
-
const token = url.searchParams.get("token") ?? "";
|
|
165
166
|
res.writeHead(200, { "Content-Type": "text/html; charset=utf-8" });
|
|
166
167
|
res.end("<!doctype html><meta charset=utf-8><title>CollectUI</title><p>Token received. You can close this window.</p>");
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
resolveToken(token);
|
|
170
|
-
}
|
|
168
|
+
pending = false;
|
|
169
|
+
resolveToken(token);
|
|
171
170
|
});
|
|
172
171
|
|
|
173
172
|
const listening = new Promise((resolve, reject) => {
|