feat(): Create log script and add to pm-install

This commit is contained in:
Chris Daßler 2024-06-29 16:02:43 +02:00
parent b3b2505a97
commit 4f15c61019
3 changed files with 137 additions and 31 deletions

71
log Executable file
View File

@ -0,0 +1,71 @@
#!/usr/bin/env node
const This = require('./this');
const util = require('util');
class Log extends This {
constructor() {
super();
this.version = '0.0.1';
this.description = 'Helper script for building log messages';
super.init(); // initialize commander with overridden version and description
}
echo(str) {
console.log(str);
}
info(str) {
console.log(`[INFO] ${str}`);
}
warn(str) {
console.warn(`[WARN] ${this._echoInYellow(str)}`);
}
error(str) {
console.error(`[ERR] ${this._echoInRed(str)}`);
}
start() {
this.program.command('echo')
.argument('<message>')
.action(message => {
this.echo(message);
});
this.program.command('info')
.argument('<message>')
.action(message => {
this.info(message);
});
this.program.command('warn')
.argument('<message>')
.action(message => {
this.warn(message);
});
this.program.command('error')
.argument('<message>')
.action(message => {
this.error(message);
});
this.program.parse();
if (Object.keys(this.program.opts()).length || this.program.args.length) {
//// Debugging commander options and arguments
const opts = util.inspect(this.program.opts(), { depth: null, colors: true, showHidden: true });
const args = util.inspect(this.program.args, { depth: null, colors: true, showHidden: true });
//this.echo(`Options: ${opts}`);
//this.echo(`Remaining arguments: ${args}`);
} else {
this.program.outputHelp();
}
}
}
// main
const log = new Log();
log.start();

View File

@ -1,18 +1,14 @@
#!/usr/bin/env node
const This = require('./this');
const { Command } = require('commander');
const program = new Command();
const errorColor = (str) => {
// Add ANSI escape codes to display text in red.
return `\x1b[31m${str}\x1b[0m`;
};
const util = require('util');
class PmInstall extends This {
constructor() {
super();
this.version = '0.0.1';
this.description = 'Install package with fake package manager'
super.init(); // initialize commander with overridden version and description
}
start() {
@ -22,36 +18,21 @@ class PmInstall extends This {
// Properties of ExampleClass: [ 'propertyOne', 'propertyTwo', 'version' ] and Version: 1.1.0
// pmInstall.discovery();
program
.name(this.scriptName)
.version('0.0.1')
.description('Install package with fake package manager')
this.program
.usage('-n 3 32 -l x y z -- op')
.configureOutput({
// Visibly override write routines as example!
//writeOut: (str) => process.stdout.write(`[OUT] ${str}`),
writeErr: (str) => process.stdout.write(`[ERR] ${str}`),
// Highlight errors in color.
outputError: (str, write) => write(errorColor(str)),
});
program
.option('-n, --number <numbers...>', 'specify numbers')
.option('-l, --letter [letters...]', 'specify letters')
.parse();
// console.log('options array');
// console.log(program.options.map(o => o.flags));
this.program.parse();
// console.log('visible options');
// const helper = program.createHelp();
// console.log(helper.visibleOptions(program).map(o => o.flags));
if (Object.keys(program.opts()).length || program.args.length) {
console.log('Options: ', program.opts());
console.log('Remaining arguments: ', program.args);
if (Object.keys(this.program.opts()).length || this.program.args.length) {
// Debugging commander options and arguments
const opts = util.inspect(this.program.opts(), { depth: null, colors: true, showHidden: true });
const args = util.inspect(this.program.args, { depth: null, colors: true, showHidden: true });
this.execCmd(`./log echo 'Options: ${opts}'`);
this.execCmd(`./log echo 'Remaining arguments: ${args}'`);
} else {
program.outputHelp();
this.program.outputHelp();
}
}
}

54
this
View File

@ -1,13 +1,31 @@
#!/usr/bin/env node
const path = require('path');
const { Command } = require('commander');
const shell = require('shelljs');
class This {
constructor() {
this.version = '0.0.1'; // Default version, can be overridden in subclasses
this.description = 'This is the parent class all other scripts should extend.'
this.scriptName = path.parse(process.argv[1]).base;
}
init() {
// implement commander abilities
this.program = new Command()
.name(this.scriptName)
.version(this.version)
.description(this.description)
.configureOutput({
// Visibly override write routines as example!
//writeOut: (str) => process.stdout.write(`[OUT] ${str}`),
//writeErr: (str) => process.stdout.write(`[ERR] ${str}`),
// Highlight errors in color.
outputError: (str, write) => write(this._echoInRed(str)),
});
}
getClassName() {
return this.constructor.name;
}
@ -35,6 +53,42 @@ class This {
callback(this);
}
start() {
// Not implemented, so it needs to be overridden!
console.warn(this._echoInYellow(`Method ${this._getCurrentFunctionName()} not implemented!`));
}
execCmd(cmd) {
// Run external tool synchronously
if (shell.exec(cmd).code !== 0) {
shell.echo('[ERR] ' + this._echoInRed(`Command '${cmd}' failed`));
shell.exit(1);
}
}
_echoInRed(str) {
// Add ANSI escape codes to display text in red.
return `\x1b[31m${str}\x1b[0m`;
}
_echoInYellow(str) {
// Add ANSI escape codes to display text in yellow.
return `\x1b[33m${str}\x1b[0m`;
}
_getCurrentFunctionName() {
// Create an Error object (but don't throw it)
const err = new Error();
// Extract the current stack trace
Error.captureStackTrace(err, this._getCurrentFunctionName);
// Extract the function name from the stack trace
const callerName = err.stack.split("\n")[1].trim().split(" ")[1];
return callerName;
}
// Higher-order function to decorate other functions and provide logging
_logDecorator(fn) {
return function (...args) {