Sha256: 9082d4f3850f0ed9ae3c1a31f8611638dccac4398d59385dd86c919002518446

Contents?: true

Size: 983 Bytes

Versions: 5

Compression:

Stored size: 983 Bytes

Contents

const { spawnSync } = require("child_process");
const os = require("os");

// Checks to see if an executable is available.
function hasCommand(name) {
  let result;

  if (os.type() === "Windows_NT") {
    result = spawnSync("where", [name]);
  } else {
    result = spawnSync("command", ["-v", name]);
  }

  return result.status === 0;
}

// Finds an netcat-like adapter to use for sending data to a socket. We order
// these by likelihood of being found so we can avoid some shell-outs.
function getCommandAndArg() {
  if (hasCommand("nc")) {
    return ["nc", "-U"];
  }

  if (hasCommand("telnet")) {
    return ["telnet", "-u"];
  }

  if (hasCommand("ncat")) {
    return ["ncat", "-U"];
  }

  if (hasCommand("socat")) {
    return ["socat", "-"];
  }

  return ["node", require.resolve("./netcat.js")];
}

let command;
let arg;

function getNetcat() {
  if (!command) {
    [command, arg] = getCommandAndArg();
  }

  return { command, arg };
}

module.exports = getNetcat;

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
prettier-1.5.3 src/parser/getNetcat.js
prettier-1.5.2 src/parser/getNetcat.js
prettier-1.5.1 src/parser/getNetcat.js
prettier-1.5.0 src/parser/getNetcat.js
prettier-1.4.0 src/parser/getNetcat.js