
Security News
vlt Launches "reproduce": A New Tool Challenging the Limits of Package Provenance
vlt's new "reproduce" tool verifies npm packages against their source code, outperforming traditional provenance adoption in the JavaScript ecosystem.
nestjs-console
Advanced tools
nestjs-console is a module that provide a cli. A ready to use service class for your modules that exposes methods to register commands and sub commands using the npm package commander
The nestjs framework is missing a cli to access the application context.
Common use case : Headless application, cront task, export data, etc...
nestjs-console provide a way to bind cli command and subcommands to providers's methods.
The console service works as a standalone process, like the classic entry point, and will initialize a NestApplicationContext (headless) instead a NestApplication. The console service will be accesible inside the container.
npm install nestjs-console
# or unig yarn
yarn add nestjs-console
Create a file at root next to your entry point named console.ts
Import your app module or any module you want to be loaded. Usually this is your main nestjs module.
You can create as many entry points as you want.
You can also extend the BootstrapConsole
class to suit your needs.
// console.ts - example of entrypoint
import { BootstrapConsole } from 'nestjs-console';
import { MyAppModule } from './my.application.module';
BootstrapConsole.init({ module: MyAppModule })
.then(({ app, boot }) => {
// do something with your app container if you need (app)
// boot the cli
boot(/*process.argv*/);
})
.catch(e => console.log('Error', e));
// module.ts - your module
import { Module } from '@nestjs/common';
import { ConsoleModule } from 'nestjs-console';
import { MyService } from './service';
@Module({
imports: [
ConsoleModule // import the console module
],
providers: [MyService]
exports: [MyService]
})
export class MyModule {}
You can now inject the ConsoleService inside any nestjs providers, controllers...
// service.ts - a nestjs provider
import { Injectable } from '@nestjs/common';
import { ConsoleService } from 'nestjs-console';
@Injectable()
export class MyService {
constructor(private readonly consoleService: ConsoleService) {
// get the root cli
const cli = this.consoleService.getCli();
// e.g create a single command (See [npm commander for more details])
cli.command('list <directory>')
.description('List content of a directory')
.action(this.myCommand.bind(this));
// e.g create a parent command container
const parentCommand = this.consoleService.subCommands(
cli,
'new',
'A command to create an item'
);
// e.g create sub command
parentCommand
.command('file <name>')
.description('Create a file')
.action((name: string) => {
console.log(`Creating a file named ${name} at path`);
process.exit(0);
});
// e.g why not an other one ?
parentCommand
.command('directory <name>')
.description('Create a directory')
.action((name: string) => {
console.log(`Creating a direcotry named ${name}`);
process.exit(0);
});
}
list(directory: string): void | Promise<void> {
// See Ora npm package for details about spinner
const spin = this.consoleService.createSpinner();
spin.start();
console.log(`Listing files at path named ${directory}`);
spin.stop();
process.exit(0);
}
}
Add scripts in your package.json (if you want to them)
{
"scripts": {
// from sources
"console:dev": "ts-node -r tsconfig-paths/register src/console.ts",
// from build (we suppose your app was built in the lib forlder)
"console": "node lib/console.js"
}
}
Call the cli (production)
# using node
node lib/console.js --help
# using npm
npm run console -- --help
# using yarn
yarn console --help
Call the cli from sources (dev)
# using ts-node
ts-node -r tsconfig-paths/register src/console.ts --help
# using npm
npm run console:dev -- --help
# using yarn
yarn console:dev --help
Usage: console [options] [command]
Options:
-h, --help output usage information
Commands:
list <directory> List content of a directory
new A command to create an item
You can create any number of custom ConsoleService and any nummber of entrypoints (BootstrapConsole).
The Commander provider can be injected using the decorators @InjectCommander()
.
The decorator can be imported from nestjs-console `import { InjectCommander } from 'nestjs-console';
Imagine we want to set the version of the cli for all commands, create a custom class then import it as a provider in your nest module.
import { Injectable } from '@nestjs/common';
import {
InjectCommander,
PatchedCommander,
ConsoleService
} from 'nestjs-console';
export class CustomConsole extends ConsoleService {
constructor(@InjectCommander() protected readonly cli: Command) {
super(cli);
}
/**
* My custom version
*/
init() {
// here we want to set a global version
this.cli.version('1.0.1', '-v, --version');
super.init();
}
}
FAQs
A NestJS module that provide a cli
We found that nestjs-console demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
vlt's new "reproduce" tool verifies npm packages against their source code, outperforming traditional provenance adoption in the JavaScript ecosystem.
Research
Security News
Socket researchers uncovered a malicious PyPI package exploiting Deezer’s API to enable coordinated music piracy through API abuse and C2 server control.
Research
The Socket Research Team discovered a malicious npm package, '@ton-wallet/create', stealing cryptocurrency wallet keys from developers and users in the TON ecosystem.