Introduction

OstroJS comes with a command line interface called Assistant. Assistant is a script that lives at the root of your application and offers you with a variety of useful commands to help you develop your project. You may use the list command to see a list of all possible Assistant commands:

node assistant -h

Every command also comes with a "help" page that lists and discusses the command's parameters and options. To see a help screen, type help before the command's name:

node assistant migrate -h

Writing Commands

You can create your own custom commands in addition to the ones supplied by Assistant. Typically, commands are saved in the app/console/commands directory.

Generating Commands

You may use the make:command Assistant command to create a new command. In the app/console/commands directory, this command will create a new command class. If this directory does not exist in your application, don't worry; it will be created when you execute the make:command Assistant command for the first time:

node assistant make:command sendEmails

Command Structure

After you've created your command, you'll need to set suitable values for the class's signature and description attributes. When your command is displayed on the list screen, these attributes will be utilised. You can also use the signature attribute to describe the input expectations for your command. When your command is performed, the handle method will be invoked. This method is where you may put your command logic.

const Command = require('@ostro/console/command')
const User  = require('~/app/models/user')
class SendEmails extends Command
{
    
    $signature = 'mail:send';

    
    $description = 'Send a marketing email to a user';


    $arguments = [
         this.createArgument('[user]').required()
    ];
    
    $options = [
         this.createOption('--force').required()
    ];
    
    async handle(){
        let user = await User.find(this.argument('user'))
        if(this.option('force')){
        	return this.info(`Forced mail sent to user ${user.email}`)
        }
        this.info(`Mail sent to user ${user.email}`)
    }
}

module.exports = SendEmails