2016-11-08 10 views
1

私のジェネレータコードは次のとおりです。私はそれを実行するときにいくつかの面白い振る舞いを見ている。一度それがの第二を尋ねると、それもwritingの下の呼び出しコード。一度プロンプトが完了した後に実行する必要はありません?私は間違って何をしていますか?yeomanジェネレータはプロンプトを表示してループを実行します

app/index.js

'use strict' 
var generators = require('yeoman-generator'); 
var questions = require('./questions'); 

module.exports = generators.Base.extend({ 
    constructor: function() { 
     generators.Base.apply(this, arguments); 
     this.argument('name', { type: String, required: true }); 
    }, 

    initializing: function() { 
     this.properties = []; 
    }, 
    prompting: { 
    askForProperties: questions.askForProperties 
    }, 
    writing: function(){ 
    this.log("brewing your domain project"); 
    } 
}); 

app/questions.js

'use strict' 

var chalk = require('chalk'); 

function askForProperties() { 
    var prompts = [ 
    { 
     type: 'confirm', 
     name: 'addProp', 
     message: 'Add property to your model (just hit enter for YES)?', 
     default: true 
    }, 
    { 
     type: 'input', 
     name: 'propName', 
     message: 'Give name to your property?', 
     when: function (response) { 
      return response.addProp; 

     } 
    }, 
    { 
     type: 'list', 
     name: 'propType', 
     message: 'Pick a type for your property', 
     choices: [ 
      { 
       value: 'string', 
       name: 'string', 
      }, 
      { 
       value: 'int', 
       name: 'int' 
      }, 
      { 
       value: 'bool', 
       name: 'bool' 
      }, 
      { 
       value: 'decimal', 
       name: 'decimal' 
      }], 
     default: 0, 
     when: function (response) { 
      return response.addProp; 

     } 
    } 
]; 

return this.prompt(prompts).then(function (answers) { 
    var property = { 
     propertyName: answers.propName, 
     propertyType: answers.propType 
    }; 

    this.properties.push(answers.propName); 

    if (answers.addProp) { 
     askForProperties.call(this); 
    } 
}.bind(this)); 
} 

module.exports = { 
    askForProperties 
}; 

答えて

0

あなたはヨーマンオブジェクトの構文を戦っているように見えます。あなたが求める方法に約束を返すされていない、それはセクションの下Yeoman docs Running Contextであるこのに探している人のために

(あなたが約束を返すaskForPropertiesメソッドを持つオブジェクトを返している)というタイトル非同期タスク

prompting() { 
    const done = this.async(); 
    this.prompt(prompts).then((answers) => { 
     this.log('My work after prompting'); 
     done(); 
    }); 
} 

これにより、システムはあなたの約束がいつ返ってきたのかを知ることができ、次の優先順位のステップを実行するのを待つことを知ることができます。

しかし、本当にしたいのは、prompting()メソッド自体から約束を返すことです。あなたの特定のケースで

prompting() { 
    const promise = this.prompt(prompts).then((answers) => { 
     this.log('Handling responses to prompts'); 
    }); 
    return promise; 
} 

あなたが約束正しくセットアップをしましたが、あなたは(ちょうどaskForPropertiesプロパティに)プロンプトプロパティに戻っていません。

askForPropertiesがすでに約束を返しているので、あなたの修正は次のようになります。

prompting: questions.askForProperties 
関連する問題