@koishi-ce/plugin-explorer 1.0.1 → 1.0.2

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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@koishi-ce/plugin-explorer",
3
3
  "description": "File explorer and editor for Koishi",
4
- "version": "1.0.1",
4
+ "version": "1.0.2",
5
5
  "type": "module",
6
6
  "main": "lib/index.mjs",
7
7
  "types": "lib/index.d.ts",
@@ -63,9 +63,9 @@
63
63
  "@koishi-ce/koishi": "^1.0.0"
64
64
  },
65
65
  "devDependencies": {
66
- "@koishi-ce/client": "^1.0.3",
67
- "@koishi-ce/plugin-console": "^1.0.3",
68
- "@koishi-ce/koishi": "^1.0.4",
66
+ "@koishi-ce/client": "^1.0.4",
67
+ "@koishi-ce/plugin-console": "^1.0.4",
68
+ "@koishi-ce/koishi": "^1.0.6",
69
69
  "@vueuse/core": "^14.4.0",
70
70
  "monaco-editor": "~0.56.0",
71
71
  "vue": "^3.5.12",
package/src/index.test.ts CHANGED
@@ -2,42 +2,76 @@
2
2
  // Copyright (c) 2019-present Shigma and Koishijs contributors.
3
3
  // Copyright (c) 2026-present Koishi-CE contributors.
4
4
 
5
- import { afterAll, beforeAll, describe, expect, it } from "bun:test";
5
+ import {
6
+ afterAll,
7
+ beforeAll,
8
+ describe,
9
+ expect,
10
+ it,
11
+ } from "bun:test";
6
12
  import { existsSync, mkdtempSync, rmSync } from "node:fs";
7
- import { mkdir, readFile, symlink, writeFile } from "node:fs/promises";
13
+ import {
14
+ mkdir,
15
+ readFile,
16
+ symlink,
17
+ writeFile,
18
+ } from "node:fs/promises";
8
19
  import { tmpdir } from "node:os";
9
20
  import { join } from "node:path";
10
21
  import { Console, type Entry } from "@koishi-ce/console";
11
22
  import { App, type Plugin } from "@koishi-ce/koishi";
12
- import explorer, { type Entry as TreeEntry } from "./index.ts";
23
+ import explorer, {
24
+ type Entry as TreeEntry,
25
+ } from "./index.ts";
13
26
 
14
27
  /** 控制台服务桩:仅实现入口登记所需的最小面。 */
15
28
  class FakeConsole extends Console {
16
- protected resolveEntry(_files: Entry.Files, _key: string): string[] {
29
+ protected resolveEntry(
30
+ _files: Entry.Files,
31
+ _key: string,
32
+ ): string[] {
17
33
  return [];
18
34
  }
19
35
  }
20
36
 
21
37
  /** 在系统临时目录构造文件管理场景(含被忽略的隐藏文件与 node_modules)。 */
22
38
  async function createScenario() {
23
- const root = mkdtempSync(join(tmpdir(), "explorer-test-"));
24
- await writeFile(join(root, "b.txt"), "hello world", "utf8");
39
+ const root = mkdtempSync(
40
+ join(tmpdir(), "explorer-test-"),
41
+ );
42
+ await writeFile(
43
+ join(root, "b.txt"),
44
+ "hello world",
45
+ "utf8",
46
+ );
25
47
  // PNG 魔数 + IHDR chunk 头(file-type 依此识别 MIME)
26
48
  await writeFile(
27
49
  join(root, "a.png"),
28
50
  Buffer.from([
29
- 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d,
30
- 0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
31
- 0x08, 0x06, 0x00, 0x00, 0x00,
51
+ 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00,
52
+ 0x00, 0x00, 0x0d, 0x49, 0x48, 0x44, 0x52, 0x00, 0x00,
53
+ 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x08, 0x06, 0x00,
54
+ 0x00, 0x00,
32
55
  ]),
33
56
  );
34
57
  await mkdir(join(root, "sub"));
35
- await writeFile(join(root, "sub", "c.txt"), "nested", "utf8");
58
+ await writeFile(
59
+ join(root, "sub", "c.txt"),
60
+ "nested",
61
+ "utf8",
62
+ );
36
63
  await mkdir(join(root, "node_modules"));
37
- await writeFile(join(root, "node_modules", "x.txt"), "ignored");
64
+ await writeFile(
65
+ join(root, "node_modules", "x.txt"),
66
+ "ignored",
67
+ );
38
68
  await writeFile(join(root, ".hidden"), "ignored");
39
69
  // win32 无特权环境下用 junction 目录链接充当符号链接
40
- await symlink(join(root, "sub"), join(root, "link"), "junction");
70
+ await symlink(
71
+ join(root, "sub"),
72
+ join(root, "link"),
73
+ "junction",
74
+ );
41
75
  return root;
42
76
  }
43
77
 
@@ -45,7 +79,9 @@ const root = await createScenario();
45
79
 
46
80
  const app = new App();
47
81
 
48
- app.plugin(FakeConsole as unknown as Plugin.Constructor<App>);
82
+ app.plugin(
83
+ FakeConsole as unknown as Plugin.Constructor<App>,
84
+ );
49
85
  app.plugin(explorer, { root });
50
86
 
51
87
  /** 以假客户端身份调用指定 RPC 监听器。 */
@@ -54,8 +90,12 @@ async function call<K extends keyof Console["listeners"]>(
54
90
  ...args: unknown[]
55
91
  ) {
56
92
  const listener = app.console.listeners[event];
57
- if (!listener) throw new Error(`listener ${String(event)} not found`);
58
- return listener.callback.call({} as never, ...(args as never[]));
93
+ if (!listener)
94
+ throw new Error(`listener ${String(event)} not found`);
95
+ return listener.callback.call(
96
+ {} as never,
97
+ ...(args as never[]),
98
+ );
59
99
  }
60
100
 
61
101
  const service = () => app.get("console.services.explorer");
@@ -72,10 +112,17 @@ describe("explorer 文件管理服务", () => {
72
112
  const entries = (await service()?.get()) as TreeEntry[];
73
113
  const names = entries.map((entry) => entry.name);
74
114
  // 目录排最前,其余按字母序;ignored 项不出现
75
- expect(names).toEqual(["sub", "a.png", "b.txt", "link"]);
115
+ expect(names).toEqual([
116
+ "sub",
117
+ "a.png",
118
+ "b.txt",
119
+ "link",
120
+ ]);
76
121
  const sub = entries[0];
77
122
  expect(sub?.type).toBe("directory");
78
- expect(sub?.children?.map((child) => child.name)).toEqual(["c.txt"]);
123
+ expect(
124
+ sub?.children?.map((child) => child.name),
125
+ ).toEqual(["c.txt"]);
79
126
  // junction 链接识别为符号链接并给出目标
80
127
  const link = entries[3];
81
128
  expect(link?.type).toBe("symlink");
@@ -88,9 +135,9 @@ describe("explorer 文件管理服务", () => {
88
135
  encoding?: string;
89
136
  mime?: string;
90
137
  };
91
- expect(Buffer.from(file.base64, "base64").toString("utf8")).toBe(
92
- "hello world",
93
- );
138
+ expect(
139
+ Buffer.from(file.base64, "base64").toString("utf8"),
140
+ ).toBe("hello world");
94
141
  expect(file.encoding).toBeTruthy();
95
142
  // 文本文件不携带 mime
96
143
  expect(file.mime).toBeUndefined();
@@ -106,7 +153,9 @@ describe("explorer 文件管理服务", () => {
106
153
 
107
154
  it("write 支持文本与 base64 二进制写入", async () => {
108
155
  await call("explorer/write", "sub/new.txt", "新内容");
109
- expect(await readFile(join(root, "sub", "new.txt"), "utf8")).toBe("新内容");
156
+ expect(
157
+ await readFile(join(root, "sub", "new.txt"), "utf8"),
158
+ ).toBe("新内容");
110
159
 
111
160
  const buffer = Buffer.from([1, 2, 3, 4]);
112
161
  await call(
@@ -115,7 +164,9 @@ describe("explorer 文件管理服务", () => {
115
164
  buffer.toString("base64"),
116
165
  true,
117
166
  );
118
- expect(await readFile(join(root, "sub", "new.bin"))).toEqual(buffer);
167
+ expect(
168
+ await readFile(join(root, "sub", "new.bin")),
169
+ ).toEqual(buffer);
119
170
  });
120
171
 
121
172
  it("mkdir / rename / remove 执行实际文件操作", async () => {
@@ -139,10 +190,14 @@ describe("explorer 文件管理服务", () => {
139
190
  // 文件变动后强制刷新得到新树
140
191
  await writeFile(join(root, "z.txt"), "tail");
141
192
  const fresh = (await svc?.get(true)) as TreeEntry[];
142
- expect(fresh.map((entry) => entry.name)).toContain("z.txt");
193
+ expect(fresh.map((entry) => entry.name)).toContain(
194
+ "z.txt",
195
+ );
143
196
  });
144
197
 
145
198
  it("read 不存在的文件时抛错", async () => {
146
- expect(call("explorer/read", "not-exist.txt")).rejects.toThrow();
199
+ expect(
200
+ call("explorer/read", "not-exist.txt"),
201
+ ).rejects.toThrow();
147
202
  });
148
203
  });
package/src/index.ts CHANGED
@@ -22,7 +22,11 @@ import {
22
22
  } from "node:fs/promises";
23
23
  import { join, relative, resolve } from "node:path";
24
24
  import { DataService } from "@koishi-ce/console";
25
- import { type Context, type Dict, Schema } from "@koishi-ce/koishi";
25
+ import {
26
+ type Context,
27
+ type Dict,
28
+ Schema,
29
+ } from "@koishi-ce/koishi";
26
30
  import type { Tester } from "anymatch";
27
31
  import * as anymatchModule from "anymatch";
28
32
 
@@ -44,7 +48,10 @@ declare module "@koishi-ce/console" {
44
48
  }
45
49
 
46
50
  interface Events {
47
- "explorer/read"(filename: string, binary?: boolean): Promise<File>;
51
+ "explorer/read"(
52
+ filename: string,
53
+ binary?: boolean,
54
+ ): Promise<File>;
48
55
  "explorer/write"(
49
56
  filename: string,
50
57
  content: string,
@@ -52,7 +59,10 @@ declare module "@koishi-ce/console" {
52
59
  ): Promise<void>;
53
60
  "explorer/mkdir"(filename: string): Promise<void>;
54
61
  "explorer/remove"(filename: string): Promise<void>;
55
- "explorer/rename"(oldValue: string, newValue: string): Promise<void>;
62
+ "explorer/rename"(
63
+ oldValue: string,
64
+ newValue: string,
65
+ ): Promise<void>;
56
66
  "explorer/refresh"(): void;
57
67
  }
58
68
  }
@@ -125,7 +135,12 @@ class Explorer extends DataService<Entry[]> {
125
135
  `${process.env["KOISHI_BASE"]}/dist/style.css`,
126
136
  ]
127
137
  : process.env["KOISHI_ENV"] === "browser"
128
- ? [import.meta.url.replace(/\/src\/[^/]+$/, "/client/index.ts")]
138
+ ? [
139
+ import.meta.url.replace(
140
+ /\/src\/[^/]+$/,
141
+ "/client/index.ts",
142
+ ),
143
+ ]
129
144
  : {
130
145
  dev: resolve(__dirname, "../client/index.ts"),
131
146
  prod: resolve(__dirname, "../dist"),
@@ -149,7 +164,9 @@ class Explorer extends DataService<Entry[]> {
149
164
  const encoding = detect(buffer);
150
165
  // exactOptionalPropertyTypes:探测失败时不上键,与原先携带 undefined
151
166
  // 值的对象在 JSON 序列化后表现一致
152
- const file: File = { base64: buffer.toString("base64") };
167
+ const file: File = {
168
+ base64: buffer.toString("base64"),
169
+ };
153
170
  if (result) file.mime = result.mime;
154
171
  if (encoding) file.encoding = encoding;
155
172
  return file;
@@ -227,34 +244,42 @@ class Explorer extends DataService<Entry[]> {
227
244
  * 命中的路径(node_modules、隐藏文件等)直接跳过。
228
245
  */
229
246
  private async traverse(root: string): Promise<Entry[]> {
230
- const dirents = await readdir(root, { withFileTypes: true });
247
+ const dirents = await readdir(root, {
248
+ withFileTypes: true,
249
+ });
231
250
  return Promise.all(
232
- dirents.map(async (dirent): Promise<Entry | undefined> => {
233
- const filename = join(root, dirent.name);
234
- if (this.globFilter(relative(this.root, filename))) return;
235
- if (dirent.isFile()) {
236
- return { type: "file", name: dirent.name };
237
- } else if (dirent.isDirectory()) {
238
- return {
239
- type: "directory",
240
- name: dirent.name,
241
- children: await this.traverse(filename),
242
- };
243
- } else if (dirent.isSymbolicLink()) {
244
- return {
245
- type: "symlink",
246
- name: dirent.name,
247
- target: await readlink(filename),
248
- };
249
- }
250
- // 其余类型(FIFO / socket 等)不展示,显式返回以通过 noImplicitReturns
251
- return;
252
- }),
251
+ dirents.map(
252
+ async (dirent): Promise<Entry | undefined> => {
253
+ const filename = join(root, dirent.name);
254
+ if (
255
+ this.globFilter(relative(this.root, filename))
256
+ )
257
+ return;
258
+ if (dirent.isFile()) {
259
+ return { type: "file", name: dirent.name };
260
+ } else if (dirent.isDirectory()) {
261
+ return {
262
+ type: "directory",
263
+ name: dirent.name,
264
+ children: await this.traverse(filename),
265
+ };
266
+ } else if (dirent.isSymbolicLink()) {
267
+ return {
268
+ type: "symlink",
269
+ name: dirent.name,
270
+ target: await readlink(filename),
271
+ };
272
+ }
273
+ // 其余类型(FIFO / socket 等)不展示,显式返回以通过 noImplicitReturns
274
+ return;
275
+ },
276
+ ),
253
277
  ).then((entries) =>
254
278
  entries
255
279
  .filter((entry): entry is Entry => !!entry)
256
280
  .sort((a, b) => {
257
- if (a.type !== b.type) return a.type === "directory" ? -1 : 1;
281
+ if (a.type !== b.type)
282
+ return a.type === "directory" ? -1 : 1;
258
283
  return a.name.localeCompare(b.name);
259
284
  }),
260
285
  );