UNPKG

@vectorx/cloud-toolkit

Version:

VectorX Cloud Toolkit

69 lines (68 loc) 2.47 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.execWithLoading = exports.loadingFactory = void 0; const ora_1 = __importDefault(require("ora")); class Loading { constructor() { process.on(EVENT.PROCESS_EXIT, this.stop.bind(this)); process.on(EVENT.PROCESS_ERROR, this.stop.bind(this)); this.spinner = (0, ora_1.default)({ discardStdin: false, }); } set text(text) { this.spinner.text = text; } start(text) { this.spinner = this.spinner.start(text); } stop() { if (this.spinner) { this.spinner = this.spinner.stop(); } } succeed(text) { if (this.spinner) { this.spinner = this.spinner.succeed(text); } } fail(text) { if (this.spinner) { this.spinner = this.spinner.fail(text); } } } const loadingFactory = () => { return new Loading(); }; exports.loadingFactory = loadingFactory; const execWithLoading = (task_1, ...args_1) => __awaiter(void 0, [task_1, ...args_1], void 0, function* (task, options = {}) { const { startTip, successTip, failTip } = options; const loading = (0, exports.loadingFactory)(); const flush = (text) => { loading.text = text; }; try { loading.start(startTip || "执行中..."); const res = yield task(flush); successTip ? loading.succeed(successTip) : loading.stop(); return res; } catch (e) { failTip ? loading.fail(failTip) : loading.stop(); throw e; } }); exports.execWithLoading = execWithLoading;