Create console (cli) application in NestJS framework context.
npm i @fastpanel/console
npm i @fastpanel/console --registry https://npmjs.desionlab.net
- Add command providers.
/**
* pizza.commands.ts
*/
import { Injectable } from '@nestjs/common';
import { Console, Command, Argument, Option } from '@fastpanel/console';
@Console({ name: 'pizza' })
@Injectable()
export class PizzaCommands {
@Command({ name: 'order', description: 'Order a pizza' })
@Argument({ synopsis: '<type>', description: 'Type of pizza' })
@Option({ synopsis: '-e, --extra-ingredients <ingredients>', description: 'Extra ingredients' })
public async orderAction({ logger, args, options }) {
logger.info('Order received: %s', args.type);
if (options.extraIngredients) {
logger.info('Extra: %s', options.extraIngredients);
}
}
@Command({ name: 'cancel', description: 'Cancel an order' })
@Argument({ synopsis: '<order-id>', description: 'Order id' })
public async cancelAction({ logger, args }) {
logger.info('Order canceled: %s', args.orderId);
}
}
- Add console module and command providers to app module.
/**
* app.module.ts
*/
import { Module } from '@nestjs/common';
import { ConsoleModule } from '@fastpanel/console';
import { PizzaCommands } from './pizza.commands.ts';
@Module({
imports: [
ConsoleModule.forRoot({
name: 'my-test-app',
version: '1.0.0',
description: 'My test app'
})
],
providers: [PizzaCommands]
})
export class AppModule {}
- Create application context.
/**
* cli.ts
*/
import { NestFactory } from '@nestjs/core';
import { ConsoleService } from '@fastpanel/console';
import { AppModule } from './app.module';
async function bootstrap() {
/* Create app instant. */
const app = await NestFactory.createApplicationContext(AppModule, {
logger: false
});
/* Get console service. */
const consoleService = app.get<ConsoleService>(ConsoleService);
/* Add default help command. */
consoleService.getHandler.action(async ({ program }) => {
await program.exec(['help']);
});
/* Run cli handler. */
await consoleService.run();
/* End app. */
await app.close();
}
/* Run app. */
bootstrap();
- Add scripts to package.json
{
"scripts": {
"console": "node dist/cli.js",
"console:dev": "ts-node -r tsconfig-paths/register src/cli.ts"
}
}
- Use in terminal.
# Run in development mode
npm run console:dev -- help
npm run console:dev -- command-name
# Run in prod or build mode
npm run console -- help
npm run console -- command-name