-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
lockfile.ts
79 lines (71 loc) · 2.19 KB
/
lockfile.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import * as vscode from "vscode";
import { spawn } from "node:child_process";
export type BunLockfile = vscode.CustomDocument & {
readonly preview: string;
};
export class BunLockfileEditorProvider implements vscode.CustomReadonlyEditorProvider {
constructor(context: vscode.ExtensionContext) {}
async openCustomDocument(
uri: vscode.Uri,
openContext: vscode.CustomDocumentOpenContext,
token: vscode.CancellationToken,
): Promise<BunLockfile> {
const preview = await previewLockfile(uri, token);
return {
uri,
preview,
dispose() {},
};
}
async resolveCustomEditor(
document: BunLockfile,
webviewPanel: vscode.WebviewPanel,
token: vscode.CancellationToken,
): Promise<void> {
const { preview } = document;
renderLockfile(webviewPanel, preview);
}
}
function renderLockfile(webviewPanel: vscode.WebviewPanel, preview: string): void {
// TODO: Improve syntax highlighting to match that of yarn.lock
webviewPanel.webview.html = `<pre><code class="language-yaml">${preview}</code></pre>`;
}
function previewLockfile(uri: vscode.Uri, token?: vscode.CancellationToken): Promise<string> {
return new Promise((resolve, reject) => {
const process = spawn("bun", [uri.fsPath], {
stdio: ["ignore", "pipe", "pipe"],
});
token.onCancellationRequested(() => {
process.kill();
});
let stdout = "";
process.stdout.on("data", (data: Buffer) => {
stdout += data.toString();
});
let stderr = "";
process.stderr.on("data", (data: Buffer) => {
stderr += data.toString();
});
process.on("error", error => {
reject(error);
});
process.on("exit", code => {
if (code === 0) {
resolve(stdout);
} else {
reject(new Error(`Bun exited with code: ${code}\n${stderr}`));
}
});
});
}
export default function (context: vscode.ExtensionContext): void {
const viewType = "bun.lockb";
const provider = new BunLockfileEditorProvider(context);
vscode.window.registerCustomEditorProvider(viewType, provider, {
supportsMultipleEditorsPerDocument: true,
webviewOptions: {
enableFindWidget: true,
retainContextWhenHidden: true,
},
});
}