Make inquirer's answers persistence even be aborted halfway
npm install inquirer-store





Make inquirer's answers persistence even be aborted halfway

1. Get default answers by store, if null, turn to step 3.
2. Reset default field of each config.
3. Detect each answer's acceptance by calling prompt.ui.process.subscribe, then calls store.set / store.write for saving.
``bash`
npm install inquirer-storeor use yarn
yarn add inquirer-store
Make inquirer's answers persistence
#### Parameters
- prompt {Function}config
- same as inquireropts
- Objectopts.store
- {Store} Use which storeopts.deniesStoreKey
- {string}deniesStoreKey
When config contains and equals true, the prompt's value will not be saved. (optional, default 'deniesStore')opts.mode
- {'duplex'|'write'|'read'} duplexThe mode about dealing with store
- : Read and then write with storewrite
- : Just write data to storeread
- : Just read data from store (optional, default 'duplex')
#### Examples
`javascript
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 timeanswers.deny
// but excluding `
})
Base Class for storing to anywhere, don't use it directly
#### Parameters
- options {object}
#### Examples
`javascript`
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 {})
Extends Store
Store's implementation in file system
#### Parameters
- options {object}options.key
- {string|null}null
When , use store from storePath as data, otherwise use store[key] as data. (optional, default null)options.storePath
- {string|null] - File path for storing data (optional, default null)options.parse
- {string: string => object} Parse the text from file storePath (optional, default JSON.parse)options.stringify
- {data: object => string} Stringify data for saving in storePath (optional, default JSON.stringify)options.fs
- It's useful for mocking data by overriding existsSync / readFileSync / writeFileSync methods (optional, default require('fs'))
Fill config's default field
#### Parameters
- config {Arraystore {Store}
#### Examples
`javascript
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' }]
`
- Fork it!
- Create your new branch:
git checkout -b feature-new or git checkout -b fix-which-buggit commit -am 'feat: some description (close #123)'
- Start your magic work now
- Make sure npm test passes
- Commit your changes:
or git commit -am 'fix: some description (fix #123)'git push`
- Push to the branch:
- Submit a pull request :)
This library is written and maintained by imcuttle, moyuyc95@gmail.com.
MIT - imcuttle 🐟