dsh-disk-cleanup 0.0.0-stage → 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/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
+ }
@@ -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
@@ -0,0 +1,6 @@
1
+ {
2
+ "meta": {
3
+ "title": "Disk cleanup",
4
+ "description": "Keeps temporary files out of the workspace, and removes expired scratch files on a schedule."
5
+ }
6
+ }
package/locale/zh.json ADDED
@@ -0,0 +1,6 @@
1
+ {
2
+ "meta": {
3
+ "title": "磁盘清理",
4
+ "description": "把临时文件移出工作区,并按保留天数自动清理过期的暂存文件。"
5
+ }
6
+ }
package/package.json CHANGED
@@ -1,6 +1,87 @@
1
1
  {
2
2
  "name": "dsh-disk-cleanup",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.3.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
+ }