0

I'm trying to create a yeoman generator to speed up some process of mine, but there are some issues doing so.

  • The input of the name is the value true, instead of the value provided
  • When not all values are provided I get a confirm, but after that nothing else happens (only when all arguments are provided)
  • the scaffoldFolders isn't creating the folders (EDIT: got that working)

does anyone know any (or even better: all) solutions to my problem?

here is the index.js I'm using:

'use strict';
var Generator = require('yeoman-generator');
var util = require('util')
var OptionOrPrompt = require('yeoman-option-or-prompt');
var mkdirp = require('mkdirp');
var _ = require('underscore.string');

var GlatGenerator = class extends Generator {

  constructor(args, opts) {
    // Calling the super constructor is important so our generator is correctly set up
    super(args, opts);
    this._optionOrPrompt = OptionOrPrompt;
    this.props = {};
  }

  prompting() {
    var done = this.async();
    // Instead of calling prompt, call _optionOrPrompt to allow parameters to be passed as command line or composeWith options. 
    this._optionOrPrompt([{
      type: 'input',
      name: 'name',
      message: 'Your component name',
      default: 'hoogwerker',
      store: true
    }, {
      type: 'confirm',
      name: 'model',
      message: 'Should we create a model for you?',
      default: true,
      store: true
    }, {
      type: 'confirm',
      name: 'service',
      message: 'Should we create a service for you?',
      default: true,
      store: true
    }], function (answers) {
      this.props.componentName = answers.name 
      this.props.createModel = answers.model
      this.props.createService = answers.service
      console.log("**********************");
      console.log("***" + (JSON.stringify(answers)));
      console.log("**********************");
      done();
    }.bind(this));
  }

  scaffoldFolders() {
    console.log('scaffoldFolders');
    var slugify = _.slugify(this.props.componentName);
    var classify = _.classify(this.props.componentName);
    var lowerName = _.decapitalize(_.classify(this.props.componentName));

    mkdirp("src/components/" + lowerName);
    mkdirp("src/components/" + lowerName + "/components");
    if (this.props.createModel) {
      mkdirp("src/components/" + lowerName + "/models");
    }
    if (this.props.createModel) {
      mkdirp("src/components/" + lowerName + "/services");
    }
  }

  copyMainFiles() {
    console.log('copyMainFiles');
    var slugify = _.slugify(this.props.componentName);
    var classify = _.classify(this.props.componentName);
    var lowerName = _.decapitalize(classify);
    var dash = _.dasherize(lowerName);

    var context = {
      component_name: slugify,
      component_name_camel: classify,
      component_name_lower: lowerName,
      component_name_dash: dash,
    };

    var base = "src/components/" + lowerName + "/";

    this.fs.copyTpl(
      this.templatePath('base-files/_component.html'),
      this.destinationPath(base + lowerName + ".component.html"),
      context
    );

    this.fs.copyTpl(
      this.templatePath('base-files/_component.scss'),
      this.destinationPath(base + lowerName + ".component.scss"),
      context
    );

    this.fs.copyTpl(
      this.templatePath('base-files/_component.ts'),
      this.destinationPath(base + lowerName + ".component.ts"),
      context
    );

    this.fs.copyTpl(
      this.templatePath('base-files/_module.ts'),
      this.destinationPath(base + lowerName + ".module.ts"),
      context
    );

    this.fs.copyTpl(
      this.templatePath('base-files/_routes.ts'),
      this.destinationPath(base + lowerName + ".routes.ts"),
      context
    );

    if (this.props.createModel) {
      this.fs.copyTpl(
        this.templatePath('model/_model.ts'),
        this.destinationPath(base + "/models/" + classify + ".ts"),
        context
      );
    }

    if (this.props.createService) {
      this.fs.copyTpl(
        this.templatePath('service/_service.ts'),
        this.destinationPath(base + "/services/" + lowerName + ".service.ts"),
        context
      );
    }
  }
};

module.exports = GlatGenerator;


// module.exports = base.extend({
//     initializing: () => {},
//     prompting:  () => {},
//     configuring:  () => {},
//     default:  () => {},
//     writing:  () => {},
//     conflicts:  () => {},
//     install:  () => {},
//     end:  () => {}
// });

and the command used:

yo glat:component --name="hoogwerker" --model --service

1 Answer 1

1

--name is being parsed as a Boolean. You need to specify the type as a string. this.option('name', {type: String})

For the second point, it's hard to help you without seeing _optionOrPrompt function. But it looks like a bug on your side, the function doesn't trigger the callback when all values are passed as options.

Sign up to request clarification or add additional context in comments.

2 Comments

the _optionOrPrompt is from a plugin(github.com/artefact-group/yeoman-option-or-prompt)
@Kiwi looks like this plugin has bugs. You should open a bug report.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.