1.1.2 • Published 7 years ago
inquirer-store v1.1.2
inquirer-store
Make inquirer's answers persistence even be aborted halfway
How it works?
- Get default answers by
store
, ifnull
, turn to step 3. - Reset
default
field of each config. - Detect each answer's acceptance by calling
prompt.ui.process.subscribe
, then callsstore.set / store.write
for saving.
Installation
npm install inquirer-store
# or use yarn
yarn add inquirer-store
API
inquirerStore
Make inquirer's answers persistence
Parameters
prompt
{Function}config
same as inquireropts
Objectopts.store
{Store} Use which storeopts.deniesStoreKey
{string} When config containsdeniesStoreKey
and equalstrue
, the prompt's value will not be saved. (optional, default'deniesStore'
)opts.mode
{'duplex'|'write'|'read'} The mode about dealing withstore
duplex
: Read and then write withstore
write
: Just write data tostore
read
: Just read data fromstore
(optional, default'duplex'
)
Examples
const inquirerStore = require('inquirer-store')
const FileStore = require('inquirer-store/FileStore')
const inquirer = require('inquirer')
inquirerStore(
inquirer.prompt,
[
{ type: 'input', message: 'Hi...', name: 'name' },
{ type: 'input', message: 'Hi...', name: 'deny', deniesStore: true }
],
{
store: new FileStore({ storePath: '/path/to/where.json' })
}
).then(answers => {
// `answers` would be setting in `default` as default value at next time
// but excluding `answers.deny`
})
Store
Base Class for storing to anywhere, don't use it directly
Parameters
options
{object}
Examples
const Store = require('inquirer-store/Store')
// Write customized store class
class MyStore extends Store {
static defaultOptions = {
data: { name: 'imcuttle' }
}
// Note: It must be a sync operation
_read() {
const { data } = this.options
return data
}
// Note: It must be a sync operation
_write(data) {
// Save data for persistence here
}
}
options
extends from this.constructor.defaultOptions
and options
Type: object
data
Existing data in actually
Type: object
get
Get this.data[name]
Parameters
name
{string}
Returns any
set
Set this.data[name]
to be value
Parameters
name
{string}value
{any}
unset
Delete this.data[name]
Parameters
name
{string}
clear
Clear this.data
write
Write this.data
for persistence
Parameters
data
(optional, default{}
)
FileStore
Extends Store
Store's implementation in file system
Parameters
options
{object}options.key
{string|null} Whennull
, use store fromstorePath
as data, otherwise usestore[key]
as data. (optional, defaultnull
)options.storePath
{string|null] - File path for storing data (optional, defaultnull
)options.parse
{string: string => object} Parse the text from filestorePath
(optional, defaultJSON.parse
)options.stringify
{data: object => string} Stringify data for saving instorePath
(optional, defaultJSON.stringify
)options.fs
It's useful for mocking data by overridingexistsSync / readFileSync / writeFileSync
methods (optional, defaultrequire('fs')
)
fillConfigDefault
Fill config's default
field
Parameters
config
{Array | object}store
{Store}
Examples
const { fillConfigDefault } = require('inquirer-store')
fillConfigDefault(
[{ type: 'input', name: 'name', default: 'foo' }],
new FileStore({ storePath: '/path/to/where.json' })
)
// [{ type: 'input', name: 'name', default: 'the value that you has inputted in last time' }]
Contributing
- Fork it!
- Create your new branch:
git checkout -b feature-new
orgit checkout -b fix-which-bug
- Start your magic work now
- Make sure npm test passes
- Commit your changes:
git commit -am 'feat: some description (close #123)'
orgit commit -am 'fix: some description (fix #123)'
- Push to the branch:
git push
- Submit a pull request :)
Authors
This library is written and maintained by imcuttle, moyuyc95@gmail.com.
License
MIT - imcuttle 🐟