Search code examples
javascriptyeomanscaffoldingyeoman-generator

how to get stored options from `this.prompt` inside yeoman context?


Basically, yeoman force you to ask everything you need from developer. Although, it’s a good thing, that you can store something and in future runs these things will be autocompleted for developer. The point is that I want to not ask developer if he already answered on that questions.

here is example of basic yeoman generator (name will be saved and autocompleted later):

var yeoman = require('yeoman-generator');

module.exports = yeoman.generators.Base.extend({
  init: function () {
    var cb = this.async();

    this.prompt([{
      name: 'name',
      message: 'your name:',
      store: true,
    }, {
      name: 'moduleName',
      message: 'module name:'
    }], function (props) {
      console.log(
        props.name,      // developer’s name
        props.moduleName // module’s name
      )
    }.bind(this));
  },
}
});

The question is how to get stored options from this.prompt inside yeoman context to do smth like this:

this.prompt([!this.name.stored && {
  name: 'name', // so after first run this will never be asked again
  message: 'your name:',
  store: true,
}, {
  name: 'moduleName',
  message: 'module name:'
}], function (props) {
  console.log(
    props.name,      // developer’s name
    props.moduleName // module’s name
  )
}.bind(this));

Solution

  • There's no public way to access the stored previous prompt answers.

    If you want to cache some data and access it later, then use the storage functionality (this.config)

    FWIW, the prompt cache is stored into the private this._globalConfig. I'm adding this detail for completeness, you probably shouldn't use it.