nuxi
Version:
Nuxt CLI
53 lines (52 loc) • 1.63 kB
JavaScript
import { spawn } from "node:child_process";
//#region ../../node_modules/.pnpm/tinyclip@0.1.12/node_modules/tinyclip/dist/index.js
const TIMEOUT = 2e3;
function checkUnixCommandExists(command) {
return new Promise((resolve) => {
const proc = spawn("which", [command]);
proc.on("error", () => resolve(false));
proc.on("close", (code) => resolve(code === 0));
});
}
async function getWriteCommand() {
switch (process.platform) {
case "darwin": return ["pbcopy", []];
case "win32": return ["clip", []];
case "linux":
case "freebsd":
case "openbsd":
if (process.env.WSL_DISTRO_NAME) return ["clip.exe", []];
if (process.env.WAYLAND_DISPLAY) return ["wl-copy", []];
if (await checkUnixCommandExists("xsel")) return ["xsel", ["--clipboard", "--input"]];
return ["xclip", [
"-selection",
"clipboard",
"-i"
]];
case "android": return ["termux-clipboard-set", []];
default: return;
}
}
/**
* Writes text to the clipboard.
*/
function writeText(text) {
return new Promise(async (resolve, reject) => {
const command = await getWriteCommand();
if (!command) return reject(/* @__PURE__ */ new Error("No clipboard tool found"));
const proc = spawn(...command, {
stdio: [
"pipe",
"ignore",
"ignore"
],
signal: AbortSignal.timeout(TIMEOUT)
});
proc.on("error", (cause) => reject(new Error("An error occurred while copying", { cause })));
proc.on("close", (code) => code === 0 ? resolve() : reject(/* @__PURE__ */ new Error("An unknown error occurred while copying")));
proc.stdin.write(text);
proc.stdin.end();
});
}
//#endregion
export { writeText };