index.cjs 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336
  1. var __create = Object.create;
  2. var __defProp = Object.defineProperty;
  3. var __defProps = Object.defineProperties;
  4. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  5. var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
  6. var __getOwnPropNames = Object.getOwnPropertyNames;
  7. var __getOwnPropSymbols = Object.getOwnPropertySymbols;
  8. var __getProtoOf = Object.getPrototypeOf;
  9. var __hasOwnProp = Object.prototype.hasOwnProperty;
  10. var __propIsEnum = Object.prototype.propertyIsEnumerable;
  11. var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
  12. var __spreadValues = (a, b) => {
  13. for (var prop in b || (b = {}))
  14. if (__hasOwnProp.call(b, prop))
  15. __defNormalProp(a, prop, b[prop]);
  16. if (__getOwnPropSymbols)
  17. for (var prop of __getOwnPropSymbols(b)) {
  18. if (__propIsEnum.call(b, prop))
  19. __defNormalProp(a, prop, b[prop]);
  20. }
  21. return a;
  22. };
  23. var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
  24. var __markAsModule = (target) => __defProp(target, "__esModule", { value: true });
  25. var __export = (target, all) => {
  26. for (var name in all)
  27. __defProp(target, name, { get: all[name], enumerable: true });
  28. };
  29. var __reExport = (target, module2, copyDefault, desc) => {
  30. if (module2 && typeof module2 === "object" || typeof module2 === "function") {
  31. for (let key of __getOwnPropNames(module2))
  32. if (!__hasOwnProp.call(target, key) && (copyDefault || key !== "default"))
  33. __defProp(target, key, { get: () => module2[key], enumerable: !(desc = __getOwnPropDesc(module2, key)) || desc.enumerable });
  34. }
  35. return target;
  36. };
  37. var __toESM = (module2, isNodeMode) => {
  38. return __reExport(__markAsModule(__defProp(module2 != null ? __create(__getProtoOf(module2)) : {}, "default", !isNodeMode && module2 && module2.__esModule ? { get: () => module2.default, enumerable: true } : { value: module2, enumerable: true })), module2);
  39. };
  40. var __toCommonJS = /* @__PURE__ */ ((cache) => {
  41. return (module2, temp) => {
  42. return cache && cache.get(module2) || (temp = __reExport(__markAsModule({}), module2, 1), cache && cache.set(module2, temp), temp);
  43. };
  44. })(typeof WeakMap !== "undefined" ? /* @__PURE__ */ new WeakMap() : 0);
  45. // src/index.ts
  46. var src_exports = {};
  47. __export(src_exports, {
  48. Listr: () => Listr,
  49. ListrError: () => ListrError,
  50. ListrErrorTypes: () => ListrErrorTypes,
  51. ListrEventType: () => ListrEventType,
  52. ListrTaskState: () => ListrTaskState,
  53. LogLevels: () => LogLevels,
  54. Logger: () => Logger,
  55. Manager: () => Manager,
  56. PromptError: () => PromptError,
  57. createPrompt: () => createPrompt,
  58. destroyPrompt: () => destroyPrompt,
  59. figures: () => figures
  60. });
  61. // src/listr.ts
  62. var import_p_map = __toESM(require("p-map"), 1);
  63. var import_rxjs2 = require("rxjs");
  64. // src/constants/state.constants.ts
  65. var ListrTaskState = /* @__PURE__ */ ((ListrTaskState2) => {
  66. ListrTaskState2["PENDING"] = "PENDING";
  67. ListrTaskState2["COMPLETED"] = "COMPLETED";
  68. ListrTaskState2["FAILED"] = "FAILED";
  69. ListrTaskState2["SKIPPED"] = "SKIPPED";
  70. ListrTaskState2["ROLLING_BACK"] = "ROLLING_BACK";
  71. ListrTaskState2["ROLLED_BACK"] = "ROLLED_BACK";
  72. ListrTaskState2["RETRY"] = "RETRY";
  73. return ListrTaskState2;
  74. })(ListrTaskState || {});
  75. // src/lib/task.ts
  76. var import_rxjs = require("rxjs");
  77. var import_stream = require("stream");
  78. // src/constants/event.constants.ts
  79. var ListrEventType = /* @__PURE__ */ ((ListrEventType2) => {
  80. ListrEventType2["TITLE"] = "TITLE";
  81. ListrEventType2["STATE"] = "STATE";
  82. ListrEventType2["ENABLED"] = "ENABLED";
  83. ListrEventType2["SUBTASK"] = "SUBTASK";
  84. ListrEventType2["DATA"] = "DATA";
  85. ListrEventType2["MESSAGE"] = "MESSAGE";
  86. return ListrEventType2;
  87. })(ListrEventType || {});
  88. // src/utils/general.ts
  89. var import_rfdc = __toESM(require("rfdc"), 1);
  90. var clone = (0, import_rfdc.default)({ circles: true });
  91. function cloneObject(obj) {
  92. return clone(obj);
  93. }
  94. // src/interfaces/listr-error.interface.ts
  95. var ListrError = class extends Error {
  96. constructor(error, type, task) {
  97. super(error.message);
  98. this.error = error;
  99. this.type = type;
  100. this.task = task;
  101. var _a2;
  102. this.name = "ListrError";
  103. this.path = [...(_a2 = task.listr.path) != null ? _a2 : [], task.title].join(" > ");
  104. if ((task == null ? void 0 : task.options.collectErrors) === "full") {
  105. this.task = cloneObject(task);
  106. this.ctx = cloneObject(task.listr.ctx);
  107. }
  108. this.stack = error == null ? void 0 : error.stack;
  109. }
  110. };
  111. var ListrErrorTypes = /* @__PURE__ */ ((ListrErrorTypes2) => {
  112. ListrErrorTypes2["WILL_RETRY"] = "WILL_RETRY";
  113. ListrErrorTypes2["WILL_ROLLBACK"] = "WILL_ROLLBACK";
  114. ListrErrorTypes2["HAS_FAILED_TO_ROLLBACK"] = "HAS_FAILED_TO_ROLLBACK";
  115. ListrErrorTypes2["HAS_FAILED"] = "HAS_FAILED";
  116. ListrErrorTypes2["HAS_FAILED_WITHOUT_ERROR"] = "HAS_FAILED_WITHOUT_ERROR";
  117. return ListrErrorTypes2;
  118. })(ListrErrorTypes || {});
  119. var PromptError = class extends Error {
  120. constructor(message) {
  121. super(message);
  122. this.name = "PromptError";
  123. }
  124. };
  125. // src/utils/assert.ts
  126. function assertFunctionOrSelf(functionOrSelf, ...args) {
  127. if (typeof functionOrSelf === "function") {
  128. return functionOrSelf(...args);
  129. } else {
  130. return functionOrSelf;
  131. }
  132. }
  133. // src/renderer/default.renderer.ts
  134. var import_cli_truncate = __toESM(require("cli-truncate"), 1);
  135. var import_log_update = __toESM(require("log-update"), 1);
  136. var import_os = require("os");
  137. var import_wrap_ansi = __toESM(require("wrap-ansi"), 1);
  138. // src/utils/colorette.ts
  139. var import_colorette = require("colorette");
  140. var _a;
  141. var colorette_default = (0, import_colorette.createColors)({ useColor: ((_a = process.env) == null ? void 0 : _a.LISTR_DISABLE_COLOR) !== "1" });
  142. // src/utils/is-unicode-supported.ts
  143. function isUnicodeSupported() {
  144. if (process.platform !== "win32") {
  145. return true;
  146. }
  147. return Boolean(process.env.CI) || Boolean(process.env.WT_SESSION) || process.env.TERM_PROGRAM === "vscode" || process.env.TERM === "xterm-256color" || process.env.TERM === "alacritty";
  148. }
  149. // src/utils/figures.ts
  150. var FIGURES_MAIN = {
  151. warning: "\u26A0",
  152. cross: "\u2716",
  153. arrowDown: "\u2193",
  154. tick: "\u2714",
  155. arrowRight: "\u2192",
  156. pointer: "\u276F",
  157. checkboxOn: "\u2612",
  158. arrowLeft: "\u2190",
  159. squareSmallFilled: "\u25FC",
  160. pointerSmall: "\u203A"
  161. };
  162. var FIGURES_FALLBACK = __spreadProps(__spreadValues({}, FIGURES_MAIN), {
  163. warning: "\u203C",
  164. cross: "\xD7",
  165. tick: "\u221A",
  166. pointer: ">",
  167. checkboxOn: "[\xD7]",
  168. squareSmallFilled: "\u25A0"
  169. });
  170. var figures = isUnicodeSupported() ? FIGURES_MAIN : FIGURES_FALLBACK;
  171. // src/utils/indent-string.ts
  172. function indentString(string, count) {
  173. return string.replace(/^(?!\s*$)/gm, " ".repeat(count));
  174. }
  175. // src/utils/parse-time.ts
  176. function parseTaskTime(duration) {
  177. const seconds = Math.floor(duration / 1e3);
  178. const minutes = Math.floor(seconds / 60);
  179. let parsedTime;
  180. if (seconds === 0 && minutes === 0) {
  181. parsedTime = `0.${Math.floor(duration / 100)}s`;
  182. }
  183. if (seconds > 0) {
  184. parsedTime = `${seconds % 60}s`;
  185. }
  186. if (minutes > 0) {
  187. parsedTime = `${minutes}m${parsedTime}`;
  188. }
  189. return parsedTime;
  190. }
  191. // src/renderer/default.renderer.ts
  192. var _DefaultRenderer = class {
  193. constructor(tasks, options, renderHook$) {
  194. this.tasks = tasks;
  195. this.options = options;
  196. this.renderHook$ = renderHook$;
  197. this.bottomBar = {};
  198. this.spinner = !isUnicodeSupported() ? ["-", "\\", "|", "/"] : ["\u280B", "\u2819", "\u2839", "\u2838", "\u283C", "\u2834", "\u2826", "\u2827", "\u2807", "\u280F"];
  199. this.spinnerPosition = 0;
  200. this.options = __spreadValues(__spreadValues({}, _DefaultRenderer.rendererOptions), this.options);
  201. }
  202. getTaskOptions(task) {
  203. return __spreadValues(__spreadValues({}, _DefaultRenderer.rendererTaskOptions), task.rendererTaskOptions);
  204. }
  205. isBottomBar(task) {
  206. const bottomBar = this.getTaskOptions(task).bottomBar;
  207. return typeof bottomBar === "number" && bottomBar !== 0 || typeof bottomBar === "boolean" && bottomBar !== false;
  208. }
  209. hasPersistentOutput(task) {
  210. return this.getTaskOptions(task).persistentOutput === true;
  211. }
  212. hasTimer(task) {
  213. return this.getTaskOptions(task).showTimer === true;
  214. }
  215. getSelfOrParentOption(task, key) {
  216. var _a2, _b, _c;
  217. return (_c = (_a2 = task == null ? void 0 : task.rendererOptions) == null ? void 0 : _a2[key]) != null ? _c : (_b = this.options) == null ? void 0 : _b[key];
  218. }
  219. getTaskTime(task) {
  220. return colorette_default.dim(`[${parseTaskTime(task.message.duration)}]`);
  221. }
  222. createRender(options) {
  223. options = __spreadValues(__spreadValues({}, {
  224. tasks: true,
  225. bottomBar: true,
  226. prompt: true
  227. }), options);
  228. const render = [];
  229. const renderTasks = this.multiLineRenderer(this.tasks);
  230. const renderBottomBar = this.renderBottomBar();
  231. const renderPrompt = this.renderPrompt();
  232. if (options.tasks && (renderTasks == null ? void 0 : renderTasks.trim().length) > 0) {
  233. render.push(renderTasks);
  234. }
  235. if (options.bottomBar && (renderBottomBar == null ? void 0 : renderBottomBar.trim().length) > 0) {
  236. render.push((render.length > 0 ? import_os.EOL : "") + renderBottomBar);
  237. }
  238. if (options.prompt && (renderPrompt == null ? void 0 : renderPrompt.trim().length) > 0) {
  239. render.push((render.length > 0 ? import_os.EOL : "") + renderPrompt);
  240. }
  241. return render.length > 0 ? render.join(import_os.EOL) : "";
  242. }
  243. render() {
  244. var _a2;
  245. if (this.id) {
  246. return;
  247. }
  248. const updateRender = () => (0, import_log_update.default)(this.createRender());
  249. if (!((_a2 = this.options) == null ? void 0 : _a2.lazy)) {
  250. this.id = setInterval(() => {
  251. this.spinnerPosition = ++this.spinnerPosition % this.spinner.length;
  252. updateRender();
  253. }, 100);
  254. }
  255. this.renderHook$.subscribe(() => {
  256. updateRender();
  257. });
  258. }
  259. end() {
  260. clearInterval(this.id);
  261. if (this.id) {
  262. this.id = void 0;
  263. }
  264. import_log_update.default.clear();
  265. import_log_update.default.done();
  266. if (!this.options.clearOutput) {
  267. process.stdout.write(this.createRender({ prompt: false }) + import_os.EOL);
  268. }
  269. }
  270. multiLineRenderer(tasks, level = 0) {
  271. var _a2, _b;
  272. let output = [];
  273. for (const task of tasks) {
  274. if (task.isEnabled()) {
  275. if (task.hasTitle()) {
  276. if (!(tasks.some((task2) => task2.hasFailed()) && !task.hasFailed() && task.options.exitOnError !== false && !(task.isCompleted() || task.isSkipped()))) {
  277. if (task.hasFailed() && this.getSelfOrParentOption(task, "collapseErrors")) {
  278. output = [
  279. ...output,
  280. this.formatString(!task.hasSubtasks() && task.message.error && this.getSelfOrParentOption(task, "showErrorMessage") ? task.message.error : task.title, this.getSymbol(task), level)
  281. ];
  282. } else if (task.isSkipped() && this.getSelfOrParentOption(task, "collapseSkips")) {
  283. output = [
  284. ...output,
  285. this.formatString(this.addSuffixToMessage(task.message.skip && this.getSelfOrParentOption(task, "showSkipMessage") ? task.message.skip : task.title, "SKIPPED", this.getSelfOrParentOption(task, "suffixSkips")), this.getSymbol(task), level)
  286. ];
  287. } else if (task.isRetrying() && this.getSelfOrParentOption(task, "suffixRetries")) {
  288. output = [...output, this.formatString(this.addSuffixToMessage(task.title, `RETRYING-${task.message.retry.count}`), this.getSymbol(task), level)];
  289. } else if (task.isCompleted() && task.hasTitle() && (this.getSelfOrParentOption(task, "showTimer") || this.hasTimer(task))) {
  290. output = [...output, this.formatString(`${task == null ? void 0 : task.title} ${this.getTaskTime(task)}`, this.getSymbol(task), level)];
  291. } else {
  292. output = [...output, this.formatString(task.title, this.getSymbol(task), level)];
  293. }
  294. } else {
  295. output = [...output, this.formatString(task.title, colorette_default.red(figures.squareSmallFilled), level)];
  296. }
  297. }
  298. if (!task.hasSubtasks() || !this.getSelfOrParentOption(task, "showSubtasks")) {
  299. if (task.hasFailed() && this.getSelfOrParentOption(task, "collapseErrors") === false && (this.getSelfOrParentOption(task, "showErrorMessage") || !this.getSelfOrParentOption(task, "showSubtasks"))) {
  300. output = [...output, this.dumpData(task, level, "error")];
  301. } else if (task.isSkipped() && this.getSelfOrParentOption(task, "collapseSkips") === false && (this.getSelfOrParentOption(task, "showSkipMessage") || !this.getSelfOrParentOption(task, "showSubtasks"))) {
  302. output = [...output, this.dumpData(task, level, "skip")];
  303. }
  304. }
  305. if (task == null ? void 0 : task.output) {
  306. if ((task.isPending() || task.isRetrying() || task.isRollingBack()) && task.isPrompt()) {
  307. this.promptBar = task.output;
  308. } else if (this.isBottomBar(task) || !task.hasTitle()) {
  309. const data = [this.dumpData(task, -1)];
  310. if (!this.bottomBar[task.id]) {
  311. this.bottomBar[task.id] = {};
  312. this.bottomBar[task.id].data = [];
  313. const bottomBar = this.getTaskOptions(task).bottomBar;
  314. if (typeof bottomBar === "boolean") {
  315. this.bottomBar[task.id].items = 1;
  316. } else {
  317. this.bottomBar[task.id].items = bottomBar;
  318. }
  319. }
  320. if (!((_b = (_a2 = this.bottomBar[task.id]) == null ? void 0 : _a2.data) == null ? void 0 : _b.some((element) => data.includes(element))) && !task.isSkipped()) {
  321. this.bottomBar[task.id].data = [...this.bottomBar[task.id].data, ...data];
  322. }
  323. } else if (task.isPending() || task.isRetrying() || task.isRollingBack() || this.hasPersistentOutput(task)) {
  324. output = [...output, this.dumpData(task, level)];
  325. }
  326. }
  327. if (this.getSelfOrParentOption(task, "showSubtasks") !== false && task.hasSubtasks() && (task.isPending() || task.hasFailed() || task.isCompleted() && !task.hasTitle() || task.isCompleted() && this.getSelfOrParentOption(task, "collapse") === false && !task.subtasks.some((subtask) => subtask.rendererOptions.collapse === true) || task.subtasks.some((subtask) => subtask.rendererOptions.collapse === false) || task.subtasks.some((subtask) => subtask.hasFailed()) || task.subtasks.some((subtask) => subtask.hasRolledBack()))) {
  328. const subtaskLevel = !task.hasTitle() ? level : level + 1;
  329. const subtaskRender = this.multiLineRenderer(task.subtasks, subtaskLevel);
  330. if ((subtaskRender == null ? void 0 : subtaskRender.trim()) !== "" && !task.subtasks.every((subtask) => !subtask.hasTitle())) {
  331. output = [...output, subtaskRender];
  332. }
  333. }
  334. if (task.isCompleted() || task.hasFailed() || task.isSkipped() || task.hasRolledBack()) {
  335. this.promptBar = null;
  336. if (!this.hasPersistentOutput(task)) {
  337. delete this.bottomBar[task.id];
  338. }
  339. }
  340. }
  341. }
  342. output = output.filter(Boolean);
  343. if (output.length > 0) {
  344. return output.join(import_os.EOL);
  345. } else {
  346. return;
  347. }
  348. }
  349. renderBottomBar() {
  350. if (Object.keys(this.bottomBar).length > 0) {
  351. this.bottomBar = Object.keys(this.bottomBar).reduce((o, key) => {
  352. if (!(o == null ? void 0 : o[key])) {
  353. o[key] = {};
  354. }
  355. o[key] = this.bottomBar[key];
  356. this.bottomBar[key].data = this.bottomBar[key].data.slice(-this.bottomBar[key].items);
  357. o[key].data = this.bottomBar[key].data;
  358. return o;
  359. }, {});
  360. return Object.values(this.bottomBar).reduce((o, value) => o = [...o, ...value.data], []).filter(Boolean).join(import_os.EOL);
  361. }
  362. }
  363. renderPrompt() {
  364. if (this.promptBar) {
  365. return this.promptBar;
  366. }
  367. }
  368. dumpData(task, level, source = "output") {
  369. let data;
  370. switch (source) {
  371. case "output":
  372. data = task.output;
  373. break;
  374. case "skip":
  375. data = task.message.skip;
  376. break;
  377. case "error":
  378. data = task.message.error;
  379. break;
  380. }
  381. if (task.hasTitle() && source === "error" && data === task.title) {
  382. return;
  383. }
  384. if (typeof data === "string") {
  385. return this.formatString(data, this.getSymbol(task, true), level + 1);
  386. }
  387. }
  388. formatString(str, icon, level) {
  389. if (str.trim() === "") {
  390. return;
  391. }
  392. str = `${icon} ${str}`;
  393. let parsedStr;
  394. let columns = process.stdout.columns ? process.stdout.columns : 80;
  395. columns = columns - level * this.options.indentation - 2;
  396. switch (this.options.formatOutput) {
  397. case "truncate":
  398. parsedStr = str.split(import_os.EOL).map((s, i) => {
  399. return (0, import_cli_truncate.default)(this.indentMultilineOutput(s, i), columns);
  400. });
  401. break;
  402. case "wrap":
  403. parsedStr = (0, import_wrap_ansi.default)(str, columns, { hard: true }).split(import_os.EOL).map((s, i) => this.indentMultilineOutput(s, i));
  404. break;
  405. default:
  406. throw new Error("Format option for the renderer is wrong.");
  407. }
  408. if (this.options.removeEmptyLines) {
  409. parsedStr = parsedStr.filter(Boolean);
  410. }
  411. return indentString(parsedStr.join(import_os.EOL), level * this.options.indentation);
  412. }
  413. indentMultilineOutput(str, i) {
  414. return i > 0 ? indentString(str.trim(), 2) : str.trim();
  415. }
  416. getSymbol(task, data = false) {
  417. var _a2, _b, _c;
  418. if (task.isPending() && !data) {
  419. return ((_a2 = this.options) == null ? void 0 : _a2.lazy) || this.getSelfOrParentOption(task, "showSubtasks") !== false && task.hasSubtasks() && !task.subtasks.every((subtask) => !subtask.hasTitle()) ? colorette_default.yellow(figures.pointer) : colorette_default.yellowBright(this.spinner[this.spinnerPosition]);
  420. } else if (task.isCompleted() && !data) {
  421. return task.hasSubtasks() && task.subtasks.some((subtask) => subtask.hasFailed()) ? colorette_default.yellow(figures.warning) : colorette_default.green(figures.tick);
  422. } else if (task.isRetrying() && !data) {
  423. return ((_b = this.options) == null ? void 0 : _b.lazy) ? colorette_default.yellow(figures.warning) : colorette_default.yellow(this.spinner[this.spinnerPosition]);
  424. } else if (task.isRollingBack() && !data) {
  425. return ((_c = this.options) == null ? void 0 : _c.lazy) ? colorette_default.red(figures.warning) : colorette_default.red(this.spinner[this.spinnerPosition]);
  426. } else if (task.hasRolledBack() && !data) {
  427. return colorette_default.red(figures.arrowLeft);
  428. } else if (task.hasFailed() && !data) {
  429. return task.hasSubtasks() ? colorette_default.red(figures.pointer) : colorette_default.red(figures.cross);
  430. } else if (task.isSkipped() && !data && this.getSelfOrParentOption(task, "collapseSkips") === false) {
  431. return colorette_default.yellow(figures.warning);
  432. } else if (task.isSkipped() && (data || this.getSelfOrParentOption(task, "collapseSkips"))) {
  433. return colorette_default.yellow(figures.arrowDown);
  434. }
  435. return !data ? colorette_default.dim(figures.squareSmallFilled) : figures.pointerSmall;
  436. }
  437. addSuffixToMessage(message, suffix, condition) {
  438. return (condition != null ? condition : true) ? message + colorette_default.dim(` [${suffix}]`) : message;
  439. }
  440. };
  441. var DefaultRenderer = _DefaultRenderer;
  442. DefaultRenderer.nonTTY = false;
  443. DefaultRenderer.rendererOptions = {
  444. indentation: 2,
  445. clearOutput: false,
  446. showSubtasks: true,
  447. collapse: true,
  448. collapseSkips: true,
  449. showSkipMessage: true,
  450. suffixSkips: true,
  451. collapseErrors: true,
  452. showErrorMessage: true,
  453. suffixRetries: true,
  454. lazy: false,
  455. showTimer: false,
  456. removeEmptyLines: true,
  457. formatOutput: "truncate"
  458. };
  459. // src/renderer/silent.renderer.ts
  460. var SilentRenderer = class {
  461. constructor(tasks, options) {
  462. this.tasks = tasks;
  463. this.options = options;
  464. }
  465. render() {
  466. }
  467. end() {
  468. }
  469. };
  470. SilentRenderer.nonTTY = true;
  471. // src/renderer/simple.renderer.ts
  472. var import_log_update2 = require("log-update");
  473. var import_os2 = require("os");
  474. var _SimpleRenderer = class {
  475. constructor(tasks, options) {
  476. this.tasks = tasks;
  477. this.options = options;
  478. this.eventTypeRendererMap = {
  479. ["SUBTASK" /* SUBTASK */]: (task) => {
  480. if (task.hasTitle()) {
  481. this.log(`${colorette_default.blue(figures.pointer)} ${task.title}`);
  482. }
  483. if (task.hasSubtasks()) {
  484. this.render(task.subtasks);
  485. }
  486. },
  487. ["STATE" /* STATE */]: (task) => {
  488. if (task.isCompleted() && task.hasTitle()) {
  489. this.log(`${colorette_default.green(figures.tick)} ${task.title}`);
  490. }
  491. },
  492. ["DATA" /* DATA */]: (task, event) => {
  493. if (task.isPrompt() && !String(event.data).match(/^\n$/)) {
  494. (0, import_log_update2.stderr)(`${event.data}`);
  495. } else {
  496. this.log(`${figures.pointerSmall} ${event.data}`);
  497. }
  498. },
  499. ["MESSAGE" /* MESSAGE */]: (task, event) => {
  500. if (event.data.error) {
  501. const title = _SimpleRenderer.formatTitle(task);
  502. this.log(`${colorette_default.red(figures.cross)}${title}: ${event.data.error}`);
  503. } else if (event.data.skip) {
  504. const title = _SimpleRenderer.formatTitle(task);
  505. const skip = task.title !== event.data.skip ? `: ${event.data.skip}` : "";
  506. this.log(`${colorette_default.yellow(figures.arrowDown)}${title} [${colorette_default.yellow(`skipped${skip}`)}]`);
  507. } else if (event.data.rollback) {
  508. const title = _SimpleRenderer.formatTitle(task);
  509. this.log(`${colorette_default.red(figures.arrowLeft)}${title}: ${event.data.rollback}`);
  510. } else if (event.data.retry) {
  511. const title = _SimpleRenderer.formatTitle(task);
  512. this.log(`[${colorette_default.yellow(`${event.data.retry.count}`)}]${title}`);
  513. }
  514. }
  515. };
  516. this.options = __spreadValues(__spreadValues({}, _SimpleRenderer.rendererOptions), options);
  517. }
  518. static now() {
  519. return new Date();
  520. }
  521. static formatTitle(task) {
  522. return (task == null ? void 0 : task.title) ? ` ${task.title}` : "";
  523. }
  524. log(output) {
  525. const logOut = (msg) => {
  526. process[this.options.output].write(msg.endsWith(import_os2.EOL) ? msg : `${msg}${import_os2.EOL}`);
  527. };
  528. if (!this.options.prefixWithTimestamp) {
  529. logOut(`${output}`);
  530. return;
  531. }
  532. const now = _SimpleRenderer.now();
  533. const timestamp = String(now.getHours()).padStart(2, "0") + ":" + String(now.getMinutes()).padStart(2, "0") + ":" + String(now.getSeconds()).padStart(2, "0");
  534. logOut(`${colorette_default.dim(`[${timestamp}]`)} ${output}`);
  535. }
  536. end() {
  537. }
  538. render(tasks) {
  539. if (tasks == null ? void 0 : tasks.length) {
  540. tasks.forEach((task) => {
  541. task.subscribe((event) => {
  542. var _a2, _b;
  543. (_b = (_a2 = this.eventTypeRendererMap)[event.type]) == null ? void 0 : _b.call(_a2, task, event);
  544. }, this.log);
  545. });
  546. } else {
  547. this.render(this.tasks);
  548. }
  549. }
  550. };
  551. var SimpleRenderer = _SimpleRenderer;
  552. SimpleRenderer.nonTTY = true;
  553. SimpleRenderer.rendererOptions = { prefixWithTimestamp: false, output: "stdout" };
  554. // src/utils/logger.constants.ts
  555. var LogLevels = /* @__PURE__ */ ((LogLevels2) => {
  556. LogLevels2["SILENT"] = "SILENT";
  557. LogLevels2["FAILED"] = "FAILED";
  558. LogLevels2["SKIPPED"] = "SKIPPED";
  559. LogLevels2["SUCCESS"] = "SUCCESS";
  560. LogLevels2["DATA"] = "DATA";
  561. LogLevels2["STARTED"] = "STARTED";
  562. LogLevels2["TITLE"] = "TITLE";
  563. LogLevels2["RETRY"] = "RETRY";
  564. LogLevels2["ROLLBACK"] = "ROLLBACK";
  565. return LogLevels2;
  566. })(LogLevels || {});
  567. // src/utils/logger.ts
  568. var Logger = class {
  569. constructor(options) {
  570. this.options = options;
  571. }
  572. fail(message) {
  573. message = this.parseMessage("FAILED" /* FAILED */, message);
  574. console.error(message);
  575. }
  576. skip(message) {
  577. message = this.parseMessage("SKIPPED" /* SKIPPED */, message);
  578. console.info(message);
  579. }
  580. success(message) {
  581. message = this.parseMessage("SUCCESS" /* SUCCESS */, message);
  582. console.log(message);
  583. }
  584. data(message) {
  585. message = this.parseMessage("DATA" /* DATA */, message);
  586. console.info(message);
  587. }
  588. start(message) {
  589. message = this.parseMessage("STARTED" /* STARTED */, message);
  590. console.log(message);
  591. }
  592. title(message) {
  593. message = this.parseMessage("TITLE" /* TITLE */, message);
  594. console.info(message);
  595. }
  596. retry(message) {
  597. message = this.parseMessage("RETRY" /* RETRY */, message);
  598. console.warn(message);
  599. }
  600. rollback(message) {
  601. message = this.parseMessage("ROLLBACK" /* ROLLBACK */, message);
  602. console.warn(message);
  603. }
  604. parseMessage(level, message) {
  605. let multiLineMessage;
  606. try {
  607. multiLineMessage = message.split("\n");
  608. } catch {
  609. multiLineMessage = [message];
  610. }
  611. multiLineMessage = multiLineMessage.map((msg) => {
  612. return this.logColoring({
  613. level,
  614. message: msg
  615. });
  616. });
  617. message = multiLineMessage.join("\n");
  618. return message;
  619. }
  620. logColoring({ level, message }) {
  621. var _a2, _b, _c, _d, _e, _f, _g, _h;
  622. let icon;
  623. let coloring = (input) => {
  624. return input;
  625. };
  626. switch (level) {
  627. case "FAILED" /* FAILED */:
  628. if ((_a2 = this.options) == null ? void 0 : _a2.useIcons) {
  629. coloring = colorette_default.red;
  630. icon = figures.cross;
  631. } else {
  632. icon = this.wrapInBrackets(level);
  633. }
  634. break;
  635. case "SKIPPED" /* SKIPPED */:
  636. if ((_b = this.options) == null ? void 0 : _b.useIcons) {
  637. coloring = colorette_default.yellow;
  638. icon = figures.arrowDown;
  639. } else {
  640. icon = this.wrapInBrackets(level);
  641. }
  642. break;
  643. case "SUCCESS" /* SUCCESS */:
  644. if ((_c = this.options) == null ? void 0 : _c.useIcons) {
  645. coloring = colorette_default.green;
  646. icon = figures.tick;
  647. } else {
  648. icon = this.wrapInBrackets(level);
  649. }
  650. break;
  651. case "DATA" /* DATA */:
  652. if ((_d = this.options) == null ? void 0 : _d.useIcons) {
  653. icon = figures.arrowRight;
  654. } else {
  655. icon = this.wrapInBrackets(level);
  656. }
  657. break;
  658. case "STARTED" /* STARTED */:
  659. if ((_e = this.options) == null ? void 0 : _e.useIcons) {
  660. icon = figures.pointer;
  661. } else {
  662. icon = this.wrapInBrackets(level);
  663. }
  664. break;
  665. case "TITLE" /* TITLE */:
  666. if ((_f = this.options) == null ? void 0 : _f.useIcons) {
  667. icon = figures.checkboxOn;
  668. } else {
  669. icon = this.wrapInBrackets(level);
  670. }
  671. break;
  672. case "RETRY" /* RETRY */:
  673. if ((_g = this.options) == null ? void 0 : _g.useIcons) {
  674. coloring = colorette_default.yellow;
  675. icon = figures.pointer;
  676. } else {
  677. icon = this.wrapInBrackets(level);
  678. }
  679. break;
  680. case "ROLLBACK" /* ROLLBACK */:
  681. if ((_h = this.options) == null ? void 0 : _h.useIcons) {
  682. coloring = colorette_default.red;
  683. icon = figures.arrowLeft;
  684. } else {
  685. icon = this.wrapInBrackets(level);
  686. }
  687. break;
  688. }
  689. return coloring(`${icon} ${message}`);
  690. }
  691. wrapInBrackets(level) {
  692. return `[${level}]`;
  693. }
  694. };
  695. // src/renderer/verbose.renderer.ts
  696. var _VerboseRenderer = class {
  697. constructor(tasks, options) {
  698. this.tasks = tasks;
  699. this.options = options;
  700. var _a2, _b, _c, _d;
  701. if (((_a2 = this.options) == null ? void 0 : _a2.logger) && ((_b = this.options) == null ? void 0 : _b.options)) {
  702. this.logger = new this.options.logger(this.options.options);
  703. } else if ((_c = this.options) == null ? void 0 : _c.logger) {
  704. this.logger = new this.options.logger();
  705. } else {
  706. this.logger = new Logger({ useIcons: (_d = this.options) == null ? void 0 : _d.useIcons });
  707. }
  708. this.options = __spreadValues(__spreadValues({}, _VerboseRenderer.rendererOptions), this.options);
  709. }
  710. render() {
  711. this.verboseRenderer(this.tasks);
  712. }
  713. end() {
  714. }
  715. verboseRenderer(tasks) {
  716. return tasks == null ? void 0 : tasks.forEach((task) => {
  717. task.subscribe((event) => {
  718. var _a2, _b, _c, _d, _e, _f, _g, _h;
  719. if (task.isEnabled()) {
  720. const taskTitle = task.hasTitle() ? task.title : "Task without title.";
  721. if (event.type === "SUBTASK" /* SUBTASK */ && task.hasSubtasks()) {
  722. this.verboseRenderer(task.subtasks);
  723. } else if (event.type === "STATE" /* STATE */) {
  724. if (((_a2 = this.options) == null ? void 0 : _a2.logEmptyTitle) !== false || task.hasTitle()) {
  725. if (task.isPending()) {
  726. this.logger.start(taskTitle);
  727. } else if (task.isCompleted()) {
  728. this.logger.success(taskTitle + (((_b = this.options) == null ? void 0 : _b.showTimer) && ((_c = task.message) == null ? void 0 : _c.duration) ? ` [${parseTaskTime(task.message.duration)}]` : ""));
  729. }
  730. }
  731. } else if (event.type === "DATA" /* DATA */ && !!event.data) {
  732. this.logger.data(String(event.data));
  733. } else if (event.type === "TITLE" /* TITLE */) {
  734. if (((_d = this.options) == null ? void 0 : _d.logTitleChange) !== false) {
  735. this.logger.title(String(event.data));
  736. }
  737. } else if (event.type === "MESSAGE" /* MESSAGE */) {
  738. if ((_e = event.data) == null ? void 0 : _e.error) {
  739. this.logger.fail(String(event.data.error));
  740. } else if ((_f = event.data) == null ? void 0 : _f.skip) {
  741. this.logger.skip(String(event.data.skip));
  742. } else if ((_g = event.data) == null ? void 0 : _g.rollback) {
  743. this.logger.rollback(String(event.data.rollback));
  744. } else if ((_h = event.data) == null ? void 0 : _h.retry) {
  745. this.logger.retry(`[${event.data.retry.count}] ` + String(taskTitle));
  746. }
  747. }
  748. }
  749. }, (err) => {
  750. this.logger.fail(err);
  751. });
  752. });
  753. }
  754. };
  755. var VerboseRenderer = _VerboseRenderer;
  756. VerboseRenderer.nonTTY = true;
  757. VerboseRenderer.rendererOptions = {
  758. useIcons: false,
  759. logEmptyTitle: true,
  760. logTitleChange: true
  761. };
  762. // src/utils/renderer.ts
  763. var renderers = {
  764. default: DefaultRenderer,
  765. simple: SimpleRenderer,
  766. verbose: VerboseRenderer,
  767. silent: SilentRenderer
  768. };
  769. function isRendererSupported(renderer) {
  770. return process.stdout.isTTY === true || renderer.nonTTY === true;
  771. }
  772. function getRendererClass(renderer) {
  773. if (typeof renderer === "string") {
  774. return renderers[renderer] || renderers.default;
  775. }
  776. return typeof renderer === "function" ? renderer : renderers.default;
  777. }
  778. function getRenderer(renderer, fallbackRenderer, fallbackCondition, silentCondition) {
  779. let returnValue;
  780. let ret = getRendererClass(renderer);
  781. returnValue = { renderer: ret, nonTTY: false };
  782. const evaluateSilent = assertFunctionOrSelf(silentCondition);
  783. const evaluateFallback = assertFunctionOrSelf(fallbackCondition);
  784. if (evaluateSilent) {
  785. ret = getRendererClass("silent");
  786. returnValue = { renderer: ret, nonTTY: true };
  787. } else if (!isRendererSupported(ret) || evaluateFallback) {
  788. ret = getRendererClass(fallbackRenderer);
  789. returnValue = { renderer: ret, nonTTY: true };
  790. }
  791. return returnValue;
  792. }
  793. // src/utils/uuid.ts
  794. function generateUUID() {
  795. return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
  796. const r = Math.random() * 16 || 0;
  797. const v = c === "x" ? r : r && 3 || 8;
  798. return v.toString(16);
  799. });
  800. }
  801. // src/lib/task.ts
  802. var Task = class extends import_rxjs.Subject {
  803. constructor(listr, tasks, options, rendererOptions) {
  804. super();
  805. this.listr = listr;
  806. this.tasks = tasks;
  807. this.options = options;
  808. this.rendererOptions = rendererOptions;
  809. this.message = {};
  810. var _a2, _b, _c, _d, _e, _f;
  811. this.id = generateUUID();
  812. this.title = (_a2 = this.tasks) == null ? void 0 : _a2.title;
  813. this.initialTitle = (_b = this.tasks) == null ? void 0 : _b.title;
  814. this.task = this.tasks.task;
  815. this.skip = (_d = (_c = this.tasks) == null ? void 0 : _c.skip) != null ? _d : false;
  816. this.enabledFn = (_f = (_e = this.tasks) == null ? void 0 : _e.enabled) != null ? _f : true;
  817. this.rendererTaskOptions = this.tasks.options;
  818. this.renderHook$ = this.listr.renderHook$;
  819. this.subscribe(() => {
  820. this.renderHook$.next();
  821. });
  822. }
  823. set state$(state) {
  824. this.state = state;
  825. this.next({
  826. type: "STATE" /* STATE */,
  827. data: state
  828. });
  829. if (this.hasSubtasks() && this.hasFailed()) {
  830. for (const subtask of this.subtasks) {
  831. if (subtask.state === "PENDING" /* PENDING */) {
  832. subtask.state$ = "FAILED" /* FAILED */;
  833. }
  834. }
  835. }
  836. }
  837. set output$(data) {
  838. this.output = data;
  839. this.next({
  840. type: "DATA" /* DATA */,
  841. data
  842. });
  843. }
  844. set message$(data) {
  845. this.message = __spreadValues(__spreadValues({}, this.message), data);
  846. this.next({
  847. type: "MESSAGE" /* MESSAGE */,
  848. data
  849. });
  850. }
  851. set title$(title) {
  852. this.title = title;
  853. this.next({
  854. type: "TITLE" /* TITLE */,
  855. data: title
  856. });
  857. }
  858. async check(ctx) {
  859. if (this.state === void 0) {
  860. this.enabled = await assertFunctionOrSelf(this.enabledFn, ctx);
  861. this.next({
  862. type: "ENABLED" /* ENABLED */,
  863. data: this.enabled
  864. });
  865. }
  866. }
  867. hasSubtasks() {
  868. var _a2;
  869. return ((_a2 = this.subtasks) == null ? void 0 : _a2.length) > 0;
  870. }
  871. isPending() {
  872. return this.state === "PENDING" /* PENDING */;
  873. }
  874. isSkipped() {
  875. return this.state === "SKIPPED" /* SKIPPED */;
  876. }
  877. isCompleted() {
  878. return this.state === "COMPLETED" /* COMPLETED */;
  879. }
  880. hasFailed() {
  881. return this.state === "FAILED" /* FAILED */;
  882. }
  883. isRollingBack() {
  884. return this.state === "ROLLING_BACK" /* ROLLING_BACK */;
  885. }
  886. hasRolledBack() {
  887. return this.state === "ROLLED_BACK" /* ROLLED_BACK */;
  888. }
  889. isRetrying() {
  890. return this.state === "RETRY" /* RETRY */;
  891. }
  892. isEnabled() {
  893. return this.enabled;
  894. }
  895. hasTitle() {
  896. return typeof (this == null ? void 0 : this.title) === "string";
  897. }
  898. isPrompt() {
  899. return !!this.prompt;
  900. }
  901. async run(context, wrapper) {
  902. var _a2, _b, _c, _d, _e;
  903. const handleResult = (result) => {
  904. if (result instanceof Listr) {
  905. result.options = __spreadValues(__spreadValues({}, this.options), result.options);
  906. result.rendererClass = getRenderer("silent").renderer;
  907. result.renderHook$.subscribe(() => {
  908. this.renderHook$.next();
  909. });
  910. this.subtasks = result.tasks;
  911. result.err = this.listr.err;
  912. this.next({ type: "SUBTASK" /* SUBTASK */ });
  913. result = result.run(context);
  914. } else if (this.isPrompt()) {
  915. } else if (result instanceof Promise) {
  916. result = result.then(handleResult);
  917. } else if (result instanceof import_stream.Readable) {
  918. result = new Promise((resolve, reject) => {
  919. result.on("data", (data) => {
  920. this.output$ = data.toString();
  921. });
  922. result.on("error", (error) => reject(error));
  923. result.on("end", () => resolve(null));
  924. });
  925. } else if (result instanceof import_rxjs.Observable) {
  926. result = new Promise((resolve, reject) => {
  927. result.subscribe({
  928. next: (data) => {
  929. this.output$ = data;
  930. },
  931. error: reject,
  932. complete: resolve
  933. });
  934. });
  935. }
  936. return result;
  937. };
  938. const startTime = Date.now();
  939. this.state$ = "PENDING" /* PENDING */;
  940. const skipped = await assertFunctionOrSelf(this.skip, context);
  941. if (skipped) {
  942. if (typeof skipped === "string") {
  943. this.message$ = { skip: skipped };
  944. } else if (this.hasTitle()) {
  945. this.message$ = { skip: this.title };
  946. } else {
  947. this.message$ = { skip: "Skipped task without a title." };
  948. }
  949. this.state$ = "SKIPPED" /* SKIPPED */;
  950. return;
  951. }
  952. try {
  953. const retryCount = ((_a2 = this.tasks) == null ? void 0 : _a2.retry) && ((_b = this.tasks) == null ? void 0 : _b.retry) > 0 ? this.tasks.retry + 1 : 1;
  954. for (let retries = 1; retries <= retryCount; retries++) {
  955. try {
  956. await handleResult(this.task(context, wrapper));
  957. break;
  958. } catch (err) {
  959. if (retries !== retryCount) {
  960. this.retry = { count: retries, withError: err };
  961. this.message$ = { retry: this.retry };
  962. this.title$ = this.initialTitle;
  963. this.output = void 0;
  964. wrapper.report(err, "WILL_RETRY" /* WILL_RETRY */);
  965. this.state$ = "RETRY" /* RETRY */;
  966. } else {
  967. throw err;
  968. }
  969. }
  970. }
  971. if (this.isPending() || this.isRetrying()) {
  972. this.message$ = { duration: Date.now() - startTime };
  973. this.state$ = "COMPLETED" /* COMPLETED */;
  974. }
  975. } catch (error) {
  976. if (this.prompt instanceof PromptError) {
  977. error = new Error(this.prompt.message);
  978. }
  979. if ((_c = this.tasks) == null ? void 0 : _c.rollback) {
  980. wrapper.report(error, "WILL_ROLLBACK" /* WILL_ROLLBACK */);
  981. try {
  982. this.state$ = "ROLLING_BACK" /* ROLLING_BACK */;
  983. await this.tasks.rollback(context, wrapper);
  984. this.state$ = "ROLLED_BACK" /* ROLLED_BACK */;
  985. this.message$ = { rollback: this.title };
  986. } catch (err) {
  987. this.state$ = "FAILED" /* FAILED */;
  988. wrapper.report(err, "HAS_FAILED_TO_ROLLBACK" /* HAS_FAILED_TO_ROLLBACK */);
  989. throw err;
  990. }
  991. if (((_d = this.listr.options) == null ? void 0 : _d.exitAfterRollback) !== false) {
  992. throw new Error(this.title);
  993. }
  994. } else {
  995. this.state$ = "FAILED" /* FAILED */;
  996. if (this.listr.options.exitOnError !== false && await assertFunctionOrSelf((_e = this.tasks) == null ? void 0 : _e.exitOnError, context) !== false) {
  997. wrapper.report(error, "HAS_FAILED" /* HAS_FAILED */);
  998. throw error;
  999. } else if (!this.hasSubtasks()) {
  1000. wrapper.report(error, "HAS_FAILED_WITHOUT_ERROR" /* HAS_FAILED_WITHOUT_ERROR */);
  1001. }
  1002. }
  1003. } finally {
  1004. this.complete();
  1005. }
  1006. }
  1007. };
  1008. // src/lib/task-wrapper.ts
  1009. var import_through = __toESM(require("through"), 1);
  1010. // src/constants/clearline-regex.constants.ts
  1011. var CLEAR_LINE_REGEX = "(?:\\u001b|\\u009b)\\[[\\=><~/#&.:=?%@~_-]*[0-9]*[\\a-ln-tqyz=><~/#&.:=?%@~_-]+";
  1012. var BELL_REGEX = /\u0007/;
  1013. // src/utils/prompt.ts
  1014. function defaultCancelCallback(settings) {
  1015. const errorMsg = "Cancelled prompt.";
  1016. if (this instanceof TaskWrapper) {
  1017. this.task.prompt = new PromptError(errorMsg);
  1018. } else if ((settings == null ? void 0 : settings.error) !== false) {
  1019. throw new Error(errorMsg);
  1020. } else {
  1021. return errorMsg;
  1022. }
  1023. }
  1024. async function createPrompt(options, settings) {
  1025. let cancelCallback;
  1026. if (settings == null ? void 0 : settings.cancelCallback) {
  1027. cancelCallback = settings.cancelCallback;
  1028. } else {
  1029. cancelCallback = defaultCancelCallback;
  1030. }
  1031. if (!Array.isArray(options)) {
  1032. options = [__spreadProps(__spreadValues({}, options), { name: "default" })];
  1033. } else if (options.length === 1) {
  1034. options = options.reduce((o, option) => {
  1035. return [...o, Object.assign(option, { name: "default" })];
  1036. }, []);
  1037. }
  1038. options = options.reduce((o, option) => {
  1039. var _a2;
  1040. return [
  1041. ...o,
  1042. Object.assign(option, {
  1043. stdout: this instanceof TaskWrapper ? (_a2 = settings == null ? void 0 : settings.stdout) != null ? _a2 : this.stdout() : process.stdout,
  1044. onCancel: cancelCallback.bind(this, settings)
  1045. })
  1046. ];
  1047. }, []);
  1048. let enquirer;
  1049. if (settings == null ? void 0 : settings.enquirer) {
  1050. enquirer = settings.enquirer;
  1051. } else {
  1052. try {
  1053. enquirer = new (await Promise.resolve().then(() => __toESM(require("enquirer"), 1))).default();
  1054. } catch (e) {
  1055. this.task.prompt = new PromptError("Enquirer is a peer dependency that must be installed separately.");
  1056. throw new Error(e);
  1057. }
  1058. }
  1059. if (this instanceof TaskWrapper) {
  1060. enquirer.on("prompt", (prompt) => this.task.prompt = prompt);
  1061. enquirer.on("submit", () => this.task.prompt = void 0);
  1062. this.task.subscribe((event) => {
  1063. if (event.type === "STATE" /* STATE */ && event.data === "SKIPPED" /* SKIPPED */) {
  1064. if (this.task.prompt && !(this.task.prompt instanceof PromptError)) {
  1065. this.task.prompt.submit();
  1066. }
  1067. }
  1068. });
  1069. }
  1070. const response = await enquirer.prompt(options);
  1071. if (options.length === 1) {
  1072. return response.default;
  1073. } else {
  1074. return response;
  1075. }
  1076. }
  1077. function destroyPrompt(throwError = false) {
  1078. if (!this.task.prompt || this.task.prompt instanceof PromptError) {
  1079. return;
  1080. }
  1081. if (throwError) {
  1082. this.task.prompt.cancel();
  1083. } else {
  1084. this.task.prompt.submit();
  1085. }
  1086. }
  1087. // src/lib/task-wrapper.ts
  1088. var TaskWrapper = class {
  1089. constructor(task, errors, options) {
  1090. this.task = task;
  1091. this.errors = errors;
  1092. this.options = options;
  1093. }
  1094. get title() {
  1095. return this.task.title;
  1096. }
  1097. set title(data) {
  1098. this.task.title$ = data;
  1099. }
  1100. get output() {
  1101. return this.task.output;
  1102. }
  1103. set output(data) {
  1104. this.task.output$ = data;
  1105. }
  1106. newListr(task, options) {
  1107. let tasks;
  1108. if (typeof task === "function") {
  1109. tasks = task(this);
  1110. } else {
  1111. tasks = task;
  1112. }
  1113. return new Listr(tasks, options, this.task);
  1114. }
  1115. report(error, type) {
  1116. var _a2, _b, _c;
  1117. if (this.task.options.collectErrors !== false) {
  1118. this.errors.push(new ListrError(error, type, this.task));
  1119. }
  1120. this.task.message$ = { error: (_c = (_b = error.message) != null ? _b : (_a2 = this.task) == null ? void 0 : _a2.title) != null ? _c : "Task with no title." };
  1121. }
  1122. skip(message) {
  1123. var _a2, _b;
  1124. this.task.state$ = "SKIPPED" /* SKIPPED */;
  1125. if (message) {
  1126. this.task.message$ = { skip: (_b = message != null ? message : (_a2 = this.task) == null ? void 0 : _a2.title) != null ? _b : "Task with no title." };
  1127. }
  1128. }
  1129. isRetrying() {
  1130. return this.task.isRetrying() ? this.task.retry : { count: 0 };
  1131. }
  1132. async prompt(options) {
  1133. var _a2;
  1134. return createPrompt.bind(this)(options, __spreadValues({}, (_a2 = this.options) == null ? void 0 : _a2.injectWrapper));
  1135. }
  1136. cancelPrompt(throwError = false) {
  1137. return destroyPrompt.bind(this)(throwError);
  1138. }
  1139. stdout() {
  1140. return (0, import_through.default)((chunk) => {
  1141. chunk = chunk.toString();
  1142. chunk = chunk.replace(new RegExp(CLEAR_LINE_REGEX, "gmi"), "");
  1143. chunk = chunk.replace(new RegExp(BELL_REGEX, "gmi"), "");
  1144. if (chunk !== "") {
  1145. this.output = chunk;
  1146. }
  1147. });
  1148. }
  1149. run(ctx) {
  1150. return this.task.run(ctx, this);
  1151. }
  1152. };
  1153. // src/listr.ts
  1154. var Listr = class {
  1155. constructor(task, options, parentTask) {
  1156. this.task = task;
  1157. this.options = options;
  1158. this.parentTask = parentTask;
  1159. this.tasks = [];
  1160. this.err = [];
  1161. this.renderHook$ = new import_rxjs2.Subject();
  1162. this.path = [];
  1163. var _a2, _b, _c;
  1164. this.options = __spreadValues(__spreadValues({}, {
  1165. concurrent: false,
  1166. renderer: "default",
  1167. nonTTYRenderer: "verbose",
  1168. exitOnError: true,
  1169. exitAfterRollback: true,
  1170. collectErrors: "minimal",
  1171. registerSignalListeners: true
  1172. }), options);
  1173. if (this.options.concurrent === true) {
  1174. this.concurrency = Infinity;
  1175. } else if (typeof this.options.concurrent === "number") {
  1176. this.concurrency = this.options.concurrent;
  1177. } else {
  1178. this.concurrency = 1;
  1179. }
  1180. const renderer = getRenderer(this.options.renderer, this.options.nonTTYRenderer, (_a2 = this.options) == null ? void 0 : _a2.rendererFallback, (_b = this.options) == null ? void 0 : _b.rendererSilent);
  1181. this.rendererClass = renderer.renderer;
  1182. if (!renderer.nonTTY) {
  1183. this.rendererClassOptions = this.options.rendererOptions;
  1184. } else {
  1185. this.rendererClassOptions = this.options.nonTTYRendererOptions;
  1186. }
  1187. this.add(task != null ? task : []);
  1188. if (parentTask) {
  1189. this.path = [...parentTask.listr.path, parentTask.title];
  1190. }
  1191. if (this.options.registerSignalListeners) {
  1192. process.once("SIGINT", () => {
  1193. this.tasks.forEach(async (task2) => {
  1194. if (task2.isPending()) {
  1195. task2.state$ = "FAILED" /* FAILED */;
  1196. }
  1197. });
  1198. this.renderer.end(new Error("Interrupted."));
  1199. process.exit(127);
  1200. }).setMaxListeners(0);
  1201. }
  1202. if ((_c = this.options) == null ? void 0 : _c.disableColor) {
  1203. process.env.LISTR_DISABLE_COLOR = "1";
  1204. }
  1205. }
  1206. add(task) {
  1207. const tasks = Array.isArray(task) ? task : [task];
  1208. tasks.forEach((task2) => {
  1209. this.tasks.push(new Task(this, task2, this.options, __spreadValues(__spreadValues({}, this.rendererClassOptions), task2.options)));
  1210. });
  1211. }
  1212. async run(context) {
  1213. var _a2, _b, _c;
  1214. if (!this.renderer) {
  1215. this.renderer = new this.rendererClass(this.tasks, this.rendererClassOptions, this.renderHook$);
  1216. }
  1217. this.renderer.render();
  1218. this.ctx = (_c = (_b = (_a2 = this.options) == null ? void 0 : _a2.ctx) != null ? _b : context) != null ? _c : {};
  1219. await this.checkAll(this.ctx);
  1220. try {
  1221. await (0, import_p_map.default)(this.tasks, async (task) => {
  1222. await task.check(this.ctx);
  1223. return this.runTask(task, this.ctx, this.err);
  1224. }, { concurrency: this.concurrency });
  1225. this.renderer.end();
  1226. } catch (err) {
  1227. if (this.options.exitOnError !== false) {
  1228. this.renderer.end(err);
  1229. throw err;
  1230. }
  1231. }
  1232. return this.ctx;
  1233. }
  1234. checkAll(context) {
  1235. return Promise.all(this.tasks.map((task) => task.check(context)));
  1236. }
  1237. runTask(task, context, errors) {
  1238. if (!task.isEnabled()) {
  1239. return Promise.resolve();
  1240. }
  1241. return new TaskWrapper(task, errors, this.options).run(context);
  1242. }
  1243. };
  1244. // src/manager.ts
  1245. var Manager = class {
  1246. constructor(options) {
  1247. this.options = options;
  1248. this.err = [];
  1249. this.tasks = [];
  1250. }
  1251. set ctx(ctx) {
  1252. this.options.ctx = ctx;
  1253. }
  1254. add(tasks, options) {
  1255. options = __spreadValues(__spreadValues({}, this.options), options);
  1256. this.tasks = [...this.tasks, this.indent(tasks, options)];
  1257. }
  1258. async runAll(options) {
  1259. options = __spreadValues(__spreadValues({}, this.options), options);
  1260. const ctx = await this.run(this.tasks, options);
  1261. this.tasks = [];
  1262. return ctx;
  1263. }
  1264. newListr(tasks, options) {
  1265. return new Listr(tasks, options);
  1266. }
  1267. indent(tasks, options, taskOptions) {
  1268. options = __spreadValues(__spreadValues({}, this.options), options);
  1269. let newTask;
  1270. if (typeof tasks === "function") {
  1271. newTask = __spreadProps(__spreadValues({}, taskOptions), {
  1272. task: (ctx) => this.newListr(tasks(ctx), options)
  1273. });
  1274. } else {
  1275. newTask = __spreadProps(__spreadValues({}, taskOptions), {
  1276. task: () => this.newListr(tasks, options)
  1277. });
  1278. }
  1279. return newTask;
  1280. }
  1281. async run(tasks, options) {
  1282. options = __spreadValues(__spreadValues({}, this.options), options);
  1283. const task = this.newListr(tasks, options);
  1284. const ctx = await task.run();
  1285. this.err = task.err;
  1286. return ctx;
  1287. }
  1288. getRuntime(pipetime) {
  1289. return `${Math.round(Date.now() - pipetime) / 1e3}s`;
  1290. }
  1291. };
  1292. module.exports = __toCommonJS(src_exports);
  1293. // Annotate the CommonJS export names for ESM import in node:
  1294. 0 && (module.exports = {
  1295. Listr,
  1296. ListrError,
  1297. ListrErrorTypes,
  1298. ListrEventType,
  1299. ListrTaskState,
  1300. LogLevels,
  1301. Logger,
  1302. Manager,
  1303. PromptError,
  1304. createPrompt,
  1305. destroyPrompt,
  1306. figures
  1307. });