outers
Version:
outers - a all in one package for your day to day use
49 lines • 2.5 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const node_child_process_1 = require("node:child_process");
const node_util_1 = require("node:util");
const node_os_1 = __importDefault(require("node:os"));
// Promisify the spawn function so that it can be used with other async functions.
const SpawnCommander = (0, node_util_1.promisify)(node_child_process_1.spawn);
/**
* Executes a command with the given arguments and options.
* @param command - The command to execute.
* @param args - The arguments to pass to the command.
* @param options - The options for executing the command.
* @returns A promise that resolves to the command result, containing the output and error.
*/
function executeCommand(command, args, options = {}) {
return __awaiter(this, void 0, void 0, function* () {
try {
const isWindows = node_os_1.default.platform() === "win32"; // check for Windows or not supported
const isShell = isWindows ? true : false; // check for Windows or not
const { stdout, stderr } = (yield SpawnCommander(command, args, Object.assign({ shell: isShell }, options)));
return {
output: stdout.toString(),
error: stderr ? stderr.toString() : "",
};
}
catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
console.error("Error executing command:", errorMessage);
return {
output: "",
error: errorMessage,
};
}
});
}
exports.default = executeCommand;
//# sourceMappingURL=spawn.command.js.map