@contextjs/system
Version:
ContextJS - System
109 lines (108 loc) • 4.75 kB
JavaScript
import readline from 'node:readline';
import { styleText } from 'node:util';
import typescript from 'typescript';
import { ObjectExtensions } from "../extensions/object.extensions.js";
import { ConsoleArgument } from "../models/console-argument.js";
export class Console {
static output = console.log;
static parseArguments(args) {
const parsedArguments = [];
if (args.length === 0)
return parsedArguments;
let currentArgument = null;
for (const argument of args) {
if (argument.startsWith("--") && argument.includes("=")) {
const [name, ...rest] = argument.split("=");
const value = rest.join("=");
const existing = parsedArguments.find(a => a.name === name);
if (existing)
existing.values.push(value);
else
parsedArguments.push(new ConsoleArgument(name, [value]));
currentArgument = null;
continue;
}
if (argument.startsWith("-")) {
if (!this.isValidArgument(argument)) {
this.writeLineError(`Invalid argument: "${argument}".`);
this.writeLineWarning("Arguments must start with a single or double dash and be followed by a name.");
process.exit(1);
}
const existing = parsedArguments.find(a => a.name === argument);
if (existing)
currentArgument = existing;
else {
currentArgument = new ConsoleArgument(argument, []);
parsedArguments.push(currentArgument);
}
}
else {
if (ObjectExtensions.isNullOrUndefined(currentArgument)) {
currentArgument = new ConsoleArgument(argument, []);
parsedArguments.push(currentArgument);
}
else
currentArgument.values.push(argument);
}
}
return parsedArguments;
}
static parseTypescriptArguments(consoleArguments, verbose = false) {
const tsArgs = [];
for (const consoleArgument of consoleArguments) {
if (!consoleArgument.name.startsWith("--"))
continue;
else if (consoleArgument.name === "--transformers" || consoleArgument.name === "-t") {
if (verbose)
this.writeLineInfo(`Skipping custom CLI flag: ${consoleArgument.name}`);
continue;
}
if (consoleArgument.values.length === 0)
tsArgs.push(consoleArgument.name);
else
tsArgs.push(consoleArgument.name, ...consoleArgument.values);
}
const parsed = typescript.parseCommandLine(tsArgs);
if (parsed.errors.length > 0) {
for (const error of parsed.errors)
this.writeLineError(typescript.flattenDiagnosticMessageText(error.messageText, "\n"));
}
if (verbose) {
this.writeLineInfo("Parsed TypeScript options:");
for (const [key, value] of Object.entries(parsed.options))
this.writeLine(` --${key}: ${JSON.stringify(value)}`);
}
return parsed.options;
}
static writeLineError(message, ...messages) {
this.writeLineFormatted({ format: 'red', text: message }, ...messages.map(m => ({ format: 'red', text: m })));
}
static writeLineWarning(message, ...messages) {
this.writeLineFormatted({ format: 'yellow', text: message }, ...messages.map(m => ({ format: 'yellow', text: m })));
}
static writeLineInfo(message, ...messages) {
this.writeLineFormatted({ format: 'blue', text: message }, ...messages.map(m => ({ format: 'blue', text: m })));
}
static writeLineSuccess(message, ...messages) {
this.writeLineFormatted({ format: 'green', text: message }, ...messages.map(m => ({ format: 'green', text: m })));
}
static writeLine(message, ...messages) {
this.output(message, ...messages);
}
static writeLineFormatted(message, ...messages) {
this.output(styleText(message.format, message.text), ...messages.map(m => styleText(m.format, m.text)));
}
static removeLastLine() {
readline.moveCursor(process.stdout, 0, -1);
readline.clearLine(process.stdout, 1);
}
static setOutput(writer) {
this.output = writer;
}
static resetOutput() {
this.output = console.log;
}
static isValidArgument(arg) {
return (arg[1] === '-' && arg.length > 2) || (arg[1] !== '-' && arg.length === 2);
}
}