WIP: Merge 'dev' into 'main' #1
71
log
Executable file
71
log
Executable 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();
|
43
pm-install
43
pm-install
|
@ -1,18 +1,14 @@
|
||||||
#!/usr/bin/env node
|
#!/usr/bin/env node
|
||||||
|
|
||||||
const This = require('./this');
|
const This = require('./this');
|
||||||
const { Command } = require('commander');
|
const util = require('util');
|
||||||
const program = new Command();
|
|
||||||
|
|
||||||
const errorColor = (str) => {
|
|
||||||
// Add ANSI escape codes to display text in red.
|
|
||||||
return `\x1b[31m${str}\x1b[0m`;
|
|
||||||
};
|
|
||||||
|
|
||||||
class PmInstall extends This {
|
class PmInstall extends This {
|
||||||
constructor() {
|
constructor() {
|
||||||
super();
|
super();
|
||||||
this.version = '0.0.1';
|
this.version = '0.0.1';
|
||||||
|
this.description = 'Install package with fake package manager'
|
||||||
|
super.init(); // initialize commander with overridden version and description
|
||||||
}
|
}
|
||||||
|
|
||||||
start() {
|
start() {
|
||||||
|
@ -22,36 +18,21 @@ class PmInstall extends This {
|
||||||
// Properties of ExampleClass: [ 'propertyOne', 'propertyTwo', 'version' ] and Version: 1.1.0
|
// Properties of ExampleClass: [ 'propertyOne', 'propertyTwo', 'version' ] and Version: 1.1.0
|
||||||
// pmInstall.discovery();
|
// pmInstall.discovery();
|
||||||
|
|
||||||
program
|
this.program
|
||||||
.name(this.scriptName)
|
|
||||||
.version('0.0.1')
|
|
||||||
.description('Install package with fake package manager')
|
|
||||||
.usage('-n 3 32 -l x y z -- op')
|
.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('-n, --number <numbers...>', 'specify numbers')
|
||||||
.option('-l, --letter [letters...]', 'specify letters')
|
.option('-l, --letter [letters...]', 'specify letters')
|
||||||
.parse();
|
|
||||||
|
|
||||||
// console.log('options array');
|
this.program.parse();
|
||||||
// console.log(program.options.map(o => o.flags));
|
|
||||||
|
|
||||||
// console.log('visible options');
|
if (Object.keys(this.program.opts()).length || this.program.args.length) {
|
||||||
// const helper = program.createHelp();
|
// Debugging commander options and arguments
|
||||||
// console.log(helper.visibleOptions(program).map(o => o.flags));
|
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 });
|
||||||
if (Object.keys(program.opts()).length || program.args.length) {
|
this.execCmd(`./log echo 'Options: ${opts}'`);
|
||||||
console.log('Options: ', program.opts());
|
this.execCmd(`./log echo 'Remaining arguments: ${args}'`);
|
||||||
console.log('Remaining arguments: ', program.args);
|
|
||||||
} else {
|
} else {
|
||||||
program.outputHelp();
|
this.program.outputHelp();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
54
this
54
this
|
@ -1,13 +1,31 @@
|
||||||
#!/usr/bin/env node
|
#!/usr/bin/env node
|
||||||
|
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
|
const { Command } = require('commander');
|
||||||
|
const shell = require('shelljs');
|
||||||
|
|
||||||
class This {
|
class This {
|
||||||
constructor() {
|
constructor() {
|
||||||
this.version = '0.0.1'; // Default version, can be overridden in subclasses
|
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;
|
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() {
|
getClassName() {
|
||||||
return this.constructor.name;
|
return this.constructor.name;
|
||||||
}
|
}
|
||||||
|
@ -35,6 +53,42 @@ class This {
|
||||||
callback(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
|
// Higher-order function to decorate other functions and provide logging
|
||||||
_logDecorator(fn) {
|
_logDecorator(fn) {
|
||||||
return function (...args) {
|
return function (...args) {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user