dsh-disk-cleanup 0.0.0-stage → 0.2.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 +149 -2
- package/cordis.patch.yml +26 -0
- package/icon.svg +11 -0
- package/lib/acl.js +122 -0
- package/lib/cleanup.js +119 -0
- package/lib/client.js +190 -0
- package/lib/config.js +124 -0
- package/lib/index.d.ts +37 -0
- package/lib/index.js +451 -0
- package/lib/prompt.js +27 -0
- package/lib/runtimes.js +178 -0
- package/lib/schema.js +125 -0
- package/lib/scratch.js +57 -0
- package/lib/shell-env.js +90 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +85 -4
package/lib/runtimes.js
ADDED
|
@@ -0,0 +1,178 @@
|
|
|
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
|
+
}
|
package/lib/schema.js
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 取 schemastery 实例,并给出一条加载不到时的退路。
|
|
3
|
+
*
|
|
4
|
+
* 设置页靠插件导出的 Config schema 渲染表单,而 schema 必须用真的 schemastery
|
|
5
|
+
* 构建出来。包随 DSH 一起分发,插件以 link 或独立 node_modules 安装时解析规则
|
|
6
|
+
* 不保证覆盖到它,所以这里显式兜底:先按普通模块解析,再按 DSH 安装位置找。
|
|
7
|
+
* @module dsh-disk-cleanup/schema
|
|
8
|
+
*/
|
|
9
|
+
import { createRequire } from 'node:module'
|
|
10
|
+
import { dirname, join } from 'node:path'
|
|
11
|
+
import { pathToFileURL } from 'node:url'
|
|
12
|
+
|
|
13
|
+
/** schemastery 的包名。 */
|
|
14
|
+
const SCHEMA_PACKAGE = '@deepseek-ai/schemastery'
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* 按 DSH 的安装位置解析平台包。
|
|
18
|
+
* @returns {string | undefined} 包内入口的绝对路径;找不到时 undefined。
|
|
19
|
+
*/
|
|
20
|
+
function fromInstallRoot() {
|
|
21
|
+
const appRoot = join(dirname(process.execPath), 'resources', 'app.asar', 'dsh', 'node_modules')
|
|
22
|
+
try {
|
|
23
|
+
return createRequire(join(appRoot, 'noop.js')).resolve(SCHEMA_PACKAGE)
|
|
24
|
+
} catch {
|
|
25
|
+
return undefined
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* 取 schemastery 模块。
|
|
31
|
+
* @returns {Promise<object | undefined>} 模块导出;哪里都找不到时 undefined。
|
|
32
|
+
*/
|
|
33
|
+
async function load() {
|
|
34
|
+
try {
|
|
35
|
+
return await import(SCHEMA_PACKAGE)
|
|
36
|
+
} catch {
|
|
37
|
+
/* 平台包不在解析路径里,继续按安装位置找 */
|
|
38
|
+
}
|
|
39
|
+
const entry = fromInstallRoot()
|
|
40
|
+
if (entry === undefined) return undefined
|
|
41
|
+
try {
|
|
42
|
+
return await import(pathToFileURL(entry).href)
|
|
43
|
+
} catch {
|
|
44
|
+
return undefined
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* 空壳构建器:任何方法都返回它自己,方法链走得通、什么也不产生。
|
|
50
|
+
* 包彻底找不到时就用它,插件的配置因此退回手写默认值,其余机制照常工作。
|
|
51
|
+
* @returns {object} 只支持链式调用的假构建器。
|
|
52
|
+
*/
|
|
53
|
+
function inertZ() {
|
|
54
|
+
return new Proxy(function z() {}, {
|
|
55
|
+
apply() {
|
|
56
|
+
return inertZ()
|
|
57
|
+
},
|
|
58
|
+
get() {
|
|
59
|
+
return () => inertZ()
|
|
60
|
+
},
|
|
61
|
+
})
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** 真的 schemastery;模块求值阶段就要用,所以同步解析一次。 */
|
|
65
|
+
let loaded
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* 同步取构建器;ESM 里没有同步 import,所以用 CommonJS 的 require。
|
|
69
|
+
* 插件装在 profile 的 node_modules 下时,宿主与平台包是同级依赖,
|
|
70
|
+
* 用 profile 的 require(即 process.cwd() 那一级)能解析到;插件被单独
|
|
71
|
+
* 装在别处时解析不到,由下面的异步兜底接手。
|
|
72
|
+
* @returns {Function | undefined} 构建器;解析不到时 undefined。
|
|
73
|
+
*/
|
|
74
|
+
function syncLoad() {
|
|
75
|
+
for (const base of [import.meta.url, join(process.cwd(), 'noop.js')]) {
|
|
76
|
+
try {
|
|
77
|
+
// 这个包是 CommonJS:require 回来的是构建器本身(不是带 default 的命名空间)。
|
|
78
|
+
const mod = createRequire(base)(SCHEMA_PACKAGE)
|
|
79
|
+
if (typeof mod === 'function') return mod
|
|
80
|
+
if (typeof mod?.default === 'function') return mod.default
|
|
81
|
+
} catch {
|
|
82
|
+
/* 这一级解析不到,换下一级 */
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
return undefined
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
loaded = syncLoad()
|
|
89
|
+
|
|
90
|
+
if (typeof loaded !== 'function') {
|
|
91
|
+
load().then((mod) => {
|
|
92
|
+
if (typeof mod?.default === 'function') loaded = mod.default
|
|
93
|
+
})
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** 当前生效的构建器;异步兜底加载到真包后也由它跟上。 */
|
|
97
|
+
function builder() {
|
|
98
|
+
return typeof loaded === 'function' ? loaded : inertZ()
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* 本插件使用的 schemastery 实例。
|
|
103
|
+
* 函数一律补绑到构建器上,取属性时同步转发。
|
|
104
|
+
*/
|
|
105
|
+
export const z = new Proxy(inertZ(), {
|
|
106
|
+
// 顶层方法要能直接调用:z() 就是构建器本身,z.object(...) 之类也走这里。
|
|
107
|
+
apply(target, thisArg, args) {
|
|
108
|
+
return Reflect.apply(builder(), thisArg, args)
|
|
109
|
+
},
|
|
110
|
+
get(target, key) {
|
|
111
|
+
// Promise 解析会读 then:不挡住的话,任何 await 都会把 z 当成 thenable。
|
|
112
|
+
if (key === 'then') return undefined
|
|
113
|
+
const source = builder()
|
|
114
|
+
const value = source[key]
|
|
115
|
+
return typeof value === 'function' ? value.bind(source) : value
|
|
116
|
+
},
|
|
117
|
+
})
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* 判断真的 schemastery 是否已经就位。
|
|
121
|
+
* @returns {boolean} 构建器来自真包时为 true。
|
|
122
|
+
*/
|
|
123
|
+
export function schemaReady() {
|
|
124
|
+
return typeof loaded === 'function'
|
|
125
|
+
}
|
package/lib/scratch.js
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 权限模式 → scratch 目录;目录创建。
|
|
3
|
+
* @module dsh-disk-cleanup/scratch
|
|
4
|
+
*/
|
|
5
|
+
import { mkdir } from 'node:fs/promises'
|
|
6
|
+
import { homedir } from 'node:os'
|
|
7
|
+
import { join } from 'node:path'
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* 解析 DSH 的 home 目录:优先环境变量 DSH_HOME,否则 ~/.dsh。
|
|
11
|
+
* @returns {string} 绝对路径。
|
|
12
|
+
*/
|
|
13
|
+
export function dshHome() {
|
|
14
|
+
const fromEnv = process.env.DSH_HOME
|
|
15
|
+
if (typeof fromEnv === 'string' && fromEnv.trim() !== '') return fromEnv.trim()
|
|
16
|
+
return join(homedir(), '.dsh')
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* 共享的 scratch 目录:DSH home 下的 cache/scratch。
|
|
21
|
+
* @returns {string} 绝对路径。
|
|
22
|
+
*/
|
|
23
|
+
export function sharedScratchDir() {
|
|
24
|
+
return join(dshHome(), 'cache', 'scratch')
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* 按权限模式给出 scratch 目录。
|
|
29
|
+
*
|
|
30
|
+
* 两种可写模式都优先用 DSH home 下的共享目录,工作区因此不会长出临时文件;
|
|
31
|
+
* 共享目录写不进去时(无法追加可写根)退到工作区里的 cache/scratch。
|
|
32
|
+
* @param {object} options - 解析输入。
|
|
33
|
+
* @param {string} [options.mode] - 沙箱模式:workspace-write / danger-full-access / read-only。
|
|
34
|
+
* @param {string} [options.workspaceRoot] - 会话工作区根。
|
|
35
|
+
* @param {string} [options.configured] - 配置里显式指定的目录,优先于模式推导。
|
|
36
|
+
* @param {boolean} [options.sharedCache] - 是否可以用 DSH home 下的共享目录。
|
|
37
|
+
* @returns {string | undefined} 绝对路径;read-only 或无可用根时返回 undefined。
|
|
38
|
+
*/
|
|
39
|
+
export function scratchDirFor({ mode, workspaceRoot, configured, sharedCache = true } = {}) {
|
|
40
|
+
if (typeof configured === 'string' && configured.trim() !== '') return configured.trim()
|
|
41
|
+
if (mode === 'read-only') return undefined
|
|
42
|
+
const shared = sharedScratchDir()
|
|
43
|
+
if (sharedCache) return shared
|
|
44
|
+
if (typeof workspaceRoot === 'string' && workspaceRoot.trim() !== '') {
|
|
45
|
+
return join(workspaceRoot, 'cache', 'scratch')
|
|
46
|
+
}
|
|
47
|
+
return shared
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* 确保目录存在(幂等)。
|
|
52
|
+
* @param {string} dir - 目录绝对路径。
|
|
53
|
+
* @returns {Promise<void>} 完成信号。
|
|
54
|
+
*/
|
|
55
|
+
export async function ensureDir(dir) {
|
|
56
|
+
await mkdir(dir, { recursive: true })
|
|
57
|
+
}
|
package/lib/shell-env.js
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 往 pwsh / bash 的命令串前注入环境变量。
|
|
3
|
+
* @module dsh-disk-cleanup/shell-env
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* 单引号包裹并转义,供 pwsh 与 bash 共用。
|
|
8
|
+
* @param {string} value - 原始字符串。
|
|
9
|
+
* @param {'pwsh' | 'bash'} shell - 目标 shell。
|
|
10
|
+
* @returns {string} 已加引号的字面量。
|
|
11
|
+
*/
|
|
12
|
+
export function quote(value, shell) {
|
|
13
|
+
if (shell === 'pwsh') return `'${String(value).replace(/'/g, "''")}'`
|
|
14
|
+
return `'${String(value).replace(/'/g, "'\\''")}'`
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* 判断命令串是否自己设置过某个环境变量(用户显式指定时不覆盖)。
|
|
19
|
+
* @param {'pwsh' | 'bash'} shell - 目标 shell。
|
|
20
|
+
* @param {string} command - 原始命令串。
|
|
21
|
+
* @param {string} name - 环境变量名。
|
|
22
|
+
* @returns {boolean} 是否已自行设置。
|
|
23
|
+
*/
|
|
24
|
+
export function alreadySets(shell, command, name) {
|
|
25
|
+
if (typeof command !== 'string' || typeof name !== 'string' || name === '') return false
|
|
26
|
+
const escaped = name.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
|
|
27
|
+
const pattern = shell === 'pwsh'
|
|
28
|
+
? new RegExp(`\\$env:${escaped}\\s*=`, 'i')
|
|
29
|
+
: new RegExp(`(?:^|[;&|(\\s])(?:export\\s+)?${escaped}\\s*=`, 'i')
|
|
30
|
+
return pattern.test(command)
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* 判断命令串是否自己设置过临时目录。
|
|
35
|
+
* @param {'pwsh' | 'bash'} shell - 目标 shell。
|
|
36
|
+
* @param {string} command - 原始命令串。
|
|
37
|
+
* @returns {boolean} 是否已自行设置。
|
|
38
|
+
*/
|
|
39
|
+
export function alreadySetsTemp(shell, command) {
|
|
40
|
+
return alreadySets(shell, command, 'TMP')
|
|
41
|
+
|| alreadySets(shell, command, 'TEMP')
|
|
42
|
+
|| (shell === 'bash' && alreadySets(shell, command, 'TMPDIR'))
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* 生成环境变量赋值前缀。
|
|
47
|
+
* @param {'pwsh' | 'bash'} shell - 目标 shell。
|
|
48
|
+
* @param {Array<[string, string]>} entries - 变量名与值。
|
|
49
|
+
* @returns {string} 可直接拼在命令前的字符串。
|
|
50
|
+
*/
|
|
51
|
+
export function envPrefix(shell, entries) {
|
|
52
|
+
const parts = entries.map(([name, value]) => (shell === 'pwsh'
|
|
53
|
+
? `$env:${name}=${quote(value, 'pwsh')}; `
|
|
54
|
+
: `export ${name}=${quote(value, 'bash')}; `))
|
|
55
|
+
return parts.join('')
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* 给命令串注入环境变量前缀;命令自己设置过的变量跳过。
|
|
60
|
+
* @param {'pwsh' | 'bash'} shell - 目标 shell。
|
|
61
|
+
* @param {string} command - 原始命令串。
|
|
62
|
+
* @param {Array<[string, string]>} entries - 变量名与值。
|
|
63
|
+
* @returns {string} 注入后的命令串(无需注入时原样返回)。
|
|
64
|
+
*/
|
|
65
|
+
export function injectShellEnv(shell, command, entries) {
|
|
66
|
+
if (typeof command !== 'string' || command === '') return command
|
|
67
|
+
const usable = []
|
|
68
|
+
for (const [name, value] of entries) {
|
|
69
|
+
if (typeof value !== 'string' || value === '') continue
|
|
70
|
+
if (alreadySets(shell, command, name)) continue
|
|
71
|
+
usable.push([name, value])
|
|
72
|
+
}
|
|
73
|
+
if (usable.length === 0) return command
|
|
74
|
+
return envPrefix(shell, usable) + command
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* 只注入临时目录(TMP / TEMP,bash 另有 TMPDIR)。
|
|
79
|
+
* @param {'pwsh' | 'bash'} shell - 目标 shell。
|
|
80
|
+
* @param {string} command - 原始命令串。
|
|
81
|
+
* @param {string | undefined} dir - 临时目录;为空表示本次不注入。
|
|
82
|
+
* @returns {string} 注入后的命令串。
|
|
83
|
+
*/
|
|
84
|
+
export function injectShellTemp(shell, command, dir) {
|
|
85
|
+
if (typeof dir !== 'string' || dir === '') return command
|
|
86
|
+
const entries = shell === 'bash'
|
|
87
|
+
? [['TMPDIR', dir], ['TMP', dir], ['TEMP', dir]]
|
|
88
|
+
: [['TMP', dir], ['TEMP', dir]]
|
|
89
|
+
return injectShellEnv(shell, command, entries)
|
|
90
|
+
}
|
package/locale/en.json
ADDED
package/locale/zh.json
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,87 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-disk-cleanup",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "为 shell 命令注入临时目录与依赖缓存变量,给 scratch 目录补齐写权限,在系统提示里点明临时文件该写哪里,并清理过期的临时文件。",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "lib/index.js",
|
|
7
|
+
"types": "lib/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./lib/index.d.ts",
|
|
11
|
+
"default": "./lib/index.js"
|
|
12
|
+
},
|
|
13
|
+
"./client": {
|
|
14
|
+
"default": "./lib/client.js"
|
|
15
|
+
},
|
|
16
|
+
"./cordis.patch.yml": "./cordis.patch.yml",
|
|
17
|
+
"./package.json": "./package.json",
|
|
18
|
+
"./locale/*.json": "./locale/*.json",
|
|
19
|
+
"./icon": "./icon.svg"
|
|
20
|
+
},
|
|
21
|
+
"files": [
|
|
22
|
+
"lib",
|
|
23
|
+
"cordis.patch.yml",
|
|
24
|
+
"README.md",
|
|
25
|
+
"locale/*.json",
|
|
26
|
+
"icon.svg"
|
|
27
|
+
],
|
|
28
|
+
"publishConfig": {
|
|
29
|
+
"registry": "https://registry.npmjs.org/",
|
|
30
|
+
"access": "public"
|
|
31
|
+
},
|
|
32
|
+
"repository": {
|
|
33
|
+
"type": "git",
|
|
34
|
+
"url": "git+https://github.com/6starlong/dsh-disk-cleanup.git"
|
|
35
|
+
},
|
|
36
|
+
"homepage": "https://github.com/6starlong/dsh-disk-cleanup#readme",
|
|
37
|
+
"bugs": {
|
|
38
|
+
"url": "https://github.com/6starlong/dsh-disk-cleanup/issues"
|
|
39
|
+
},
|
|
40
|
+
"icon": "./icon.svg",
|
|
41
|
+
"scripts": {
|
|
42
|
+
"build": "node scripts/build.mjs",
|
|
43
|
+
"test": "node --test \"test/*.test.mjs\"",
|
|
44
|
+
"test:e2e": "node test/e2e-shell.mjs"
|
|
45
|
+
},
|
|
46
|
+
"dependencies": {},
|
|
47
|
+
"peerDependencies": {
|
|
48
|
+
"@deepseek-ai/cordis": "^4.1.0",
|
|
49
|
+
"@deepseek-ai/dsh-client-ui-primitives": "^0.2.2-rc.3",
|
|
50
|
+
"@deepseek-ai/schemastery": "*"
|
|
51
|
+
},
|
|
52
|
+
"peerDependenciesMeta": {
|
|
53
|
+
"@deepseek-ai/cordis": {
|
|
54
|
+
"optional": true
|
|
55
|
+
},
|
|
56
|
+
"@deepseek-ai/dsh-client-ui-primitives": {
|
|
57
|
+
"optional": true
|
|
58
|
+
},
|
|
59
|
+
"@deepseek-ai/schemastery": {
|
|
60
|
+
"optional": true
|
|
61
|
+
}
|
|
62
|
+
},
|
|
63
|
+
"dsh": {
|
|
64
|
+
"bundle": {
|
|
65
|
+
"patch": "./cordis.patch.yml"
|
|
66
|
+
},
|
|
67
|
+
"client": {
|
|
68
|
+
"inject": [
|
|
69
|
+
"dsh-client-ui-primitives"
|
|
70
|
+
],
|
|
71
|
+
"platform": "web"
|
|
72
|
+
}
|
|
73
|
+
},
|
|
74
|
+
"keywords": [
|
|
75
|
+
"dsh",
|
|
76
|
+
"deepseek-harness",
|
|
77
|
+
"plugin",
|
|
78
|
+
"scratch",
|
|
79
|
+
"temp",
|
|
80
|
+
"cleanup"
|
|
81
|
+
],
|
|
82
|
+
"license": "MIT",
|
|
83
|
+
"engines": {
|
|
84
|
+
"node": "^22.19.0 || >=24.0.0",
|
|
85
|
+
"dsh": ">=0.2.1-rc.2"
|
|
86
|
+
}
|
|
87
|
+
}
|