The project assembles the functionality of two libraries: commander and inversifyjs. It represents the commander functionality in inversify way.
You can install inversify-commander-utils
using npm:
npm install inversify inversify-commander-utils reflect-metadata --save
The inversify-commander-utils
type definitions are included in the npm module and require TypeScript 2.0.
Please refer to the InversifyJS documentation to learn more about the installation process.
import 'reflect-metadata';
import {group, action, build, registerGroups} from 'inversify-commander-utils';
import {Container, inject, injectable} from 'inversify';
import program from 'commander';
@injectable()
class TodoContainer {
public printPaper() {
return 'Paper';
}
}
The group is represented as a container where we inject another container to. The injected container is available in actions of the group.
@group('printer')
class TestGroup {
@inject(TodoContainer)
public todoContainer!: TodoContainer;
@action(
'A <parameter>',
[
{ pattern: '-c, --count <mode>', description: 'Number of prints.' }
]
)
public testA(parameter: string, command: any) {
console.log(this.todoContainer.printPaper());
console.log(parameter, command.count);
}
}
const container = new Container();
container.bind(TodoContainer).to(TodoContainer);
registerGroups(container);
build(program, container);
commander
.parse(process.argv);
node ./src/cli.js printer:A A4 -c 3
InversifyJS is a very interesting library. We are developing a web application using the approach. At first we used InversifyJS utility alone. Later we checked a module inversify-express-utils, studied it better added some functionality. And we realized that this is a good example to wrap any module for our needs. This is used in our projects and it is being upgraded. Maybe another modules will also be wrapped.