のは、次の2つのサブ・ジェネレータ(ブログ・サーバーおよびブログ・クライアント)と発電機generator-blog
(BlogGenerator)持って考えてみましょう:あなたはyo blog
を実行するときですから、いくつかのオプションをユーザーに何をお願いする
app\index.js
client\index.js
server\index.js
をし、 (オプションで)サブジェネレータを実行します。
サブジェネレータを実行するには、this.invoke("generator_namespace", {options: {}})
を呼び出す必要があります。 私たちが渡した2番目の引数は、options
フィールドを持つことができます - それはジェネレータに渡されるオプションオブジェクトです。アプリの\のindex.jsで
:クライアントの\ index.jsで
BlogGenerator.prototype.askFor = function askFor() {
var cb = this.async();
// have Yeoman greet the user.
console.log(this.yeoman);
var prompts = [{
name: 'appName',
message: 'Enter your app name',
default: 'MyBlog'
}, {
type: 'confirm',
name: 'createServer',
message: 'Would you like to create server project?',
default: true
}, {
type: 'confirm',
name: 'createClient',
message: 'Whould you like to create client project?',
default: true
}];
this.prompt(prompts, function (props) {
this.appName = props.appName;
this.createServer = props.createServer;
this.createClient = props.createClient;
cb();
}.bind(this));
}
BlogGenerator.prototype.main = function app() {
if (this.createClient) {
// Here: we'are calling the nested generator (via 'invoke' with options)
this.invoke("blog:client", {options: {nested: true, appName: this.appName}});
}
if (this.createServer) {
this.invoke("blog:server", {options: {nested: true, appName: this.appName}});
}
};
:
var BlogGenerator = module.exports = function BlogGenerator(args, options, config) {
var that = this;
yeoman.Base.apply(this, arguments);
// in this.options we have the object passed to 'invoke' in app/index.js:
this.appName = that.options.appName;
this.nested = that.options.nested;
};
BlogGenerator .prototype.askFor = function askFor() {
var cb = this.async();
if (!this.options.nested) {
console.log(this.yeoman);
}
}
UPDATE 2015年12月21日:
廃止とすべきであるinvoke
を使用してcomposeWith
に置き換えてください。しかし、それは可能な限り簡単ではありません。 invoke
とcomposeWith
の主な違いは、サブジェネレータを制御する能力がないことです。あなたはそれらを使って宣言することしかできませんでした。また、私はyeoman.Base
でyeoman.generators.Base
を置き換え削除
BlogGenerator.prototype.main = function app() {
if (this.createClient) {
this.composeWith("blog:client", {
options: {
nested: true,
appName: this.appName
}
}, {
local: require.resolve("./../client")
});
}
if (this.createServer) {
this.composeWith("blog:server", {
options: {
nested: true,
appName: this.appName
}
}, {
local: require.resolve("./../server")
});
}
};
:ここ
は、上記からmain
方法は次のようになります方法です。
**正確には**私が探していたものです。ありがとう。 – Fiona
私はこれが古いことを知っていますが、私が探しているものに近いです。私の問題は、私のサブジェネレータが "yeoman.generators.NamedBase"を拡張していることです。このコードを実行すると、「エラー:必須の引数名を指定しませんでした!」というメッセージが表示されます。 .invoke()によって呼び出されるサブジェネレータにも名前を渡す(つまり、 "yo make:controller test"では "test")方法を理解しようとしています。どんな助け? – ItJustWerks
これを得ました:this.invoke( "make:controller"、{options:{入れ子:真、...}、args:[this.name]});このエラーが発生した場合は、 – ItJustWerks