{"size": 848, "ext": "js", "max_stars_repo_path": "src/function/ink-exec.js", "max_stars_repo_name": "elricb/ink-control-components", "max_stars_repo_stars_event_min_datetime": "2022-01-12T17:36:57.000Z", "max_stars_repo_stars_event_max_datetime": "2022-01-12T17:36:57.000Z", "max_issues_repo_path": "src/function/ink-exec.js", "max_issues_repo_name": "elricb/ink-control-components", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/function/ink-exec.js", "max_forks_repo_name": "elricb/ink-control-components", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 22.3157894737, "max_line_length": 57, "alphanum_fraction": 0.5625}
import process from "process";
import {exec} from "child_process";
import childProcessError from "./child-process-error.js";

/// Exec shell command
const inkExec = function ({
  command,
  options = {},
  onDone = () => {},
  onError = () => {},
  writeOut = process.stdout.write,
  writeErr = process.stderr.write
}) {
  let errors = "";
  const cp = exec(command, options);
  cp.stdout.on("data", data => writeOut(data));
  cp.stderr.on("data", data => {
    errors += String(data);
    writeErr(data);
  });
  cp.on("close", code => {
    if (code != "0" && errors !== "") {
      onError(
        childProcessError(errors.trim("\n"), {
          errno: code,
          path: command,
          syscall: `exec ${command}`
        })
      );
    }

    onDone(code);
  });
  cp.on("error", error => onError(error));
};

export default inkExec;
