UNPKG

utools-utils

Version:
110 lines (106 loc) 3.5 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/preload.ts var preload_exports = {}; __export(preload_exports, { execAppleScript: () => execAppleScript, execAppleScriptSync: () => execAppleScriptSync, execCommand: () => execCommand, execPowerShell: () => execPowerShell, execScript: () => execScript, spawnCommand: () => spawnCommand }); module.exports = __toCommonJS(preload_exports); var import_node_child_process = require("child_process"); // src/common.ts function hideAndOutPlugin() { utools.hideMainWindow(); utools.outPlugin(); } // src/preload.ts async function execCommand(command, options) { return new Promise((resolve, reject) => { const child = (0, import_node_child_process.exec)(command, options, (err, stdout) => { if (err) { reject(err); } else { resolve({ stdout: stdout.toString(), kill: () => child.kill() }); } }); }); } function spawnCommand(command, args, inputs, options) { return new Promise((resolve, reject) => { const child = (0, import_node_child_process.spawn)(command, args, options); if (inputs !== void 0 && inputs.length > 0) { for (const input of inputs) { child.stdin.write(input); } child.stdin.end(); } let stderr = Buffer.from(""); child.stderr.on("data", (chunk) => stderr += chunk); let stdout = Buffer.from(""); child.stdout.on("data", (chunk) => stdout += chunk); child.on("close", (code) => { if (code === 0) { resolve({ stdout: stdout.toString(), kill: () => child.kill() }); } else { reject(new Error(stderr.toString())); } }); child.on("error", reject); }); } async function execPowerShell(script) { return await execCommand(script, { shell: "powershell.exe" }); } async function execAppleScript(script) { return new Promise((resolve, reject) => { const child = (0, import_node_child_process.execFile)("osascript", ["-e", script], (err, stdout) => { if (err) { reject(err); } else { resolve({ stdout: stdout.toString(), kill: () => child.kill() }); } }); }); } function execAppleScriptSync(script) { return (0, import_node_child_process.execFileSync)("osascript", ["-e", script]).toString(); } async function execScript(script, defaultShell = false) { hideAndOutPlugin(); if (defaultShell) return execCommand(script); if (utools.isWindows()) return execPowerShell(script); if (utools.isMacOS()) return execAppleScript(script); return execCommand(script); } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { execAppleScript, execAppleScriptSync, execCommand, execPowerShell, execScript, spawnCommand });