Make inquirer's answers persistence even be aborted halfway
- Get default answers by
store, ifnull, turn to step 3. - Reset
defaultfield of each config. - Detect each answer's acceptance by calling
prompt.ui.process.subscribe, then callsstore.set / store.writefor saving.
npm install inquirer-store
# or use yarn
yarn add inquirer-storeMake inquirer's answers persistence
prompt{Function}configsame as inquireroptsObjectopts.store{Store} Use which storeopts.deniesStoreKey{string} When config containsdeniesStoreKeyand equalstrue, the prompt's value will not be saved. (optional, default'deniesStore')opts.mode{'duplex'|'write'|'read'}The mode about dealing withstoreduplex: Read and then write withstorewrite: Just write data tostoreread: Just read data fromstore(optional, default'duplex')
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`
})Base Class for storing to anywhere, don't use it directly
options{object}
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
}
}extends from this.constructor.defaultOptions and options
Type: object
Existing data in actually
Type: object
Get this.data[name]
name{string}
Returns any
Set this.data[name] to be value
name{string}value{any}
Delete this.data[name]
name{string}
Clear this.data
Write this.data for persistence
data(optional, default{})
Extends Store
Store's implementation in file system
options{object}options.key{string|null} Whennull, use store fromstorePathas 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.fsIt's useful for mocking data by overridingexistsSync / readFileSync / writeFileSyncmethods (optional, defaultrequire('fs'))
Fill config's default field
config{Array | object}store{Store}- Fork it!
- Create your new branch:
git checkout -b feature-neworgit 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 :)
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' }]This library is written and maintained by imcuttle, moyuyc95@gmail.com.
MIT - imcuttle 🐟