UNPKG

listr2-toolkit

Version:

Batteries for Listr2

72 lines 3.42 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.selectPrinter = exports.attach = exports.schedule = void 0; const listr2_scheduler_1 = require("listr2-scheduler"); var listr2_scheduler_2 = require("listr2-scheduler"); Object.defineProperty(exports, "schedule", { enumerable: true, get: function () { return listr2_scheduler_2.schedule; } }); function attach(worker) { function print(target, args) { const { inspect } = require("node:util"); const toStr = (it) => (typeof it === "string" ? it : inspect(it)); (0, listr2_scheduler_1.decorateLines)(worker, args.map(toStr).join(" ") + "\n", target); } function log(...args) { print(process.stdout, args); } log.e = (...args) => print(process.stderr, args); log.v = (...args) => worker.printer === "verbose" && print(process.stderr, args); return { log, async run(command, options, config) { const { execa, $ } = await import("execa"); const settings = config != null ? config : !Array.isArray(options) ? options : undefined; const reject = settings?.reject ?? true; const verbose = worker.printer === "verbose"; const timestamp = settings?.timestamp ?? true; const cwd = settings?.cwd; const sub = Array.isArray(options) ? execa(command, options, { reject, cwd }) : $({ reject, cwd })(tsa(command)); worker.reportStatus(Array.isArray(options) ? [command, ...options].join(" ") : command); if (verbose) { const cnfo = { getTag: worker.getTag, timestamp }; sub.stdout && (0, listr2_scheduler_1.decorateLines)(cnfo, sub.stdout, process.stdout); const cnfe = { getTag: () => `E${worker.getTag()}`, timestamp }; sub.stderr && (0, listr2_scheduler_1.decorateLines)(cnfe, sub.stderr, process.stderr); } const result = await sub; return result.exitCode; }, async read(command, options, config) { const { execa, $ } = await import("execa"); const settings = config != null ? config : !Array.isArray(options) ? options : undefined; const reject = [true, "status"].includes(settings?.reject ?? true); const all = !(settings?.reject === "stderr" || settings?.reject === true); const cwd = settings?.cwd; const sub = Array.isArray(options) ? execa(command, options, { reject, all, cwd }) : $({ reject, all, cwd })(tsa(command)); const result = await sub; if (!all) { if (result.stderr.trim().length > 0) { throw new Error(result.stderr); } else { return result.stdout ? result.stdout.trim() : ""; } } return result.all ? result.all.trim() : ""; }, }; } exports.attach = attach; const verbosity = /^-(-verbose|.*v.*)$/; function selectPrinter(args, forceVerbose) { const verbose = forceVerbose || args.some((value) => value.match(verbosity)); return verbose ? "verbose" : "vivid"; } exports.selectPrinter = selectPrinter; function tsa(value) { return Object.defineProperty([value], "raw", { value }); } //# sourceMappingURL=index.js.map