@uncarved/jade 0.3.1 → 0.4.1

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.
Files changed (206) hide show
  1. package/README.md +1 -0
  2. package/dist/api.d.ts +1 -1
  3. package/dist/api.js +1 -1
  4. package/dist/host.d.ts +2 -0
  5. package/dist/host.js +39 -5
  6. package/dist/host.js.map +1 -1
  7. package/dist/remote.js +5 -0
  8. package/dist/remote.js.map +1 -1
  9. package/dist/session.d.ts +7 -0
  10. package/dist/session.js +490 -29
  11. package/dist/session.js.map +1 -1
  12. package/package.json +4 -4
  13. package/plugins/approval/index.ts +36 -14
  14. package/plugins/approval/policy.ts +15 -10
  15. package/plugins/approval/review.ts +9 -5
  16. package/plugins/checks/checks.ts +10 -7
  17. package/plugins/checks/index.ts +13 -13
  18. package/plugins/checks/plugin.json +1 -1
  19. package/plugins/checks/project.ts +37 -38
  20. package/plugins/commands/index.ts +27 -3
  21. package/plugins/compact/compact.ts +13 -5
  22. package/plugins/compact/index.ts +20 -3
  23. package/plugins/compact/style.ts +13 -14
  24. package/plugins/completions/index.ts +35 -0
  25. package/plugins/completions/plugin.json +7 -0
  26. package/plugins/copy/index.ts +46 -0
  27. package/plugins/copy/plugin.json +7 -0
  28. package/plugins/editor/index.ts +10 -4
  29. package/plugins/fs/diff.ts +7 -6
  30. package/plugins/fs/fs.ts +8 -4
  31. package/plugins/fs/grep.ts +42 -9
  32. package/plugins/fs/index.ts +2 -7
  33. package/plugins/fs/plugin.json +1 -1
  34. package/plugins/fs/renderers.ts +16 -15
  35. package/plugins/fs/style.ts +16 -15
  36. package/plugins/hooks/index.ts +3 -2
  37. package/plugins/keys/index.ts +5 -1
  38. package/plugins/mcp/index.ts +197 -78
  39. package/plugins/mcp/install.ts +5 -3
  40. package/plugins/mcp/mcp.ts +163 -11
  41. package/plugins/mcp/plugin.json +2 -1
  42. package/plugins/mcp/project.ts +47 -50
  43. package/plugins/memory/index.ts +4 -4
  44. package/plugins/memory/style.ts +13 -14
  45. package/plugins/mentions/candidates.ts +67 -0
  46. package/plugins/mentions/index.ts +12 -5
  47. package/plugins/mentions/plugin.json +1 -1
  48. package/plugins/mentions/style.ts +13 -14
  49. package/plugins/models/configure.ts +47 -12
  50. package/plugins/models/index.ts +27 -1
  51. package/plugins/models/provider.ts +24 -0
  52. package/plugins/models/saved.ts +1 -1
  53. package/plugins/modes/index.ts +13 -2
  54. package/plugins/print/index.ts +7 -4
  55. package/plugins/prompts/index.ts +0 -0
  56. package/plugins/renderers/index.ts +10 -5
  57. package/plugins/repeats/index.ts +8 -8
  58. package/plugins/schedule/index.ts +5 -1
  59. package/plugins/screenshot/browser.ts +10 -0
  60. package/plugins/sessions/index.ts +48 -2
  61. package/plugins/shell/index.ts +27 -28
  62. package/plugins/shell/plugin.json +1 -1
  63. package/plugins/shell/renderers.ts +9 -7
  64. package/plugins/shell/shell.ts +3 -1
  65. package/plugins/skills/index.ts +28 -19
  66. package/plugins/skills/project.ts +12 -7
  67. package/plugins/steer/index.ts +5 -4
  68. package/plugins/task/index.ts +80 -50
  69. package/plugins/task/plugin.json +6 -2
  70. package/plugins/task/style.ts +13 -14
  71. package/plugins/task/subagent.ts +67 -184
  72. package/plugins/todo/index.ts +39 -9
  73. package/plugins/tools/index.ts +9 -2
  74. package/plugins/tui/frontend.ts +139 -89
  75. package/plugins/tui/index.ts +2 -3
  76. package/plugins/tui/mentions.ts +11 -80
  77. package/plugins/tui/plugin.json +1 -1
  78. package/plugins/tui/screen.ts +55 -5
  79. package/plugins/tui/transcript.ts +17 -7
  80. package/plugins/tui/tui.ts +337 -101
  81. package/plugins/tui/vendor.ts +1 -1
  82. package/plugins/undo/checkpoint.ts +8 -4
  83. package/plugins/undo/index.ts +170 -31
  84. package/plugins/undo/plugin.json +1 -0
  85. package/plugins/undo/snapshot.ts +87 -14
  86. package/plugins/undo/style.ts +13 -14
  87. package/plugins/web/index.ts +7 -18
  88. package/plugins/web/plugin.json +1 -1
  89. package/plugins-dist/approval/index.js +363 -418
  90. package/plugins-dist/approval/policy.js +391 -508
  91. package/plugins-dist/approval/review.js +132 -158
  92. package/plugins-dist/approval/schema.js +50 -55
  93. package/plugins-dist/ask/index.js +115 -129
  94. package/plugins-dist/budget/index.js +27 -31
  95. package/plugins-dist/checks/checks.js +293 -329
  96. package/plugins-dist/checks/index.js +119 -139
  97. package/plugins-dist/checks/plugin.json +2 -1
  98. package/plugins-dist/checks/project.js +57 -65
  99. package/plugins-dist/commands/index.js +47 -29
  100. package/plugins-dist/compact/compact.js +233 -249
  101. package/plugins-dist/compact/index.js +98 -95
  102. package/plugins-dist/compact/style.js +9 -14
  103. package/plugins-dist/completions/index.js +30 -0
  104. package/plugins-dist/completions/plugin.json +9 -0
  105. package/plugins-dist/continue/index.js +40 -52
  106. package/plugins-dist/copy/index.js +40 -0
  107. package/plugins-dist/copy/plugin.json +9 -0
  108. package/plugins-dist/cost/index.js +98 -115
  109. package/plugins-dist/custom-commands/index.js +55 -72
  110. package/plugins-dist/editor/index.js +64 -72
  111. package/plugins-dist/fs/diff.js +111 -124
  112. package/plugins-dist/fs/fs.js +426 -469
  113. package/plugins-dist/fs/grep.js +262 -253
  114. package/plugins-dist/fs/index.js +20 -34
  115. package/plugins-dist/fs/plugin.json +1 -2
  116. package/plugins-dist/fs/renderers.js +100 -112
  117. package/plugins-dist/fs/style.js +11 -15
  118. package/plugins-dist/fs/sys.js +31 -43
  119. package/plugins-dist/hooks/index.js +107 -127
  120. package/plugins-dist/keys/index.js +27 -31
  121. package/plugins-dist/mcp/index.js +271 -199
  122. package/plugins-dist/mcp/install.js +123 -137
  123. package/plugins-dist/mcp/mcp.js +638 -577
  124. package/plugins-dist/mcp/plugin.json +3 -1
  125. package/plugins-dist/mcp/project.js +98 -115
  126. package/plugins-dist/memory/index.js +83 -88
  127. package/plugins-dist/memory/memory.js +111 -128
  128. package/plugins-dist/memory/style.js +9 -14
  129. package/plugins-dist/mentions/attach.js +91 -99
  130. package/plugins-dist/mentions/candidates.js +49 -0
  131. package/plugins-dist/mentions/index.js +71 -81
  132. package/plugins-dist/mentions/plugin.json +2 -1
  133. package/plugins-dist/mentions/style.js +9 -14
  134. package/plugins-dist/models/config.js +29 -43
  135. package/plugins-dist/models/configure.js +421 -474
  136. package/plugins-dist/models/context.js +14 -22
  137. package/plugins-dist/models/index.js +182 -189
  138. package/plugins-dist/models/provider.js +263 -307
  139. package/plugins-dist/models/saved.js +60 -72
  140. package/plugins-dist/modes/index.js +138 -165
  141. package/plugins-dist/plugins/index.js +34 -34
  142. package/plugins-dist/print/index.js +192 -175
  143. package/plugins-dist/prompts/index.js +86 -89
  144. package/plugins-dist/prompts/notes.js +31 -37
  145. package/plugins-dist/prompts/prompts.js +155 -193
  146. package/plugins-dist/prompts/results.js +40 -53
  147. package/plugins-dist/prompts/sys.js +39 -50
  148. package/plugins-dist/renderers/index.js +32 -32
  149. package/plugins-dist/repeats/index.js +131 -174
  150. package/plugins-dist/schedule/cron.js +49 -56
  151. package/plugins-dist/schedule/index.js +230 -247
  152. package/plugins-dist/schedule/store.js +93 -109
  153. package/plugins-dist/scratch/index.js +12 -18
  154. package/plugins-dist/screenshot/browser.js +136 -154
  155. package/plugins-dist/screenshot/index.js +37 -46
  156. package/plugins-dist/screenshot/sys.js +53 -65
  157. package/plugins-dist/screenshot/tool.js +98 -126
  158. package/plugins-dist/sessions/index.js +149 -117
  159. package/plugins-dist/shell/index.js +117 -135
  160. package/plugins-dist/shell/plugin.json +1 -1
  161. package/plugins-dist/shell/renderers.js +28 -33
  162. package/plugins-dist/shell/service.js +17 -23
  163. package/plugins-dist/shell/shell.js +307 -362
  164. package/plugins-dist/shell/sys.js +64 -79
  165. package/plugins-dist/skills/index.js +193 -211
  166. package/plugins-dist/skills/install.js +122 -149
  167. package/plugins-dist/skills/project.js +65 -73
  168. package/plugins-dist/skills/skills.js +135 -171
  169. package/plugins-dist/steer/index.js +108 -125
  170. package/plugins-dist/suggest/index.js +235 -259
  171. package/plugins-dist/task/index.js +130 -117
  172. package/plugins-dist/task/plugin.json +5 -2
  173. package/plugins-dist/task/style.js +9 -14
  174. package/plugins-dist/task/subagent.js +156 -279
  175. package/plugins-dist/todo/index.js +79 -63
  176. package/plugins-dist/tools/index.js +36 -42
  177. package/plugins-dist/tui/editor.js +145 -160
  178. package/plugins-dist/tui/frontend.js +639 -678
  179. package/plugins-dist/tui/history.js +34 -37
  180. package/plugins-dist/tui/index.js +16 -18
  181. package/plugins-dist/tui/markdown.js +203 -247
  182. package/plugins-dist/tui/mascot.js +266 -355
  183. package/plugins-dist/tui/mentions.js +67 -144
  184. package/plugins-dist/tui/plugin.json +1 -2
  185. package/plugins-dist/tui/render.js +35 -45
  186. package/plugins-dist/tui/screen.js +197 -180
  187. package/plugins-dist/tui/transcript.js +436 -468
  188. package/plugins-dist/tui/tui.js +1554 -1656
  189. package/plugins-dist/tui/vendor.js +1088 -1388
  190. package/plugins-dist/undo/checkpoint.js +97 -118
  191. package/plugins-dist/undo/index.js +281 -192
  192. package/plugins-dist/undo/plugin.json +3 -0
  193. package/plugins-dist/undo/snapshot.js +134 -99
  194. package/plugins-dist/undo/style.js +9 -14
  195. package/plugins-dist/update/index.js +26 -28
  196. package/plugins-dist/web/env.js +55 -68
  197. package/plugins-dist/web/index.js +80 -110
  198. package/plugins-dist/web/plugin.json +1 -2
  199. package/plugins-dist/web/renderers.js +4 -5
  200. package/plugins-dist/web/web.js +514 -593
  201. package/sdk/python/__pycache__/jade_plugin.cpython-312.pyc +0 -0
  202. package/sdk/python/jade_plugin.py +9 -2
  203. package/src/api.ts +1 -1
  204. package/src/host.ts +39 -4
  205. package/src/remote.ts +6 -1
  206. package/src/session.ts +496 -36
