fix(): Rename fn discovery(), add debugLevel and rework command parser

This commit is contained in:
Chris Daßler 2024-06-30 21:05:05 +02:00
parent d6336f4836
commit a09d060593
4 changed files with 64 additions and 34 deletions

32
log
View File

@ -60,20 +60,28 @@ class Log extends This {
/**
* @program()
*/
this.program.exitOverride();
if (this.debugLevel > 2) {
this.program.exitOverride();
try {
try {
this.program.parse();
} catch (err) {
// this.echo('\n');
// this.error(err);
process.exit(1);
}
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('\n');
this.echo(`Options: ${opts}`);
this.echo(`Remaining arguments: ${args}`);
}
} else {
// don't debug log if level is < 3 or the output will be hard to read
this.program.parse();
} catch (err) {
// custom processing...
}
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}`);
}
}
}

View File

@ -14,7 +14,7 @@ class MyUpdateSubCommand extends This {
const myUpdateSubCommand = new MyUpdateSubCommand();
// Output: Methods of ExampleClass: [ 'methodOne', 'methodTwo' ]
// Properties of ExampleClass: [ 'propertyOne', 'propertyTwo', 'version' ] and Version: 1.1.0
// myUpdateSubCommand.discovery();
// myUpdateSubCommand.discover();
if (!shell.which('git')) {
shell.echo('Sorry, this script requires git');

12
pm
View File

@ -51,8 +51,8 @@ class Pm extends This {
// Calls stand-alone excutable `pm-list` because of @command(<description>) in docstring
}
// override This.discovery()
discovery() {
// override This.discover()
discover() {
const methods = this.listMethods();
return methods;
}
@ -67,7 +67,7 @@ class Pm extends This {
pmMethods.includes('search') &&
pmMethods.includes('update') &&
pmMethods.includes('list') &&
pmMethods.includes('discovery') &&
pmMethods.includes('discover') &&
pmMethods.includes('start');
const testInternalMethods = !pmMethods.includes('_testListMethod');
@ -127,9 +127,9 @@ class Pm extends This {
// main
const pm = new Pm();
// // applies logDecorator to this.discovery() and binds it to the instance to maintain the correct context
// const logDecoratedDiscovery = pm._logDecorator(pm.discovery).bind(pm);
// logDecoratedDiscovery();
// // applies logDecorator to this.discover() and binds it to the instance to maintain the correct context
// const logDecoratedDiscover = pm._logDecorator(pm.discover).bind(pm);
// logDecoratedDiscover();
pm.start();
//pm.selfTest();

52
this
View File

@ -11,6 +11,7 @@ class This {
this.description = 'This is the parent class all other scripts should extend.';
this.scriptName = path.parse(process.argv[1]).base;
this.workingDir = path.parse(process.argv[1]).dir;
this.debugLevel = 0;
}
init() {
@ -49,10 +50,14 @@ class This {
return properties;
}
discovery() {
console.log(`My name is '${this.getClassName()}' and I have the version: ${this.version}`);
console.log(`My methods are:`, this.listMethods());
console.log(`My properties are:`, this.listProperties());
discover() {
const methods = util.inspect(this.listMethods(), { depth: null, colors: true, showHidden: true });
const properties = util.inspect(this.listProperties(), { depth: null, colors: true, showHidden: true });
this.execCmd(
`${this.workingDir}/log echo 'My name is "${this.getClassName()}" and I have the version: ${this.version}'`
);
this.execCmd(`${this.workingDir}/log echo 'My methods are: ${methods}'`);
this.execCmd(`${this.workingDir}/log echo 'My properties are: ${properties}'`);
}
// Method to create a context manager for this instance
@ -64,20 +69,37 @@ class This {
/**
* @program()
*/
this.program.exitOverride();
if (this.debugLevel > 0) {
this.program.exitOverride();
try {
try {
this.program.parse();
// show help even if there are no commands registered
if (this.program.commands.length == 0) {
this.program.outputHelp();
}
} catch (err) {
// this.execCmd(`${this.workingDir}/log echo '\n'`);
// this.execCmd(`${this.workingDir}/log error '${err}'`);
process.exit(1);
}
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(`${this.workingDir}/log echo '\n'`);
this.execCmd(`${this.workingDir}/log echo 'Options: ${opts}'`);
this.execCmd(`${this.workingDir}/log echo 'Remaining arguments: ${args}'`);
}
} else {
this.program.parse();
} catch (err) {
// custom processing...
}
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(`${this.workingDir}/log echo 'Options: ${opts}'`);
this.execCmd(`${this.workingDir}/log echo 'Remaining arguments: ${args}'`);
// show help even if there are no commands registered
if (this.program.commands.length == 0) {
this.program.outputHelp();
}
}
}