1.1.0 • Published 9 years ago
cli-ext
Licence
MIT
Version
1.1.0
Deps
3
Vulns
0
Weekly
0
cliExt
A package for cli input handling. This package simplifies the hassle to allow file input, piped input. It even supports editor inputs when no input is given.
Installation
Via npm
npm install cli-ext
Usage
import * as cliExt from 'cli-ext'
// get stdin input or if none is given open an editor
cliExt.input()
.then((contents) => console.log('stdin or editor input: ', contents))
// if the second process argument is a file it opens that file
// otherwise check stdin
// default to an editor if none of the above is set
cliExt.input(process.argv[2])
.then((contents) => console.log(contents))
// opens an editor to edit the file `config.json`
cliExt.edit('./config.json')
.then((json) => ...)
// opens an editor to edit the given text
cliExt.editContent('text to display in the editor')
.then((text) => ...)
// use a function to verify the contents of the input.
// Here we want to ensure that the content is at least 100 characters long
cliExt.input(process.argv[2], {verify: (content) => content.length > 100})
.then((text) => ...)
API
This package exports three methods:
| Method | Description |
|---|---|
.input(file, config) |
Try to read the file if none is given look for stdin input. If there is no stdin input open the editor specified by the environment variable $EDITOR (default nano). Returns a promise with the resulting text. The configuration config accepts the following keys.
|
edit(file, verify) |
Opens an editor to edit the given file. It is possible to specify a verify function that checks the user input and prompts the user to abort, or review when verify returns false. |
editContent(content, verify) |
Opens an editor with the given content. It is possible to specify a verify function that checks the user input and prompts the user to abort, or review when verify returns false. |