space-lens
其他 活跃维护

space-lens

HuakunShen/space-lens

本地优先的磁盘占用分析工具,集成 Tauri 桌面端、Rust CLI/TUI 与 Svelte 工作台,扫描任意文件夹并暂存清理候选,可视化地图呈现,数据不离开设备。

0
Stars 标星
0
Forks 分支
0
Watchers 关注
1
Open Issues
TypeScript
主要语言
MIT
开源协议
10.5 MB
仓库大小
1 个月前
最后推送
一键安装扩展 / 插件指令
dsh plugin --profile web add github:HuakunShen/space-lens
git clone https://github.com/HuakunShen/space-lens.git
git clone git@github.com:HuakunShen/space-lens.git
README.md main

Space Lens

npm version
npm downloads
crates.io
CI
Workbench
License

Fast directory scanning and cleanup candidate utilities, powered by Rust and napi-rs.

Where the engine went (2026-09-14): the Rust engine that lived in
packages/space-lens — scanner, snapshot, cleanup, iCloud eviction — moved to
Kuntu as kuntu-scan (tag v0.3.0,
pinned here at vendors/kuntu). This repo is the product shells: the
SwiftUI apps (via space-lens-ffi), the npm package, the CLI/TUI, and the
standalone iCloud app. The space_lens:: Rust API is unchanged — the
dependency is aliased.

Installation

npm install space-lens

Install the CLI

The Rust CLI is the crate spacelens (scan, candidates, clean, dirty-git,
icloud). Once published to crates.io:

cargo install spacelens        # build from source
cargo binstall spacelens       # or: prebuilt binary from the GitHub release
brew install HuakunShen/homebrew-tap/spacelens

Until then, install straight from this repository (cargo checks out the
vendors/kuntu submodule itself):

cargo install --git https://github.com/HuakunShen/space-lens --locked spacelens

Get the desktop app

Installers are published on GitHub Releases
by the tag-triggered desktop pipeline (.github/workflows/workbench.yml on app-v* tags):

Platform Artifacts
macOS (Apple Silicon) Space.Lens_<version>_aarch64.dmg
macOS (Intel) Space.Lens_<version>_x64.dmg
Linux x64 .deb and .AppImage
Windows x64 NSIS installer (.exe)

Every release also carries the updater feed (latest.json + .sig, minisign-signed),
the VS Code .vsix, and the rendered Homebrew cask (space-lens.rb).

macOS without Apple code signing. The binaries are ad-hoc signed only — there is no
Apple Developer ID behind them yet — so Gatekeeper blocks the first launch of a copy
downloaded from a browser. Clear the quarantine flag once, then open normally:

xattr -cr "/Applications/Space Lens.app"

(or right-click the app and choose Open once, or approve it under System Settings →
Privacy & Security
). Installing through Homebrew is not affected — Homebrew does not
quarantine casks.

Homebrew (macOS). The cask is versioned off the release DMG URLs and has a livecheck
watching new releases:

brew install --cask HuakunShen/homebrew-tap/space-lens

The cask's source of truth lives in
packaging/homebrew/Casks/space-lens.rb.
Each app-v* release renders it with the real version + sha256 (scripts/render-cask.py)
and attaches it to the GitHub release; the owner copies it into
HuakunShen/homebrew-tap as Casks/space-lens.rb.

Updates

The app checks https://github.com/HuakunShen/space-lens/releases/latest/download/latest.json
(the releases/latest marker always points at the newest app-v* release — see
docs/release.md). Update packages are verified against the minisign
public key shipped in tauri.conf.json, so an unsigned feed is refused.
The cask sets auto_updates true, so Homebrew won't fight the in-app updater.

Build the desktop app from source

yarn install
yarn desktop:build   # web desktop flavor -> apps/web/build-desktop, then tauri build
# -> apps/desktop/src-tauri/target/release/bundle/macos/Space Lens.app (macOS)

node scripts/build-desktop.ts --web-only builds just the frontend (frontend-only changes).
The order is load-bearing: Tauri embeds apps/web/build-desktop at compile time, so a
stale frontend would ship silently. Details live in
apps/desktop/README.md.

Releasing

Version bumping, publishing (crates.io / npm / Homebrew), tags, and the
packaging gotchas are documented step by step in docs/release.md.

Serve the web workbench

