@celljs/ta-workspace 4.8.0 → 4.9.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/README.md +2 -2
- package/README.zh-cn.md +2 -2
- package/package.json +13 -4
- package/web/index.js +913 -294
- package/web-src/browser-agent.ts +0 -440
- package/web-src/browser-common.tsx +0 -278
- package/web-src/browser-import.tsx +0 -126
- package/web-src/browser-webview.tsx +0 -270
- package/web-src/browser.tsx +0 -80
- package/web-src/common.tsx +0 -296
- package/web-src/editors.tsx +0 -471
- package/web-src/explorer.tsx +0 -717
- package/web-src/file-icon.tsx +0 -21
- package/web-src/host.ts +0 -121
- package/web-src/i18n.ts +0 -395
- package/web-src/index.tsx +0 -71
- package/web-src/open-files.tsx +0 -80
- package/web-src/preview.tsx +0 -132
- package/web-src/react.ts +0 -8
- package/web-src/scm.tsx +0 -388
- package/web-src/search.tsx +0 -485
- package/web-src/style.ts +0 -254
- package/web-src/terminal.tsx +0 -269
- package/web-src/util/browser-snapshot.spec.ts +0 -96
- package/web-src/util/browser-snapshot.ts +0 -225
- package/web-src/util/file-icon.spec.ts +0 -35
- package/web-src/util/file-icon.ts +0 -23
- package/web-src/util/git.ts +0 -203
- package/web-src/util/preview.spec.ts +0 -48
- package/web-src/util/preview.ts +0 -90
- package/web-src/util/search.spec.ts +0 -164
- package/web-src/util/search.ts +0 -274
- package/web-src/util/seti-font.ts +0 -6
- package/web-src/util/seti.ts +0 -761
- package/web-src/util/upload.spec.ts +0 -66
- package/web-src/util/upload.ts +0 -123
- package/web-src/util/workspace.spec.ts +0 -220
- package/web-src/util/workspace.ts +0 -396
- package/web-src/util/zip.spec.ts +0 -60
- package/web-src/util/zip.ts +0 -137
- package/web-src/workspace-client.ts +0 -221
|
@@ -1,221 +0,0 @@
|
|
|
1
|
-
import { TA_UPLOAD_PART_BYTES, type TaCommandOutput, type TaDirEntry, type TaRunCommandOptions } from '@celljs/ta-protocol';
|
|
2
|
-
import { pi } from './host';
|
|
3
|
-
import { type FileDiff, type GitStatus, GitUtil } from './util/git';
|
|
4
|
-
import { type Searcher, SearchUtil, type TextQuery, type TextResult } from './util/search';
|
|
5
|
-
import { type UploadBatch, UploadUtil } from './util/upload';
|
|
6
|
-
import { WorkspaceUtil } from './util/workspace';
|
|
7
|
-
import { type ZipEntry, ZipUtil } from './util/zip';
|
|
8
|
-
|
|
9
|
-
const SEARCH_LIMIT = 2000;
|
|
10
|
-
const SEARCH_BYTES = 8 * 1024 * 1024;
|
|
11
|
-
const SEARCHERS: Searcher[] = ['rg', 'git', 'grep'];
|
|
12
|
-
/** Pulling and pushing wait for the network. */
|
|
13
|
-
const REMOTE_TIMEOUT_MS = 120000;
|
|
14
|
-
/** A folder is downloaded as a zip made in the page: up to this much. */
|
|
15
|
-
const MAX_ARCHIVE_BYTES = 1024 * 1024 * 1024;
|
|
16
|
-
/** Files of a folder read at the same time while it is packed. */
|
|
17
|
-
const DOWNLOAD_CONCURRENCY = 4;
|
|
18
|
-
|
|
19
|
-
/** Bytes done of all, while files go up or a folder is packed; `name` is the file at hand. */
|
|
20
|
-
export type TransferProgress = (done: number, total: number, name: string) => void;
|
|
21
|
-
|
|
22
|
-
/** Search, git and changes to files for the views: the programs of the server (runCommand), read here. */
|
|
23
|
-
export namespace WorkspaceClient {
|
|
24
|
-
/** The searchers that could not run, by directory: the next search skips them. */
|
|
25
|
-
const missing = new Map<string, Set<Searcher>>();
|
|
26
|
-
|
|
27
|
-
export async function search(cwd: string, query: TextQuery): Promise<TextResult> {
|
|
28
|
-
const expression = SearchUtil.queryRegExp(query);
|
|
29
|
-
const filter = query.include || query.exclude ? SearchUtil.pathFilter(query) : undefined;
|
|
30
|
-
const skip = missing.get(cwd) ?? new Set<Searcher>();
|
|
31
|
-
missing.set(cwd, skip);
|
|
32
|
-
for (const searcher of SEARCHERS.filter(candidate => !skip.has(candidate))) {
|
|
33
|
-
const { command, args } = SearchUtil.command(searcher, query);
|
|
34
|
-
const output = await pi.workspace.runCommand(command, args, { cwd, maxBytes: SEARCH_BYTES });
|
|
35
|
-
if (SearchUtil.unavailable(searcher, output.exitCode, output.timedOut)) {
|
|
36
|
-
skip.add(searcher);
|
|
37
|
-
continue;
|
|
38
|
-
}
|
|
39
|
-
if (output.exitCode !== 0 && output.exitCode !== 1 && !output.stdout) {
|
|
40
|
-
throw new Error(output.stderr.trim() || `${command} failed.`);
|
|
41
|
-
}
|
|
42
|
-
const result = SearchUtil.parseMatches(output.stdout, expression, SEARCH_LIMIT, filter);
|
|
43
|
-
return { ...result, truncated: result.truncated || output.truncated || output.timedOut };
|
|
44
|
-
}
|
|
45
|
-
throw new Error('Neither rg, git nor grep can run here.');
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
/** With `ignored`, also what .gitignore leaves out, for the explorer. */
|
|
49
|
-
export async function status(cwd: string, ignored = false): Promise<GitStatus> {
|
|
50
|
-
const prefix = await pi.workspace.runCommand('git', GitUtil.PREFIX, { cwd });
|
|
51
|
-
if (prefix.exitCode !== 0) {
|
|
52
|
-
return { repository: false, files: [] };
|
|
53
|
-
}
|
|
54
|
-
const status = await pi.workspace.runCommand('git', GitUtil.status(ignored), { cwd, maxBytes: SEARCH_BYTES });
|
|
55
|
-
if (status.exitCode !== 0) {
|
|
56
|
-
throw new Error(status.stderr.trim() || 'git status failed.');
|
|
57
|
-
}
|
|
58
|
-
return GitUtil.parseStatus(status.stdout, prefix.stdout.trim());
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
/** Staged: the index against HEAD. Not staged: the working tree against the index. */
|
|
62
|
-
export async function diff(cwd: string, path: string, staged: boolean): Promise<FileDiff> {
|
|
63
|
-
const show = async (revision: string) => {
|
|
64
|
-
const shown = await pi.workspace.runCommand('git', GitUtil.show(revision, path), { cwd });
|
|
65
|
-
return shown.exitCode === 0 ? shown.stdout : '';
|
|
66
|
-
};
|
|
67
|
-
if (staged) {
|
|
68
|
-
return { path, before: await show('HEAD'), after: await show('') };
|
|
69
|
-
}
|
|
70
|
-
const tracked = (await pi.workspace.runCommand('git', GitUtil.tracked(path), { cwd })).exitCode === 0;
|
|
71
|
-
const before = tracked ? await show('') : '';
|
|
72
|
-
const after = await pi.workspace.readFile(path, cwd).then(
|
|
73
|
-
file => file.text ?? '',
|
|
74
|
-
() => '',
|
|
75
|
-
);
|
|
76
|
-
return { path, before, after };
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
/** Runs git; a failure throws what git said. */
|
|
80
|
-
export async function git(cwd: string, args: string[], options: TaRunCommandOptions = {}): Promise<TaCommandOutput> {
|
|
81
|
-
const output = await pi.workspace.runCommand('git', args, { ...options, cwd });
|
|
82
|
-
if (output.timedOut) {
|
|
83
|
-
throw new Error(`git ${args[0]} took too long.`);
|
|
84
|
-
}
|
|
85
|
-
if (output.exitCode !== 0) {
|
|
86
|
-
throw new Error(output.stderr.trim() || output.stdout.trim() || `git ${args[0]} failed.`);
|
|
87
|
-
}
|
|
88
|
-
return output;
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
export function commit(cwd: string, message: string): Promise<TaCommandOutput> {
|
|
92
|
-
return git(cwd, GitUtil.COMMIT, { input: message });
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
export function remote(cwd: string, command: 'pull' | 'push' | 'fetch'): Promise<TaCommandOutput> {
|
|
96
|
-
return git(cwd, [command], { timeoutMs: REMOTE_TIMEOUT_MS });
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
/** Runs a program of the files of `cwd`; a failure throws what it said. */
|
|
100
|
-
async function run(cwd: string, command: string, args: string[]): Promise<void> {
|
|
101
|
-
const output = await pi.workspace.runCommand(command, args, { cwd });
|
|
102
|
-
if (output.exitCode !== 0) {
|
|
103
|
-
throw new Error(output.stderr.trim() || `${command} failed.`);
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
/** An empty file; the folders on its way are made. */
|
|
108
|
-
export async function createFile(cwd: string, path: string): Promise<void> {
|
|
109
|
-
const dir = WorkspaceUtil.dirname(path);
|
|
110
|
-
if (dir) {
|
|
111
|
-
await run(cwd, 'mkdir', ['-p', '--', dir]);
|
|
112
|
-
}
|
|
113
|
-
await pi.workspace.writeFile(path, '', cwd);
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
export function createFolder(cwd: string, path: string): Promise<void> {
|
|
117
|
-
return run(cwd, 'mkdir', ['-p', '--', path]);
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
/** Never over another file: `mv -n` keeps what is there. */
|
|
121
|
-
export function rename(cwd: string, from: string, to: string): Promise<void> {
|
|
122
|
-
return run(cwd, 'mv', ['-n', '--', from, to]);
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
export function remove(cwd: string, path: string): Promise<void> {
|
|
126
|
-
return run(cwd, 'rm', ['-rf', '--', path]);
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
/** Into `folder` ('' for `cwd`), never over what is there: `mv -n` keeps it. */
|
|
130
|
-
export function move(cwd: string, paths: string[], folder: string): Promise<void> {
|
|
131
|
-
return run(cwd, 'mv', ['-n', '--', ...paths, folder || '.']);
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
/** Files of the user's machine into `folder` of `cwd`, a part at a time; answers how many. */
|
|
135
|
-
export async function upload(cwd: string, folder: string, batch: UploadBatch, onProgress?: TransferProgress): Promise<number> {
|
|
136
|
-
for (const empty of UploadUtil.emptyFolders(batch)) {
|
|
137
|
-
await createFolder(cwd, WorkspaceUtil.join(folder, empty));
|
|
138
|
-
}
|
|
139
|
-
const total = UploadUtil.totalBytes(batch);
|
|
140
|
-
let sent = 0;
|
|
141
|
-
for (const { path, file } of batch.files) {
|
|
142
|
-
const target = WorkspaceUtil.join(folder, UploadUtil.segments(path).join('/'));
|
|
143
|
-
for (const [start, end] of UploadUtil.parts(file.size, TA_UPLOAD_PART_BYTES)) {
|
|
144
|
-
onProgress?.(sent, total, file.name);
|
|
145
|
-
const bytes = new Uint8Array(await file.slice(start, end).arrayBuffer());
|
|
146
|
-
await pi.workspace.uploadFile(target, UploadUtil.toBase64(bytes), { cwd, append: start > 0 });
|
|
147
|
-
sent += end - start;
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
onProgress?.(sent, total, '');
|
|
151
|
-
return batch.files.length;
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
/** The files of a folder at any depth, `.git` left out as the explorer does, with their sizes. */
|
|
155
|
-
export async function filesOf(cwd: string, folder: string): Promise<TaDirEntry[]> {
|
|
156
|
-
const files: TaDirEntry[] = [];
|
|
157
|
-
let level = [folder];
|
|
158
|
-
while (level.length) {
|
|
159
|
-
const listed = await Promise.all(level.map(dir => pi.workspace.listDirectory(dir, cwd)));
|
|
160
|
-
level = [];
|
|
161
|
-
for (const entry of listed.flat()) {
|
|
162
|
-
if (entry.kind === 'directory') {
|
|
163
|
-
level.push(entry.path);
|
|
164
|
-
} else {
|
|
165
|
-
files.push(entry);
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
return files;
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
/** A file saved by the browser; a folder as a zip of its files, made here (`''` for `cwd` itself). */
|
|
173
|
-
export async function download(cwd: string, entry: Pick<TaDirEntry, 'path' | 'name' | 'kind'>, onProgress?: TransferProgress): Promise<void> {
|
|
174
|
-
if (entry.kind === 'file') {
|
|
175
|
-
save(await pi.workspace.fileUrl(entry.path, cwd), entry.name);
|
|
176
|
-
return;
|
|
177
|
-
}
|
|
178
|
-
const files = await filesOf(cwd, entry.path);
|
|
179
|
-
const total = files.reduce((sum, file) => sum + (file.size ?? 0), 0);
|
|
180
|
-
if (total > MAX_ARCHIVE_BYTES) {
|
|
181
|
-
throw new Error(`${WorkspaceUtil.size(total)} is more than a folder downloaded here may hold (${WorkspaceUtil.size(MAX_ARCHIVE_BYTES)}).`);
|
|
182
|
-
}
|
|
183
|
-
// The URL of the folder: those of its files are below it, as a page there loads its own.
|
|
184
|
-
const base = (await pi.workspace.fileUrl(entry.path, cwd)).replace(/\/?$/, '/');
|
|
185
|
-
const offset = entry.path ? entry.path.length + 1 : 0;
|
|
186
|
-
const entries: ZipEntry[] = new Array(files.length);
|
|
187
|
-
let done = 0;
|
|
188
|
-
let next = 0;
|
|
189
|
-
const worker = async () => {
|
|
190
|
-
while (next < files.length) {
|
|
191
|
-
const index = next++;
|
|
192
|
-
const file = files[index];
|
|
193
|
-
const relative = file.path.slice(offset);
|
|
194
|
-
onProgress?.(done, total, file.name);
|
|
195
|
-
const response = await fetch(base + relative.split('/').map(encodeURIComponent).join('/'));
|
|
196
|
-
if (!response.ok) {
|
|
197
|
-
throw new Error(`${file.path}: HTTP ${response.status}`);
|
|
198
|
-
}
|
|
199
|
-
entries[index] = { path: `${entry.name}/${relative}`, data: new Uint8Array(await response.arrayBuffer()) };
|
|
200
|
-
done += file.size ?? 0;
|
|
201
|
-
}
|
|
202
|
-
};
|
|
203
|
-
await Promise.all(Array.from({ length: Math.min(DOWNLOAD_CONCURRENCY, files.length) }, worker));
|
|
204
|
-
onProgress?.(total, total, '');
|
|
205
|
-
const zip = await ZipUtil.create(entries, ZipUtil.browserDeflate());
|
|
206
|
-
const url = URL.createObjectURL(new Blob([zip], { type: 'application/zip' }));
|
|
207
|
-
save(url, `${entry.name}.zip`);
|
|
208
|
-
setTimeout(() => URL.revokeObjectURL(url), 60000);
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
function save(url: string, name: string): void {
|
|
212
|
-
const link = document.createElement('a');
|
|
213
|
-
link.href = url;
|
|
214
|
-
link.download = name;
|
|
215
|
-
link.rel = 'noopener';
|
|
216
|
-
link.style.display = 'none';
|
|
217
|
-
document.body.append(link);
|
|
218
|
-
link.click();
|
|
219
|
-
link.remove();
|
|
220
|
-
}
|
|
221
|
-
}
|