@listr2/prompt-adapter-enquirer
Version:
Listr2 prompt adapter for enquirer.
128 lines (125 loc) • 4.16 kB
JavaScript
;
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 __name = (target, value) => __defProp(target, "name", { value, configurable: true });
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/index.ts
var src_exports = {};
__export(src_exports, {
ListrEnquirerPromptAdapter: () => ListrEnquirerPromptAdapter
});
module.exports = __toCommonJS(src_exports);
// src/prompt.ts
var import_listr2 = require("listr2");
var ListrEnquirerPromptAdapter = class extends import_listr2.ListrPromptAdapter {
static {
__name(this, "ListrEnquirerPromptAdapter");
}
error;
prompt;
/**
* Get the current running instance of `enquirer`.
*/
get instance() {
return this.prompt;
}
/**
* Create a new prompt with `enquirer`.
*
* `enquirer` is a peer dependency that must be installed seperately.
*/
async run(options, settings) {
if (!Array.isArray(options)) {
options = [{ ...options, name: "default" }];
} else if (options.length === 1) {
options = options.map((option) => {
return { ...option, name: "default" };
});
}
options = options.map((option) => {
return {
onCancel: /* @__PURE__ */ __name(() => {
this.error = new import_listr2.PromptError("Cancelled prompt.");
return true;
}, "onCancel"),
...option,
stdout: settings?.stdout ?? this.wrapper.stdout(import_listr2.ListrTaskEventType.PROMPT)
};
});
let enquirer;
if (settings?.enquirer) {
enquirer = settings.enquirer;
} else {
try {
enquirer = await import("enquirer").then((imported) => imported.default ? new imported.default() : new imported());
} catch {
this.reportFailed();
throw new import_listr2.PromptError("Enquirer is a optional peer dependency that must be installed separately.");
}
}
this.reportStarted();
this.task.on(import_listr2.ListrTaskEventType.STATE, (event) => {
if (event === import_listr2.ListrTaskState.SKIPPED && this.prompt && !this.error) {
this.cancel({ throw: false });
}
});
let response;
try {
response = await enquirer.once("prompt", (prompt) => this.prompt = prompt).prompt(options);
} catch (e) {
this.reportFailed();
if (this.error) {
throw this.error;
}
throw e;
}
this.reportCompleted();
if (options.length === 1) {
return response.default;
} else {
return response;
}
}
/**
* Cancel the ongoing prompt.
*/
cancel(options) {
if (!this.prompt || this.error) {
return;
}
if (options?.throw) {
this.prompt.cancel();
} else {
this.prompt.submit();
}
this.reportFailed();
}
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
ListrEnquirerPromptAdapter
});