spacelens serve starts an authenticated HTTP host that serves the web UI and
speaks a closed JSON contract (@space-lens/contract): single-use pairing
tickets, bearer sessions, REST reads, and SSE scan events. Cleanup over the
web is trash-only; permanent deletion stays a CLI/TUI concern.

npx spacelens serve                    # loopback, read-only, current directory
npx spacelens serve --host 0.0.0.0 --allow-cidr 192.168.1.0/24
npx spacelens serve --allow-cleanup    # grant trash-based cleanup

Served pages cannot reach a model endpoint themselves (their CSP is
connect-src 'self'), so the AI advisor is proxied by the host. It is
operator-configured, never client-configured, and the API key stays on the
host:

SPACLENS_AI_API_KEY=sk-… npx spacelens serve \
  --ai-base-url https://api.openai.com/v1 --ai-model gpt-4o-mini

--ai-base-url and --ai-model are required together and enable the feature
(https requires; plaintext http is accepted only for localhost /
127.0.0.1 / [::1]). --ai-disable-thinking sends thinking:{type:"disabled"}
for reasoning models. The key comes only from SPACLENS_AI_API_KEY — there is
no key flag, it is never printed, and no API route lets a client change the
base URL, model or key.

API Usage

import { scanDirectory } from 'space-lens'

const tree = scanDirectory({
  directories: [process.cwd()],
  ignoreHidden: false,
  fullPath: false,
  respectGitignore: true,
  ignoredMode: 'summarize',
})

console.dir(tree, { depth: 3 })

Find cleanup candidates without deleting anything:

import { findCleanupCandidates, planCleanup } from 'space-lens'

const candidates = findCleanupCandidates({
  directories: [process.cwd()],
  presets: ['node', 'rust', 'gitignored'],
})

const plan = planCleanup({
  directories: [process.cwd()],
  presets: ['node'],
})

Directory Scanning

scanDirectory is intended for large folders where keeping every file node in memory is too expensive.
With ignoredMode: 'summarize', ignored directories such as target/ or node_modules/ are scanned for total size but returned as collapsed leaf nodes:

{
  name: 'target',
  path: '/path/to/project/target',
  size: 1238249472,
  children: [],
  ignored: true,
  collapsed: true
}

Use ignoredMode: 'exclude' to skip ignored paths entirely.

Cleanup Candidates

Cleanup APIs are dry-run oriented. findCleanupCandidates reports matching paths and sizes, and planCleanup returns a removal plan. The npm package does not execute deletion.

Initial presets:

  • node: reports node_modules.
  • rust: reports Cargo target directories.
  • gitignored: reports paths matched by .gitignore.

Rust CLI

The workspace includes a simple Rust CLI app:

cargo run -p spacelens -- scan ~/Dev --json
cargo run -p spacelens -- candidates ~/Dev --preset node
cargo run -p spacelens -- clean ~/Dev --preset node

clean defaults to dry-run. Add --execute only when you want to remove the planned paths.

iCloud local-copy MVP

The Rust core and CLI also include a macOS-only iCloud inspection/eviction flow. It is separate from the existing deletion cleanup code:

cargo run --release -p spacelens -- icloud --help
cargo run --release -p spacelens -- icloud inspect <absolute-path>
cargo run --release -p spacelens -- icloud plan <disposable-iCloud-test-folder>
cargo run --release -p spacelens -- icloud evict <disposable-iCloud-test-folder>

evict is dry-run by default. Real execution requires --execute and the interactive confirmation phrase. The implementation never falls back to deleting files. Do not use a real Lightroom or Photos folder as the first test target.

Rust eviction uses a bounded worker pool (8 concurrent items by default, capped
at 32), so a large plan does not create one task per file or process entries
serially. The same bounded execution path is used by the Rust CLI service.

The NAPI package exposes the same native boundary through ICloudSession, with opaque one-shot plan IDs and decimal-string byte counts. The current main TUI does not yet expose these iCloud actions; it still provides the existing scan/cleanup UI.

The published space-lens package supports both the native library and the TUI executable:

import { scanDirectory } from 'space-lens'
npx space-lens ~/Dev --preset rust

space-lens TUI CLI

The space-lens package remains importable as a native library and also exposes the Solid/Uniview TUI as a space-lens executable. It has two modes: scan for a disk usage tree and clean for selecting cleanup candidates and deleting them after confirmation. In Scan mode, d deletes the current file or directory after confirmation, while A deletes all discovered preset candidates at once. Preset candidates are highlighted in red, and scan rows support mouse focus and folder expansion. It runs on Node.js 20 or newer:

