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

10
log
View File

@ -60,21 +60,29 @@ class Log extends This {
/** /**
* @program() * @program()
*/ */
if (this.debugLevel > 2) {
this.program.exitOverride(); this.program.exitOverride();
try { try {
this.program.parse(); this.program.parse();
} catch (err) { } catch (err) {
// custom processing... // this.echo('\n');
// this.error(err);
process.exit(1);
} }
if (Object.keys(this.program.opts()).length || this.program.args.length) { if (Object.keys(this.program.opts()).length || this.program.args.length) {
//// Debugging commander options and arguments //// Debugging commander options and arguments
const opts = util.inspect(this.program.opts(), { depth: null, colors: true, showHidden: true }); 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 }); const args = util.inspect(this.program.args, { depth: null, colors: true, showHidden: true });
this.echo('\n');
this.echo(`Options: ${opts}`); this.echo(`Options: ${opts}`);
this.echo(`Remaining arguments: ${args}`); 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();
}
} }
} }

View File

@ -14,7 +14,7 @@ class MyUpdateSubCommand extends This {
const myUpdateSubCommand = new MyUpdateSubCommand(); const myUpdateSubCommand = new MyUpdateSubCommand();
// Output: Methods of ExampleClass: [ 'methodOne', 'methodTwo' ] // Output: Methods of ExampleClass: [ 'methodOne', 'methodTwo' ]
// Properties of ExampleClass: [ 'propertyOne', 'propertyTwo', 'version' ] and Version: 1.1.0 // Properties of ExampleClass: [ 'propertyOne', 'propertyTwo', 'version' ] and Version: 1.1.0
// myUpdateSubCommand.discovery(); // myUpdateSubCommand.discover();
if (!shell.which('git')) { if (!shell.which('git')) {
shell.echo('Sorry, this script requires 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 // Calls stand-alone excutable `pm-list` because of @command(<description>) in docstring
} }
// override This.discovery() // override This.discover()
discovery() { discover() {
const methods = this.listMethods(); const methods = this.listMethods();
return methods; return methods;
} }
@ -67,7 +67,7 @@ class Pm extends This {
pmMethods.includes('search') && pmMethods.includes('search') &&
pmMethods.includes('update') && pmMethods.includes('update') &&
pmMethods.includes('list') && pmMethods.includes('list') &&
pmMethods.includes('discovery') && pmMethods.includes('discover') &&
pmMethods.includes('start'); pmMethods.includes('start');
const testInternalMethods = !pmMethods.includes('_testListMethod'); const testInternalMethods = !pmMethods.includes('_testListMethod');
@ -127,9 +127,9 @@ class Pm extends This {
// main // main
const pm = new Pm(); const pm = new Pm();
// // applies logDecorator to this.discovery() and binds it to the instance to maintain the correct context // // applies logDecorator to this.discover() and binds it to the instance to maintain the correct context
// const logDecoratedDiscovery = pm._logDecorator(pm.discovery).bind(pm); // const logDecoratedDiscover = pm._logDecorator(pm.discover).bind(pm);
// logDecoratedDiscovery(); // logDecoratedDiscover();
pm.start(); pm.start();
//pm.selfTest(); //pm.selfTest();

32
this
View File

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