dthree / vorpal

Node's framework for interactive CLIs
http://vorpal.js.org
MIT License
5.64k stars 280 forks source link

Access localStorage outside vorpal context #227

Closed florentsorel closed 7 years ago

florentsorel commented 7 years ago

Hi,

I would like to access the localStorage outside a Vorpal context to autocomplete a command by the array in my localStorage.

I add data in localStorage like this:


const cli = require('../../../cli'); // just an export: module.exports = require('vorpal')();

cli
  .command('author add <name>', 'Add an author to the preferences.')
  .action((args, cb) => {

    let authors = JSON.parse(cli.localStorage.getItem('authors'));

    if (authors === null) {
      authors = [];
    }

    authors.push(args.name);

    cli.localStorage.setItem('authors', JSON.stringify(authors));
    cb();
  });

The command who need to get access to the localStorage:

// Remove an author to the local storage
cli
  .command('author remove <name>', 'Remove an author to the preferences.')
  .autocomplete(cli.localStorage.getItem('authors')) // How to get the localStorage here?
  .action((args, cb) => {
    cli.log(args);
    cb();
  });

The error thrown is: cli.localStorage.getItem is not a function

florentsorel commented 7 years ago

The issue is not an issue anymore :).

autocomplete can take a function: .autocomplete(() => JSON.parse(cli.localStorage.getItem('authors')))