2016-03-25 4 views
2

どのようにして、エモマンプロンプトが分割されますか? 私は、各パートのタイトルが付いたパートに分割したいと思っています。このようなYeomanは複数のグループに分けられます。

CSS 
- prompt1 

HTML 
-prompt 2 

何か:

prompt1: function(){ 
    var done = this.async(); 
    condole.log('title 1'); 
    var prompts = [{ 
     name: 'prompt1', 
     message: 'Prompt 1:', 
    }] 
}, 


prompt2: function(){ 
    var done = this.async(); 
    condole.log('title 2'); 
    var prompts = [{ 
     name: 'prompt2', 
     message: 'Prompt 2:', 
    }] 
}, 

ありがとう!

答えて

0

@Deimytsの注釈のように、元のコードは機能しなくなりました。これはAPIの変更によるものですInquirer.JSdocumented here

  • ベースのAPIインタフェースは今inquirer.prompt(questions).then()です。コールバック機能はもうありません。
  • すべての非同期質問関数は、this.async()ではなく、戻り値として約束しています。一言で言えば

、代わりにちょうどprompting機能(see docs)からの約束を返す古いvar done = this.async() APIを使用してdone()とコールバックの内側にプロンプ​​トを解決します。詳細については

prompt1: function() { 
    this.log("HTML") 
    return this.prompt([ 
     // configure prompts as before 
    ]).then(function (answers) { 
     // callback body as before, but no more calling done() 
    }.bind(this)); 
}, 

@Deimytsは下記お答え参照してください。


ヨーマンはあなたにあなたの行動を置くために使用することができ、特定の事前定義された優先順位に実行ループを使用しています。 ☞ docsで説明したように、複数のメソッドを1つの優先度でグループ化できます。例えば、あなたがさらにあなたのコードをモジュール化できヨーマンのこの機能を使用する

'use strict'; 

var generators = require('yeoman-generator'); 

module.exports = generators.Base.extend({ 

    constructor: function() { 
    generators.Base.apply(this, arguments); 
    }, 

    prompting: { 
    prompt1: function() { 
     this.log("HTML") 
     var done = this.async(); 
     this.prompt([{ 
      type : 'input', 
      name : 'foo', 
      message : 'Foo', 
     }, { 
      type : 'input', 
      name : 'bar', 
      message : 'Bar' 
     }], function (answers) { 
      this.foo = answers.foo; 
      this.bar = answers.bar; 
      done(); 
     }.bind(this)); 
    }, 

    prompt2: function() { 
     this.log("CSS") 
     var done = this.async(); 
     this.prompt([{ 
      type : 'input', 
      name : 'bam', 
      message : 'Bam', 
     }], function (answers) { 
      this.bam = answers.bam; 
      done(); 
     }.bind(this)); 
    } 
    }, 

    configuring: function() { 
    console.log(this.foo, this.bar, this.bam); 
    } 
}); 

:プロンプトは二つのグループHTMLCSSに分割してここで発電機を説明するためのスニペットですさまざまなプロンプトを別々のコードファイルに入れ、require/importをジェネレータファイルに入れます。しかし、基本的に上記のスニペットはトリックを行う必要があります。

それが役立つかどうか教えてください。

+0

はい、これは動作しているようです。ありがとう! – michaeldeboeve

+0

すばらしい、聞いてうれしい。 – sthzg

+1

この解決法は私のためには機能しません。私はyeoman-generator v0.23.4を使用しています。ジェネレータは 'prompt1'だけを要求し、終了します。 'var done = this.async();'と 'done();'を削除すると、両方の質問をすることができますが、答えを待つことはありません。 – Deimyts

0

サンプルコードをいくつか変更するまで、これまでの回答はうまくいかなかった。

私は100%確信することはできませんが、違いはyeoman-generatorモジュールの異なるバージョンに起因すると考えられます。だから、他の誰かが同じ問題に遭遇した場合に備えて、私はこれをここに記録しています。参考のため

、私はyeoman-generator v0.23.4yo v1.8.4node v6.2.2、& npm v3.9.5を使用しています。

変更:

  1. var done = this.async();done()のすべてのインスタンスを削除します。

    async()関数は、prompt1の後にジェネレータを終了させ、prompt2またはconfiguring関数を実行しませんでした。

  2. this.prompt()を呼び出す前にreturnを追加します。

    async()を削除すると、応答を待たずにプロンプ​​トが表示されます。 returnを追加するとこれが修正されます。

  3. this.prompt()のコールバック関数を.then()に置き換えます。

    この変更を行う前に、ジェネレータはプロンプトを正しく実行しますが、回答は保存されず、configuringは単にundefined undefined undefinedと記録されます。

    オリジナル:改訂this.prompt(prompts, callback(answers).bind(this))

    this.prompt(prompts).then(callback(answers).bind(this));

全例:

'use strict'; 

var generators = require('yeoman-generator'); 

module.exports = generators.Base.extend({ 

    constructor: function() { 
    generators.Base.apply(this, arguments); 
    }, 

    prompting: { 
    prompt1: function() { 
     this.log("HTML") 
     return this.prompt([{ 
     type : 'input', 
     name : 'foo', 
     message : 'Foo', 
     }, { 
     type : 'input', 
     name : 'bar', 
     message : 'Bar' 
     }]).then(function (answers) { 
     this.foo = answers.foo; 
     this.bar = answers.bar; 
     }.bind(this)); 
    }, 

    prompt2: function() { 
     this.log("CSS") 
     return this.prompt([{ 
     type : 'input', 
     name : 'bam', 
     message : 'Bam', 
     }]) 
     .then(function(answers) { 
     this.bam = answers.bam; 
     }.bind(this)); 
    } 
    }, 

    configuring: function() { 
    console.log(this.foo, this.bar, this.bam); 
    console.log('Config: ', this.config); 
    }, 
}); 
関連する問題