1.1.2 • Published 7 years ago

inquirer-store v1.1.2

Weekly downloads
1
License
MIT
Repository
github
Last release
7 years ago

inquirer-store

Build status Test coverage NPM version NPM Downloads Prettier Conventional Commits

Make inquirer's answers persistence even be aborted halfway

npm.io

How it works?

  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.

Installation

npm install inquirer-store
# or use yarn
yarn add inquirer-store

API

inquirerStore

index.js:37-62

Make inquirer's answers persistence

Parameters

  • prompt {Function}
  • config same as inquirer
  • opts Object
    • opts.store {Store} Use which store
    • opts.deniesStoreKey {string} When config contains deniesStoreKey and equals true, the prompt's value will not be saved. (optional, default 'deniesStore')
    • opts.mode {'duplex'|'write'|'read'} The mode about dealing with store
      • duplex: Read and then write with store
      • write: Just write data to store
      • read: Just read data from store (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

Store.js:30-98

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

Store.js:37-37

extends from this.constructor.defaultOptions and options

Type: object

data

Store.js:43-43

Existing data in actually

Type: object

get

Store.js:57-59

Get this.data[name]

Parameters
  • name {string}

Returns any

set

Store.js:67-69

Set this.data[name] to be value

Parameters
  • name {string}
  • value {any}

unset

Store.js:76-78

Delete this.data[name]

Parameters
  • name {string}

clear

Store.js:84-86

Clear this.data

write

Store.js:95-97

Write this.data for persistence

Parameters
  • data (optional, default {})

FileStore

FileStore.js:24-52

Extends Store

Store's implementation in file system

Parameters

  • options {object}
    • options.key {string|null} When null, 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'))

fillConfigDefault

index.js:79-89

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' }]

Returns Array<object>

Contributing

  • Fork it!
  • Create your new branch:
    git checkout -b feature-new or git 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)' or git 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 🐟