Skip to content

Instantly share code, notes, and snippets.

@cem2ran
Created November 24, 2019 15:27
Show Gist options
  • Save cem2ran/0d02a658adf81fded8f1f1bf4879b571 to your computer and use it in GitHub Desktop.
Save cem2ran/0d02a658adf81fded8f1f1bf4879b571 to your computer and use it in GitHub Desktop.

Revisions

  1. cem2ran created this gist Nov 24, 2019.
    61 changes: 61 additions & 0 deletions Child_process.re
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,61 @@
    open Belt.Result;

    type options;

    module Stdio: {
    type t = pri string;

    [@bs.inline "inherit"]
    let inherit_: t;

    [@bs.inline "ignore"]
    let ignore: t;

    [@bs.inline "pipe"]
    let pipe: t;

    let _array: array(t) => t;
    let make: (t, t, t) => t;
    } = {
    type t = string;

    [@bs.inline]
    let inherit_ = "inherit";

    [@bs.inline]
    let ignore = "ignore";

    [@bs.inline]
    let pipe = "pipe";

    external _array: array(t) => t = "%identity";

    let make = (stdin, stdout, stderr) => [|stdin, stdout, stderr|] |> _array;
    };

    [@bs.obj]
    external options:
    (~cwd: string=?, ~stdio: Stdio.t=?, ~encoding: string=?, unit) => options =
    "";

    [@bs.module "child_process"]
    external _execSync: (string, options) => string = "execSync";

    let execSync = cmd =>
    try (
    Ok(
    {
    _execSync(
    cmd,
    options(
    ~encoding="utf-8",
    ~stdio=Stdio.(make(pipe, pipe, pipe)),
    (),
    ),
    )
    |> String.trim;
    },
    )
    ) {
    | e => Error(e)
    };