dsh-disk-cleanup 0.2.0 → 0.3.0
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 +10 -46
- package/cordis.patch.yml +1 -3
- package/lib/cleanup.js +91 -3
- package/lib/client.js +7 -3
- package/lib/config.js +2 -10
- package/lib/index.d.ts +1 -5
- package/lib/index.js +27 -44
- package/lib/prompt.js +1 -5
- package/locale/en.json +1 -1
- package/locale/zh.json +1 -1
- package/package.json +2 -2
- package/lib/runtimes.js +0 -178
package/README.md
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
# dsh-disk-cleanup
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
DSH 插件:为 shell 命令注入临时目录与依赖缓存变量,给 scratch 目录补齐写权限,在系统提示里说明临时文件落点,并自动清理过期的临时文件。
|
|
4
4
|
|
|
5
|
-
DSH
|
|
5
|
+
DSH 本身没有「临时文件目录」这个概念;模型写出的探针脚本默认都落在工作区内。这个插件提供统一的环境注入、scratch 目录授权、一段系统提示和一份过期清理,让临时文件有地方可去、也不会越攒越多。
|
|
6
6
|
|
|
7
7
|
## 它做什么
|
|
8
8
|
|
|
@@ -10,10 +10,10 @@ DSH 本身没有「临时文件目录」这个概念;模型写出的探针脚
|
|
|
10
10
|
|
|
11
11
|
| 机制 | 覆盖的情况 | 做法 |
|
|
12
12
|
|---|---|---|
|
|
13
|
-
| shell 环境注入 | 脚本内部用 `tempfile` / `[System.IO.Path]::GetTempPath()`
|
|
13
|
+
| shell 环境注入 | 脚本内部用 `tempfile` / `[System.IO.Path]::GetTempPath()` 之类 | 在 `pwsh` / `bash` 命令串前设置 `TMP` / `TEMP` / `TMPDIR`,指向 `<DSH_HOME>/cache/scratch` |
|
|
14
14
|
| 权限补齐 | workspace-write 下 scratch 目录对受限子进程不可写 | 给会话注册可写根 + 给目录补访问控制项 |
|
|
15
15
|
| 系统提示 | 模型不知道该往哪写 | 加一段祈使句,点明 scratch 位置、临时文件写那儿、用户文件留在工作区;路径每次构建时现算,只读会话不出这段 |
|
|
16
|
-
| 过期清理 | scratch 只增不减,旧探针脚本一直留着 | 按保留天数删掉 `<DSH_HOME>/cache/scratch`
|
|
16
|
+
| 过期清理 | scratch 只增不减,旧探针脚本一直留着 | 按保留天数删掉 `<DSH_HOME>/cache/scratch` 里过期的条目,并清掉残留的空目录,插件加载时与之后定期各扫一次 |
|
|
17
17
|
|
|
18
18
|
不改写任何工具入参的路径:模型给什么路径就写什么路径。要不要把临时文件放进 scratch 由提示词引导,不由插件替模型决定。
|
|
19
19
|
|
|
@@ -37,42 +37,17 @@ workspace-write 下写到工作区外面要两步授权,插件在会话创建
|
|
|
37
37
|
|
|
38
38
|
## 过期清理
|
|
39
39
|
|
|
40
|
-
|
|
40
|
+
清 `<DSH_HOME>/cache/scratch` 里的过期条目与空目录,另外删 `<DSH_HOME>` 下一级的空目录:
|
|
41
41
|
|
|
42
42
|
- **删什么**:`cache/scratch` 下的顶层条目,最后修改时间早于「现在减保留天数」的才删;目录整个删掉,含里面的内容。
|
|
43
|
-
-
|
|
43
|
+
- **空目录**:`cache/scratch` 里自底向上删空目录(父目录因此变空也一并删),另外删 `<DSH_HOME>` 下一级的空目录;都只删没有任何条目的目录。
|
|
44
|
+
- **不删什么**:只处理 `<DSH_HOME>/cache/scratch` 与 `<DSH_HOME>` 下一级的空目录,不往其它子目录里递归;配置里自己指了 `scratchDir` 的,那个目录也不自动清,里面的东西未必可丢。
|
|
44
45
|
- **怎么算过期**:看条目自己的最后修改时间,不看访问时间(Windows 默认不更新访问时间)。目录里某个子文件刚被写过,目录的修改时间会一并刷新,正在用的工作目录不会被误删。
|
|
45
46
|
- **何时清**:插件加载时一次,之后定期再扫,进程重启等于重新起算。
|
|
46
47
|
- **单个条目删不掉**(Windows 上被别的进程占着)就跳过,不中断整批。
|
|
47
48
|
|
|
48
49
|
在插件**详情页**的「过期清理」卡片里可以关掉自动清理,也可以改保留天数(1–365):天数留空恢复默认的 7 天。
|
|
49
50
|
|
|
50
|
-
## 运行时缓存
|
|
51
|
-
|
|
52
|
-
启动时在 `PATH` 里找 `pnpm` / `bun` / `uv` / `npm`,按 `runtimeCache` 决定接管哪些:
|
|
53
|
-
|
|
54
|
-
| 取值 | 行为 |
|
|
55
|
-
|---|---|
|
|
56
|
-
| `auto`(默认) | 只接管**没有现成缓存**的。用户已经有自己缓存位置的,用他自己的,不打扰 |
|
|
57
|
-
| `always` | 全部接管,缓存一律进 `<DSH_HOME>/cache/pkg` |
|
|
58
|
-
| `never` | 一个都不碰 |
|
|
59
|
-
|
|
60
|
-
`auto` 判的是「用户有没有现成的缓存」,不是「命令在不在 PATH 上」:缓存环境变量已经设了,或者该工具的默认缓存目录已经存在,就算用户有。命令在 PATH 上但一个缓存都没有时仍然接管:插件给的位置就是它第一次写缓存的地方。
|
|
61
|
-
|
|
62
|
-
接管的工具会被注入这些变量:
|
|
63
|
-
|
|
64
|
-
| 工具 | 变量 | 接管后落在 | 判默认位置 |
|
|
65
|
-
|---|---|---|---|
|
|
66
|
-
| pnpm | `pnpm_config_store_dir` / `pnpm_config_cache_dir` / `pnpm_config_state_dir` | `cache/pkg/pnpm/{store,cache,state}` | `%LOCALAPPDATA%\pnpm\store` |
|
|
67
|
-
| bun | `BUN_INSTALL_CACHE_DIR` | `cache/pkg/bun/cache` | `~/.bun/install/cache` |
|
|
68
|
-
| uv | `UV_CACHE_DIR` | `cache/pkg/uv/cache` | `%LOCALAPPDATA%\uv\cache` |
|
|
69
|
-
| npm | `npm_config_cache` | `cache/pkg/npm/cache` | `%LOCALAPPDATA%\npm-cache` |
|
|
70
|
-
|
|
71
|
-
pnpm 的三个目录键用 `pnpm_config_*` 前缀,npm 用 `npm_config_cache`,bun 用 `BUN_INSTALL_CACHE_DIR`,uv 用 `UV_CACHE_DIR`:各工具只认自己那套,不要混用前缀("配置键里的 `-` 写成 `_`" 这条规律只适用于 npm / pnpm 这类 npm 系配置)。命令里已经自己设过同名变量的不覆盖。
|
|
72
|
-
|
|
73
|
-
你的 pnpm 版本如果不认 `pnpm_config_*`,用 `pnpm config set store-dir <路径> --global` 自己把缓存位置写进配置,`runtimeCache` 保持 `auto` 即可。
|
|
74
|
-
|
|
75
|
-
项目自己的 `node_modules` 不接管——那是项目产物,属于工作区。同理,Python 项目的 `.venv`、Rust 的 `target`、各类 `__pycache__` 也一概不碰:插件只改包管理器的**全局缓存位置**,不动项目内的任何目录。
|
|
76
51
|
|
|
77
52
|
## 安装
|
|
78
53
|
|
|
@@ -82,12 +57,6 @@ pnpm 的三个目录键用 `pnpm_config_*` 前缀,npm 用 `npm_config_cache`
|
|
|
82
57
|
dsh plugin --profile <profile> add dsh-disk-cleanup
|
|
83
58
|
```
|
|
84
59
|
|
|
85
|
-
改插件源码时也可以用本地目录,路径按自己机器换:
|
|
86
|
-
|
|
87
|
-
```powershell
|
|
88
|
-
dsh plugin --profile <profile> add "link:<插件目录>"
|
|
89
|
-
```
|
|
90
|
-
|
|
91
60
|
安装后重启 DSH 生效。
|
|
92
61
|
|
|
93
62
|
## 配置
|
|
@@ -99,10 +68,9 @@ dsh plugin --profile <profile> add "link:<插件目录>"
|
|
|
99
68
|
name: 'dsh-disk-cleanup'
|
|
100
69
|
config:
|
|
101
70
|
enabled: true
|
|
102
|
-
scratchDir: '' #
|
|
71
|
+
scratchDir: '' # 留空 = 默认暂存目录并自动清理;自定义目录不清理
|
|
103
72
|
injectShellTemp: true # pwsh/bash 的 TMP/TEMP 指向 scratch
|
|
104
73
|
grantProcessWrite: true # 给 scratch 补访问控制项,让子进程也能写
|
|
105
|
-
runtimeCache: auto # auto / always / never
|
|
106
74
|
promptSection: true # 往系统提示里加一段说明
|
|
107
75
|
autoCleanup: true # 启动时清掉过期的临时文件(插件页也能改)
|
|
108
76
|
cleanupMaxAgeDays: 7 # 保留天数,1–365(插件页也能改)
|
|
@@ -115,7 +83,7 @@ node --test "test/*.test.mjs"
|
|
|
115
83
|
node test/e2e-shell.mjs
|
|
116
84
|
```
|
|
117
85
|
|
|
118
|
-
- `plugin` / `scratch` / `cleanup`:插件装配、纯函数与目录清理,覆盖 shell 包装、环境变量注入、可写根登记、提示词 section
|
|
86
|
+
- `plugin` / `scratch` / `cleanup`:插件装配、纯函数与目录清理,覆盖 shell 包装、环境变量注入、可写根登记、提示词 section、过期判定、空目录清理与节流。
|
|
119
87
|
- `e2e-shell`:生成的前缀真的交给 pwsh 执行,确认 TMP/TEMP 落到 scratch。
|
|
120
88
|
- `schema-probe`:核对 `Config` 能被真的 schemastery 构建,且默认值与实际生效的一致;要在装了本插件的 profile 目录里跑。
|
|
121
89
|
- `client-probe`:核对客户端产物 `lib/client.js` 能注册到插件页槽位、保存时写哪几个字段。
|
|
@@ -143,8 +111,4 @@ node scripts/build.mjs
|
|
|
143
111
|
- `run_code` 不覆盖(工具名被宿主保留,注册会抛错),也不在覆盖范围内。
|
|
144
112
|
- 挂载后会话一建立就会建 scratch 目录(`agent/created` 时一次性建好),不等第一次写文件。
|
|
145
113
|
- `sandbox_permissions` / `justification` 原样透传,权限升级链路不断。
|
|
146
|
-
- 客户端半边注册失败(比如宿主没有槽位服务)只影响插件页的卡片,shell 注入、授权与清理照常。
|
|
147
|
-
|
|
148
|
-
## 不做的事
|
|
149
|
-
|
|
150
|
-
`attachments` 与派生图片缓存的回收、会话归档触发的清理、包管理器缓存的体积上限。DSH 的图片缓存目前不会自动删除,处理它属于另一件事。
|
|
114
|
+
- 客户端半边注册失败(比如宿主没有槽位服务)只影响插件页的卡片,shell 注入、授权与清理照常。
|
package/cordis.patch.yml
CHANGED
|
@@ -7,10 +7,9 @@
|
|
|
7
7
|
# name: 'dsh-disk-cleanup'
|
|
8
8
|
# config:
|
|
9
9
|
# enabled: true
|
|
10
|
-
# # scratchDir: '' #
|
|
10
|
+
# # scratchDir: '' # 留空 = 默认暂存目录并自动清理;自定义目录不清理
|
|
11
11
|
# injectShellTemp: true # pwsh/bash 的 TMP/TEMP 指向 scratch
|
|
12
12
|
# grantProcessWrite: true # 给 scratch 补访问控制项,让子进程也能写
|
|
13
|
-
# runtimeCache: auto # auto / always / never
|
|
14
13
|
# promptSection: true # 往系统提示里加一段 scratch 说明
|
|
15
14
|
# autoCleanup: true # 启动时清理 scratch(插件页也能改)
|
|
16
15
|
# cleanupMaxAgeDays: 7 # 保留天数(插件页也能改)
|
|
@@ -22,5 +21,4 @@
|
|
|
22
21
|
enabled: true
|
|
23
22
|
injectShellTemp: true
|
|
24
23
|
grantProcessWrite: true
|
|
25
|
-
runtimeCache: auto
|
|
26
24
|
promptSection: true
|
package/lib/cleanup.js
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* scratch
|
|
2
|
+
* 临时目录的清理:scratch 的过期条目,以及残留的空目录。
|
|
3
3
|
*
|
|
4
4
|
* 只删 scratch 自己的条目:临时文件都是「写完就不再需要」的一次性产物,
|
|
5
|
-
*
|
|
5
|
+
* 删掉不会影响工作区里的项目。
|
|
6
|
+
* 空目录清理只认「目录为空」,不猜是谁留下的,也不删有内容的目录。
|
|
6
7
|
* @module dsh-disk-cleanup/cleanup
|
|
7
8
|
*/
|
|
8
|
-
import { readdir, rm, stat } from 'node:fs/promises'
|
|
9
|
+
import { lstat, readdir, rm, rmdir, stat } from 'node:fs/promises'
|
|
9
10
|
import { join } from 'node:path'
|
|
10
11
|
|
|
11
12
|
/** 一天有多少毫秒。 */
|
|
@@ -117,3 +118,90 @@ export function throttled(intervalMs) {
|
|
|
117
118
|
}
|
|
118
119
|
}
|
|
119
120
|
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* 清掉目录下的空目录,只删「一个条目都没有」的目录。
|
|
124
|
+
*
|
|
125
|
+
* recursive 为真时自底向上:空的子目录先删,父目录因此变空后一并删;
|
|
126
|
+
* 为假时只看直接子目录,不往里走。两种都不删 dir 本身。
|
|
127
|
+
* 用 rmdir 而不是 rm:目录此刻若被写入了内容,rmdir 会失败,不会连内容一起删。
|
|
128
|
+
* @param {object} options - 清理输入。
|
|
129
|
+
* @param {string} options.dir - 待清理的目录根。
|
|
130
|
+
* @param {boolean} [options.recursive] - 是否递归进子目录。
|
|
131
|
+
* @param {boolean} [options.dryRun] - 只统计不删除。
|
|
132
|
+
* @returns {Promise<{removed: number, failed: number, kept: number}>} 统计结果;目录不存在时全为 0。
|
|
133
|
+
*/
|
|
134
|
+
export async function cleanEmptyDirs({ dir, recursive = false, dryRun = false }) {
|
|
135
|
+
const result = { removed: 0, failed: 0, kept: 0 }
|
|
136
|
+
if (typeof dir !== 'string' || dir === '') return result
|
|
137
|
+
await pruneChildren(dir, recursive, dryRun, result)
|
|
138
|
+
return result
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* 删掉 dir 下所有空的子目录,返回 dir 清理后是否也空了。
|
|
143
|
+
*
|
|
144
|
+
* 返回值一路往上传,dryRun 下也能算出「子目录删掉后父目录跟着变空」的连锁,
|
|
145
|
+
* 而不是只看目录此刻的内容。
|
|
146
|
+
* @param {string} dir - 当前层目录。
|
|
147
|
+
* @param {boolean} recursive - 是否继续往子目录里走。
|
|
148
|
+
* @param {boolean} dryRun - 只统计不删除。
|
|
149
|
+
* @param {{removed: number, failed: number, kept: number}} result - 累计统计。
|
|
150
|
+
* @returns {Promise<boolean>} dir 清理后是否为空;读不到时按非空处理。
|
|
151
|
+
*/
|
|
152
|
+
async function pruneChildren(dir, recursive, dryRun, result) {
|
|
153
|
+
let names
|
|
154
|
+
try {
|
|
155
|
+
names = await readdir(dir)
|
|
156
|
+
} catch (error) {
|
|
157
|
+
if (error?.code !== 'ENOENT') result.failed += 1
|
|
158
|
+
return false
|
|
159
|
+
}
|
|
160
|
+
let hasContent = false
|
|
161
|
+
for (const name of names) {
|
|
162
|
+
const target = join(dir, name)
|
|
163
|
+
let info
|
|
164
|
+
try {
|
|
165
|
+
info = await lstat(target)
|
|
166
|
+
} catch {
|
|
167
|
+
hasContent = true
|
|
168
|
+
continue
|
|
169
|
+
}
|
|
170
|
+
// 符号链接和 junction 不进也不删,否则会递归到别的目录树里。
|
|
171
|
+
if (!info.isDirectory() || info.isSymbolicLink()) {
|
|
172
|
+
hasContent = true
|
|
173
|
+
continue
|
|
174
|
+
}
|
|
175
|
+
let empty
|
|
176
|
+
if (recursive) {
|
|
177
|
+
empty = await pruneChildren(target, true, dryRun, result)
|
|
178
|
+
} else {
|
|
179
|
+
let left
|
|
180
|
+
try {
|
|
181
|
+
left = await readdir(target)
|
|
182
|
+
empty = left.length === 0
|
|
183
|
+
} catch {
|
|
184
|
+
hasContent = true
|
|
185
|
+
result.kept += 1
|
|
186
|
+
continue
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
if (!empty) {
|
|
190
|
+
hasContent = true
|
|
191
|
+
result.kept += 1
|
|
192
|
+
continue
|
|
193
|
+
}
|
|
194
|
+
if (dryRun) {
|
|
195
|
+
result.removed += 1
|
|
196
|
+
continue
|
|
197
|
+
}
|
|
198
|
+
try {
|
|
199
|
+
await rmdir(target)
|
|
200
|
+
result.removed += 1
|
|
201
|
+
} catch {
|
|
202
|
+
result.failed += 1
|
|
203
|
+
hasContent = true
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
return !hasContent
|
|
207
|
+
}
|
package/lib/client.js
CHANGED
|
@@ -32,6 +32,9 @@ window.__ModuleLoader__.load({
|
|
|
32
32
|
/** 本地化命名空间。 */
|
|
33
33
|
const NS = 'settings.diskCleanup'
|
|
34
34
|
|
|
35
|
+
/** 保留天数默认值,与 lib/config.js 的 schema 默认值保持一致;输入框留空时置灰显示。 */
|
|
36
|
+
const DEFAULT_MAX_AGE_DAYS = '7'
|
|
37
|
+
|
|
35
38
|
/** 卡片里最小的必要样式:开关行左右排布、说明文字弱化。 */
|
|
36
39
|
const STYLE = {
|
|
37
40
|
row: { display: 'flex', alignItems: 'center', justifyContent: 'space-between', gap: '12px' },
|
|
@@ -43,9 +46,9 @@ window.__ModuleLoader__.load({
|
|
|
43
46
|
const MESSAGES = {
|
|
44
47
|
zh: {
|
|
45
48
|
title: '过期清理',
|
|
46
|
-
description: '
|
|
47
|
-
on: '
|
|
48
|
-
off: '
|
|
49
|
+
description: '自动清理 scratch 里过期的临时文件与残留的空目录。',
|
|
50
|
+
on: '开启后自动清理过期的临时文件与残留的空目录。',
|
|
51
|
+
off: '关闭后不再自动清理。',
|
|
49
52
|
maxAge: '保留天数',
|
|
50
53
|
maxAgeHint: 'scratch 里超过这么多天没改动过的条目会被删除。',
|
|
51
54
|
invalid: '请填一个正整数,或留空使用默认值。',
|
|
@@ -125,6 +128,7 @@ window.__ModuleLoader__.load({
|
|
|
125
128
|
resetLabel: t('reset'),
|
|
126
129
|
invalidLabel: t('invalid'),
|
|
127
130
|
numeric: true,
|
|
131
|
+
placeholder: DEFAULT_MAX_AGE_DAYS,
|
|
128
132
|
disabled,
|
|
129
133
|
...state.maxAgeDays,
|
|
130
134
|
onEdit: text => { props.edit('cleanupMaxAgeDays', text) },
|
package/lib/config.js
CHANGED
|
@@ -12,21 +12,17 @@ export const MAX_AGE_MIN = 1
|
|
|
12
12
|
/** 保留天数上限。 */
|
|
13
13
|
export const MAX_AGE_MAX = 365
|
|
14
14
|
|
|
15
|
-
/** runtimeCache 的合法取值。 */
|
|
16
|
-
export const RUNTIME_CACHE_POLICIES = ['auto', 'always', 'never']
|
|
17
|
-
|
|
18
15
|
/**
|
|
19
16
|
* 配置 schema,宿主用它渲染设置页表单。
|
|
20
17
|
* 标了 volatile 的字段改完立即生效,不必重载插件。
|
|
21
18
|
*/
|
|
22
19
|
export const Config = z.object({
|
|
23
|
-
enabled: z.boolean().default(true).description('
|
|
20
|
+
enabled: z.boolean().default(true).description('启用插件:注入临时目录、补齐写权限、注入系统提示'),
|
|
24
21
|
scratchDir: z.string().default('').description('scratch 目录;留空用 <DSH_HOME>/cache/scratch'),
|
|
25
22
|
injectShellTemp: z.boolean().default(true).description('把 pwsh / bash 的 TMP / TEMP / TMPDIR 指向 scratch'),
|
|
26
23
|
grantProcessWrite: z.boolean().default(true).description('Windows 上给 scratch 补访问控制项,让受限子进程也能写'),
|
|
27
|
-
runtimeCache: z.union(RUNTIME_CACHE_POLICIES).default('auto').description('包管理器缓存:auto 只接管没有现成缓存的,always 全接管,never 不碰'),
|
|
28
24
|
promptSection: z.boolean().default(true).description('往系统提示里加一段临时文件落点说明'),
|
|
29
|
-
autoCleanup: z.boolean().default(true).volatile().description('
|
|
25
|
+
autoCleanup: z.boolean().default(true).volatile().description('启动时清掉过期的临时文件与残留的空目录'),
|
|
30
26
|
cleanupMaxAgeDays: z.number().step(1).min(MAX_AGE_MIN).max(MAX_AGE_MAX).default(7).volatile()
|
|
31
27
|
.description('保留天数:scratch 里超过这么多天没改动过的条目会被删除'),
|
|
32
28
|
})
|
|
@@ -55,7 +51,6 @@ function fallbackDefaults() {
|
|
|
55
51
|
scratchDir: '',
|
|
56
52
|
injectShellTemp: true,
|
|
57
53
|
grantProcessWrite: true,
|
|
58
|
-
runtimeCache: 'auto',
|
|
59
54
|
promptSection: true,
|
|
60
55
|
autoCleanup: true,
|
|
61
56
|
cleanupMaxAgeDays: 7,
|
|
@@ -114,9 +109,6 @@ export function resolveConfig(raw) {
|
|
|
114
109
|
scratchDir: text(input.scratchDir, DEFAULT_CONFIG.scratchDir).trim(),
|
|
115
110
|
injectShellTemp: bool(input.injectShellTemp, DEFAULT_CONFIG.injectShellTemp),
|
|
116
111
|
grantProcessWrite: bool(input.grantProcessWrite, DEFAULT_CONFIG.grantProcessWrite),
|
|
117
|
-
runtimeCache: RUNTIME_CACHE_POLICIES.includes(input.runtimeCache)
|
|
118
|
-
? input.runtimeCache
|
|
119
|
-
: DEFAULT_CONFIG.runtimeCache,
|
|
120
112
|
promptSection: bool(input.promptSection, DEFAULT_CONFIG.promptSection),
|
|
121
113
|
autoCleanup: bool(input.autoCleanup, DEFAULT_CONFIG.autoCleanup),
|
|
122
114
|
cleanupMaxAgeDays: days,
|
package/lib/index.d.ts
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
import type { Context } from '@deepseek-ai/cordis'
|
|
2
2
|
|
|
3
|
-
/** runtimeCache 的合法取值。 */
|
|
4
|
-
export type RuntimeCachePolicy = 'auto' | 'always' | 'never'
|
|
5
3
|
|
|
6
4
|
/** 插件配置(与 `cordis.patch.yml` 里的 config 一致,全部可省略)。 */
|
|
7
5
|
export interface Config {
|
|
@@ -13,11 +11,9 @@ export interface Config {
|
|
|
13
11
|
injectShellTemp?: boolean
|
|
14
12
|
/** Windows 上给 scratch 补访问控制项,让受限子进程也能写。默认 true。 */
|
|
15
13
|
grantProcessWrite?: boolean
|
|
16
|
-
/** 包管理器缓存接管策略。默认 'auto'。 */
|
|
17
|
-
runtimeCache?: RuntimeCachePolicy
|
|
18
14
|
/** 往系统提示里加一段说明。默认 true。 */
|
|
19
15
|
promptSection?: boolean
|
|
20
|
-
/**
|
|
16
|
+
/** 启动时清掉过期的临时文件与残留的空目录。默认 true。 */
|
|
21
17
|
autoCleanup?: boolean
|
|
22
18
|
/** 保留天数,范围 1–365。默认 7。 */
|
|
23
19
|
cleanupMaxAgeDays?: number
|
package/lib/index.js
CHANGED
|
@@ -2,21 +2,19 @@
|
|
|
2
2
|
* dsh-disk-cleanup —— 给临时文件一个专用目录。
|
|
3
3
|
*
|
|
4
4
|
* 四条机制:
|
|
5
|
-
* - shell 环境注入:在 pwsh / bash
|
|
6
|
-
*
|
|
5
|
+
* - shell 环境注入:在 pwsh / bash 命令串前把 TMP / TEMP / TMPDIR 指向 scratch,
|
|
6
|
+
* 让脚本里的临时文件默认落在工作区之外。
|
|
7
7
|
* - 权限补齐:会话一创建就给 scratch 追加可写根(文件工具)和访问控制
|
|
8
8
|
* 列表授权(受限子进程),不依赖 shell 命令是否执行。
|
|
9
9
|
* - 系统提示:说明临时文件写 scratch、用户文件留在工作区。
|
|
10
|
-
* -
|
|
10
|
+
* - 清理:插件加载时扫一遍 scratch 删掉过期条目与空目录,并清掉 DSH home 下一级的空目录,之后定时检查。
|
|
11
11
|
* @module dsh-disk-cleanup
|
|
12
12
|
*/
|
|
13
|
-
import { join } from 'node:path'
|
|
14
13
|
import { Config, resolveConfig } from './config.js'
|
|
15
|
-
import { CLEANUP_INTERVAL_MS, cleanScratch, throttled } from './cleanup.js'
|
|
14
|
+
import { CLEANUP_INTERVAL_MS, cleanEmptyDirs, cleanScratch, throttled } from './cleanup.js'
|
|
16
15
|
import { dshHome, ensureDir, scratchDirFor, sharedScratchDir } from './scratch.js'
|
|
17
|
-
import {
|
|
16
|
+
import { injectShellTemp } from './shell-env.js'
|
|
18
17
|
import { grantProcessWrite, revokeAll } from './acl.js'
|
|
19
|
-
import { managedRuntimes, probeRuntimes, runtimeCacheDirs, runtimeCacheEnv } from './runtimes.js'
|
|
20
18
|
import { scratchPromptText } from './prompt.js'
|
|
21
19
|
|
|
22
20
|
/** Cordis 插件身份(与包名一致)。 */
|
|
@@ -54,19 +52,9 @@ export function apply(ctx, config) {
|
|
|
54
52
|
} catch { /* 宿主没有设置服务时保持默认,不影响其余机制 */ }
|
|
55
53
|
})
|
|
56
54
|
|
|
57
|
-
/** 包管理器探测结果,只在第一次需要时执行。 */
|
|
58
|
-
let probeCache
|
|
59
|
-
/**
|
|
60
|
-
* 取包管理器探测结果(进程内缓存)。
|
|
61
|
-
* @returns {Record<string, {present: boolean, path?: string}>} 探测结果。
|
|
62
|
-
*/
|
|
63
|
-
function probes() {
|
|
64
|
-
if (probeCache === undefined) probeCache = probeRuntimes(process.env)
|
|
65
|
-
return probeCache
|
|
66
|
-
}
|
|
67
55
|
|
|
68
56
|
/**
|
|
69
|
-
*
|
|
57
|
+
* 清理入口。插件加载时跑一次,之后每 CLEANUP_INTERVAL_MS 最多再跑一次;
|
|
70
58
|
* 没有常驻定时器,也不落状态文件——进程重启等于重新起算。
|
|
71
59
|
* @returns {Promise<void>}
|
|
72
60
|
*/
|
|
@@ -75,13 +63,19 @@ export function apply(ctx, config) {
|
|
|
75
63
|
* 执行一次清理(受节流控制)。
|
|
76
64
|
*
|
|
77
65
|
* 只清共享 scratch:配置里自己指了别处的用户,目录里的东西未必可丢,不替他决定。
|
|
66
|
+
* DSH home 下一级的空目录与 scratch 无关,单独清,只受总开关控制。
|
|
78
67
|
* @returns {Promise<void>}
|
|
79
68
|
*/
|
|
80
|
-
async function
|
|
81
|
-
if (!cfg.autoCleanup
|
|
69
|
+
async function cleanup() {
|
|
70
|
+
if (!cfg.autoCleanup) return
|
|
82
71
|
try {
|
|
83
72
|
await runCleanup(async () => {
|
|
84
|
-
|
|
73
|
+
if (cfg.scratchDir === '') {
|
|
74
|
+
const scratch = sharedScratchDir()
|
|
75
|
+
await cleanScratch({ dir: scratch, maxAgeDays: cfg.cleanupMaxAgeDays })
|
|
76
|
+
await cleanEmptyDirs({ dir: scratch, recursive: true })
|
|
77
|
+
}
|
|
78
|
+
await cleanEmptyDirs({ dir: dshHome() })
|
|
85
79
|
})
|
|
86
80
|
} catch {
|
|
87
81
|
/* 清理只是收尾,失败了不能连带插件加载失败 */
|
|
@@ -157,26 +151,19 @@ export function apply(ctx, config) {
|
|
|
157
151
|
* 每次调用都重新读策略:模式可能在会话中途变化,缓存住旧模式会把临时文件
|
|
158
152
|
* 送进当时并不允许写的目录。
|
|
159
153
|
* @param {object | undefined} session - 会话对象。
|
|
160
|
-
* @returns {{mode?: string, workspaceRoot?: string, scratchDir?: string
|
|
154
|
+
* @returns {{mode?: string, workspaceRoot?: string, scratchDir?: string}} 落点信息。
|
|
161
155
|
*/
|
|
162
156
|
function resolveState(session) {
|
|
163
157
|
const policy = policyFor(session)
|
|
164
158
|
const mode = policy.mode
|
|
165
159
|
const workspaceRoot = policy.workspaceRoot ?? session?.header?.cwd
|
|
166
|
-
const state = { mode, workspaceRoot, scratchDir: undefined
|
|
160
|
+
const state = { mode, workspaceRoot, scratchDir: undefined }
|
|
167
161
|
if (mode === undefined || mode === 'read-only') return state
|
|
168
162
|
|
|
169
163
|
const scratchDir = scratchDirFor({ mode, workspaceRoot, configured: cfg.scratchDir })
|
|
170
164
|
if (scratchDir === undefined) return state
|
|
171
165
|
state.scratchDir = scratchDir
|
|
172
166
|
|
|
173
|
-
// 依赖缓存只在真能写时才接管:写不进去的路径会让安装直接失败。
|
|
174
|
-
if (cfg.runtimeCache !== 'never') {
|
|
175
|
-
const cacheRoot = join(dshHome(), 'cache')
|
|
176
|
-
const managed = managedRuntimes(cfg.runtimeCache, probes())
|
|
177
|
-
state.cacheDirs = runtimeCacheDirs(cacheRoot, managed)
|
|
178
|
-
state.shellEnv = Object.entries(runtimeCacheEnv(cacheRoot, managed))
|
|
179
|
-
}
|
|
180
167
|
return state
|
|
181
168
|
}
|
|
182
169
|
|
|
@@ -232,11 +219,11 @@ export function apply(ctx, config) {
|
|
|
232
219
|
function activate(session, state) {
|
|
233
220
|
if (session === undefined || session === null) return Promise.resolve(false)
|
|
234
221
|
if (state.scratchDir === undefined) return Promise.resolve(false)
|
|
235
|
-
const key = [state.mode, state.scratchDir
|
|
222
|
+
const key = [state.mode, state.scratchDir].join('|')
|
|
236
223
|
const cached = prepared.get(session)
|
|
237
224
|
if (cached !== undefined && cached.key === key) return cached.promise
|
|
238
225
|
const promise = (async () => {
|
|
239
|
-
const dirs = [state.scratchDir
|
|
226
|
+
const dirs = [state.scratchDir]
|
|
240
227
|
let ok = true
|
|
241
228
|
for (const dir of dirs) {
|
|
242
229
|
if (!(await makeWritable(session, state.mode, state.workspaceRoot, dir))) {
|
|
@@ -259,8 +246,6 @@ export function apply(ctx, config) {
|
|
|
259
246
|
if (fallback === undefined) return false
|
|
260
247
|
if (!(await makeWritable(session, state.mode, state.workspaceRoot, fallback))) return false
|
|
261
248
|
state.scratchDir = fallback
|
|
262
|
-
state.cacheDirs = []
|
|
263
|
-
state.shellEnv = []
|
|
264
249
|
effective.set(session, state)
|
|
265
250
|
return true
|
|
266
251
|
})()
|
|
@@ -305,13 +290,13 @@ export function apply(ctx, config) {
|
|
|
305
290
|
}
|
|
306
291
|
|
|
307
292
|
/**
|
|
308
|
-
* 包装 shell
|
|
293
|
+
* 包装 shell 工具:在命令串前注入临时目录。
|
|
309
294
|
* @param {object} agentCtx - agent 作用域上下文。
|
|
310
295
|
* @param {'pwsh' | 'bash'} shell - shell 类型。
|
|
311
296
|
* @returns {boolean} 是否注册成功。
|
|
312
297
|
*/
|
|
313
298
|
function wrapShell(agentCtx, shell) {
|
|
314
|
-
if (!cfg.injectShellTemp
|
|
299
|
+
if (!cfg.injectShellTemp) return false
|
|
315
300
|
const live = ctx.tools.get(shell)
|
|
316
301
|
if (live === undefined) return false
|
|
317
302
|
agentCtx.tools.register({
|
|
@@ -325,8 +310,7 @@ export function apply(ctx, config) {
|
|
|
325
310
|
if (state.scratchDir === undefined) return live.execute(args, exec)
|
|
326
311
|
if (!(await activate(session, state))) return live.execute(args, exec)
|
|
327
312
|
// activate 成功即保证 scratchDir 有值。
|
|
328
|
-
|
|
329
|
-
if (state.shellEnv.length > 0) command = injectShellEnv(shell, command, state.shellEnv)
|
|
313
|
+
const command = injectShellTemp(shell, input.command, state.scratchDir)
|
|
330
314
|
if (command === input.command) return live.execute(args, exec)
|
|
331
315
|
return live.execute({ ...input, command }, exec)
|
|
332
316
|
},
|
|
@@ -397,13 +381,13 @@ export function apply(ctx, config) {
|
|
|
397
381
|
void prepareAgent(agent)
|
|
398
382
|
}
|
|
399
383
|
|
|
400
|
-
//
|
|
384
|
+
// 启动就跑一次清理,之后每 CLEANUP_INTERVAL_MS 再触发一次。
|
|
401
385
|
// 用原生定时器而不是 ctx.interval:后者要注入 cordis 的 timer 服务,
|
|
402
386
|
// 宿主没装就会整个插件起不来,为一次清理不值得。
|
|
403
387
|
// 定时器随插件的 effect 一起清掉,节流器保证两次真正落地间隔不小于间隔值。
|
|
404
|
-
void
|
|
388
|
+
void cleanup()
|
|
405
389
|
ctx.effect(() => {
|
|
406
|
-
const timer = setInterval(() => { void
|
|
390
|
+
const timer = setInterval(() => { void cleanup() }, CLEANUP_INTERVAL_MS)
|
|
407
391
|
// 不阻止进程退出:清理本身不紧急,宿主该退就退。
|
|
408
392
|
if (typeof timer.unref === 'function') timer.unref()
|
|
409
393
|
return () => clearInterval(timer)
|
|
@@ -430,8 +414,7 @@ export function apply(ctx, config) {
|
|
|
430
414
|
// 退路可能把 scratch 挪到了工作区,提示要跟着改口;但模式变了(比如切成只读)就先以新模式为准。
|
|
431
415
|
const applied = session === undefined ? undefined : effective.get(session)
|
|
432
416
|
const state = fresh.scratchDir === undefined ? fresh : applied ?? fresh
|
|
433
|
-
|
|
434
|
-
return scratchPromptText({ scratchDir: state.scratchDir, cacheDirs: state.cacheDirs })
|
|
417
|
+
return scratchPromptText({ scratchDir: state.scratchDir })
|
|
435
418
|
},
|
|
436
419
|
})
|
|
437
420
|
})
|
|
@@ -448,4 +431,4 @@ export function apply(ctx, config) {
|
|
|
448
431
|
revokeAll()
|
|
449
432
|
})
|
|
450
433
|
}
|
|
451
|
-
}
|
|
434
|
+
}
|
package/lib/prompt.js
CHANGED
|
@@ -10,18 +10,14 @@
|
|
|
10
10
|
* 模型既改不了也用不上,写进提示只是噪音。
|
|
11
11
|
* @param {object} options - 生成输入。
|
|
12
12
|
* @param {string} [options.scratchDir] - 当前会话的 scratch 目录。
|
|
13
|
-
* @param {string[]} [options.cacheDirs] - 被接管的依赖缓存目录。
|
|
14
13
|
* @returns {string} 一段提示。
|
|
15
14
|
*/
|
|
16
|
-
export function scratchPromptText({ scratchDir
|
|
15
|
+
export function scratchPromptText({ scratchDir } = {}) {
|
|
17
16
|
const lines = [
|
|
18
17
|
'Write temporary files (probe scripts, one-off checks, scratch drafts) to the scratch directory'
|
|
19
18
|
+ (scratchDir === undefined ? '' : ` (${scratchDir})`)
|
|
20
19
|
+ ': the shell\'s TMP/TEMP point there, and these files may be cleaned up later.',
|
|
21
20
|
'Files the user asked for stay in the workspace: give them a real relative path (for example src/main.ts or report.md) and they are written exactly where you say.',
|
|
22
21
|
]
|
|
23
|
-
if (cacheDirs.length > 0) {
|
|
24
|
-
lines.push(`Package-manager caches are configured under ${cacheDirs.length === 1 ? cacheDirs[0] : cacheDirs.join(', ')}: installs put their global store outside the workspace. Files a project generates itself, such as node_modules or .venv, still belong in the project directory.`)
|
|
25
|
-
}
|
|
26
22
|
return lines.join(' ')
|
|
27
23
|
}
|
package/locale/en.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"meta": {
|
|
3
3
|
"title": "Disk cleanup",
|
|
4
|
-
"description": "Keeps temporary files
|
|
4
|
+
"description": "Keeps temporary files out of the workspace, and removes expired scratch files on a schedule."
|
|
5
5
|
}
|
|
6
6
|
}
|
package/locale/zh.json
CHANGED
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-disk-cleanup",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "为 shell
|
|
3
|
+
"version": "0.3.0",
|
|
4
|
+
"description": "为 shell 命令注入临时目录,给 scratch 目录补齐写权限,在系统提示里点明临时文件该写哪里,并清理过期的临时文件。",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "lib/index.js",
|
|
7
7
|
"types": "lib/index.d.ts",
|
package/lib/runtimes.js
DELETED
|
@@ -1,178 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 探测本机装了哪些包管理器,并给出它们该用的缓存目录。
|
|
3
|
-
* @module dsh-disk-cleanup/runtimes
|
|
4
|
-
*/
|
|
5
|
-
import { accessSync, constants, statSync } from 'node:fs'
|
|
6
|
-
import { delimiter, join } from 'node:path'
|
|
7
|
-
|
|
8
|
-
/** Windows 上可执行文件的后缀表。 */
|
|
9
|
-
const EXECUTABLE_EXTS = (process.env.PATHEXT ?? '.COM;.EXE;.BAT;.CMD')
|
|
10
|
-
.split(';')
|
|
11
|
-
.map(item => item.trim())
|
|
12
|
-
.filter(item => item !== '')
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* 在 PATH 里找一个可执行文件。
|
|
16
|
-
* @param {string} command - 命令名,不带后缀。
|
|
17
|
-
* @param {NodeJS.ProcessEnv} [env] - 用于查找的环境变量,默认取当前进程。
|
|
18
|
-
* @returns {string | undefined} 命中时返回绝对路径。
|
|
19
|
-
*/
|
|
20
|
-
export function findOnPath(command, env = process.env) {
|
|
21
|
-
const raw = env.PATH ?? env.Path ?? ''
|
|
22
|
-
const dirs = raw.split(delimiter).filter(dir => dir !== '')
|
|
23
|
-
const suffixes = process.platform === 'win32' ? ['', ...EXECUTABLE_EXTS] : ['']
|
|
24
|
-
for (const dir of dirs) {
|
|
25
|
-
for (const suffix of suffixes) {
|
|
26
|
-
const candidate = join(dir, command + suffix)
|
|
27
|
-
try {
|
|
28
|
-
accessSync(candidate, constants.X_OK)
|
|
29
|
-
return candidate
|
|
30
|
-
} catch {
|
|
31
|
-
/* 不存在或不可执行,继续找 */
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
return undefined
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* 受管的包管理器。
|
|
40
|
-
*
|
|
41
|
-
* 两条判据都要能独立回答「用户是不是已经有自己的缓存」:
|
|
42
|
-
* - `cacheVars`:该工具认的缓存环境变量,已经设了就别覆盖;
|
|
43
|
-
* - `defaultCache`:没设环境变量时它会用的默认位置,存在说明用户在用它。
|
|
44
|
-
*
|
|
45
|
-
* pnpm 的三个目录变量用 `pnpm_config_` 前缀,npm 的用 `npm_config_` 前缀:
|
|
46
|
-
* 两者都是各工具认的配置键(配置键里的 `-` 写成 `_`),不要混用。
|
|
47
|
-
* @type {ReadonlyArray<{name: string, commands: string[], cacheVars: string[], defaultCache: (env: NodeJS.ProcessEnv) => string | undefined, env: (cacheRoot: string) => Record<string, string>}>}
|
|
48
|
-
*/
|
|
49
|
-
export const RUNTIMES = [
|
|
50
|
-
{
|
|
51
|
-
name: 'pnpm',
|
|
52
|
-
commands: ['pnpm'],
|
|
53
|
-
cacheVars: ['npm_config_store_dir', 'npm_config_cache_dir', 'npm_config_state_dir'],
|
|
54
|
-
defaultCache: env => env.LOCALAPPDATA === undefined ? undefined : join(env.LOCALAPPDATA, 'pnpm', 'store'),
|
|
55
|
-
env: cacheRoot => ({
|
|
56
|
-
npm_config_store_dir: join(cacheRoot, 'pnpm', 'store'),
|
|
57
|
-
npm_config_cache_dir: join(cacheRoot, 'pnpm', 'cache'),
|
|
58
|
-
npm_config_state_dir: join(cacheRoot, 'pnpm', 'state'),
|
|
59
|
-
}),
|
|
60
|
-
},
|
|
61
|
-
{
|
|
62
|
-
name: 'bun',
|
|
63
|
-
commands: ['bun'],
|
|
64
|
-
cacheVars: ['BUN_INSTALL_CACHE_DIR'],
|
|
65
|
-
defaultCache: env => env.USERPROFILE === undefined ? undefined : join(env.USERPROFILE, '.bun', 'install', 'cache'),
|
|
66
|
-
env: cacheRoot => ({
|
|
67
|
-
BUN_INSTALL_CACHE_DIR: join(cacheRoot, 'bun', 'cache'),
|
|
68
|
-
}),
|
|
69
|
-
},
|
|
70
|
-
{
|
|
71
|
-
name: 'uv',
|
|
72
|
-
commands: ['uv'],
|
|
73
|
-
cacheVars: ['UV_CACHE_DIR'],
|
|
74
|
-
defaultCache: env => env.LOCALAPPDATA === undefined ? undefined : join(env.LOCALAPPDATA, 'uv', 'cache'),
|
|
75
|
-
env: cacheRoot => ({
|
|
76
|
-
UV_CACHE_DIR: join(cacheRoot, 'uv', 'cache'),
|
|
77
|
-
}),
|
|
78
|
-
},
|
|
79
|
-
{
|
|
80
|
-
name: 'npm',
|
|
81
|
-
commands: ['npm'],
|
|
82
|
-
cacheVars: ['npm_config_cache'],
|
|
83
|
-
defaultCache: env => env.LOCALAPPDATA === undefined ? undefined : join(env.LOCALAPPDATA, 'npm-cache'),
|
|
84
|
-
env: cacheRoot => ({
|
|
85
|
-
npm_config_cache: join(cacheRoot, 'npm', 'cache'),
|
|
86
|
-
}),
|
|
87
|
-
},
|
|
88
|
-
]
|
|
89
|
-
|
|
90
|
-
/**
|
|
91
|
-
* 探测一个目录是否存在。
|
|
92
|
-
* @param {string | undefined} path - 目标目录;探不出来位置时为 undefined。
|
|
93
|
-
* @returns {boolean} 是否已经存在一个目录。
|
|
94
|
-
*/
|
|
95
|
-
function directoryExists(path) {
|
|
96
|
-
if (path === undefined) return false
|
|
97
|
-
try {
|
|
98
|
-
return statSync(path).isDirectory()
|
|
99
|
-
} catch {
|
|
100
|
-
return false
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
/**
|
|
105
|
-
* 逐个探测包管理器在本机的状态。
|
|
106
|
-
*
|
|
107
|
-
* `present` 只看 PATH:命令跑不跑得起来。
|
|
108
|
-
* `hasCache` 回答「用户是不是已经有自己的缓存」——环境变量设了,或默认位置已经存在。
|
|
109
|
-
* 两者不等价:装了 npm 却没进 PATH 时 present 为假,缓存却真实存在。
|
|
110
|
-
* @param {NodeJS.ProcessEnv} [env] - 用于查找的环境变量。
|
|
111
|
-
* @returns {Record<string, {present: boolean, path?: string, hasCache: boolean, cachePath?: string}>} 按名字索引的探测结果。
|
|
112
|
-
*/
|
|
113
|
-
export function probeRuntimes(env = process.env) {
|
|
114
|
-
const result = {}
|
|
115
|
-
for (const runtime of RUNTIMES) {
|
|
116
|
-
let found
|
|
117
|
-
for (const command of runtime.commands) {
|
|
118
|
-
found = findOnPath(command, env)
|
|
119
|
-
if (found !== undefined) break
|
|
120
|
-
}
|
|
121
|
-
const fromEnv = runtime.cacheVars.find(name => {
|
|
122
|
-
const value = env[name]
|
|
123
|
-
return typeof value === 'string' && value !== ''
|
|
124
|
-
})
|
|
125
|
-
const cachePath = fromEnv === undefined ? runtime.defaultCache(env) : env[fromEnv]
|
|
126
|
-
const hasCache = fromEnv !== undefined
|
|
127
|
-
|| (cachePath !== undefined && directoryExists(cachePath))
|
|
128
|
-
result[runtime.name] = {
|
|
129
|
-
...found === undefined ? {} : { path: found },
|
|
130
|
-
present: found !== undefined,
|
|
131
|
-
hasCache,
|
|
132
|
-
...cachePath === undefined ? {} : { cachePath },
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
return result
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
/**
|
|
139
|
-
* 决定哪些包管理器需要改缓存位置。
|
|
140
|
-
*
|
|
141
|
-
* auto 的判据是「用户有没有现成的缓存」,不是「命令在不在 PATH」:
|
|
142
|
-
* 有现成缓存的跳过,没有的才接管。命令在 PATH 上但没有任何缓存时接管是安全的,
|
|
143
|
-
* 插件给的位置就是它第一次写缓存的地方。
|
|
144
|
-
* @param {'auto' | 'always' | 'never'} policy - auto 只接管没有现成缓存的,always 全部接管,never 一个都不碰。
|
|
145
|
-
* @param {Record<string, {hasCache?: boolean}>} probes - probeRuntimes 的结果。
|
|
146
|
-
* @returns {Array<typeof RUNTIMES[number]>} 需要接管的条目。
|
|
147
|
-
*/
|
|
148
|
-
export function managedRuntimes(policy, probes) {
|
|
149
|
-
if (policy === 'never') return []
|
|
150
|
-
if (policy === 'always') return [...RUNTIMES]
|
|
151
|
-
return RUNTIMES.filter(runtime => probes[runtime.name]?.hasCache !== true)
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
/**
|
|
155
|
-
* 生成需要注入给子进程的缓存环境变量。
|
|
156
|
-
* @param {string} cacheRoot - 缓存根目录。
|
|
157
|
-
* @param {Array<typeof RUNTIMES[number]>} managed - 需要接管的条目。
|
|
158
|
-
* @returns {Record<string, string>} 环境变量表。
|
|
159
|
-
*/
|
|
160
|
-
export function runtimeCacheEnv(cacheRoot, managed) {
|
|
161
|
-
const env = {}
|
|
162
|
-
for (const runtime of managed) Object.assign(env, runtime.env(cacheRoot))
|
|
163
|
-
return env
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
/**
|
|
167
|
-
* 列出这些包管理器会用到的缓存目录(用于建目录与授权)。
|
|
168
|
-
* @param {string} cacheRoot - 缓存根目录。
|
|
169
|
-
* @param {Array<typeof RUNTIMES[number]>} managed - 需要接管的条目。
|
|
170
|
-
* @returns {string[]} 目录列表。
|
|
171
|
-
*/
|
|
172
|
-
export function runtimeCacheDirs(cacheRoot, managed) {
|
|
173
|
-
const dirs = new Set()
|
|
174
|
-
for (const runtime of managed) {
|
|
175
|
-
for (const value of Object.values(runtime.env(cacheRoot))) dirs.add(value)
|
|
176
|
-
}
|
|
177
|
-
return [...dirs]
|
|
178
|
-
}
|