micloud-cli 0.0.0-stage → 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/LICENSE +661 -0
- package/README.en.md +118 -0
- package/README.md +117 -2
- package/dist/cli.js +59 -0
- package/dist/commands/account.js +76 -0
- package/dist/core/account.js +22 -0
- package/dist/core/auth.js +338 -0
- package/dist/core/config.js +81 -0
- package/dist/core/http.js +172 -0
- package/dist/core/lock.js +90 -0
- package/dist/core/output.js +137 -0
- package/dist/core/template.js +39 -0
- package/dist/core/utils.js +205 -0
- package/dist/note/client.js +298 -0
- package/dist/note/commands/create.js +44 -0
- package/dist/note/commands/delete.js +32 -0
- package/dist/note/commands/export.js +20 -0
- package/dist/note/commands/folder.js +101 -0
- package/dist/note/commands/get.js +35 -0
- package/dist/note/commands/list.js +57 -0
- package/dist/note/commands/move.js +37 -0
- package/dist/note/commands/pin.js +41 -0
- package/dist/note/commands/search.js +45 -0
- package/dist/note/commands/shared.js +37 -0
- package/dist/note/commands/sync.js +217 -0
- package/dist/note/commands/update.js +61 -0
- package/dist/note/commands/upload-image.js +27 -0
- package/dist/note/config.js +166 -0
- package/dist/note/converter.js +664 -0
- package/dist/note/extra-info.js +23 -0
- package/dist/note/images.js +100 -0
- package/dist/note/register.js +172 -0
- package/dist/note/sync-diff.js +143 -0
- package/dist/note/sync.js +748 -0
- package/dist/note/text-diff.js +71 -0
- package/dist/note/types.js +2 -0
- package/dist/recording/client.js +115 -0
- package/dist/recording/commands/config.js +73 -0
- package/dist/recording/commands/delete.js +171 -0
- package/dist/recording/commands/list.js +161 -0
- package/dist/recording/commands/shared.js +47 -0
- package/dist/recording/commands/transfer.js +192 -0
- package/dist/recording/commands/trash.js +140 -0
- package/dist/recording/config.js +104 -0
- package/dist/recording/dates.js +61 -0
- package/dist/recording/download.js +308 -0
- package/dist/recording/progress.js +77 -0
- package/dist/recording/pull.js +433 -0
- package/dist/recording/recording.js +294 -0
- package/dist/recording/register.js +202 -0
- package/dist/recording/template.js +44 -0
- package/dist/recording/types.js +2 -0
- package/package.json +53 -4
package/README.en.md
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
English | [简体中文](./README.md)
|
|
2
|
+
|
|
3
|
+
# micloud
|
|
4
|
+
|
|
5
|
+
A command-line tool for Xiaomi Cloud (i.mi.com), built for AI agents and scripts. Log in once to work with:
|
|
6
|
+
|
|
7
|
+
- **Notes**: read and write as Markdown; export, or sync two-way with a local folder
|
|
8
|
+
- **Recordings**: incrementally pull recordings your phone syncs to the cloud, then clean up the cloud copies
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
npm install -g micloud-cli
|
|
12
|
+
micloud login # log in with your Xiaomi account in a browser; renewed automatically
|
|
13
|
+
micloud note sync -o ./notes --mode two-way # two-way sync notes with a local folder
|
|
14
|
+
micloud rec pull -o ./recordings # pull new recordings to disk
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
> Requires Node.js >= 18 and Chrome (opened for login). Without Chrome, run `npx playwright install chromium`.
|
|
18
|
+
|
|
19
|
+
For AI agents:
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
npx -y skills add ceynri/micloud-cli --all
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
> ⚠️ Experimental: minor versions (`0.Y.z`) may contain breaking changes. Pin the minor version (e.g. `~0.5.0`) if you need stability.
|
|
26
|
+
|
|
27
|
+
## For AI agents and scripts
|
|
28
|
+
|
|
29
|
+
- **Use `--json`**: success prints `{"ok":true,"data":...}`, failure prints `{"ok":false,"error":"..."}`. Logs and progress go to stderr.
|
|
30
|
+
- **Exit codes**: `0` success; `2` partial failure (still `ok:true`, failed items listed in `data`); `1` failure.
|
|
31
|
+
- **Never hangs**: in non-interactive mode (`--json` or no terminal) it never opens a browser. If the login has expired it fails right away; the user runs `micloud login` once in a terminal.
|
|
32
|
+
- **Deletes need explicit confirmation**: in non-interactive mode, deleting requires `-y`, otherwise it errors. Preview with `--dry-run` first.
|
|
33
|
+
|
|
34
|
+
## Notes: `micloud note`
|
|
35
|
+
|
|
36
|
+
| Command | Description |
|
|
37
|
+
|---|---|
|
|
38
|
+
| `list` / `get <id>` / `search <keyword>` | List, view (as Markdown; `--raw` for the original format), search |
|
|
39
|
+
| `create` / `update <id>` | Write Markdown from `--content`, `--file`, or stdin |
|
|
40
|
+
| `delete <id>` / `move <id> <folder>` / `pin` / `unpin` | Delete (to trash by default), move, pin |
|
|
41
|
+
| `folders` / `folder create\|rename\|delete` | Manage folders |
|
|
42
|
+
| `upload-image <path>` | Upload an image and get a reference to embed in a note |
|
|
43
|
+
| `export -o <dir>` | One-way export to Markdown; never changes the cloud |
|
|
44
|
+
| `sync -o <dir> --mode <mode>` | Two-way sync; `--dry-run` to preview |
|
|
45
|
+
|
|
46
|
+
Sync modes: `cloud-first` (download only), `local-first` (upload only), `two-way` (automatic both ways), `manual` (default, asks for each change). Real conflicts (both sides changed, or deleted on one side and edited on the other) are skipped and reported in non-interactive mode; data is never deleted on a guess. Cloud deletions only move notes to the trash.
|
|
47
|
+
|
|
48
|
+
Supported Markdown: headings, lists, checkboxes, quotes, dividers, bold / italic / strikethrough, links, inline code, and images. Tables and code-block languages are not supported; try important notes on a small scale first.
|
|
49
|
+
|
|
50
|
+
## Recordings: `micloud rec`
|
|
51
|
+
|
|
52
|
+
`rec` is short for `recording`.
|
|
53
|
+
|
|
54
|
+
| Command | Description |
|
|
55
|
+
|---|---|
|
|
56
|
+
| `list` / `search` / `info <id>` / `stats` | List, search, details, statistics |
|
|
57
|
+
| `pull -o <dir>` | Incremental pull: already-pulled recordings are skipped, even if you moved the files away; `--limit n` pulls the newest n not-yet-pulled ones per run |
|
|
58
|
+
| `download <id...>` | Download by id; `--stdout` to pipe |
|
|
59
|
+
| `url <id>` / `play <id>` | Temporary direct link (no login needed, about 4 hours), play in a player |
|
|
60
|
+
| `delete` | Move to trash: by id, by filter, or `--pulled` (pulled and verified locally) |
|
|
61
|
+
| `trash` / `trash restore` / `trash purge` | Trash: list, restore, delete permanently |
|
|
62
|
+
|
|
63
|
+
Filters (for list / stats / pull / delete): `--type recorder,phonecall,fm,app`, `--since 7d`, `--until 2026-07-31`, `-q keyword`.
|
|
64
|
+
|
|
65
|
+
Pull to disk, hand off to transcription, then clean up the cloud:
|
|
66
|
+
|
|
67
|
+
```bash
|
|
68
|
+
micloud rec pull -o ./recordings --json | jq -r '.data.pulled[].file' # paths of newly pulled files
|
|
69
|
+
micloud rec delete --pulled -o ./recordings --dry-run # preview the cleanup
|
|
70
|
+
micloud rec delete --pulled -o ./recordings -y # move to trash
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
- **Safe downloads**: each file is written to `.part` first and only kept once its size and SHA-1 match the cloud; interrupted runs resume; existing files with the same name are never overwritten.
|
|
74
|
+
- **Safe cleanup**: `--pulled` only deletes recordings whose cloud copy is unchanged and whose local file is still intact. Add `--allow-moved` if you have moved the files elsewhere.
|
|
75
|
+
- **Deletes reach your phone**: Xiaomi Cloud syncs deletions to signed-in devices, so the recording is deleted from the phone too. The trash keeps recordings for 30 to 180 days depending on membership.
|
|
76
|
+
|
|
77
|
+
File names default to the phone's recording names. Customize with `--template`, e.g. `'${YYYY}-${MM}-${DD}_${HH}${mm}[_${contact}]'`; see `micloud rec pull --help` for placeholders.
|
|
78
|
+
|
|
79
|
+
## Configuration
|
|
80
|
+
|
|
81
|
+
Project settings live in `.micloud/config.json` in the current directory, one section per service, all optional:
|
|
82
|
+
|
|
83
|
+
```json
|
|
84
|
+
{
|
|
85
|
+
"note": { "output": "notes", "syncMode": "two-way", "fileNameTemplate": "${YYYY}-${MM}-${DD}[_${title}]" },
|
|
86
|
+
"recording": { "output": "recordings", "fileNameTemplate": "${title}", "concurrency": 3 }
|
|
87
|
+
}
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
- `-o` on the command line overrides `output`. Without either, `.micloud/note/` and `.micloud/recording/` are used.
|
|
91
|
+
- Sync / pull state is kept inside the output folder (`.micloud-note.state.json`, `.micloud-recording.json`), so the folder can be moved as a whole.
|
|
92
|
+
- The login lives in the system cache directory (`~/Library/Caches/micloud/` on macOS); delete it and run `login` again if needed.
|
|
93
|
+
- The default output folders `.micloud/note/` and `.micloud/recording/` hold your notes and recordings; add them to `.gitignore` inside a git repository. `config.json` can be committed.
|
|
94
|
+
|
|
95
|
+
## Limitations
|
|
96
|
+
|
|
97
|
+
- Only mainland China accounts (i.mi.com).
|
|
98
|
+
- With Advanced Data Protection (end-to-end encryption) enabled, recordings cannot be downloaded (you get a clear error); listing and deleting still work.
|
|
99
|
+
- Special note types such as private notes and mind maps are exported on a best-effort basis and cannot be edited.
|
|
100
|
+
- Recordings cannot be uploaded or renamed (the web app can't either).
|
|
101
|
+
|
|
102
|
+
## Upgrading from mi-note-cli
|
|
103
|
+
|
|
104
|
+
Add `note` to your commands: `mi-note-cli sync` becomes `micloud note sync`. Your login, configuration, and sync folders carry over automatically, with no need to log in or sync from scratch. See [MIGRATION.md](./MIGRATION.md) (Chinese).
|
|
105
|
+
|
|
106
|
+
## Development
|
|
107
|
+
|
|
108
|
+
```bash
|
|
109
|
+
pnpm install
|
|
110
|
+
pnpm typecheck && pnpm test # type check and unit tests (no network)
|
|
111
|
+
MICLOUD_INTEGRATION=1 pnpm test:integration # integration tests against a real account (log in first)
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
API notes: [docs/recording-api.md](./docs/recording-api.md). Issues and suggestions are welcome in [issues](https://github.com/ceynri/micloud-cli/issues).
|
|
115
|
+
|
|
116
|
+
## License
|
|
117
|
+
|
|
118
|
+
[AGPL-3.0](./LICENSE)
|
package/README.md
CHANGED
|
@@ -1,3 +1,118 @@
|
|
|
1
|
-
|
|
1
|
+
[English](./README.en.md) | 简体中文
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
# micloud
|
|
4
|
+
|
|
5
|
+
小米云服务(i.mi.com)的命令行工具,为 AI / 脚本调用设计。一次登录,同时管理:
|
|
6
|
+
|
|
7
|
+
- **笔记**:读写,转成 Markdown;导出,或与本地目录双向同步
|
|
8
|
+
- **录音**:把手机同步上云的录音增量拉到本地,再清理云端
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
npm install -g micloud-cli
|
|
12
|
+
micloud login # 打开浏览器登录小米账号,之后自动续期
|
|
13
|
+
micloud note sync -o ./notes --mode two-way # 笔记与本地目录双向同步
|
|
14
|
+
micloud rec pull -o ./recordings # 录音增量拉取到本地
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
> 需要 Node.js >= 18 和 Chrome(登录时打开浏览器用)。没有 Chrome 时运行 `npx playwright install chromium`。
|
|
18
|
+
|
|
19
|
+
给 AI Agent 用:
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
npx -y skills add ceynri/micloud-cli --all
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
> ⚠️ 实验阶段:minor 版本(`0.Y.z`)可能含破坏性变更,要稳定请锁定 minor 版本(如 `~0.5.0`)。
|
|
26
|
+
|
|
27
|
+
## 给 AI / 脚本调用
|
|
28
|
+
|
|
29
|
+
- **加 `--json`**:成功输出 `{"ok":true,"data":...}`,失败输出 `{"ok":false,"error":"..."}`;日志和进度走 stderr,不影响解析。
|
|
30
|
+
- **退出码**:`0` 成功;`2` 部分失败(仍是 `ok:true`,失败项列在 `data` 里);`1` 失败。
|
|
31
|
+
- **不会卡住**:非交互环境(`--json` 或没有终端)不弹浏览器。登录失效时直接报错,需要用户在终端运行一次 `micloud login`。
|
|
32
|
+
- **删除要显式确认**:删除类操作在非交互环境下必须带 `-y`,否则直接报错。先用 `--dry-run` 预览。
|
|
33
|
+
|
|
34
|
+
## 笔记 `micloud note`
|
|
35
|
+
|
|
36
|
+
| 命令 | 说明 |
|
|
37
|
+
|---|---|
|
|
38
|
+
| `list` / `get <id>` / `search <关键词>` | 列出、查看(转 Markdown,`--raw` 看原始格式)、搜索 |
|
|
39
|
+
| `create` / `update <id>` | 用 Markdown 写入,内容来自 `--content`、`--file` 或管道 |
|
|
40
|
+
| `delete <id>` / `move <id> <文件夹>` / `pin` / `unpin` | 删除(默认进回收站)、移动、置顶 |
|
|
41
|
+
| `folders` / `folder create\|rename\|delete` | 文件夹管理 |
|
|
42
|
+
| `upload-image <路径>` | 上传图片,得到可放进笔记的引用 |
|
|
43
|
+
| `export -o <目录>` | 单向导出为 Markdown,永不修改云端 |
|
|
44
|
+
| `sync -o <目录> --mode <模式>` | 双向同步,`--dry-run` 预览 |
|
|
45
|
+
|
|
46
|
+
同步模式:`cloud-first` 只下行、`local-first` 只上行、`two-way` 双向自动、`manual`(默认)逐条询问。真冲突(两边都改、一边删一边改)在非交互环境下跳过并报告,不会擅自删数据。云端删除只进回收站。
|
|
47
|
+
|
|
48
|
+
Markdown 支持标题、列表、复选框、引用、分割线、加粗 / 斜体 / 删除线、链接、行内代码和图片。表格、代码块语言标识等不在支持范围内,重要笔记先小范围试一下。
|
|
49
|
+
|
|
50
|
+
## 录音 `micloud rec`
|
|
51
|
+
|
|
52
|
+
`rec` 是 `recording` 的简写。
|
|
53
|
+
|
|
54
|
+
| 命令 | 说明 |
|
|
55
|
+
|---|---|
|
|
56
|
+
| `list` / `search` / `info <id>` / `stats` | 列出、搜索、详情、统计 |
|
|
57
|
+
| `pull -o <目录>` | 增量拉取:拉过的自动跳过,即使你已把文件移走;`--limit n` 每次只拉最新 n 条待拉取的 |
|
|
58
|
+
| `download <id...>` | 按 id 下载;`--stdout` 写到管道 |
|
|
59
|
+
| `url <id>` / `play <id>` | 临时直链(免登录、约 4 小时有效)、在线播放 |
|
|
60
|
+
| `delete` | 移到回收站:按 id、筛选条件,或 `--pulled`(已拉到本地且校验一致的) |
|
|
61
|
+
| `trash` / `trash restore` / `trash purge` | 回收站:列出、恢复、永久删除 |
|
|
62
|
+
|
|
63
|
+
筛选参数(list / stats / pull / delete 通用):`--type recorder,phonecall,fm,app`、`--since 7d`、`--until 2026-07-31`、`-q 关键词`。
|
|
64
|
+
|
|
65
|
+
拉到本地、交给转写,再清理云端:
|
|
66
|
+
|
|
67
|
+
```bash
|
|
68
|
+
micloud rec pull -o ./recordings --json | jq -r '.data.pulled[].file' # 新拉到的文件路径
|
|
69
|
+
micloud rec delete --pulled -o ./recordings --dry-run # 预览要清理的
|
|
70
|
+
micloud rec delete --pulled -o ./recordings -y # 移到回收站
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
- **下载安全**:每个文件先写 `.part`,大小和 SHA-1 都与云端一致才落盘;中断后重跑会断点续传;不会覆盖已有的同名文件。
|
|
74
|
+
- **清理安全**:`--pulled` 只删云端内容没变、且拉取目录里的文件仍完好的录音。文件已转移到别处时加 `--allow-moved`。
|
|
75
|
+
- **会同步到手机**:小米云会把删除同步到已登录的设备,手机上的录音也会被删除。回收站按会员等级保留 30~180 天。
|
|
76
|
+
|
|
77
|
+
文件名默认沿用手机上的录音名,可用 `--template` 自定义,如 `'${YYYY}-${MM}-${DD}_${HH}${mm}[_${contact}]'`,占位符见 `micloud rec pull --help`。
|
|
78
|
+
|
|
79
|
+
## 配置
|
|
80
|
+
|
|
81
|
+
项目配置放在当前目录的 `.micloud/config.json`,笔记和录音各一节,都可省略:
|
|
82
|
+
|
|
83
|
+
```json
|
|
84
|
+
{
|
|
85
|
+
"note": { "output": "notes", "syncMode": "two-way", "fileNameTemplate": "${YYYY}-${MM}-${DD}[_${title}]" },
|
|
86
|
+
"recording": { "output": "recordings", "fileNameTemplate": "${title}", "concurrency": 3 }
|
|
87
|
+
}
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
- 命令行的 `-o` 优先于配置里的 `output`。都没有时用 `.micloud/note/`、`.micloud/recording/`。
|
|
91
|
+
- 同步 / 拉取状态记在输出目录里(`.micloud-note.state.json`、`.micloud-recording.json`),目录整体搬走也能继续用。
|
|
92
|
+
- 登录状态在系统缓存目录(macOS 是 `~/Library/Caches/micloud/`),删掉后重新 `login` 即可。
|
|
93
|
+
- 默认输出目录 `.micloud/note/`、`.micloud/recording/` 里是你的笔记和录音,在 git 仓库里使用时把它们加进 `.gitignore`;`config.json` 可以提交。
|
|
94
|
+
|
|
95
|
+
## 限制
|
|
96
|
+
|
|
97
|
+
- 只支持中国大陆区账号(i.mi.com)。
|
|
98
|
+
- 开启「高级数据保护」(端到端加密)后无法下载录音,会给出明确提示;列表、删除不受影响。
|
|
99
|
+
- 笔记的私密笔记、思维导图等特殊类型只能尽力导出,不支持编辑。
|
|
100
|
+
- 录音不能上传、改名(网页端也没有这些能力)。
|
|
101
|
+
|
|
102
|
+
## 从 mi-note-cli 升级
|
|
103
|
+
|
|
104
|
+
命令前加 `note` 即可,如 `mi-note-cli sync` 改为 `micloud note sync`。登录状态、配置和同步目录都会自动沿用,不需要重新登录或重新同步。详见 [MIGRATION.md](./MIGRATION.md)。
|
|
105
|
+
|
|
106
|
+
## 开发
|
|
107
|
+
|
|
108
|
+
```bash
|
|
109
|
+
pnpm install
|
|
110
|
+
pnpm typecheck && pnpm test # 类型检查与单元测试(无需网络)
|
|
111
|
+
MICLOUD_INTEGRATION=1 pnpm test:integration # 真实账号的集成测试(需先登录)
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
接口说明:[docs/recording-api.md](./docs/recording-api.md)。问题与建议欢迎提 [issue](https://github.com/ceynri/micloud-cli/issues)。
|
|
115
|
+
|
|
116
|
+
## License
|
|
117
|
+
|
|
118
|
+
[AGPL-3.0](./LICENSE)
|
package/dist/cli.js
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { createRequire } from "node:module";
|
|
3
|
+
import { Command } from "commander";
|
|
4
|
+
import { setJsonMode } from "./core/output.js";
|
|
5
|
+
import { loginCommand, logoutCommand, whoamiCommand } from "./commands/account.js";
|
|
6
|
+
import { registerNoteCommands } from "./note/register.js";
|
|
7
|
+
import { registerRecordingCommands } from "./recording/register.js";
|
|
8
|
+
const require = createRequire(import.meta.url);
|
|
9
|
+
const { version, description } = require("../package.json");
|
|
10
|
+
// 下游管道提前关闭(如 `| head`)时安静退出,而不是抛出 EPIPE 堆栈
|
|
11
|
+
process.stdout.on("error", (err) => {
|
|
12
|
+
if (err.code === "EPIPE")
|
|
13
|
+
process.exit(process.exitCode ?? 0);
|
|
14
|
+
throw err;
|
|
15
|
+
});
|
|
16
|
+
// Ctrl+C / kill 时走正常退出流程,让目录锁等 exit 清理逻辑得以执行
|
|
17
|
+
for (const signal of ["SIGINT", "SIGTERM"]) {
|
|
18
|
+
process.once(signal, () => {
|
|
19
|
+
process.stderr.write("\n已中断\n");
|
|
20
|
+
process.exit(130);
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
export function buildProgram() {
|
|
24
|
+
const program = new Command();
|
|
25
|
+
program
|
|
26
|
+
.name("micloud")
|
|
27
|
+
.description(description)
|
|
28
|
+
.version(version, "-v, --version", "显示版本号")
|
|
29
|
+
.option("--json", "以 JSON 格式输出(便于 AI / 脚本解析)")
|
|
30
|
+
.hook("preAction", (thisCommand) => {
|
|
31
|
+
if (thisCommand.opts().json)
|
|
32
|
+
setJsonMode(true);
|
|
33
|
+
});
|
|
34
|
+
program.addHelpText("after", `
|
|
35
|
+
输出约定:
|
|
36
|
+
全局 --json 下,所有命令统一输出 JSON:成功 {"ok":true,"data":...},失败 {"ok":false,"error":"..."}。
|
|
37
|
+
日志/进度走 stderr,结构化结果走 stdout,可安全用管道解析。
|
|
38
|
+
批量操作部分失败时仍输出 ok:true(失败项列在 data 里),退出码为 2;整体失败退出码为 1。
|
|
39
|
+
|
|
40
|
+
典型流程:
|
|
41
|
+
1) micloud login # 首次登录(浏览器),笔记与录音通用
|
|
42
|
+
2) micloud note list --json # 笔记
|
|
43
|
+
3) micloud rec pull -o ./recordings # 录音:增量拉取到本地
|
|
44
|
+
|
|
45
|
+
非交互/AI 调用:
|
|
46
|
+
无 TTY 或带 --json 时不会弹浏览器;登录态失效且无法静默续期时直接报错,需先人工 login。
|
|
47
|
+
删除类操作在非交互模式下必须带 -y,否则直接报错,绝不擅自删除。
|
|
48
|
+
`);
|
|
49
|
+
program.command("login").description("登录小米账号(打开浏览器完成登录)").action(() => loginCommand());
|
|
50
|
+
program.command("logout").description("清除登录态").action(() => logoutCommand());
|
|
51
|
+
program
|
|
52
|
+
.command("whoami")
|
|
53
|
+
.description("查看登录状态、账号与云空间用量")
|
|
54
|
+
.action(() => whoamiCommand());
|
|
55
|
+
registerNoteCommands(program);
|
|
56
|
+
registerRecordingCommands(program);
|
|
57
|
+
return program;
|
|
58
|
+
}
|
|
59
|
+
buildProgram().parseAsync(process.argv);
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { ensureAuth, peekOrRefreshAuth, clearAuthCache, refreshAuth } from "../core/auth.js";
|
|
2
|
+
import { getProfile, getQuota, LEVEL_NAMES, TRASH_RETENTION_DAYS, } from "../core/account.js";
|
|
3
|
+
import { MiCloudHttp } from "../core/http.js";
|
|
4
|
+
import { success, logInfo, fail } from "../core/output.js";
|
|
5
|
+
import { formatBytes } from "../core/utils.js";
|
|
6
|
+
/** login:强制打开浏览器登录,刷新缓存 */
|
|
7
|
+
export async function loginCommand() {
|
|
8
|
+
try {
|
|
9
|
+
const auth = await ensureAuth(true);
|
|
10
|
+
success({ loggedIn: true, userId: auth.userId }, () => {
|
|
11
|
+
logInfo(`✅ 登录成功${auth.userId ? `(userId: ${auth.userId})` : ""}`);
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
catch (err) {
|
|
15
|
+
fail(err);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
/** logout:清除登录态 */
|
|
19
|
+
export async function logoutCommand() {
|
|
20
|
+
try {
|
|
21
|
+
await clearAuthCache();
|
|
22
|
+
success({ loggedOut: true }, () => logInfo("✅ 已清除登录态"));
|
|
23
|
+
}
|
|
24
|
+
catch (err) {
|
|
25
|
+
fail(err);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
/** whoami:登录状态、账号昵称与云空间用量 */
|
|
29
|
+
export async function whoamiCommand() {
|
|
30
|
+
try {
|
|
31
|
+
const auth = await peekOrRefreshAuth();
|
|
32
|
+
if (!auth) {
|
|
33
|
+
success({ loggedIn: false }, () => logInfo("未登录(运行 micloud login 登录)"));
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
const http = new MiCloudHttp(auth, refreshAuth);
|
|
37
|
+
const [profile, quota] = await Promise.allSettled([getProfile(http), getQuota(http)]);
|
|
38
|
+
const data = buildWhoami(http.userId, profile.status === "fulfilled" ? profile.value : undefined, quota.status === "fulfilled" ? quota.value : undefined);
|
|
39
|
+
success(data, () => {
|
|
40
|
+
logInfo(`已登录:${data.nickname ?? ""}(userId: ${data.userId || "未知"})`);
|
|
41
|
+
if (data.quota) {
|
|
42
|
+
const q = data.quota;
|
|
43
|
+
const parts = Object.values(q.usedDetail).map((d) => `${d.label} ${formatBytes(d.size)}`);
|
|
44
|
+
logInfo(`云空间:已用 ${formatBytes(q.used)} / 共 ${formatBytes(q.total)}${parts.length ? `(${parts.join(",")})` : ""}`);
|
|
45
|
+
}
|
|
46
|
+
if (data.levelName) {
|
|
47
|
+
const days = data.trashRetentionDays ? `,回收站保留 ${data.trashRetentionDays} 天` : "";
|
|
48
|
+
logInfo(`会员等级:${data.levelName}${days}`);
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
catch (err) {
|
|
53
|
+
fail(err);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
export function buildWhoami(userId, profile, quota) {
|
|
57
|
+
const data = { loggedIn: true, userId };
|
|
58
|
+
if (profile?.nickname)
|
|
59
|
+
data.nickname = profile.nickname;
|
|
60
|
+
if (quota) {
|
|
61
|
+
if (quota.level) {
|
|
62
|
+
data.level = quota.level;
|
|
63
|
+
data.levelName = LEVEL_NAMES[quota.level] ?? quota.level;
|
|
64
|
+
data.trashRetentionDays = TRASH_RETENTION_DAYS[quota.level];
|
|
65
|
+
}
|
|
66
|
+
if (typeof quota.totalQuota === "number" && typeof quota.used === "number") {
|
|
67
|
+
const usedDetail = {};
|
|
68
|
+
for (const [key, detail] of Object.entries(quota.usedDetail ?? {})) {
|
|
69
|
+
if (typeof detail?.size === "number")
|
|
70
|
+
usedDetail[key] = { label: detail.text || key, size: detail.size };
|
|
71
|
+
}
|
|
72
|
+
data.quota = { total: quota.totalQuota, used: quota.used, usedDetail };
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
return data;
|
|
76
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { unwrapEnvelope } from "./http.js";
|
|
2
|
+
export const LEVEL_NAMES = {
|
|
3
|
+
None: "普通用户",
|
|
4
|
+
FirstLevel: "黄金会员",
|
|
5
|
+
SecondLevel: "白金会员",
|
|
6
|
+
ThirdLevel: "钻石会员",
|
|
7
|
+
};
|
|
8
|
+
/** 回收站保留天数(按会员等级,取自网页端) */
|
|
9
|
+
export const TRASH_RETENTION_DAYS = {
|
|
10
|
+
None: 30,
|
|
11
|
+
FirstLevel: 60,
|
|
12
|
+
SecondLevel: 90,
|
|
13
|
+
ThirdLevel: 180,
|
|
14
|
+
};
|
|
15
|
+
/** 账号昵称与头像 */
|
|
16
|
+
export async function getProfile(http) {
|
|
17
|
+
return await http.getJson("/status/lite/profile", {}, (json) => unwrapEnvelope(json));
|
|
18
|
+
}
|
|
19
|
+
/** 云空间用量与会员等级 */
|
|
20
|
+
export async function getQuota(http) {
|
|
21
|
+
return await http.getJson("/status/alldetail", {}, (json) => unwrapEnvelope(json));
|
|
22
|
+
}
|