Repository navigation
Expand file tree
/
Copy pathio.ts
More file actions
114 lines (108 loc) · 3.17 KB
/
Copy pathio.ts
File metadata and controls
114 lines (108 loc) · 3.17 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import { spawn } from "node:child_process";
import { readFile } from "node:fs/promises";
import { platform } from "node:os";
function runCapture(cmd: string, args: string[]): Promise<string> {
return new Promise((resolve, reject) => {
const child = spawn(cmd, args);
const chunks: Buffer[] = [];
const errChunks: Buffer[] = [];
child.stdout.on("data", (d) => chunks.push(d));
child.stderr.on("data", (d) => errChunks.push(d));
child.on("error", reject);
child.on("close", (code) => {
if (code === 0) resolve(Buffer.concat(chunks).toString("utf8"));
else
reject(
new Error(
`${cmd} exited ${code}: ${Buffer.concat(errChunks).toString("utf8")}`,
),
);
});
});
}
function runWithStdin(cmd: string, args: string[], input: string): Promise<void> {
return new Promise((resolve, reject) => {
const child = spawn(cmd, args);
child.on("error", reject);
child.stdin.on("error", reject);
child.stdin.end(input, "utf8");
child.on("close", (code) => {
if (code === 0) resolve();
else reject(new Error(`${cmd} exited ${code}`));
});
});
}
export async function readStdin(): Promise<Buffer> {
const chunks: Buffer[] = [];
for await (const chunk of process.stdin) {
chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
}
return Buffer.concat(chunks);
}
export async function readClipboard(): Promise<string> {
const os = platform();
try {
if (os === "darwin") return runCapture("pbpaste", []);
if (os === "win32") {
return runCapture("powershell", [
"-NoProfile",
"-Command",
"Get-Clipboard -Raw",
]);
}
try {
return await runCapture("wl-paste", ["-n"]);
} catch {
return runCapture("xclip", ["-selection", "clipboard", "-o"]);
}
} catch {
throw new Error(
"Could not read clipboard (need pbpaste / wl-paste / xclip / Get-Clipboard)",
);
}
}
export async function writeClipboard(text: string): Promise<void> {
const os = platform();
try {
if (os === "darwin") {
await runWithStdin("pbcopy", [], text);
return;
}
if (os === "win32") {
await runWithStdin(
"powershell",
["-NoProfile", "-Command", "$input | Set-Clipboard"],
text,
);
return;
}
try {
await runWithStdin("wl-copy", [], text);
} catch {
await runWithStdin("xclip", ["-selection", "clipboard"], text);
}
} catch {
throw new Error(
"Could not write clipboard (need pbcopy / wl-copy / xclip / Set-Clipboard)",
);
}
}
export async function readInputSource(
input: string,
opts: { clipboard?: boolean } = {},
): Promise<{ label: string; buffer: Buffer; textMode: boolean }> {
if (opts.clipboard || input === "--clipboard") {
const text = await readClipboard();
return {
label: "<clipboard>",
buffer: Buffer.from(text, "utf8"),
textMode: true,
};
}
if (input === "-" || input === "/dev/stdin") {
const buffer = await readStdin();
return { label: "<stdin>", buffer, textMode: true };
}
const buffer = await readFile(input);
return { label: input, buffer, textMode: false };
}