The Grind CLI is triggered via bin/cli
. Running bin/cli --help
will show you a list of available commands to run.
All commands support passing a --help
command to tell you more about the command, including options and arguments available.
$ bin/cli make:model --helpUsage:make:model [options] <name?>Arguments:name The name of the model.Options:--table[=TABLE] Name of the table to create the model for--help Display this help message--no-ansi Disable ANSI outputHelp:Create a model class
The fastest way to create a new command is by using the command generator via bin/cli make:command
.
You can invoke make:command
with a few different arguments:
bin/cli make:command MakeThingCommand
will createapp/Commands/MakeThingCommand.js
, but will not infer a command name.bin/cli make:command --command=make:thing
will also createapp/Commands/MakeThingCommand.js
and will set the command name for you.- You can also pass both a class name and a command name at the same time — though this isn’t advised.
All command names should be formatted as namespace:name
, in the above example make
is the namespace and thing
is the name.
Class names should follow the command name with a Command suffix, so make:thing
becomes MakeThingCommand
.
Once you’ve triggered make:command
, a class is generated for you that looks like this:
// Name of the commandname = 'make:thing'// Description of the command to show in helpdescription = 'Command description'// Arguments available for this commandarguments ='requiredArg' InputArgumentVALUE_REQUIRED 'This argument is required''optionalArg' InputArgumentVALUE_OPTIONAL 'This argument is optional' 'Default value'// Options for this commandoptions ='someOption' InputOptionVALUE_OPTIONAL 'This is an optional option' 'Default Value''quiet' InputOptionVALUE_NONE 'This is a flag option'{// Build something great!}
The name of the command is what you invoke via bin/cli
, so for MakeThingCommand we would invoke it by calling bin/cli make:thing
The description is what shows up in bin/cli --help
for your command. You should provide a short, concise description of what your command does.
Arguments are data passed into your command, in the order they’re declared.
Options are flags passed to your command via two leading dashes, if options expect a value, the value is passed by using an equals sign:
bin/cli make:thing --some-option
givessome-option
a value oftrue
bin/cli make:thing --some-option=grind
givessome-option
a value ofgrind
Options can be passed in before or after arguments and will not affect the order in which arguments are processed in.
The run
function is what is called when your command is invoked. CLI supports run
returning a promise for asynchronous support.
You can also add a ready
function to your command to perform startup tasks to be run when your command is invoked, but before run
is called. CLI supports ready
returning a promise for asynchronous support.
For instance, you may want to ready
to load data to be used during the execution of the command:
{return FS}
Now when run
is called, it will already have this.countries
populated.
You can retrieve arguments and options via the argument
and option
functions.
const value = this
name
— The name of the argument to get the value offallback
— Optional parameter to provide a default value for optional arguments. Passing a value here for a required argument will have no effect, since the your command won’t execute unless all required arguments are satisfied.
const value = this
name
— The name of the option to get the value offallback
— Optional parameter to provide a default value for an option not provided
You prompt the user for input via the ask
and confirm
functions.
this
The ask
function takes a single question
parameter, which is then outputted to the user, and returns a promise. The promise will resolve with the user’s answer to the question.
this
The confirm
function is a helpful wrapper around ask
for asking binary questions. The first parameter is question
, which is outputted to the user. The second parameter is defaultAnswer
, the defaultAnswer
answer is used when the user provides no response and just hits enter.
confirm
will accept accept the following user answers as true
(case-insensitive):
- true
- t
- 1
- yes
- y
All other input from the user will be evaluated as false
.
The Command class has the following methods available for writing output to the terminal:
// Outputs default text color// Outputs as blue text// Outputs as yellow text// Outputs as white text with a red background// Output as green text
These methods simply call the same method on app.cli.output
, which itself redirects to the global Log
class. For more information logging and providing your own logger, see the Logging documentation.
Grind’s providers ship with a bunch of default commands to help make development easier.
Assets includes the assets:publish
and assets:unpublish
commands to help precompile your assets during deployment. See the Assets documentation for more information.
As noted above, the CLI includes a built in make:command
command to generate commands for you.
You can generate controllers via make:controller
. You can also pass a --resource
flag to generate a full resource controller.
The database provider includes a number of commands for migrating and seeding the database:
make:migration
– Creates a migration filemake:seed
– Creates a database seed filemigrate:current-version
– Displays the current migration versionmigrate:latest
— Runs all outstanding migrationsmigrate:rollback
— Reverts the last set of migrations performeddb:seed
— Runs all seed files to populate your database
See the Migrations & Seeds documentation for more information.
ORM has a single make:model
command that generates model classes for you. See the ORM documentation for more information.