← Back to Posts

A new way of creating console applications in Imba

11/22/2021

A new way of creating console applications in Imba

Earlier this year I built a custom API framework for Imba, The Formidable Framework. I implemented a lot of cool features, and one of those features is a CLI tool named Craftsman. This CLI tool is built ontop @oclif/dev-cli.

@formidablejs/console

I will be moving away from @oclif/dev-cli. @formidablejs/console will give me more freedom and allow me to experiment with new features.

Craftsman's new core is a Zero-Dependency module written entirely in Imba that draws its inspiration from Laravel's Artisan.

Here's what a Command looks like:

class DownCommand < Command

	# The name and signature of the console command.
	#
	# @type {String}

	get signature
		'down {?--allow : IP Address to add to allow-list}'

    # The console command description.
    #
    # @type {String}

    get description
        'Put the application into maintenance / demo mode'

	# Command props.
	#
	# @type {Object}

	get props
		{
			allow: Prop.string!.multiple!
		}

	# Execute the console command.
	#
	# @returns {mixed}

	def handle
		self.table self.option('allow')

Results:

craftsman-down

Default Command

You can register a default command, this command will typically run before all the other registered commands. By default, @formidablejs/console uses its registered default command to display the help interface for all commands, you can however replace the default command with your own by adding a default getter in your command:

class DefaultCommand < Command

	# Register as a default command.
	#
	# @type {Boolean}

	get default
		true

	# The name and signature of the console command.
	#
	# @type {String}

	get signature
		'default {?--help} {?--version}'

	# Command props.
	#
	# @type {Object}

	get props
		{
			help: Prop.boolean!.alias('h').default(false)
			version: Prop.boolean!.alias('v').default(false)
		}

	# A list of help options.
	#
	# @type {Object[]}

	get helpOptions
		[
			{
				alias: 'h'
				name: 'help'
				description: 'Display help for the given command. When no command is given display help for the <fg:green>list</fg:green> command'
			}
			{
				alias: 'v'
				name: 'version',
				description: 'Display this application version'
			}
		]

	...

Calling the cli without any arguments, invokes the default command:

default-command

Closing comments

While this was initially created for Formidable, I'm going to ship it as a standalone console application creator.

There's still a lot of testing I still need to do before I can ship this. If you would like to help, please get in touch via twitter.