"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = pager;
exports.end = end;

var _child_process = require("child_process");

var _stream = require("stream");

/**
 * External dependencies
 */
let proc;

function pager() {
  let less;

  switch (process.platform) {
    case 'win32':
      // PROGRA~1 is the short name for Program Files
      // we're using it here to avoid the space which is broken by .split( ' ' )
      less = 'C:\\PROGRA~1\\Git\\usr\\bin\\less.exe -FRX';
      break;

    default:
      less = 'less -FRX';
  }

  const args = (process.env.PAGER || less).split(' ');
  const bin = args.shift(); // passthrough pipe so we can change the output pipe if necessary

  const pipe = new _stream.PassThrough();
  proc = (0, _child_process.spawn)(bin, args, {
    stdio: ['pipe', process.stdout, process.stderr]
  });
  proc.on('exit', () => {
    proc.stdin.emit('done');
  }); // If we can't spawn less, pipe directly to stdout

  pipe.pipe(proc.stdin);
  proc.on('error', () => {
    pipe.pipe(process.stdout);
  });
  return pipe;
}

function end() {
  proc.end();
}