@listr2/manager
Version: 
Extension for Listr2 to easily create sane defaults for task lists.
89 lines (86 loc) • 2.32 kB
JavaScript
//#region rolldown:runtime
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __copyProps = (to, from, except, desc) => {
	if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
		key = keys[i];
		if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
			get: ((k) => from[k]).bind(null, key),
			enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
		});
	}
	return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
	value: mod,
	enumerable: true
}) : target, mod));
//#endregion
let listr2 = require("listr2");
listr2 = __toESM(listr2);
//#region src/manager.ts
/**
* Creates a new Listr2 task manager.
*
* Useful for creating a single instance of Listr2 with pre-set settings.
*
* @see {@link https://listr2.kilic.dev/listr/manager.html}
*/
var Manager = class {
	errors = [];
	tasks = [];
	constructor(options) {
		this.options = options;
	}
	get ctx() {
		return this.options.ctx;
	}
	set ctx(ctx) {
		this.options.ctx = ctx;
	}
	add(tasks, options) {
		this.tasks = [...this.tasks, this.indent(tasks, options)];
	}
	async runAll(options) {
		options = {
			...this.options,
			...options
		};
		const tasks = [...this.tasks];
		this.tasks = [];
		return await this.run(tasks, options);
	}
	newListr(tasks, options) {
		return new listr2.Listr(tasks, options);
	}
	indent(tasks, options, taskOptions) {
		options = {
			...this.options,
			...options
		};
		if (typeof tasks === "function") return {
			...taskOptions,
			task: (ctx) => this.newListr(tasks(ctx), options)
		};
		return {
			...taskOptions,
			task: () => this.newListr(tasks, options)
		};
	}
	async run(tasks, options) {
		options = {
			...this.options,
			...options
		};
		const task = this.newListr(tasks, options);
		const ctx = await task.run();
		this.errors.push(...task.errors);
		return ctx;
	}
};
//#endregion
exports.Manager = Manager;