yargs-cli-cmd
Create a yargs cli command module using ESM syntax.
For template versions prior to 1.0.24, a new folder is created when generating a new instance of this template. This issue has been resolved in version 1.0.24. If you are using an incompatible version and wish to avoid a new folder being created, please refer to our guide on preventing new folder.
Usage
tps yargs-cli-cmd <cmd-name>
# or long build path
tps yargs-cli-cmd path/to/dir/<cmd-name>
| - <cmd-name>.js
Example
- default
- long build path
tps yargs-cli-cmd publish
Produces:
|- publish.js
export const command = 'publish';
export const aliases = [];
export const describe = 'This is my publish cli command';
export const builder = {
flag: {
alias: '',
describe: '...',
type: 'boolean',
},
};
export const handler = async (argv) => {
// code ...
};
tps yargs-cli-cmd cli/commands/publish
Produces:
| - cli/
| - commands/
| - publish.js
export const command = 'publish';
export const aliases = [];
export const describe = 'This is my publish cli command';
export const builder = {
flag: {
alias: '',
describe: '...',
type: 'boolean',
},
};
export const handler = async (argv) => {
// code ...
};
Installation
This templates is a part of Templates library. If you've already installed Templates, you'll have instant access to this template, and you can disregard this command.
npm i -g templates-mo
Options
name | description | option | alias | default | hidden |
---|---|---|---|---|---|
type | What type of file style do you want to use? (namedExport, defaultExport) | --type | namedExport | false | |
typescript | Would you like to use typescript | --typescript | false | false | |
extension | What type of extension do you want for your file? | --extension | -e, --ext, --extention | js | false |
description | Please add a description | --description | --desc | ... | false |
formatter | Type of formatter you would like to use to format the component (none, prettier, biome) | --formatter | --format | none | true |
Copy
If you like this template, but want to modify a few things use the copy command. It allows you to duplicate the template into your project and tailor it to your needs.
# if your not initialized run
tps init
# copy template
tps copy yargs-cli-cmd
Examples
How to use
If your using yargs modules you can use this template to generate command files. Lets say your projects cli looks something like this:
| - <cwd>/
| - cli/
| - index.js
| - commands/
| - some-command.js
| - index.js
If you want to add a new command called list
you can run the following
tps yargs-cli-cmd cli/commands/list
You will be prompted to answer the following questions:
? What type of file style do you want to use?
? Would you like to use typescript?
? What type of extension do you want for your file?
? Please add a description?
Based on your responses to the prompts, templates will generate a list.js
file
within your ./cli/commands/
directory.
| - <cwd>/
| - cli/
| - index.js
| - commands/
| - list.js
| - some-command.js
| - index.js
export const command = 'list';
export const aliases = [];
export const describe = 'This is my list cli command';
export const builder = {
flag: {
alias: '',
describe: '...',
type: 'boolean',
},
};
export const handler = async (argv) => {
// code ...
};
Set a loction for new instances
You can configure this template to always generate in the same directory by
adding a extendDest
option
to your .tps/tpsrc
file.
If you havent already initialize your repo:
tps init
lets say you have a directory structure like this
| - <cwd>/
| - cli/
| - index.js
| - commands/
| - some-command.js
| - index.js
If you wanted all your new commands to be generated in the cli/commands
directory then you can add the following to your tpsrc file.
{
"yargs-cli-cmd": {
"opts": {
"extendDest": "./cli/commands"
}
}
}
Now you can generate a new instance without passing in a path and it will be
created in the cli/commands
folder.
tps yargs-cli-cmd list
| - <cwd>/
| - cli/
| - index.js
| - commands/
| - list.js
| - some-command.js
| - index.js
Want to create sub commands? Add a build path and let the magic unfold
tps yargs-cli-cmd list_commands/template
| - <cwd>/
| - cli/
| - index.js
| - commands/
| - list_commands/
| - template.js
| - list.js
| - some-command.js
| - index.js
Dont create a new folder
Versions equal to or higher than 1.0.24
automatically support this behavior,
requiring no additional action within this section.
If you dont want your yarg cmd file in a new folder you can turn off the new
folder option in your .tps/tpsrc
If you havent already initialize your repo:
tps init
{
"yargs-cli-cmd": {
"opts": {
"newFolder": false
}
}
}
Now when you run the following it will produce the cmd file with a new folder
tps yargs-cli-cmd publish
Produces:
| - <cwd>
| - publish.js