Yeoman发电机不按预期工作

时间:2017-01-18 14:36:10

标签: yeoman yeoman-generator

我正在尝试创建一个自动生成器,以加快我的某些过程, 但是有一些问题在这样做。

  • 名称的输入是值true,而不是提供的值
  • 如果没有提供所有值,我会得到确认,但之后没有其他事情发生(仅当提供所有参数时)
  • scaffoldFolders没有创建文件夹 (编辑:有效)

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

这是我正在使用的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

1 个答案:

答案 0 :(得分:1)

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

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