yarn tui ~/Dev --preset rust
yarn tui ~/Dev --preset node,gitignored --sort path
npx space-lens ~/Dev --preset rust

Inside the TUI, press tab to switch modes, space to select a cleanup candidate, x to request deletion, and enter to confirm. In Scan mode, use d for the current row or A for all preset candidates. Use Ctrl+C or q to quit.

Benchmark CLI

This repository includes a local CLI for benchmarking the directory scanner and exporting trees:

yarn bench ~/Dev
yarn bench ~/Dev --no-json-size
yarn bench ~/Dev --export-tree tree.json

Options:

--export-tree PATH
--json-size / --no-json-size
--ignore-hidden
--full-path
--respect-gitignore / --no-respect-gitignore
--ignored-mode summarize|exclude

Development

yarn install
yarn workspace space-lens build:debug
yarn workspace @space-lens/cli build
yarn workspace space-lens test
yarn workspace @space-lens/cli test
yarn workspace space-lens typecheck
cargo test --workspace

Useful local commands:

  • yarn build: build release bindings for the current platform.
  • yarn build:debug: build debug bindings for local testing.
  • yarn tui: run the Solid/Uniview cleanup candidate viewer.
  • yarn test: run Rust workspace tests and AVA tests.
  • yarn typecheck: type-check the TypeScript workspaces.
  • yarn bench: run the benchmark CLI from the space-lens npm workspace.

iCloud Free macOS app

The SwiftUI app lives in apps/icloud-free. From the repository root, the convenience recipes are:

just icloud-build       # release build and signed local .app bundle
just icloud-open        # open the existing bundle
just icloud-build-open  # build, then open
just icloud-test        # run Swift tests

Use CONFIGURATION=debug just icloud-build for a debug bundle. The app bundle is written to apps/icloud-free/.build/ICloudFree.app and is not installed into /Applications automatically.

Space Lens native macOS prototype

The native Space Lens macOS app lives in apps/space-lens-mac. It keeps a
synthetic demo map for safe UI/performance checks and now supports a read-only
real-folder scan through the separate Rust FFI facade. The scan can be
exported as the portable JSON snapshot. Normal filesystem cleanup uses an
explicit review flow that moves selected real nodes to the macOS Trash, so
they remain recoverable through Finder; it does not permanently delete files.
The MCP adapter is read-only. The macOS app also exposes the Apple-only iCloud Local Copies
capability and its bounded concurrent eviction flow; it never requests
downloads for cloud-only files.

just space-lens-mac-test
just space-lens-mac-build
just space-lens-mac-open

The portable snapshot contract is in packages/space-lens/src/snapshot.rs; the
separate C ABI facade and header are in packages/space-lens-ffi. The FFI
facade exposes read-only filesystem snapshots plus an opaque macOS-only iCloud
plan session. The native app can execute that session only after its explicit
confirmation flow; MCP remains read-only and does not expose the execution API.

Rust MCP adapter

The CLI has an opt-in, read-only MCP stdio adapter. Prebuilt installs (brew,
cargo binstall) carry it out of the box; source builds enable the mcp
feature explicitly:

spacelens mcp                                            # prebuilt binaries
cargo install spacelens --features mcp                   # from source
cargo run --release -p spacelens --features mcp -- mcp   # from a checkout
just space-lens-mcp                                      # from a checkout

Point any MCP client (Claude Desktop, ZCode, Cursor, …) at spacelens mcp
over stdio. It implements the MCP initialize lifecycle and
tools/list/tools/call for filesystem snapshot export, discovery of large
files / developer caches / gitignored paths, cleanup-candidate inspection,
platform capabilities, and read-only iCloud plans. It exposes no delete,
Trash, cloud eviction, or download tool. MCP messages use newline-delimited
JSON-RPC on stdin/stdout; diagnostics must not be written to stdout.

Web UI and desktop frontend

apps/web is the SvelteKit workbench: one SPA, two flavors. yarn workspace @space-lens/web build
is the browser flavor (served by spacelens serve and deployable to Cloudflare as a PWA);
yarn workspace @space-lens/web build:desktop is the desktop flavor compiled into the Tauri
shell (apps/desktop, yarn desktop:build). Both speak the same closed JSON contract
(@space-lens/contract) over HTTP/SSE (browser) or Tauri IPC (desktop).