@unimpaired/cli
v0.0.1
Published
This package is designed to be used as a CLI for node projects
Downloads
1
Readme
CAM CLI
This is the cam
cli. It's here because we are dumb and forget stuff.
Usage
- build the monorepo project with:
rushx build
- run
npm link
from this projects directory - type
cam
into your shell
Adding New Commands
After any updates, you must re run rush build
and you must navigate to this projects directory and run npm link
.
IMPORTANT NOTE: This is a rush repo, so using npm link
can be screwy. If you get errors, run rush unlink
and try again. Note that you will not be able to run npm link
inside of the "scripts." That will cause issues with Rush.
Basic Example
Step 1 - Create a function that does shit in any file in src/helpers/whatever-you-want.ts
import 'zx' // the $ syntax won't work without this
const printShitToConsole = async (shit: string) => {
await $`echo "${shit}"`
}
Step 2 - Add a function that returns a new command with Commander to src/commands.ts
import { Command } from 'commander'
import { printShitToConsole } from './helpers/printShitToConsole'
const doShitCommand = () =>
new Command('printShit') // "cam printShit" is the text you would type to run this command
.alias('ps') // you could use "cam ps" instead of "cam printShit"
.addHelpText('after', "You can't be helped") // if you ran "cam ps --help" then this text would be displayed after
.description('prints stupid shit to console') // If you ran "cam" without arguments, this text would show up next to the command
.action(() => {
// This is the stuff that happens when the command is run
printShitToConsole('shit')
})
Step 3 - Add command to object inside of src/cli.ts
src / cli.js
const UNIMPAIRED_CLI = new Command('cam')
// ... all commands are in this chain
.addCommand(doShitCommand()) // This is the line you will add to this file
How to Add A Select Field
We use the inquirer
package to do this
import inquirer from 'inquirer'
const askForDisability = () => {
const prompt = inquirer.prompt(
// First Question
[
name: 'firstQuestion',
message: 'Do you want to quit life?',
type: 'confirm',
choices: ['yes', 'no'],
]
// Second Question
[
name: 'secondQuestion',
message: 'Which character traits match you?',
type: 'checkbox', // they will select check the options they want
choices: ['idiot', 'freak', 'sexual deviant', 'mindless murderer']
]
).then((prompt) => {
// prompt is an object whose 'keys' equal the 'name' property in the prompt
// the value is equal to the 'choices' that the user decided
const firstQuestionData = prompt.firstQuestion
const secondQuestionData = prompt.secondQuestion
console.log('Go fuck yourself, ', secondQuestionData.join(' '))
})
}
NOTES
File Structure
/src
cli.ts // This is the file we run/export to use the command line
commands.ts // This file contains all the commands that we use
/helpers
// ... any code that is imported into the above files
// ... there really shouldn't be that much shit here. Keep it simple.
Important Packages
commander
: creates the CLIinquirer
: allows the cli to prompt a user for multiple optionszx
: allows us to run bash scripts in our TS code