@bravejs/console v1.0.3
BraveJS Console Component
This is a simple Node.js console framework, a part of BraveJS framework
Installation
npm i @bravejs/console
Usage
Let's say that you want to create a command like this make:model User
import { Kernel, Command as BaseCommand } from '@bravejs/console'
class ModelCommand extends BaseCommand {
/**
* Set the command name that the user will call it in the console.
*/
public static commandName = 'make:model'
/**
* A description for the command.
*/
public static description = 'Blah Blah'
public async handle () {
console.log('Hello World !!')
}
}
This is the simplest thing you can do with this component.
Now you want maybe to add arguments for the command, it is simple. Let's back to our ModelCommand
example.
This component provides you a decorators to set arguments, and flags as you want
import { Kernel, Command as BaseCommand } from '@bravejs/console'
import { args, flags } from '@bravejs/console'
class ModelCommand extends BaseCommand {
/**
* Set the command name that the user will call it in the console.
*/
public static commandName = 'make:model'
/**
* A description for the command.
*/
public static description = 'Blah Blah'
/**
* Command arguments
*/
@agrs.string({ description: 'Model name', required: true , type: string })
public name: string
public async handle () {
if (this.name === 'Blah') {
throw new Error('Blah Blah')
}
}
}
As you can see this is how you set an argument for your command.
Adding flags is as same as adding arguments, Let's head back to our ModelCommand
example to set some flags.
import { Kernel, Command as BaseCommand } from '@bravejs/console'
import { args, flags } from '@bravejs/console'
class ModelCommand extends BaseCommand {
/**
* Set the command name that the user will call it in the console.
*/
public static commandName = 'make:model'
/**
* A description for the command.
*/
public static description = 'Blah Blah'
/**
* Command arguments.
*/
@agrs.string({ description: 'Model name', required: true , type: string })
public name: string
/**
* Command flags.
*/
@flags.string({ description: 'Any description you want', alias: 'f' })
public flag: string
public async handle () {
if (this.name === 'Blah') {
throw new Error('Blah Blah')
}
console.log(this.flag)
}
}
I think i forgot to tell you that @args
, and @flags
has types
@args
Type | Description |
---|---|
string | A simple string argumnt |
spread | Same as array |
@flags
Type | Description |
---|---|
string | A simple string argumnt |
number | A simple numeric argumnt |
array | It's just an array value (string[]) |
numArray | number[] |
boolean | It's just a boolean value (true, or false) |
I thing we are done here so let's build our application, Yaaay!!
import Container from 'typescript-container'
import { Application, Kernel } from '@bravejs/console'
const application = new Application(__dirname, new Container(), {}, {})
const kernel = new Kernel(app)
// Let's register our commands.
kernel.register([ModelCommand])
// Listen for commands.
kernel.handle(process.argv.splice(2)).catch((error) => {
// Do something.
})
You thing the application is done? you are right, but what if you want to set a global flag?
kernel.flag(
'env',
(value) => {
process.env.NODE_ENV = value
},
{ type: 'string' }
)
Pre-made command
I created a command called help
, this command will show you all commands on the application with it's description, and if you want to use help
to show details about another command you can simply do help [commandName]
as [coomandName]
is the command you want to show details about.