feat(): Add higher-order fn to decorate other fns for extended logging
This commit is contained in:
parent
94cc6816d5
commit
e77fab3b84
47
pm
47
pm
|
@ -1,8 +1,7 @@
|
||||||
#!/usr/bin/env node
|
#!/usr/bin/env node
|
||||||
|
|
||||||
const This = require('./this');
|
const This = require('./this');
|
||||||
const {Command} = require('commander');
|
const { Command } = require('commander');
|
||||||
const program = new Command();
|
|
||||||
|
|
||||||
// Example of subcommands which are implemented as stand-alone executable files.
|
// Example of subcommands which are implemented as stand-alone executable files.
|
||||||
//
|
//
|
||||||
|
@ -14,6 +13,16 @@ class Pm extends This {
|
||||||
constructor() {
|
constructor() {
|
||||||
super();
|
super();
|
||||||
this.version = '0.0.1';
|
this.version = '0.0.1';
|
||||||
|
this.description = 'Fake package manager';
|
||||||
|
|
||||||
|
// implement commander abilities
|
||||||
|
this.program = new Command().name(this.scriptName).version(this.version).description(this.description);
|
||||||
|
}
|
||||||
|
|
||||||
|
// override This.discovery()
|
||||||
|
discovery() {
|
||||||
|
const methods = this.listMethods();
|
||||||
|
return methods;
|
||||||
}
|
}
|
||||||
|
|
||||||
start() {
|
start() {
|
||||||
|
@ -23,20 +32,24 @@ class Pm extends This {
|
||||||
// Properties of ExampleClass: [ 'propertyOne', 'propertyTwo', 'version' ] and Version: 1.1.0
|
// Properties of ExampleClass: [ 'propertyOne', 'propertyTwo', 'version' ] and Version: 1.1.0
|
||||||
// pm.discovery();
|
// pm.discovery();
|
||||||
|
|
||||||
program
|
// this.program
|
||||||
.name(this.scriptName)
|
// .name(this.scriptName)
|
||||||
.version('0.0.1')
|
// .version('0.0.1')
|
||||||
.description('Fake package manager')
|
// .description('Fake package manager')
|
||||||
.command('install [name]', 'install one or more packages')
|
// .command('install [name]', 'install one or more packages')
|
||||||
.alias('i')
|
// .alias('i')
|
||||||
.command('search [query]', 'search with optional query')
|
// .command('search [query]', 'search with optional query')
|
||||||
.alias('s')
|
// .alias('s')
|
||||||
.command('update', 'update installed packages', {
|
// .command('update', 'update installed packages', {
|
||||||
executableFile: 'myUpdateSubCommand',
|
// executableFile: 'myUpdateSubCommand',
|
||||||
})
|
// })
|
||||||
.command('list', 'list packages installed', {isDefault: false});
|
// .command('list', 'list packages installed', { isDefault: false });
|
||||||
|
|
||||||
program.parse();
|
//this.program.parse();
|
||||||
|
|
||||||
|
// applies logDecorator to this.discovery() and binds it to 'this' to maintain the correct context
|
||||||
|
const logDecoratedDiscovery = this._logDecorator(this.discovery).bind(this);
|
||||||
|
logDecoratedDiscovery();
|
||||||
|
|
||||||
// Try the following on macOS or Linux:
|
// Try the following on macOS or Linux:
|
||||||
// ./examples/pm
|
// ./examples/pm
|
||||||
|
@ -52,4 +65,8 @@ 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
|
||||||
|
// const logDecoratedDiscovery = pm._logDecorator(pm.discovery).bind(pm);
|
||||||
|
// logDecoratedDiscovery();
|
||||||
|
|
||||||
pm.start();
|
pm.start();
|
||||||
|
|
10
this
10
this
|
@ -34,6 +34,16 @@ class This {
|
||||||
withContext(callback) {
|
withContext(callback) {
|
||||||
callback(this);
|
callback(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Higher-order function to decorate other functions and provide logging
|
||||||
|
_logDecorator(fn) {
|
||||||
|
return function (...args) {
|
||||||
|
console.log(`Calling ${fn.name} with arguments:`, args);
|
||||||
|
const result = fn.apply(this, args); // Use apply to maintain context
|
||||||
|
console.log(`Result of ${fn.name}:`, result);
|
||||||
|
return result;
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = This;
|
module.exports = This;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user