Yeoman 生成器未按预期工作

Yeoman generator not working as intended

我正在尝试创建一个 yeoman 生成器来加速我的一些过程, 但是这样做有一些问题。

有谁知道我的问题的任何(甚至更好:所有)解决方案?

这是我正在使用的index.js

'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:  () => {}
// });

和使用的命令:

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

--name 被解析为布尔值。您需要将类型指定为字符串。 this.option('name', {type: String})

对于第二点,没有看到_optionOrPrompt函数很难帮到你。但它看起来像你这边的一个错误,当所有值都作为选项传递时,该函数不会触发回调。