UNPKG

@jakechampion/cli-testing-library

Version:

Small but powerful library for testing CLI the way it is used by people.

45 lines (44 loc) 1.8 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.createExecute = void 0; const child_process_1 = require("child_process"); const os_1 = require("os"); const path_1 = __importDefault(require("path")); const createExecute = (base, output, currentProcessRef, exitCodeRef) => (runner, command, runFrom) => { return new Promise((accept) => { output.replaceStrings = { [(0, os_1.homedir)()]: '{{homedir}}', [`/private${base}`]: '{{base}}', [base]: '{{base}}', '[\\u001b\\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]': '', '': '', // you might not see it, but there is a special ESC symbol }; const args = command .split(' ') .map((arg) => arg.includes('./') ? path_1.default.join(process.cwd(), arg) : arg); let shell = (0, child_process_1.spawn)(runner, args, { cwd: runFrom ? path_1.default.join(base, runFrom) : path_1.default.join(base), }); shell.stdin.setDefaultEncoding('utf8'); if (currentProcessRef) { currentProcessRef.current = shell; } shell.stdout.on('data', (s) => (output.stdout = [s])); shell.stderr.on('data', (s) => (output.stderr = [s])); shell.on('close', (code) => { if (exitCodeRef) { exitCodeRef.current = code; } return accept({ code, stdout: output.stdout, stderr: output.stderr, }); }); shell = null; }); }; exports.createExecute = createExecute;