@@ -1,111 +1,146 @@
1
- // 工作区快照:shell 命令改了什么无从得知,所以每轮第一次跑命令之前给整个工作区拍一张快照,/undo 时把之后变了的文件恢复回去。
2
- //
3
- // 快照存在 $JADE_HOME 里的一个影子 git 仓库(<dir>/shadow/.git),工作区指向项目的仓库根目录:
4
- // 项目自己的 .git(暂存区、提交、stash)一点不碰。只在 git 仓库里做:要靠 .gitignore 跳过 node_modules 这类目录,
5
- // 对象库借用项目的(alternates),已经提交过的内容不再存一份。被忽略的文件不在快照里,撤销不了。
6
- // 快照是 tree 对象,不建提交;哪一轮对应哪个 tree 记在 snapshots.json。
7
1
  import { execFile } from "node:child_process";
8
2
  import { mkdir, readFile, rm, writeFile } from "node:fs/promises";
9
3
  import path from "node:path";
10
4
  const FILE = "snapshots.json";
11
- /** 一次 git 命令最多带这么多路径:Windows 的命令行长度有限 */
12
5
  const CHUNK = 100;
6
+ const queues = new Map;
7
+ function serial(key, fn) {
8
+ const next = (queues.get(key) ?? Promise.resolve()).then(fn, fn);
9
+ const tail = next.catch(() => {});
10
+ queues.set(key, tail);
11
+ tail.then(() => {
12
+ if (queues.get(key) === tail)
13
+ queues.delete(key);
14
+ });
15
+ return next;
16
+ }
17
+ const instances = new Map;
18
+ export function snapshotsAt(dir, cwd) {
19
+ const key = `${dir}|${cwd}`;
20
+ let s = instances.get(key);
21
+ if (!s)
22
+ instances.set(key, s = new Snapshots(dir, cwd));
23
+ return s;
24
+ }
25
+
13
26
  export class Snapshots {
14
- #dir;
15
- #cwd;
16
- /** 影子仓库准备好之后的 git 环境;不在 git 仓库里(或没有 git)时 undefined */
17
- #ready;
18
- /** dir:这个会话的快照目录;cwd:会话的工作目录 */
19
- constructor(dir, cwd) {
20
- this.#dir = dir;
21
- this.#cwd = cwd;
22
- }
23
- #prepare() {
24
- return (this.#ready ??= (async () => {
25
- let root;
26
- let objects;
27
- try {
28
- root = path.resolve((await git(["rev-parse", "--show-toplevel"], { cwd: this.#cwd })).trim());
29
- objects = path.resolve(this.#cwd, (await git(["rev-parse", "--git-common-dir"], { cwd: this.#cwd })).trim(), "objects");
30
- }
31
- catch {
32
- return undefined;
33
- }
34
- const shadow = path.join(this.#dir, "shadow");
35
- const gitDir = path.join(shadow, ".git");
36
- const env = { ...process.env, GIT_DIR: gitDir, GIT_WORK_TREE: root };
37
- delete env.GIT_INDEX_FILE;
38
- try {
39
- await readFile(path.join(gitDir, "HEAD"));
40
- }
41
- catch {
42
- await mkdir(shadow, { recursive: true });
43
- await git(["init", "--quiet", shadow], { cwd: shadow });
44
- // 原样存、原样取:不做换行转换,不跑 LFS 之类的 filter。info/attributes 比工作区里的 .gitattributes 优先
45
- await mkdir(path.join(gitDir, "info"), { recursive: true });
46
- await writeFile(path.join(gitDir, "info", "attributes"), "* -text -eol -filter -ident -working-tree-encoding\n");
47
- await writeFile(path.join(gitDir, "objects", "info", "alternates"), `${objects.split(path.sep).join("/")}\n`);
48
- for (const [k, v] of [["core.autocrlf", "false"], ["core.safecrlf", "false"], ["core.longpaths", "true"], ["gc.auto", "0"]]) {
49
- await git(["config", k, v], { cwd: root, env });
50
- }
51
- }
52
- return { env, root };
53
- })());
54
- }
55
- /** 给工作区拍一张快照,返回 tree;不在 git 仓库里时 undefined */
56
- async take() {
57
- const r = await this.#prepare();
58
- if (!r)
59
- return undefined;
60
- await git(["add", "-A", "--ignore-errors", "."], { cwd: r.root, env: r.env }).catch(() => {
61
- // 有的文件加不进去(被别的程序锁着、嵌套的仓库):其余的照样拍
62
- });
63
- return (await git(["write-tree"], { cwd: r.root, env: r.env })).trim();
64
- }
65
- /** 工作区现在和快照 tree 相比变了的文件 */
66
- async changed(tree) {
67
- const r = await this.#prepare();
68
- const now = await this.take();
69
- if (!r || !now || now === tree)
70
- return [];
71
- const out = await git(["diff-tree", "-r", "--no-renames", "--name-status", "-z", tree, now], { cwd: r.root, env: r.env });
72
- const parts = out.split("\0");
73
- const changes = [];
74
- for (let i = 0; i + 1 < parts.length; i += 2)
75
- changes.push({ path: path.join(r.root, parts[i + 1]), added: parts[i] === "A" });
76
- return changes;
77
- }
78
- /** 把这些文件恢复成快照 tree 里的样子:之后新出现的删掉,其余的取回 */
79
- async restore(tree, changes) {
80
- const r = await this.#prepare();
81
- if (!r)
82
- return;
83
- for (const c of changes)
84
- if (c.added)
85
- await rm(c.path, { force: true });
86
- const back = changes.filter((c) => !c.added).map((c) => path.relative(r.root, c.path).split(path.sep).join("/"));
87
- for (let i = 0; i < back.length; i += CHUNK) {
88
- await git(["--literal-pathspecs", "checkout", tree, "--", ...back.slice(i, i + CHUNK)], { cwd: r.root, env: r.env });
89
- }
90
- }
91
- /** 本会话记下的快照,最新的在前 */
92
- async list() {
93
- try {
94
- const all = JSON.parse(await readFile(path.join(this.#dir, FILE), "utf8"));
95
- return Array.isArray(all) ? all.sort((a, b) => b.at - a.at) : [];
96
- }
97
- catch {
98
- return [];
27
+ #dir;
28
+ #cwd;
29
+ #ready;
30
+ constructor(dir, cwd) {
31
+ this.#dir = dir;
32
+ this.#cwd = cwd;
33
+ }
34
+ #prepare() {
35
+ return this.#ready ??= (async () => {
36
+ let root;
37
+ let objects;
38
+ try {
39
+ root = path.resolve((await git(["rev-parse", "--show-toplevel"], { cwd: this.#cwd })).trim());
40
+ objects = path.resolve(this.#cwd, (await git(["rev-parse", "--git-common-dir"], { cwd: this.#cwd })).trim(), "objects");
41
+ } catch {
42
+ return;
43
+ }
44
+ const shadow = path.join(this.#dir, "shadow");
45
+ const gitDir = path.join(shadow, ".git");
46
+ const env = { ...process.env, GIT_DIR: gitDir, GIT_WORK_TREE: root };
47
+ delete env.GIT_INDEX_FILE;
48
+ try {
49
+ await readFile(path.join(gitDir, "HEAD"));
50
+ } catch {
51
+ await mkdir(shadow, { recursive: true });
52
+ await git(["init", "--quiet", shadow], { cwd: shadow });
53
+ await mkdir(path.join(gitDir, "info"), { recursive: true });
54
+ await writeFile(path.join(gitDir, "info", "attributes"), `* -text -eol -filter -ident -working-tree-encoding
55
+ `);
56
+ await writeFile(path.join(gitDir, "objects", "info", "alternates"), `${objects.split(path.sep).join("/")}
57
+ `);
58
+ for (const [k, v] of [["core.autocrlf", "false"], ["core.safecrlf", "false"], ["core.longpaths", "true"], ["gc.auto", "0"]]) {
59
+ await git(["config", k, v], { cwd: root, env });
99
60
  }
61
+ }
62
+ return { env, root };
63
+ })();
64
+ }
65
+ #serial(fn) {
66
+ return serial(this.#dir, fn);
67
+ }
68
+ take() {
69
+ return this.#serial(() => this.#take());
70
+ }
71
+ async#take() {
72
+ const r = await this.#prepare();
73
+ if (!r)
74
+ return;
75
+ await git(["add", "-A", "--ignore-errors", "."], { cwd: r.root, env: r.env }).catch(() => {});
76
+ return (await git(["write-tree"], { cwd: r.root, env: r.env })).trim();
77
+ }
78
+ changes(snap) {
79
+ return this.#serial(async () => {
80
+ const r = await this.#prepare();
81
+ const now = await this.#take();
82
+ if (!r || !now)
83
+ return { changes: [], conflicts: new Set };
84
+ const end = snap.after ?? now;
85
+ const changes = await this.#diff(r, snap.tree, end);
86
+ const later = new Set((await this.#diff(r, end, now)).map((c) => c.path));
87
+ return { changes, conflicts: new Set(changes.map((c) => c.path).filter((p) => later.has(p))) };
88
+ });
89
+ }
90
+ async#diff(r, from, to) {
91
+ if (from === to)
92
+ return [];
93
+ const out = await git(["diff-tree", "-r", "--no-renames", "--name-status", "-z", from, to], { cwd: r.root, env: r.env });
94
+ const parts = out.split("\x00");
95
+ const changes = [];
96
+ for (let i = 0;i + 1 < parts.length; i += 2)
97
+ changes.push({ path: path.join(r.root, parts[i + 1]), added: parts[i] === "A" });
98
+ return changes;
99
+ }
100
+ restore(tree, changes) {
101
+ return this.#serial(() => this.#restore(tree, changes));
102
+ }
103
+ async#restore(tree, changes) {
104
+ const r = await this.#prepare();
105
+ if (!r)
106
+ return;
107
+ for (const c of changes)
108
+ if (c.added)
109
+ await rm(c.path, { force: true });
110
+ const back = changes.filter((c) => !c.added).map((c) => path.relative(r.root, c.path).split(path.sep).join("/"));
111
+ for (let i = 0;i < back.length; i += CHUNK) {
112
+ await git(["--literal-pathspecs", "checkout", tree, "--", ...back.slice(i, i + CHUNK)], { cwd: r.root, env: r.env });
100
113
  }
101
- async save(snapshot) {
102
- const all = await this.list();
103
- await mkdir(this.#dir, { recursive: true });
104
- await writeFile(path.join(this.#dir, FILE), JSON.stringify([snapshot, ...all]));
114
+ }
115
+ list() {
116
+ return this.#serial(() => this.#list());
117
+ }
118
+ async#list() {
119
+ try {
120
+ const all = JSON.parse(await readFile(path.join(this.#dir, FILE), "utf8"));
121
+ return Array.isArray(all) ? all.sort((a, b) => b.at - a.at) : [];
122
+ } catch {
123
+ return [];
105
124
  }
125
+ }
126
+ save(snapshot) {
127
+ return this.#serial(async () => this.#write([snapshot, ...await this.#list()]));
128
+ }
129
+ close(toolUseId) {
130
+ return this.#serial(async () => {
131
+ const after = await this.#take();
132
+ if (!after)
133
+ return;
134
+ await this.#write((await this.#list()).map((s) => s.toolUseId === toolUseId ? { ...s, after } : s));
135
+ });
136
+ }
137
+ async#write(all) {
138
+ await mkdir(this.#dir, { recursive: true });
139
+ await writeFile(path.join(this.#dir, FILE), JSON.stringify(all));
140
+ }
106
141
  }
107
142
  function git(args, opts) {
108
- return new Promise((resolve, reject) => {
109
- execFile("git", args, { cwd: opts.cwd, env: opts.env, maxBuffer: 64 * 1024 * 1024, windowsHide: true }, (err, stdout) => (err ? reject(err) : resolve(stdout)));
110
- });
143
+ return new Promise((resolve, reject) => {
144
+ execFile("git", args, { cwd: opts.cwd, env: opts.env, maxBuffer: 64 * 1024 * 1024, windowsHide: true }, (err, stdout) => err ? reject(err) : resolve(stdout));
145
+ });
111
146
  }
@@ -1,14 +1,9 @@
1
- // 显示用的小工具:截断和 ANSI 颜色。插件之间不共享代码(只能共享类型),所以这里也有一份(同 jade/fs 的 style.ts)。
2
- export const clip = (s, n) => (s.length > n ? s.slice(0, n) + "…" : s);
3
- // 设了 NO_COLOR(非空)或标准输出不是终端时不输出颜色,见 https://no-color.org
4
- const colors = Boolean(process.stdout.isTTY) && !process.env.NO_COLOR;
5
- /**
6
- * 每种样式用自己的关闭码,不用 \x1b[0m 全部重置:外面套着别的样式时,后面的文字仍保持外层样式。
7
- * 里面出现同一个关闭码(例如 dim 里套 bold,两者都用 22 关闭)时紧接着重新打开外层的样式
8
- */
9
- const paint = (open, close) => (s) => colors ? `\x1b[${open}m${s.replaceAll(`\x1b[${close}m`, `\x1b[${close}m\x1b[${open}m`)}\x1b[${close}m` : s;
10
- export const dim = paint("2", "22");
11
- export const red = paint("31", "39");
12
- export const green = paint("32", "39");
13
- export const yellow = paint("33", "39");
14
- export const bold = paint("1", "22");
1
+ export const clip = (s, n) => s.length > n ? s.slice(0, n) + "…" : s;
2
+ const styled = (style) => (text) => ({ text, style });
3
+ export const dim = styled("dim");
4
+ export const bold = styled("bold");
5
+ export const ok = styled("ok");
6
+ export const warn = styled("warn");
7
+ export const error = styled("error");
8
+ export const lines = (rows) => rows.flatMap((r, i) => i ? [`
9
+ `, r] : [r]);
@@ -1,33 +1,31 @@
1
- // jade/update:启动时(每天最多一次)查 npm 上有没有更新的 jade,有就在后台升级,下次启动生效;
2
- // 配置 auto 为 false 或 JADE_AUTO_UPDATE=off 时只提示。仓库里的开发版和 npm link 既不提示也不升级。
3
- // 查询和升级经核心的 update@1 服务,和 jade update 是同一套;不想要它就 jade plugins disable jade/update。
4
1
  import { definePlugin } from "@uncarved/jade-api";
5
- /** 查一次,按结果提示;出什么错都不打扰用户 */
6
2
  export async function checkForUpdate(o) {
7
- const { update } = o;
8
- if (update.install === "dev")
9
- return;
10
- const latest = await update.newer().catch(() => undefined);
11
- if (!latest)
12
- return;
13
- const manual = () => o.notify(`jade ${latest} is available (you have ${update.version}); run jade update`);
14
- if (!o.auto || !update.autoEnabled())
15
- return manual();
16
- const result = await update.upgrade(latest).catch(() => undefined);
17
- if (result?.status === "installed")
18
- o.notify(`jade ${latest} was installed in the background; restart jade to use it`);
19
- else if (result?.status === "failed")
20
- o.notify(`automatic update to jade ${latest} failed (see ${result.log}); run jade update`, "warning");
21
- else if (result?.status !== "skipped" || result.why !== "busy")
22
- manual();
3
+ const { update } = o;
4
+ if (update.install === "dev")
5
+ return;
6
+ const latest = await update.newer().catch(() => {
7
+ return;
8
+ });
9
+ if (!latest)
10
+ return;
11
+ const manual = () => o.notify(`jade ${latest} is available (you have ${update.version}); run jade update`);
12
+ if (!o.auto || !update.autoEnabled())
13
+ return manual();
14
+ const result = await update.upgrade(latest).catch(() => {
15
+ return;
16
+ });
17
+ if (result?.status === "installed")
18
+ o.notify(`jade ${latest} was installed in the background; restart jade to use it`);
19
+ else if (result?.status === "failed")
20
+ o.notify(`automatic update to jade ${latest} failed (see ${result.log}); run jade update`, "warning");
21
+ else if (result?.status !== "skipped" || result.why !== "busy")
22
+ manual();
23
23
  }
24
24
  export default definePlugin({
25
- process(ctx) {
26
- const ui = ctx.service("ui@1");
27
- // 不能提问的前端(jade -p)是脚本在用,不提示也不升级
28
- if (!ui?.interactive)
29
- return;
30
- // 作用域关闭后 ui 失效,再提示会抛错:那时也没人看了
31
- void checkForUpdate({ update: ctx.require("update@1"), auto: ctx.config.auto !== false, notify: (m, level) => ui.notify(m, level) }).catch(() => { });
32
- },
25
+ process(ctx) {
26
+ const ui = ctx.service("ui@1");
27
+ if (!ui?.interactive)
28
+ return;
29
+ checkForUpdate({ update: ctx.require("update@1"), auto: ctx.config.auto !== false, notify: (m, level) => ui.notify(m, level) }).catch(() => {});
30
+ }
33
31
  });
@@ -1,80 +1,67 @@
1
- // 搜索服务的 API key:shell 里的环境变量 + $JADE_HOME/.env(shell 里已经设置的优先),和 jade/models 读同一个文件。
2
- // 从旧 jade 的 config.ts 复制:插件之间不共享代码,jade/models 也有一份 parseEnv。
3
1
  import { mkdir, readFile, writeFile } from "node:fs/promises";
4
2
  import path from "node:path";
5
- /** 解析 KEY=VALUE 格式:忽略空行和 # 注释,允许 export 前缀,去掉成对的引号(双引号里的 \n 换成换行) */
6
3
  export function parseEnv(text) {
7
- const out = {};
8
- for (const raw of text.split(/\r?\n/)) {
9
- const m = /^\s*(?:export\s+)?([A-Za-z_][A-Za-z0-9_]*)\s*=\s*(.*?)\s*$/.exec(raw);
10
- if (!m)
11
- continue;
12
- let value = m[2];
13
- const quote = value[0];
14
- if ((quote === '"' || quote === "'") && value.length >= 2 && value.endsWith(quote)) {
15
- value = value.slice(1, -1);
16
- if (quote === '"')
17
- value = value.replaceAll("\\n", "\n");
18
- }
19
- else {
20
- // 未加引号的值里,空白之后的 # 是注释
21
- value = value.replace(/\s+#.*$/, "");
22
- }
23
- out[m[1]] = value;
4
+ const out = {};
5
+ for (const raw of text.split(/\r?\n/)) {
6
+ const m = /^\s*(?:export\s+)?([A-Za-z_][A-Za-z0-9_]*)\s*=\s*(.*?)\s*$/.exec(raw);
7
+ if (!m)
8
+ continue;
9
+ let value = m[2];
10
+ const quote = value[0];
11
+ if ((quote === '"' || quote === "'") && value.length >= 2 && value.endsWith(quote)) {
12
+ value = value.slice(1, -1);
13
+ if (quote === '"')
14
+ value = value.replaceAll("\\n", `
15
+ `);
16
+ } else {
17
+ value = value.replace(/\s+#.*$/, "");
24
18
  }
25
- return out;
19
+ out[m[1]] = value;
20
+ }
21
+ return out;
26
22
  }
27
- /** 配置文件里的变量,env 里已有的优先;文件不存在时就是 env */
28
23
  export async function envWithFile(file, env = process.env) {
29
- let text = "";
30
- try {
31
- text = await readFile(file, "utf8");
32
- }
33
- catch {
34
- // 没有配置文件
35
- }
36
- return { ...parseEnv(text), ...Object.fromEntries(Object.entries(env).filter(([, v]) => v !== undefined)) };
24
+ let text = "";
25
+ try {
26
+ text = await readFile(file, "utf8");
27
+ } catch {}
28
+ return { ...parseEnv(text), ...Object.fromEntries(Object.entries(env).filter(([, v]) => v !== undefined)) };
37
29
  }
38
- /**
39
- * 改写配置文件里的变量:已有的行原地替换,没有的追加在末尾,值为 undefined 的删除;注释和其他行保持不变。
40
- * 同时更新 env,让改动在本次运行中生效
41
- */
42
30
  export async function updateEnvFile(file, updates, env) {
43
- let text = "";
44
- try {
45
- text = await readFile(file, "utf8");
46
- }
47
- catch {
48
- // 还没有配置文件
49
- }
50
- const lines = text ? text.replace(/\r?\n$/, "").split(/\r?\n/) : [];
51
- const out = [];
52
- const written = new Set();
53
- for (const raw of lines) {
54
- const key = /^\s*(?:export\s+)?([A-Za-z_][A-Za-z0-9_]*)\s*=/.exec(raw)?.[1];
55
- if (!key || !(key in updates)) {
56
- out.push(raw);
57
- continue;
58
- }
59
- // 同一个键出现多次时只保留第一处
60
- const value = updates[key];
61
- if (value !== undefined && !written.has(key))
62
- out.push(format(key, value));
63
- written.add(key);
64
- }
65
- for (const [key, value] of Object.entries(updates)) {
66
- if (value !== undefined && !written.has(key))
67
- out.push(format(key, value));
68
- }
69
- await mkdir(path.dirname(file), { recursive: true });
70
- await writeFile(file, out.join("\n") + "\n");
71
- for (const [key, value] of Object.entries(updates)) {
72
- if (value === undefined)
73
- delete env[key];
74
- else
75
- env[key] = value;
31
+ let text = "";
32
+ try {
33
+ text = await readFile(file, "utf8");
34
+ } catch {}
35
+ const lines = text ? text.replace(/\r?\n$/, "").split(/\r?\n/) : [];
36
+ const out = [];
37
+ const written = new Set;
38
+ for (const raw of lines) {
39
+ const key = /^\s*(?:export\s+)?([A-Za-z_][A-Za-z0-9_]*)\s*=/.exec(raw)?.[1];
40
+ if (!key || !(key in updates)) {
41
+ out.push(raw);
42
+ continue;
76
43
  }
44
+ const value = updates[key];
45
+ if (value !== undefined && !written.has(key))
46
+ out.push(format(key, value));
47
+ written.add(key);
48
+ }
49
+ for (const [key, value] of Object.entries(updates)) {
50
+ if (value !== undefined && !written.has(key))
51
+ out.push(format(key, value));
52
+ }
53
+ await mkdir(path.dirname(file), { recursive: true });
54
+ await writeFile(file, out.join(`
55
+ `) + `
56
+ `);
57
+ for (const [key, value] of Object.entries(updates)) {
58
+ if (value === undefined)
59
+ delete env[key];
60
+ else
61
+ env[key] = value;
62
+ }
77
63
  }
78
64
  function format(key, value) {
79
- return /[\s#"']/.test(value) ? `${key}="${value.replaceAll("\n", "\\n")}"` : `${key}=${value}`;
65
+ return /[\s#"']/.test(value) ? `${key}="${value.replaceAll(`
66
+ `, "\\n")}"` : `${key}=${value}`;
80
67
  }