javascript - 生成器在 async() 调用后退出

标签 javascript yeoman yeoman-generator

我开始学习 Yeoman 生成器,并决定按照一些教程尝试自己做一个。这是非常基本的,但可以满足要求。我的问题是,在我要求用户输入名称后,它只是退出/结束而不写入任何文件,没有错误就退出。我已经隔离了可能的罪魁祸首,它归结为 async() 方法,因为如果我将其注释掉,生成器就会运行良好。我安装了其他生成器,它们也使用相同类型的提示+异步结构,我对这些没有任何问题,所以我想知道我做错了什么。以下是index.js 文件:

'use strict';
var yeoman = require('yeoman-generator');
var chalk = require('chalk');
var yosay = require('yosay');

module.exports = yeoman.Base.extend({

  constructor: function () {
    // Calling the super constructor is important so our generator is correctly set up
    yeoman.Base.apply(this, arguments);
  },

  prompting: function() {

    var done = this.async();
    this.prompt({
      type: 'input',
      name: 'name',
      message: 'Whats the project name?',
      default: this.appname
    }, function(answers) {
      done();
    }.bind(this));

   this.props = {
     name: "yosass"
   };
  },

  writing: {
    //Copy the configuration files
    config: function() {
      this.log("CONFIG COPYING");
      this.props.name = "yosass";
      this.fs.copyTpl(
        this.templatePath('_package.json'),
        this.destinationPath('package.json'), {
          name: this.props.name
        }
      );

      this.fs.copy(
        this.templatePath('_gulpfile.js'),
        this.destinationPath('gulpfile.js')
      )
    },

    //Copy application files
    app: function() {
      this.log("APP COPYING");
      this.fs.copy(
        this.templatePath('_css/_main.scss'),
        this.destinationPath("css/main.scss")
      );

      this.fs.copy(
        this.templatePath("_css/_globals/_variables.scss"),
        this.destinationPath("css/globals/_variables.scss")
      );

      this.fs.copy(
        this.templatePath("_css/_globals/_mixins.scss"),
        this.destinationPath("css/globals/_mixins.scss")
      );

      this.fs.copy(
        this.templatePath("_css/_globals/_colors.scss"),
        this.destinationPath("css/globals/_colors.scss")
      );
    }
  },

  _copy: function(from, to) {
    this.fs.copy(this.templatePath(from), this.destinationPath(to));
  },

  _copyTpl: function(from, to) {
    this.fs.copy(this.templatePath(from), this.destinationPath(to));
  },

  //Install Dependencies
  install: function() {
    this.installDependencies();
  }

});

最佳答案

this.prompt 不接受回调。它返回一个 promise 。

关于javascript - 生成器在 async() 调用后退出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40767434/

相关文章:

javascript - 在提示之间传递数据

javascript - Yeoman生成器下载后调用函数

yeoman - 在 Yeoman Generator 中创建空目录

npm generator-ionic 安装需要 yo@>=1.0.0

npm - 由于 0.9.5.tar.gz 的 406,Yeoman 不会安装

javascript - 如何将 angularJS 与 Yeoman 的 webapp-generator 一起使用

javascript - JSON.stringify()不能按预期工作

JavaScript - 我想要但按钮有延迟,因此只能每 15 秒单击一次

javascript - 使用 Ajax 和 PHP 上传图像

javascript - hasOwnProperty 和对象的属性访问