3.6.0 • Published 12 months ago

@arcaelas/command v3.6.0

Weekly downloads
-
License
ISC
Repository
github
Last release
12 months ago

Arcaelas Insiders Banner

Arcaelas Insiders Banner

Welcome to Arcaelas Insiders!

Hello, if this is your first time reading the Arcaelas Insiders documentation, let me tell you that you have found a good place to learn.

Our team and community are happy to write and make methods simple to implement and understand, but I think you already know that.

Let's start with the basic implementation steps.

> npm i --save @arcaelas/command
> yarn add --save @arcaelas/command

Implementation

// Class Import Statement
import Command from  '@arcaelas/command'

// commonjs
const { default: Command }  =  require('@arcaelas/command')

// tsconfig:
//	allowSyntheticDefaultImports: true
const Command  =  require('@arcaelas/command')

Command

Today a wide variety of applications have commands to simplify processes and be intuitive, for this reason the arcaelas development team has implemented a library dedicated to the construction of commands.

const serve  =  new Command({
	description: "Start server!"
});

Prompts

It's a simple thing, your command is already stored in the list of enabled commands for the environment. We could now assume that your command requires a list of parameters, including the port number where you want to run the server.

You can read documentation for inquirer in npm

const serve  =  Command({
	questions:{
		port:  {
			type: "number",
			default: 8080,
		}
	}
});

Your command now expects the given port number to be 8080, but in case the command runs like this:

serve --port 3000 Then the port property would be 3000 and not 8080.

Formats

We can also talk about formatting the expected data in a property, it could be the case that our application requires a specific type of data (string, object, number, array).

const serve  =  new Command({
	options:{
		id:{
			type: "number",
		},
		port:{
			type: "string",
			transformer(input, answers, flags){
				return Number( input )
			},
		}
	}
});

Whatever value is passed on the command line, the command handler will use the "Number" function to format the indicated data, this means that serve --port 3000 would result in the property port with the numeric value of 3000, while the command serve --port uno would return its value to NaN.

Another advantage of using Command is the implementation of arrays within its argument line, we could say that your command can only be executed with certain routes, you can indicate them like this serve --routes /home /dashboard /profile - -port 3000 Naturally, only the first value (/home) would be taken into account. To make it an array of values, we use the option with the Array type.

const serve  =  new Command({
	options:{
		port:{
			type:"number"
		},
		routes:{
			type: "checkbox",
		}
	}
})

Handler

Declaring properties and types is a very useful thing to do, but it's useless until you can read those values and make your command actually do what it's supposed to do.

To achieve this we use the action() property of the configurations.

const serve  =  new Command("serve",  {
	options:{
		port:{
			type: "number",
			default: 8080
		}
	},
	action(options: object, argv: string[]): any | Promise<any> {}
})

Now you can bring your command to life, from the action() function.

Execution

Everything seems to be very comfortable to implement and understand, but how do we call our command?

const serve  =  new Command({...});

// or using inherit method
serve.exec(["--port",  8080,  "--routes",  "/home",  "/dashboard",  "/profile /configs"]);

// or using object
serve.exec({
	port: 8080,
	routes:[
		"/home",
		"/dashboard",
		"/profile",
		"/configs",
	]
});

// You can pass command line arguments
serve.exec( process.argv  );

¿Want to discuss any of my open source projects, or something else?Send me a direct message on Twitter. If you already use these libraries and want to support us to continue development, you can sponsor us at Github Sponsors.

1.2.0

1 year ago

3.5.11

12 months ago

3.5.10

12 months ago

2.29.0

1 year ago

2.25.0

1 year ago

2.27.0

1 year ago

2.32.0

1 year ago

2.34.0

1 year ago

2.30.0

1 year ago

3.5.7

12 months ago

3.5.6

12 months ago

3.5.5

12 months ago

3.5.4

12 months ago

3.5.9

12 months ago

3.4.0

12 months ago

3.2.0

12 months ago

3.6.0

12 months ago

2.19.0

1 year ago

2.17.0

1 year ago

2.36.0

1 year ago

2.15.0

1 year ago

2.38.0

1 year ago

2.13.0

1 year ago

2.20.0

1 year ago

2.22.0

1 year ago

1.1.0

1 year ago

2.28.0

1 year ago

2.26.0

1 year ago

2.31.0

1 year ago

2.12.0

1 year ago

2.33.0

1 year ago

3.3.0

12 months ago

3.1.1

12 months ago

3.1.0

1 year ago

3.5.3

12 months ago

3.5.2

12 months ago

3.5.1

12 months ago

3.5.0

12 months ago

2.39.0

1 year ago

2.18.0

1 year ago

2.35.0

1 year ago

2.16.0

1 year ago

2.37.0

1 year ago

2.14.0

1 year ago

2.21.0

1 year ago

2.40.0

1 year ago

1.19.0

1 year ago

2.11.0

1 year ago

2.3.0

1 year ago

2.10.0

1 year ago

2.4.0

1 year ago

2.7.0

1 year ago

2.9.0

1 year ago

2.8.0

1 year ago

1.21.0

1 year ago

1.22.0

1 year ago

1.20.0

1 year ago

1.23.0

1 year ago

1.15.0

2 years ago

1.14.0

2 years ago

1.17.0

2 years ago

1.16.0

2 years ago

1.11.0

2 years ago

1.10.0

2 years ago

1.9.0

2 years ago

1.8.0

2 years ago

1.7.0

2 years ago

1.6.0

2 years ago

1.5.0

2 years ago

1.4.0

2 years ago

1.3.0

2 years ago