@vortex.so/cli
Version:
CLI to interact with Vortex.
144 lines (141 loc) • 3.35 kB
JavaScript
import path from 'node:path';
import c from 'chalk';
import { createConsola } from 'consola';
import fig from 'figures';
import notifier from 'node-notifier';
import ora from 'ora';
import { detectNewline } from '../string/string.line.mjs';
const consola = createConsola({
formatOptions: {
date: false
}
});
class Log {
title;
constructor(title) {
this.title = title;
}
intro(message) {
this.ok(c.bold.green(message));
return this;
}
space() {
console.log("");
return this;
}
start(message, opts) {
const msg = this.message(message || "Starting...", {
icon: opts?.icon || c.gray(fig.tick),
title: opts?.title
});
consola.log(msg);
return this;
}
wait(message) {
const o = ora({
text: this.message(message || "Loading..."),
color: "yellow"
}).start();
return {
ora: o,
text: (message2, opts) => {
o.text = this.message(message2, opts);
},
persist: (message2, opts) => {
let msg = message2 || "";
const hasNewLine = !!detectNewline(msg);
if (hasNewLine) {
msg = `${c.dim("...")}
${msg}`;
}
o.stopAndPersist({
text: this.message(msg, opts),
symbol: c.dim(opts?.icon || fig.info)
});
o.start();
},
ok: (message2) => {
let msg = message2 || "";
const hasNewLine = !!detectNewline(msg);
if (hasNewLine) {
msg = `${c.dim("...")}
${msg}`;
}
o.succeed(this.message(msg || "Done!"));
},
fail: (message2) => {
let msg = message2 || "";
const hasNewLine = !!detectNewline(msg);
if (hasNewLine) {
msg = `${c.dim("...")}
${msg}`;
}
o.fail(this.message(msg || "Failed!"));
}
};
}
abort(message, opts) {
const msg = this.message(message || "Aborted.", {
icon: opts?.icon || c.yellow(fig.cross),
title: opts?.title
});
consola.log(msg);
}
ok(message, opts) {
const msg = this.message(message || "Done!", {
icon: opts?.icon || c.green(fig.tick),
title: opts?.title
});
consola.log(msg);
}
fail(message, opts) {
const msg = this.message(message || "Failed!", {
icon: opts?.icon || c.red(fig.cross),
title: opts?.title
});
consola.log(msg);
}
warn(message, opts) {
const msg = this.message(message, {
icon: opts?.icon || c.yellow(fig.warning),
title: opts?.title
});
consola.log(msg);
}
info(message, opts) {
const msg = this.message(message, {
icon: opts?.icon || c.blue(fig.info),
title: opts?.title
});
consola.log(msg);
}
notify(subtitle, message) {
const icon = path.join(__dirname, "vrt-icon.png");
notifier.notify({
title: `Vortex CLI`,
subtitle,
message,
icon,
contentImage: icon,
sound: "Ping",
wait: true
});
}
message(message, opts) {
const parts = [];
if (opts?.icon) {
parts.push(opts?.icon);
}
if (opts?.title !== null) {
parts.push(c.dim.bold(opts?.title || this.title));
}
if (opts?.title !== null && message) {
parts.push(c.dim(fig.pointerSmall));
}
if (message) {
parts.push(message);
}
return parts.join(" ");
}
}
export { Log };