@uncarved/jade 0.1.3 → 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/bin/jade.js +5 -27
- package/dist/api.d.ts +266 -0
- package/dist/api.js +38 -0
- package/dist/api.js.map +1 -0
- package/dist/config.d.ts +13 -32
- package/dist/config.js +39 -121
- package/dist/config.js.map +1 -1
- package/dist/fetch.d.ts +20 -0
- package/dist/fetch.js +190 -0
- package/dist/fetch.js.map +1 -0
- package/dist/hash.d.ts +2 -0
- package/dist/hash.js +42 -0
- package/dist/hash.js.map +1 -0
- package/dist/host.d.ts +71 -0
- package/dist/host.js +400 -0
- package/dist/host.js.map +1 -0
- package/dist/index.d.ts +11 -30
- package/dist/index.js +11 -30
- package/dist/index.js.map +1 -1
- package/dist/install.d.ts +78 -0
- package/dist/install.js +292 -0
- package/dist/install.js.map +1 -0
- package/dist/main.d.ts +1 -2
- package/dist/main.js +159 -1362
- package/dist/main.js.map +1 -1
- package/dist/manifest.d.ts +23 -0
- package/dist/manifest.js +159 -0
- package/dist/manifest.js.map +1 -0
- package/dist/membrane.d.ts +33 -0
- package/dist/membrane.js +140 -0
- package/dist/membrane.js.map +1 -0
- package/dist/remote.d.ts +10 -0
- package/dist/remote.js +292 -0
- package/dist/remote.js.map +1 -0
- package/dist/rescue.d.ts +10 -0
- package/dist/rescue.js +216 -0
- package/dist/rescue.js.map +1 -0
- package/dist/resolve.d.ts +34 -0
- package/dist/resolve.js +178 -0
- package/dist/resolve.js.map +1 -0
- package/dist/rpc.d.ts +26 -0
- package/dist/rpc.js +144 -0
- package/dist/rpc.js.map +1 -0
- package/dist/secrets.d.ts +12 -0
- package/dist/secrets.js +125 -0
- package/dist/secrets.js.map +1 -0
- package/dist/session.d.ts +45 -0
- package/dist/session.js +401 -0
- package/dist/session.js.map +1 -0
- package/dist/source.d.ts +12 -0
- package/dist/source.js +72 -0
- package/dist/source.js.map +1 -0
- package/dist/state.d.ts +37 -0
- package/dist/state.js +93 -0
- package/dist/state.js.map +1 -0
- package/dist/update.d.ts +2 -0
- package/dist/update.js +6 -3
- package/dist/update.js.map +1 -1
- package/package.json +10 -8
- package/plugins/approval/index.ts +297 -0
- package/plugins/approval/plugin.json +18 -0
- package/{src/plugins → plugins/approval}/policy.ts +21 -100
- package/{src/plugins → plugins/approval}/review.ts +62 -16
- package/plugins/ask/index.ts +159 -0
- package/plugins/ask/plugin.json +8 -0
- package/plugins/budget/index.ts +38 -0
- package/plugins/budget/plugin.json +13 -0
- package/{src/plugins → plugins/checks}/checks.ts +48 -38
- package/plugins/checks/index.ts +158 -0
- package/plugins/checks/plugin.json +14 -0
- package/plugins/checks/project.ts +81 -0
- package/plugins/commands/index.ts +76 -0
- package/plugins/commands/plugin.json +7 -0
- package/{src/plugins → plugins/compact}/compact.ts +73 -38
- package/plugins/compact/index.ts +115 -0
- package/plugins/compact/plugin.json +17 -0
- package/plugins/compact/style.ts +17 -0
- package/plugins/continue/index.ts +58 -0
- package/plugins/continue/plugin.json +13 -0
- package/{src/ui → plugins/fs}/diff.ts +2 -2
- package/{src/tools → plugins/fs}/fs.ts +19 -42
- package/{src/tools → plugins/fs}/grep.ts +1 -1
- package/plugins/fs/index.ts +34 -0
- package/plugins/fs/plugin.json +7 -0
- package/{src/ui → plugins/fs}/renderers.ts +25 -70
- package/plugins/fs/style.ts +18 -0
- package/plugins/fs/sys.ts +81 -0
- package/plugins/keys/index.ts +53 -0
- package/plugins/keys/plugin.json +7 -0
- package/plugins/mcp/index.ts +192 -0
- package/{src → plugins/mcp}/mcp.ts +32 -32
- package/plugins/mcp/plugin.json +7 -0
- package/plugins/mcp/project.ts +93 -0
- package/plugins/memory/index.ts +105 -0
- package/{src → plugins/memory}/memory.ts +20 -2
- package/plugins/memory/notes.ts +43 -0
- package/plugins/memory/plugin.json +7 -0
- package/plugins/memory/style.ts +17 -0
- package/plugins/mentions/attach.ts +127 -0
- package/plugins/mentions/index.ts +79 -0
- package/plugins/mentions/plugin.json +7 -0
- package/plugins/mentions/style.ts +17 -0
- package/plugins/models/config.ts +53 -0
- package/plugins/models/configure.ts +455 -0
- package/plugins/models/context.ts +32 -0
- package/plugins/models/index.ts +198 -0
- package/plugins/models/plugin.json +8 -0
- package/{src → plugins/models}/provider.ts +16 -119
- package/plugins/models/saved.ts +94 -0
- package/plugins/modes/index.ts +222 -0
- package/plugins/modes/plugin.json +9 -0
- package/plugins/plugins/index.ts +38 -0
- package/plugins/plugins/plugin.json +7 -0
- package/plugins/print/index.ts +142 -0
- package/plugins/print/plugin.json +8 -0
- package/plugins/prompts/index.ts +83 -0
- package/plugins/prompts/plugin.json +8 -0
- package/plugins/prompts/prompts.ts +253 -0
- package/plugins/prompts/sys.ts +72 -0
- package/plugins/renderers/index.ts +78 -0
- package/plugins/renderers/plugin.json +7 -0
- package/plugins/repeats/index.ts +165 -0
- package/plugins/repeats/plugin.json +6 -0
- package/plugins/schedule/cron.ts +74 -0
- package/plugins/schedule/index.ts +292 -0
- package/plugins/schedule/plugin.json +13 -0
- package/plugins/schedule/store.ts +149 -0
- package/plugins/scratch/index.ts +31 -0
- package/plugins/scratch/plugin.json +7 -0
- package/plugins/screenshot/index.ts +40 -0
- package/plugins/screenshot/plugin.json +7 -0
- package/plugins/screenshot/sys.ts +84 -0
- package/{src/tools/screenshot.ts → plugins/screenshot/tool.ts} +6 -6
- package/plugins/sessions/index.ts +148 -0
- package/plugins/sessions/plugin.json +8 -0
- package/plugins/shell/index.ts +33 -0
- package/plugins/shell/plugin.json +17 -0
- package/plugins/shell/renderers.ts +45 -0
- package/plugins/shell/service.ts +44 -0
- package/{src/tools → plugins/shell}/shell.ts +31 -20
- package/{src → plugins/shell}/sys.ts +2 -43
- package/plugins/skills/index.ts +96 -0
- package/plugins/skills/plugin.json +7 -0
- package/plugins/skills/project.ts +49 -0
- package/{src → plugins/skills}/skills.ts +4 -11
- package/plugins/suggest/index.ts +286 -0
- package/plugins/suggest/plugin.json +18 -0
- package/plugins/task/index.ts +167 -0
- package/plugins/task/plugin.json +15 -0
- package/plugins/task/style.ts +17 -0
- package/plugins/task/subagent.ts +257 -0
- package/{src/tools/todo.ts → plugins/todo/index.ts} +33 -7
- package/plugins/todo/plugin.json +7 -0
- package/{src/ui → plugins/tui}/editor.ts +3 -0
- package/plugins/tui/frontend.ts +596 -0
- package/plugins/tui/index.ts +37 -0
- package/plugins/tui/mentions.ts +113 -0
- package/plugins/tui/plugin.json +10 -0
- package/{src/ui → plugins/tui}/render.ts +8 -2
- package/{src/ui → plugins/tui}/screen.ts +8 -3
- package/{src/ui → plugins/tui}/transcript.ts +74 -99
- package/{src/ui/tui.tsx → plugins/tui/tui.ts} +358 -387
- package/{src/tools → plugins/undo}/checkpoint.ts +21 -60
- package/plugins/undo/index.ts +139 -0
- package/plugins/undo/notes.ts +43 -0
- package/plugins/undo/plugin.json +7 -0
- package/plugins/undo/style.ts +17 -0
- package/plugins/update/index.ts +40 -0
- package/plugins/update/plugin.json +13 -0
- package/plugins/web/env.ts +78 -0
- package/plugins/web/index.ts +133 -0
- package/plugins/web/plugin.json +15 -0
- package/plugins/web/renderers.ts +11 -0
- package/{src/tools → plugins/web}/web.ts +58 -26
- package/plugins-dist/approval/index.js +271 -0
- package/plugins-dist/approval/plugin.json +24 -0
- package/{dist/plugins → plugins-dist/approval}/policy.js +15 -81
- package/{dist/plugins → plugins-dist/approval}/review.js +56 -10
- package/{dist/plugins → plugins-dist/approval}/schema.js +0 -1
- package/plugins-dist/ask/index.js +145 -0
- package/plugins-dist/ask/plugin.json +12 -0
- package/plugins-dist/budget/index.js +37 -0
- package/plugins-dist/budget/plugin.json +15 -0
- package/{dist/plugins → plugins-dist/checks}/checks.js +46 -35
- package/plugins-dist/checks/index.js +150 -0
- package/plugins-dist/checks/plugin.json +21 -0
- package/plugins-dist/checks/project.js +76 -0
- package/plugins-dist/commands/index.js +33 -0
- package/plugins-dist/commands/plugin.json +9 -0
- package/{dist/plugins → plugins-dist/compact}/compact.js +51 -33
- package/plugins-dist/compact/index.js +100 -0
- package/plugins-dist/compact/plugin.json +25 -0
- package/plugins-dist/compact/style.js +14 -0
- package/plugins-dist/continue/index.js +57 -0
- package/plugins-dist/continue/plugin.json +15 -0
- package/{dist/ui → plugins-dist/fs}/diff.js +2 -3
- package/{dist/tools → plugins-dist/fs}/fs.js +17 -42
- package/{dist/tools → plugins-dist/fs}/grep.js +1 -2
- package/plugins-dist/fs/index.js +36 -0
- package/plugins-dist/fs/plugin.json +10 -0
- package/{dist/ui → plugins-dist/fs}/renderers.js +24 -61
- package/plugins-dist/fs/style.js +15 -0
- package/plugins-dist/fs/sys.js +54 -0
- package/plugins-dist/keys/index.js +36 -0
- package/plugins-dist/keys/plugin.json +9 -0
- package/plugins-dist/mcp/index.js +187 -0
- package/{dist → plugins-dist/mcp}/mcp.js +34 -30
- package/plugins-dist/mcp/plugin.json +12 -0
- package/plugins-dist/mcp/project.js +90 -0
- package/plugins-dist/memory/index.js +94 -0
- package/{dist → plugins-dist/memory}/memory.js +21 -3
- package/plugins-dist/memory/notes.js +36 -0
- package/plugins-dist/memory/plugin.json +11 -0
- package/plugins-dist/memory/style.js +14 -0
- package/plugins-dist/mentions/attach.js +120 -0
- package/plugins-dist/mentions/index.js +69 -0
- package/plugins-dist/mentions/plugin.json +9 -0
- package/plugins-dist/mentions/style.js +14 -0
- package/plugins-dist/models/config.js +53 -0
- package/plugins-dist/models/configure.js +415 -0
- package/plugins-dist/models/context.js +27 -0
- package/plugins-dist/models/index.js +167 -0
- package/plugins-dist/models/plugin.json +14 -0
- package/{dist → plugins-dist/models}/provider.js +18 -81
- package/plugins-dist/models/saved.js +80 -0
- package/plugins-dist/modes/index.js +190 -0
- package/plugins-dist/modes/plugin.json +18 -0
- package/plugins-dist/plugins/index.js +37 -0
- package/plugins-dist/plugins/plugin.json +10 -0
- package/plugins-dist/print/index.js +153 -0
- package/plugins-dist/print/plugin.json +14 -0
- package/plugins-dist/prompts/index.js +57 -0
- package/plugins-dist/prompts/plugin.json +13 -0
- package/plugins-dist/prompts/prompts.js +227 -0
- package/plugins-dist/prompts/sys.js +46 -0
- package/plugins-dist/renderers/index.js +36 -0
- package/plugins-dist/renderers/plugin.json +9 -0
- package/plugins-dist/repeats/index.js +165 -0
- package/plugins-dist/repeats/plugin.json +6 -0
- package/plugins-dist/schedule/cron.js +67 -0
- package/plugins-dist/schedule/index.js +281 -0
- package/plugins-dist/schedule/plugin.json +16 -0
- package/plugins-dist/schedule/store.js +125 -0
- package/plugins-dist/scratch/index.js +26 -0
- package/plugins-dist/scratch/plugin.json +10 -0
- package/plugins-dist/screenshot/index.js +35 -0
- package/plugins-dist/screenshot/plugin.json +11 -0
- package/plugins-dist/screenshot/sys.js +76 -0
- package/{dist/tools/screenshot.js → plugins-dist/screenshot/tool.js} +5 -4
- package/plugins-dist/sessions/index.js +136 -0
- package/plugins-dist/sessions/plugin.json +12 -0
- package/plugins-dist/shell/index.js +33 -0
- package/plugins-dist/shell/plugin.json +19 -0
- package/plugins-dist/shell/renderers.js +40 -0
- package/plugins-dist/shell/service.js +29 -0
- package/{dist/tools → plugins-dist/shell}/shell.js +31 -22
- package/{dist → plugins-dist/shell}/sys.js +2 -43
- package/plugins-dist/skills/index.js +90 -0
- package/plugins-dist/skills/plugin.json +12 -0
- package/plugins-dist/skills/project.js +47 -0
- package/{dist → plugins-dist/skills}/skills.js +3 -7
- package/plugins-dist/suggest/index.js +282 -0
- package/plugins-dist/suggest/plugin.json +23 -0
- package/plugins-dist/task/index.js +151 -0
- package/plugins-dist/task/plugin.json +23 -0
- package/plugins-dist/task/style.js +14 -0
- package/plugins-dist/task/subagent.js +208 -0
- package/{dist/tools/todo.js → plugins-dist/todo/index.js} +29 -7
- package/plugins-dist/todo/plugin.json +9 -0
- package/{dist/ui → plugins-dist/tui}/editor.js +3 -1
- package/plugins-dist/tui/frontend.js +586 -0
- package/{dist → plugins-dist/tui}/history.js +0 -1
- package/plugins-dist/tui/index.js +21 -0
- package/{dist/ui → plugins-dist/tui}/markdown.js +0 -1
- package/plugins-dist/tui/mentions.js +115 -0
- package/plugins-dist/tui/plugin.json +25 -0
- package/{dist/ui → plugins-dist/tui}/render.js +7 -3
- package/{dist/ui → plugins-dist/tui}/screen.js +6 -2
- package/{dist/ui → plugins-dist/tui}/transcript.js +76 -98
- package/{dist/ui → plugins-dist/tui}/tui.js +185 -53
- package/{dist/tools → plugins-dist/undo}/checkpoint.js +21 -55
- package/plugins-dist/undo/index.js +140 -0
- package/plugins-dist/undo/notes.js +36 -0
- package/plugins-dist/undo/plugin.json +9 -0
- package/plugins-dist/undo/style.js +14 -0
- package/plugins-dist/update/index.js +34 -0
- package/plugins-dist/update/plugin.json +15 -0
- package/plugins-dist/web/env.js +80 -0
- package/plugins-dist/web/index.js +124 -0
- package/plugins-dist/web/plugin.json +21 -0
- package/plugins-dist/web/renderers.js +7 -0
- package/{dist/tools → plugins-dist/web}/web.js +50 -24
- package/sdk/python/__pycache__/jade_plugin.cpython-312.pyc +0 -0
- package/sdk/python/jade_plugin.py +380 -0
- package/src/api.ts +298 -0
- package/src/config.ts +32 -134
- package/src/fetch.ts +216 -0
- package/src/hash.ts +39 -0
- package/src/host.ts +429 -0
- package/src/index.ts +23 -30
- package/src/install.ts +355 -0
- package/src/main.ts +155 -1387
- package/src/manifest.ts +160 -0
- package/src/membrane.ts +142 -0
- package/src/remote.ts +288 -0
- package/src/rescue.ts +217 -0
- package/src/resolve.ts +199 -0
- package/src/rpc.ts +144 -0
- package/src/secrets.ts +122 -0
- package/src/session.ts +458 -0
- package/src/source.ts +83 -0
- package/src/state.ts +120 -0
- package/src/update.ts +265 -262
- package/LICENSE +0 -21
- package/README.md +0 -204
- package/dist/commands.d.ts +0 -50
- package/dist/commands.js +0 -126
- package/dist/commands.js.map +0 -1
- package/dist/context.d.ts +0 -17
- package/dist/context.js +0 -53
- package/dist/context.js.map +0 -1
- package/dist/distro.d.ts +0 -141
- package/dist/distro.js +0 -462
- package/dist/distro.js.map +0 -1
- package/dist/history.d.ts +0 -10
- package/dist/history.js.map +0 -1
- package/dist/mcp.d.ts +0 -114
- package/dist/mcp.js.map +0 -1
- package/dist/memory.d.ts +0 -19
- package/dist/memory.js.map +0 -1
- package/dist/mentions.d.ts +0 -35
- package/dist/mentions.js +0 -130
- package/dist/mentions.js.map +0 -1
- package/dist/plugin-cli.d.ts +0 -16
- package/dist/plugin-cli.js +0 -294
- package/dist/plugin-cli.js.map +0 -1
- package/dist/plugins/budget.d.ts +0 -9
- package/dist/plugins/budget.js +0 -30
- package/dist/plugins/budget.js.map +0 -1
- package/dist/plugins/builtins.d.ts +0 -10
- package/dist/plugins/builtins.js +0 -25
- package/dist/plugins/builtins.js.map +0 -1
- package/dist/plugins/checks.d.ts +0 -71
- package/dist/plugins/checks.js.map +0 -1
- package/dist/plugins/compact.d.ts +0 -26
- package/dist/plugins/compact.js.map +0 -1
- package/dist/plugins/core.d.ts +0 -47
- package/dist/plugins/core.js +0 -229
- package/dist/plugins/core.js.map +0 -1
- package/dist/plugins/host.d.ts +0 -52
- package/dist/plugins/host.js +0 -115
- package/dist/plugins/host.js.map +0 -1
- package/dist/plugins/install.d.ts +0 -68
- package/dist/plugins/install.js +0 -377
- package/dist/plugins/install.js.map +0 -1
- package/dist/plugins/loader.d.ts +0 -410
- package/dist/plugins/loader.js +0 -407
- package/dist/plugins/loader.js.map +0 -1
- package/dist/plugins/policy.d.ts +0 -98
- package/dist/plugins/policy.js.map +0 -1
- package/dist/plugins/prompts.d.ts +0 -43
- package/dist/plugins/prompts.js +0 -284
- package/dist/plugins/prompts.js.map +0 -1
- package/dist/plugins/repeats.d.ts +0 -16
- package/dist/plugins/repeats.js +0 -151
- package/dist/plugins/repeats.js.map +0 -1
- package/dist/plugins/review.d.ts +0 -18
- package/dist/plugins/review.js.map +0 -1
- package/dist/plugins/schema.d.ts +0 -2
- package/dist/plugins/schema.js.map +0 -1
- package/dist/plugins/sessions.d.ts +0 -7
- package/dist/plugins/sessions.js +0 -110
- package/dist/plugins/sessions.js.map +0 -1
- package/dist/provider.d.ts +0 -165
- package/dist/provider.js.map +0 -1
- package/dist/session-lifecycle.d.ts +0 -61
- package/dist/session-lifecycle.js +0 -167
- package/dist/session-lifecycle.js.map +0 -1
- package/dist/session-scope.d.ts +0 -53
- package/dist/session-scope.js +0 -116
- package/dist/session-scope.js.map +0 -1
- package/dist/sessions.d.ts +0 -28
- package/dist/sessions.js +0 -73
- package/dist/sessions.js.map +0 -1
- package/dist/skills.d.ts +0 -37
- package/dist/skills.js.map +0 -1
- package/dist/sys.d.ts +0 -51
- package/dist/sys.js.map +0 -1
- package/dist/tools/ask.d.ts +0 -11
- package/dist/tools/ask.js +0 -62
- package/dist/tools/ask.js.map +0 -1
- package/dist/tools/checkpoint.d.ts +0 -43
- package/dist/tools/checkpoint.js.map +0 -1
- package/dist/tools/fs.d.ts +0 -39
- package/dist/tools/fs.js.map +0 -1
- package/dist/tools/grep.d.ts +0 -6
- package/dist/tools/grep.js.map +0 -1
- package/dist/tools/screenshot.d.ts +0 -15
- package/dist/tools/screenshot.js.map +0 -1
- package/dist/tools/shell.d.ts +0 -55
- package/dist/tools/shell.js.map +0 -1
- package/dist/tools/task.d.ts +0 -39
- package/dist/tools/task.js +0 -117
- package/dist/tools/task.js.map +0 -1
- package/dist/tools/todo.d.ts +0 -10
- package/dist/tools/todo.js.map +0 -1
- package/dist/tools/web.d.ts +0 -53
- package/dist/tools/web.js.map +0 -1
- package/dist/ui/configure.d.ts +0 -20
- package/dist/ui/configure.js +0 -447
- package/dist/ui/configure.js.map +0 -1
- package/dist/ui/diff.d.ts +0 -20
- package/dist/ui/diff.js.map +0 -1
- package/dist/ui/editor.d.ts +0 -41
- package/dist/ui/editor.js.map +0 -1
- package/dist/ui/markdown.d.ts +0 -22
- package/dist/ui/markdown.js.map +0 -1
- package/dist/ui/render.d.ts +0 -14
- package/dist/ui/render.js.map +0 -1
- package/dist/ui/renderers.d.ts +0 -8
- package/dist/ui/renderers.js.map +0 -1
- package/dist/ui/screen.d.ts +0 -118
- package/dist/ui/screen.js.map +0 -1
- package/dist/ui/transcript.d.ts +0 -46
- package/dist/ui/transcript.js.map +0 -1
- package/dist/ui/tui.d.ts +0 -47
- package/dist/ui/tui.js.map +0 -1
- package/src/commands.ts +0 -144
- package/src/context.ts +0 -64
- package/src/distro.ts +0 -577
- package/src/mentions.ts +0 -135
- package/src/plugin-cli.ts +0 -278
- package/src/plugins/budget.ts +0 -37
- package/src/plugins/builtins.ts +0 -34
- package/src/plugins/core.ts +0 -257
- package/src/plugins/host.ts +0 -146
- package/src/plugins/install.ts +0 -429
- package/src/plugins/loader.ts +0 -742
- package/src/plugins/prompts.ts +0 -327
- package/src/plugins/repeats.ts +0 -157
- package/src/plugins/sessions.ts +0 -109
- package/src/session-lifecycle.ts +0 -194
- package/src/session-scope.ts +0 -135
- package/src/sessions.ts +0 -78
- package/src/tools/ask.ts +0 -72
- package/src/tools/task.ts +0 -149
- package/src/ui/configure.ts +0 -471
- /package/{src/plugins → plugins/approval}/schema.ts +0 -0
- /package/{src → plugins/tui}/history.ts +0 -0
- /package/{src/ui → plugins/tui}/markdown.ts +0 -0
package/src/config.ts
CHANGED
|
@@ -1,146 +1,44 @@
|
|
|
1
|
-
//
|
|
2
|
-
//
|
|
3
|
-
// jade 在任意目录启动,不能读当前目录的 .env(那是被操作项目自己的配置,里面的 OPENAI_API_KEY 等会改掉模型选择),
|
|
4
|
-
// 所以入口用 bun --no-env-file 启动,由这里读取用户配置。shell 里已经设置的变量优先,不被覆盖。
|
|
1
|
+
// 插件的配置(第 8 节):清单里的默认值 < 用户 config.json < 项目 .jade/config.json,按插件、按键合并。
|
|
2
|
+
// 两个文件的形状相同:{ "<插件 id>": { "<键>": 值 } }。不执行插件代码。
|
|
5
3
|
|
|
6
|
-
import {
|
|
7
|
-
import
|
|
4
|
+
import { readFile } from "node:fs/promises";
|
|
5
|
+
import type { PluginSource } from "./api.ts";
|
|
8
6
|
|
|
9
|
-
|
|
10
|
-
export function parseEnv(text: string): Record<string, string> {
|
|
11
|
-
const out: Record<string, string> = {};
|
|
12
|
-
for (const raw of text.split(/\r?\n/)) {
|
|
13
|
-
const m = /^\s*(?:export\s+)?([A-Za-z_][A-Za-z0-9_]*)\s*=\s*(.*?)\s*$/.exec(raw);
|
|
14
|
-
if (!m) continue;
|
|
15
|
-
let value = m[2]!;
|
|
16
|
-
const quote = value[0];
|
|
17
|
-
if ((quote === '"' || quote === "'") && value.length >= 2 && value.endsWith(quote)) {
|
|
18
|
-
value = value.slice(1, -1);
|
|
19
|
-
if (quote === '"') value = value.replaceAll("\\n", "\n");
|
|
20
|
-
} else {
|
|
21
|
-
// 未加引号的值里,空白之后的 # 是注释
|
|
22
|
-
value = value.replace(/\s+#.*$/, "");
|
|
23
|
-
}
|
|
24
|
-
out[m[1]!] = value;
|
|
25
|
-
}
|
|
26
|
-
return out;
|
|
27
|
-
}
|
|
7
|
+
export type ConfigFile = Record<string, Record<string, unknown>>;
|
|
28
8
|
|
|
29
|
-
/**
|
|
30
|
-
export async function
|
|
31
|
-
let
|
|
32
|
-
try {
|
|
33
|
-
text = await readFile(file, "utf8");
|
|
34
|
-
} catch {
|
|
35
|
-
return false;
|
|
36
|
-
}
|
|
37
|
-
for (const [k, v] of Object.entries(parseEnv(text))) if (env[k] === undefined) env[k] = v;
|
|
38
|
-
return true;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* token 数:整数,或者带 K(千)、M(百万)的写法,例如 32000、512K、1M、1.5M,不区分大小写,可以用 _ 分隔数字。
|
|
43
|
-
* 不是正整数时抛出,错误信息里带变量名 name
|
|
44
|
-
*/
|
|
45
|
-
export function parseTokens(name: string, text: string): number {
|
|
46
|
-
const m = /^\s*(\d+(?:\.\d+)?)\s*([km])?\s*$/i.exec(text.replaceAll("_", ""));
|
|
47
|
-
const unit = m?.[2] ? (m[2].toLowerCase() === "k" ? 1_000 : 1_000_000) : 1;
|
|
48
|
-
const n = m ? (unit === 1 ? Number(m[1]) : Math.round(Number(m[1]) * unit)) : NaN;
|
|
49
|
-
if (!Number.isInteger(n) || n <= 0) throw new Error(`${name} must be a number of tokens such as 32000, 512K or 1M, got ${JSON.stringify(text)}`);
|
|
50
|
-
return n;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
/** 至少配置了一种模型 */
|
|
54
|
-
export function hasProvider(env: Record<string, string | undefined> = process.env): boolean {
|
|
55
|
-
return Boolean(env.JADE_PROVIDER || env.OPENAI_API_KEY || env.OPENAI_COMPAT_BASE_URL || env.ANTHROPIC_API_KEY);
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* 改写配置文件里的变量:已有的行原地替换,没有的追加在末尾,值为 undefined 的删除;注释和其他行保持不变。
|
|
60
|
-
* 同时更新 env,让改动在本次运行中生效
|
|
61
|
-
*/
|
|
62
|
-
export async function updateEnvFile(
|
|
63
|
-
file: string,
|
|
64
|
-
updates: Record<string, string | undefined>,
|
|
65
|
-
env: Record<string, string | undefined> = process.env,
|
|
66
|
-
): Promise<void> {
|
|
67
|
-
let text = "";
|
|
9
|
+
/** 文件不存在时为空;格式不对时为空并给出原因 */
|
|
10
|
+
export async function readConfigFile(file: string): Promise<{ config: ConfigFile; error?: string }> {
|
|
11
|
+
let raw: unknown;
|
|
68
12
|
try {
|
|
69
|
-
|
|
70
|
-
} catch {
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
const lines = text ? text.replace(/\r?\n$/, "").split(/\r?\n/) : [];
|
|
74
|
-
const out: string[] = [];
|
|
75
|
-
const written = new Set<string>();
|
|
76
|
-
for (const raw of lines) {
|
|
77
|
-
const key = /^\s*(?:export\s+)?([A-Za-z_][A-Za-z0-9_]*)\s*=/.exec(raw)?.[1];
|
|
78
|
-
if (!key || !(key in updates)) {
|
|
79
|
-
out.push(raw);
|
|
80
|
-
continue;
|
|
81
|
-
}
|
|
82
|
-
// 同一个键出现多次时只保留第一处
|
|
83
|
-
const value = updates[key];
|
|
84
|
-
if (value !== undefined && !written.has(key)) out.push(format(key, value));
|
|
85
|
-
written.add(key);
|
|
13
|
+
raw = JSON.parse(await readFile(file, "utf8"));
|
|
14
|
+
} catch (e) {
|
|
15
|
+
if ((e as NodeJS.ErrnoException).code === "ENOENT") return { config: {} };
|
|
16
|
+
return { config: {}, error: `${file}: ${e instanceof Error ? e.message : String(e)}; ignored` };
|
|
86
17
|
}
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
else env[key] = value;
|
|
18
|
+
const isObject = (v: unknown): v is Record<string, unknown> => typeof v === "object" && v !== null && !Array.isArray(v);
|
|
19
|
+
if (!isObject(raw)) return { config: {}, error: `${file}: expected an object of plugin ids; ignored` };
|
|
20
|
+
const config: ConfigFile = {};
|
|
21
|
+
const bad: string[] = [];
|
|
22
|
+
for (const [id, section] of Object.entries(raw)) {
|
|
23
|
+
if (isObject(section)) config[id] = section;
|
|
24
|
+
else bad.push(id);
|
|
95
25
|
}
|
|
26
|
+
return bad.length > 0 ? { config, error: `${file}: the sections for ${bad.join(", ")} are not objects; ignored` } : { config };
|
|
96
27
|
}
|
|
97
28
|
|
|
98
29
|
/**
|
|
99
|
-
*
|
|
100
|
-
*
|
|
30
|
+
* 一个插件的配置。项目层的文件只能设置项目层插件的键,和清单里标了 project 的键:
|
|
31
|
+
* 克隆的仓库不能改你的审批模式、模型配置(第 7 节)。被忽略的键在 ignored 里
|
|
101
32
|
*/
|
|
102
|
-
export
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
/** 这个模型自己的设置变量 */
|
|
112
|
-
settings: Record<string, string>;
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
const strings = (v: unknown): Record<string, string> =>
|
|
116
|
-
typeof v === "object" && v !== null ? (Object.fromEntries(Object.entries(v).filter(([, x]) => typeof x === "string")) as Record<string, string>) : {};
|
|
117
|
-
|
|
118
|
-
/** 读不了或格式不对时返回空数组 */
|
|
119
|
-
export async function readSavedServices(file: string): Promise<SavedService[]> {
|
|
120
|
-
try {
|
|
121
|
-
const data = JSON.parse(await readFile(file, "utf8")) as { services?: unknown };
|
|
122
|
-
if (!Array.isArray(data.services)) return [];
|
|
123
|
-
return data.services.flatMap((raw: unknown) => {
|
|
124
|
-
const s = raw as { env?: unknown; models?: unknown } | null;
|
|
125
|
-
if (!s || !Array.isArray(s.models)) return [];
|
|
126
|
-
const models = s.models.flatMap((m: unknown) => {
|
|
127
|
-
const name = (m as { name?: unknown } | null)?.name;
|
|
128
|
-
return typeof name === "string" && name ? [{ name, settings: strings((m as { settings?: unknown }).settings) }] : [];
|
|
129
|
-
});
|
|
130
|
-
return models.length ? [{ env: strings(s.env), models }] : [];
|
|
131
|
-
});
|
|
132
|
-
} catch {
|
|
133
|
-
return [];
|
|
33
|
+
export function pluginConfig(source: PluginSource, user: ConfigFile, project: ConfigFile): { config: Record<string, unknown>; ignored: string[] } {
|
|
34
|
+
const { id, config: keys = {} } = source.manifest;
|
|
35
|
+
const config: Record<string, unknown> = {};
|
|
36
|
+
for (const [k, v] of Object.entries(keys)) if (v.default !== undefined) config[k] = structuredClone(v.default);
|
|
37
|
+
Object.assign(config, user[id]);
|
|
38
|
+
const ignored: string[] = [];
|
|
39
|
+
for (const [k, v] of Object.entries(project[id] ?? {})) {
|
|
40
|
+
if (source.layer === "project" || keys[k]?.project) config[k] = v;
|
|
41
|
+
else ignored.push(k);
|
|
134
42
|
}
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
export async function writeSavedServices(file: string, services: SavedService[]): Promise<void> {
|
|
138
|
-
await mkdir(path.dirname(file), { recursive: true });
|
|
139
|
-
// 里面有 API 密钥
|
|
140
|
-
await writeFile(file, JSON.stringify({ services }, null, 2) + "\n", { mode: 0o600 });
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
/** 含空白、# 或引号的值加双引号,parseEnv 能原样读回 */
|
|
144
|
-
function format(key: string, value: string): string {
|
|
145
|
-
return /[\s#"']/.test(value) ? `${key}="${value.replaceAll("\n", "\\n")}"` : `${key}=${value}`;
|
|
43
|
+
return { config: Object.freeze(config), ignored };
|
|
146
44
|
}
|
package/src/fetch.ts
ADDED
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
// 把插件来源下载到 staging 目录(5.4):
|
|
2
|
+
// - git:clone(可以指定分支、tag 或提交),删掉 .git;
|
|
3
|
+
// - npm:用包管理器把包安装到一个空目录,再把包目录复制过来;
|
|
4
|
+
// - 本地目录:复制(不复制 node_modules 和 .git)。
|
|
5
|
+
//
|
|
6
|
+
// 下载完检查 plugin.json,安装 package.json 里的 dependencies(--ignore-scripts,不执行插件的代码),
|
|
7
|
+
// 然后返回插件目录、提交、清单和内容哈希。清理 staging 是调用方的事。
|
|
8
|
+
|
|
9
|
+
import { execFile } from "node:child_process";
|
|
10
|
+
import { accessSync, constants, existsSync, statSync } from "node:fs";
|
|
11
|
+
import { cp, mkdir, readFile, rm, writeFile } from "node:fs/promises";
|
|
12
|
+
import path from "node:path";
|
|
13
|
+
import type { Manifest } from "./api.ts";
|
|
14
|
+
import { treeHash } from "./hash.ts";
|
|
15
|
+
import { parseManifest } from "./manifest.ts";
|
|
16
|
+
import type { PluginSourceSpec } from "./source.ts";
|
|
17
|
+
|
|
18
|
+
export interface Fetched {
|
|
19
|
+
/** 插件目录(staging 里的某个子目录) */
|
|
20
|
+
dir: string;
|
|
21
|
+
/** git 安装的提交 */
|
|
22
|
+
commit?: string;
|
|
23
|
+
manifest: Manifest;
|
|
24
|
+
/** 插件源码的哈希,安装/更新时记进安装记录 */
|
|
25
|
+
hash: string;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export async function fetchPlugin(
|
|
29
|
+
source: PluginSourceSpec,
|
|
30
|
+
staging: string,
|
|
31
|
+
subdir?: string,
|
|
32
|
+
log?: (text: string) => void,
|
|
33
|
+
): Promise<Fetched> {
|
|
34
|
+
await mkdir(staging, { recursive: true });
|
|
35
|
+
let root: string;
|
|
36
|
+
let commit: string | undefined;
|
|
37
|
+
if (source.kind === "git") {
|
|
38
|
+
({ root, commit } = await fetchGit(source, staging, log));
|
|
39
|
+
} else if (source.kind === "npm") {
|
|
40
|
+
root = await fetchNpm(source.location, staging, log);
|
|
41
|
+
} else {
|
|
42
|
+
root = path.join(staging, "local");
|
|
43
|
+
log?.(`copying ${source.location}`);
|
|
44
|
+
await cp(source.location, root, {
|
|
45
|
+
recursive: true,
|
|
46
|
+
filter: (src) => !/[\\/](node_modules|\.git)$/.test(src),
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const dir = subdir ? inSubdir(root, subdir) : root;
|
|
51
|
+
const manifest = await readPluginManifest(dir);
|
|
52
|
+
const hash = await treeHash(dir);
|
|
53
|
+
await installDependencies(dir, log);
|
|
54
|
+
return { dir, commit, manifest, hash };
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** 读一个插件目录的 plugin.json;不是插件时给出能看懂的错误 */
|
|
58
|
+
export async function readPluginManifest(dir: string): Promise<Manifest> {
|
|
59
|
+
let raw: unknown;
|
|
60
|
+
try {
|
|
61
|
+
raw = JSON.parse(await readFile(path.join(dir, "plugin.json"), "utf8"));
|
|
62
|
+
} catch (e) {
|
|
63
|
+
throw new Error(`${dir}: not a jade plugin (${e instanceof Error ? e.message : String(e)})`);
|
|
64
|
+
}
|
|
65
|
+
return parseManifest(raw);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** 把 --path 限制在来源目录里,不能跑到外面去 */
|
|
69
|
+
export function inSubdir(root: string, subdir: string): string {
|
|
70
|
+
const full = path.resolve(root, subdir);
|
|
71
|
+
const rel = path.relative(root, full);
|
|
72
|
+
if (rel === ".." || rel.startsWith(`..${path.sep}`) || path.isAbsolute(rel)) {
|
|
73
|
+
throw new Error(`--path ${subdir} points outside the source`);
|
|
74
|
+
}
|
|
75
|
+
return full;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// ---------- git ----------
|
|
79
|
+
|
|
80
|
+
async function fetchGit(source: PluginSourceSpec, staging: string, log?: (text: string) => void): Promise<{ root: string; commit: string }> {
|
|
81
|
+
const root = path.join(staging, "repo");
|
|
82
|
+
log?.(`cloning ${source.location}${source.ref ? ` (${source.ref})` : ""}`);
|
|
83
|
+
try {
|
|
84
|
+
await run("git", ["clone", "--depth", "1", ...(source.ref ? ["--branch", source.ref] : []), "--", source.location, root], staging);
|
|
85
|
+
} catch (err) {
|
|
86
|
+
// --branch 不认提交哈希:完整克隆后再切过去
|
|
87
|
+
if (!source.ref) throw err;
|
|
88
|
+
await rm(root, { recursive: true, force: true });
|
|
89
|
+
await run("git", ["clone", "--", source.location, root], staging);
|
|
90
|
+
await run("git", ["checkout", "--detach", source.ref], root);
|
|
91
|
+
}
|
|
92
|
+
const commit = (await run("git", ["rev-parse", "HEAD"], root)).trim();
|
|
93
|
+
await rm(path.join(root, ".git"), { recursive: true, force: true });
|
|
94
|
+
return { root, commit };
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// ---------- npm ----------
|
|
98
|
+
|
|
99
|
+
async function fetchNpm(spec: string, staging: string, log?: (text: string) => void): Promise<string> {
|
|
100
|
+
const work = path.join(staging, "npm");
|
|
101
|
+
await mkdir(work, { recursive: true });
|
|
102
|
+
await writeFile(path.join(work, "package.json"), JSON.stringify({ name: "jade-plugin-download", private: true }));
|
|
103
|
+
log?.(`downloading ${spec}`);
|
|
104
|
+
const pm = packageManager();
|
|
105
|
+
await run(pm.cmd, [...pm.prefix, ...pm.add(spec)], work);
|
|
106
|
+
|
|
107
|
+
const deps = Object.keys((JSON.parse(await readFile(path.join(work, "package.json"), "utf8")).dependencies ?? {}) as Record<string, string>);
|
|
108
|
+
if (deps.length !== 1) throw new Error(`could not tell which package ${spec} installed`);
|
|
109
|
+
const root = path.join(staging, "package");
|
|
110
|
+
// 包的依赖装在外层的 node_modules 里;复制过去之后在插件目录里重新安装
|
|
111
|
+
await cp(path.join(work, "node_modules", deps[0]!), root, { recursive: true, dereference: true });
|
|
112
|
+
// bun 在包目录里留下的标记,不属于插件
|
|
113
|
+
await rm(path.join(root, ".bun-tag"), { force: true });
|
|
114
|
+
await rm(work, { recursive: true, force: true });
|
|
115
|
+
return root;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
async function installDependencies(dir: string, log?: (text: string) => void): Promise<void> {
|
|
119
|
+
let pkg: { dependencies?: Record<string, string> };
|
|
120
|
+
try {
|
|
121
|
+
pkg = JSON.parse(await readFile(path.join(dir, "package.json"), "utf8"));
|
|
122
|
+
} catch {
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
if (!pkg.dependencies || Object.keys(pkg.dependencies).length === 0) return;
|
|
126
|
+
log?.("installing dependencies");
|
|
127
|
+
const pm = packageManager();
|
|
128
|
+
await run(pm.cmd, [...pm.prefix, ...pm.install], dir);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
interface PackageManager {
|
|
132
|
+
cmd: string;
|
|
133
|
+
/** 放在子命令前面的参数(npm 的 CLI 脚本路径) */
|
|
134
|
+
prefix: string[];
|
|
135
|
+
add(spec: string): string[];
|
|
136
|
+
install: string[];
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** 跑在 Bun 下用 bun 自己;跑在 Node 下用 npm(Windows 上的 npm.cmd 不能直接执行,用 node 运行 npm 的 CLI 脚本) */
|
|
140
|
+
function packageManager(): PackageManager {
|
|
141
|
+
if (process.versions.bun) {
|
|
142
|
+
return {
|
|
143
|
+
cmd: process.execPath,
|
|
144
|
+
prefix: [],
|
|
145
|
+
add: (spec) => ["add", "--ignore-scripts", spec],
|
|
146
|
+
install: ["install", "--production", "--ignore-scripts"],
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
const npm = npmCommand();
|
|
150
|
+
if (!npm) throw new Error("npm was not found; it is needed to install plugins from npm or plugins with dependencies");
|
|
151
|
+
const quiet = ["--ignore-scripts", "--no-audit", "--no-fund", "--no-package-lock", "--loglevel=error"];
|
|
152
|
+
return {
|
|
153
|
+
cmd: npm[0]!,
|
|
154
|
+
prefix: npm.slice(1),
|
|
155
|
+
add: (spec) => ["install", ...quiet, spec],
|
|
156
|
+
install: ["install", "--omit=dev", ...quiet],
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/** npm 的 node 脚本;没有 npm 时返回 undefined */
|
|
161
|
+
export function npmCommand(): string[] | undefined {
|
|
162
|
+
const dirs = [path.dirname(process.execPath)];
|
|
163
|
+
const onPath = which(process.platform === "win32" ? "npm.cmd" : "npm");
|
|
164
|
+
if (onPath) dirs.push(path.dirname(onPath));
|
|
165
|
+
for (const dir of dirs) {
|
|
166
|
+
for (const cli of [
|
|
167
|
+
path.join(dir, "node_modules", "npm", "bin", "npm-cli.js"),
|
|
168
|
+
path.join(dir, "..", "lib", "node_modules", "npm", "bin", "npm-cli.js"),
|
|
169
|
+
]) {
|
|
170
|
+
if (existsSync(cli)) return [process.execPath, cli];
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
// POSIX 上 npm 本身就是可执行的脚本
|
|
174
|
+
if (onPath && process.platform !== "win32") return [onPath];
|
|
175
|
+
return undefined;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/** 在 PATH 里找一个可执行文件;Windows 上按 PATHEXT 补扩展名 */
|
|
179
|
+
export function which(bin: string): string | undefined {
|
|
180
|
+
const win = process.platform === "win32";
|
|
181
|
+
const exts = win ? ["", ...(process.env.PATHEXT ?? ".COM;.EXE;.BAT;.CMD").split(";").filter(Boolean)] : [""];
|
|
182
|
+
const dirs = (process.env.PATH ?? process.env.Path ?? "").split(path.delimiter);
|
|
183
|
+
for (const raw of dirs) {
|
|
184
|
+
const dir = raw.replace(/^"(.*)"$/, "$1");
|
|
185
|
+
if (!dir) continue;
|
|
186
|
+
for (const ext of exts) {
|
|
187
|
+
if (win && ext === "" && !path.extname(bin)) continue;
|
|
188
|
+
const file = path.join(dir, bin + ext);
|
|
189
|
+
try {
|
|
190
|
+
if (!statSync(file).isFile()) continue;
|
|
191
|
+
if (!win) accessSync(file, constants.X_OK);
|
|
192
|
+
return file;
|
|
193
|
+
} catch {
|
|
194
|
+
// 继续找
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
return undefined;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
function run(cmd: string, args: string[], cwd: string): Promise<string> {
|
|
202
|
+
return new Promise((resolve, reject) => {
|
|
203
|
+
execFile(
|
|
204
|
+
cmd,
|
|
205
|
+
args,
|
|
206
|
+
{ cwd, windowsHide: true, maxBuffer: 16 * 1024 * 1024, env: { ...process.env, GIT_TERMINAL_PROMPT: "0" } },
|
|
207
|
+
(err, stdout, stderr) => {
|
|
208
|
+
if (!err) return resolve(stdout);
|
|
209
|
+
const detail = `${stderr || stdout}`.trim().split(/\r?\n/).slice(-6).join("\n");
|
|
210
|
+
const missing = (err as NodeJS.ErrnoException).code === "ENOENT";
|
|
211
|
+
const name = args[0]?.endsWith(".js") ? `npm ${args[1]}` : `${path.basename(cmd)} ${args[0]}`;
|
|
212
|
+
reject(new Error(missing ? `${cmd} is not installed or not on PATH` : `${name} failed${detail ? `:\n${detail}` : ""}`));
|
|
213
|
+
},
|
|
214
|
+
);
|
|
215
|
+
});
|
|
216
|
+
}
|
package/src/hash.ts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
// 插件目录内容的哈希:安装时记下来,更新时用来判断装好的插件有没有被手动改过。
|
|
2
|
+
//
|
|
3
|
+
// 和内核的 hashDir 不同,这里只算插件自己的源码:
|
|
4
|
+
// - node_modules 是包装出来的,不参与;
|
|
5
|
+
// - .git 不参与;
|
|
6
|
+
// - 包管理器生成的 lock 文件不参与(更新时会被重新生成)。
|
|
7
|
+
|
|
8
|
+
import { createHash } from "node:crypto";
|
|
9
|
+
import { readFile, readdir, readlink } from "node:fs/promises";
|
|
10
|
+
import path from "node:path";
|
|
11
|
+
|
|
12
|
+
const SKIP_DIRS: ReadonlySet<string> = new Set(["node_modules", ".git"]);
|
|
13
|
+
const SKIP_FILES: ReadonlySet<string> = new Set(["package-lock.json", "bun.lock", "bun.lockb", "yarn.lock", "pnpm-lock.yaml"]);
|
|
14
|
+
|
|
15
|
+
/** 按相对路径排序,依次计入路径、长度、内容;和 hashDir 的格式一致,但不跟随符号链接(只计入链接指向的路径) */
|
|
16
|
+
export async function treeHash(root: string): Promise<string> {
|
|
17
|
+
const entries = await readdir(root, { recursive: true, withFileTypes: true });
|
|
18
|
+
const files: string[] = [];
|
|
19
|
+
const links: string[] = [];
|
|
20
|
+
for (const e of entries) {
|
|
21
|
+
const abs = path.join(e.parentPath ?? root, e.name);
|
|
22
|
+
const rel = path.relative(root, abs).split(path.sep).join("/");
|
|
23
|
+
const parts = rel.split("/");
|
|
24
|
+
if (parts.some((p) => SKIP_DIRS.has(p))) continue;
|
|
25
|
+
if (SKIP_FILES.has(parts.at(-1) ?? "")) continue;
|
|
26
|
+
if (e.isFile()) files.push(rel);
|
|
27
|
+
else if (e.isSymbolicLink()) links.push(rel);
|
|
28
|
+
}
|
|
29
|
+
files.sort();
|
|
30
|
+
links.sort();
|
|
31
|
+
const h = createHash("sha256");
|
|
32
|
+
for (const rel of files) {
|
|
33
|
+
const content = await readFile(path.join(root, rel));
|
|
34
|
+
h.update(`${rel}\0${content.length}\0`).update(content);
|
|
35
|
+
}
|
|
36
|
+
// 符号链接不跟随,只计入它指向哪里:链接被换成指向别处时也看得出来。没有链接时哈希和以前一样
|
|
37
|
+
for (const rel of links) h.update(`${rel}\0->\0${await readlink(path.join(root, rel))}\0`);
|
|
38
|
+
return h.digest("hex");
|
|
39
|
+
}
|