@x-otto/explore 0.0.0-stage → 0.4.0-beta.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/dist/build-revision.json +4 -0
- package/dist/index.d.ts +23 -0
- package/dist/index.js +145 -0
- package/package.json +24 -4
- package/README.md +0 -3
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { KernelMentionResolver, SandboxPort } from "@x-otto/kernel";
|
|
2
|
+
import * as _$_x_otto_universal0 from "@x-otto/universal";
|
|
3
|
+
|
|
4
|
+
//#region src/component.d.ts
|
|
5
|
+
interface ExploreOptions {
|
|
6
|
+
cwd?: string;
|
|
7
|
+
}
|
|
8
|
+
declare const Explore: _$_x_otto_universal0.ComponentDefinition<ExploreOptions>;
|
|
9
|
+
//#endregion
|
|
10
|
+
//#region src/file-resolver.d.ts
|
|
11
|
+
interface FileMentionScanState {
|
|
12
|
+
files: number;
|
|
13
|
+
truncated: boolean;
|
|
14
|
+
}
|
|
15
|
+
interface FileMentionResolver extends KernelMentionResolver {
|
|
16
|
+
scanState(): FileMentionScanState | undefined;
|
|
17
|
+
}
|
|
18
|
+
declare function createFileMentionResolver(cwd: string, sandbox?: SandboxPort): FileMentionResolver;
|
|
19
|
+
//#endregion
|
|
20
|
+
//#region src/view-content.d.ts
|
|
21
|
+
declare function createViewContent(absolutePath: string): (onChunk: (chunk: string) => void, signal: AbortSignal) => Promise<void>;
|
|
22
|
+
//#endregion
|
|
23
|
+
export { Explore, type ExploreOptions, createFileMentionResolver, createViewContent };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import { SLOTS } from "@x-otto/kernel";
|
|
2
|
+
import { defineComponent } from "@x-otto/universal";
|
|
3
|
+
import { stat } from "node:fs/promises";
|
|
4
|
+
import { join, resolve, sep } from "node:path";
|
|
5
|
+
import { isPathWithin, walkTree } from "@x-otto/shared";
|
|
6
|
+
import { createReadStream } from "node:fs";
|
|
7
|
+
|
|
8
|
+
//#region src/view-content.ts
|
|
9
|
+
function createViewContent(absolutePath) {
|
|
10
|
+
return (onChunk, signal) => new Promise((resolve, reject) => {
|
|
11
|
+
if (signal.aborted) {
|
|
12
|
+
resolve();
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
const stream = createReadStream(absolutePath, { signal });
|
|
16
|
+
let settled = false;
|
|
17
|
+
const finish = () => {
|
|
18
|
+
if (settled) return;
|
|
19
|
+
settled = true;
|
|
20
|
+
resolve();
|
|
21
|
+
};
|
|
22
|
+
stream.on("data", (chunk) => {
|
|
23
|
+
onChunk(chunk.toString("utf8"));
|
|
24
|
+
});
|
|
25
|
+
stream.on("end", finish);
|
|
26
|
+
stream.on("close", finish);
|
|
27
|
+
stream.on("error", (error) => {
|
|
28
|
+
if (settled) return;
|
|
29
|
+
settled = true;
|
|
30
|
+
if (signal.aborted) {
|
|
31
|
+
resolve();
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
reject(error);
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
//#endregion
|
|
40
|
+
//#region src/file-resolver.ts
|
|
41
|
+
const MAX_RESULTS = 200;
|
|
42
|
+
const SCAN_TTL_MS = 3e3;
|
|
43
|
+
async function scanFiles(cwd, isHidden) {
|
|
44
|
+
const results = [];
|
|
45
|
+
return {
|
|
46
|
+
files: results,
|
|
47
|
+
truncated: (await walkTree(cwd, {
|
|
48
|
+
base: cwd,
|
|
49
|
+
hidden: isHidden,
|
|
50
|
+
onFile: (rel) => {
|
|
51
|
+
results.push(rel.split(sep).join("/"));
|
|
52
|
+
}
|
|
53
|
+
})).truncated
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
async function createItemForPath(value, absolutePath) {
|
|
57
|
+
const info = await stat(absolutePath).catch((error) => {
|
|
58
|
+
if (error.code === "ENOENT") return;
|
|
59
|
+
throw error;
|
|
60
|
+
});
|
|
61
|
+
if (info === void 0) throw new Error(`file not found: ${value}`);
|
|
62
|
+
if (!info.isFile()) throw new Error(`not a regular file: ${value}`);
|
|
63
|
+
return {
|
|
64
|
+
value,
|
|
65
|
+
label: value,
|
|
66
|
+
viewContent: createViewContent(absolutePath)
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
function createFileMentionResolver(cwd, sandbox) {
|
|
70
|
+
let cached;
|
|
71
|
+
let inflight;
|
|
72
|
+
const scan = () => {
|
|
73
|
+
const snapshot = sandbox?.snapshot({ cwd });
|
|
74
|
+
return scanFiles(cwd, snapshot?.isHidden);
|
|
75
|
+
};
|
|
76
|
+
const refresh = () => {
|
|
77
|
+
inflight ??= scan().then((scanned) => {
|
|
78
|
+
cached = {
|
|
79
|
+
at: Date.now(),
|
|
80
|
+
files: scanned.files,
|
|
81
|
+
truncated: scanned.truncated
|
|
82
|
+
};
|
|
83
|
+
inflight = void 0;
|
|
84
|
+
return scanned;
|
|
85
|
+
}, (error) => {
|
|
86
|
+
inflight = void 0;
|
|
87
|
+
throw error;
|
|
88
|
+
});
|
|
89
|
+
return inflight;
|
|
90
|
+
};
|
|
91
|
+
const listFiles = async () => {
|
|
92
|
+
const snapshot = cached;
|
|
93
|
+
if (snapshot === void 0) return (await refresh()).files;
|
|
94
|
+
if (Date.now() - snapshot.at >= SCAN_TTL_MS) refresh().catch(() => {});
|
|
95
|
+
return snapshot.files;
|
|
96
|
+
};
|
|
97
|
+
return {
|
|
98
|
+
kind: "file",
|
|
99
|
+
label: "File",
|
|
100
|
+
chipKind: "file",
|
|
101
|
+
async search(query) {
|
|
102
|
+
const files = await listFiles();
|
|
103
|
+
const q = query.toLowerCase();
|
|
104
|
+
return (q === "" ? files : files.filter((f) => f.toLowerCase().includes(q))).slice(0, MAX_RESULTS).map((value) => ({
|
|
105
|
+
value,
|
|
106
|
+
label: value,
|
|
107
|
+
viewContent: createViewContent(join(cwd, value))
|
|
108
|
+
}));
|
|
109
|
+
},
|
|
110
|
+
async resolveItem(value) {
|
|
111
|
+
if (value === "") return;
|
|
112
|
+
const snapshot = sandbox?.snapshot({ cwd });
|
|
113
|
+
if (snapshot === void 0) {
|
|
114
|
+
const root = resolve(cwd);
|
|
115
|
+
const target = resolve(root, value);
|
|
116
|
+
if (!isPathWithin(root, target)) throw new Error(`path escapes working directory: ${value}`);
|
|
117
|
+
return createItemForPath(value, target);
|
|
118
|
+
}
|
|
119
|
+
const verdict = snapshot.check("read", cwd, value);
|
|
120
|
+
if (!verdict.allowed) throw new Error(verdict.reason);
|
|
121
|
+
return createItemForPath(value, verdict.resolved);
|
|
122
|
+
},
|
|
123
|
+
scanState() {
|
|
124
|
+
if (cached === void 0) return;
|
|
125
|
+
return {
|
|
126
|
+
files: cached.files.length,
|
|
127
|
+
truncated: cached.truncated
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
//#endregion
|
|
134
|
+
//#region src/component.ts
|
|
135
|
+
const Explore = defineComponent("Explore", (ctx, options = {}) => {
|
|
136
|
+
const sandbox = ctx.maybe(SLOTS.sandbox);
|
|
137
|
+
ctx.effect(() => {
|
|
138
|
+
if (options.cwd === void 0) return;
|
|
139
|
+
const resolver = createFileMentionResolver(options.cwd, sandbox());
|
|
140
|
+
ctx.contribute(SLOTS.mentionSources, resolver.kind, resolver);
|
|
141
|
+
});
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
//#endregion
|
|
145
|
+
export { Explore, createFileMentionResolver, createViewContent };
|
package/package.json
CHANGED
|
@@ -1,6 +1,26 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@x-otto/explore",
|
|
3
|
-
"version": "0.0.0
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.4.0-beta.0",
|
|
4
|
+
"description": "文件探索组件:贡献 mention 数据源(文件名 + 查看内容),承接自 default-tools 搬迁的职责(mention-explore-rfc)",
|
|
5
|
+
"files": [
|
|
6
|
+
"dist"
|
|
7
|
+
],
|
|
8
|
+
"type": "module",
|
|
9
|
+
"main": "./dist/index.js",
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"types": "./dist/index.d.ts",
|
|
14
|
+
"import": "./dist/index.js"
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
"dependencies": {
|
|
18
|
+
"@x-otto/kernel": "^0.4.0-beta.0",
|
|
19
|
+
"@x-otto/shared": "^0.4.0-beta.0",
|
|
20
|
+
"@x-otto/universal": "^0.4.0-beta.0"
|
|
21
|
+
},
|
|
22
|
+
"publishConfig": {
|
|
23
|
+
"access": "public",
|
|
24
|
+
"tag": "beta"
|
|
25
|
+
}
|
|
26
|
+
}
|
package/README.md
DELETED