@sunamo/sunodejs
Version:
Node.js utilities for file system operations, process management, and Electron apps. Includes TypeScript support with functions for file operations, directory management, and cross-platform compatibility.
69 lines (68 loc) • 2.17 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.executeCommandInCmd = executeCommandInCmd;
exports.executeCommandInCmdInDirAsync = executeCommandInCmdInDirAsync;
exports.executeCommandInCmdAsync = executeCommandInCmdAsync;
const child_process_1 = require("child_process");
function executeCommandInCmd(props) {
const { error } = props.log;
const { command } = props;
let { args, options } = props;
args ?? (args = []);
options ?? (options = {});
return new Promise((resolve, reject) => {
const defaultOptions = {
shell: true,
windowsHide: true,
};
const mergedOptions = { ...defaultOptions, ...options };
const process = (0, child_process_1.spawn)(command, args, mergedOptions);
let stdoutData = "";
let stderrData = "";
process.stdout.on("data", (data) => {
stdoutData += data;
});
process.stderr.on("data", (data) => {
stderrData += data;
error(`stderr: ${data} in folder ${props.options?.cwd}`);
});
process.on("close", (code) => {
if (code === 0) {
resolve(stdoutData);
}
else {
resolve("Something went wrong");
}
});
process.on("error", (err) => {
reject(err);
});
});
}
function executeCommandInCmdInDirAsync(command, workingDir) {
const options = {
cwd: workingDir,
};
const result = new Promise((resolve, reject) => {
(0, child_process_1.exec)(command, options, (error, stdout, stderr) => {
if (error) {
reject(error);
return;
}
resolve(stdout);
});
});
return result;
}
async function executeCommandInCmdAsync({ command, }) {
const result = new Promise((resolve, reject) => {
(0, child_process_1.exec)(command, (error, stdout, stderr) => {
if (error) {
reject(error);
return;
}
resolve(stdout);
});
});
return result;
}