Yeoman 生成器:复制所有文件后如何 运行 异步命令
Yeoman generator: how to run async command after all files copied
我正在编写一个自耕农生成器。
复制所有文件后,我需要 运行 一些 shell 脚本。
生成器被称为子生成器,因此它应该等到脚本完成。
该脚本是一些命令文件 运行 通过 spawn
:
that.spawnCommand('createdb.cmd');
由于脚本依赖于生成器创建的文件,因此它不能 运行 就在生成器的方法内部,因为所有 copy/template 操作都是异步的并且尚未执行:
MyGenerator.prototype.serverApp = function serverApp() {
if (this.useLocalDb) {
this.copy('App_Data/createdb.cmd', 'App_Data/createdb.cmd');
// here I cannot run spawn with createdb.cmd as it doesn't exist
}
}
所以我找到的唯一可以 运行 生成的地方是 'end' 事件处理程序:
var MyGenerator = module.exports = function MyGenerator (args, options, config) {
this.on('end', function () {
if (that.useLocalDb) {
that.spawnCommand('createdb.cmd')
}
}
}
脚本 运行 成功但生成器比子进程早完成。我需要告诉 Yeoman 等待我的子进程。
像这样:
this.on('end', function (done) {
this.spawnCommand('createdb.cmd')
.on('close', function () {
done();
});
}.bind(this));
但是 'end' 处理程序没有 'dine' 回调的参数。
如何操作?
更新:
感谢@SimonBoudrias 我让它工作了。
完整的工作代码如下。
顺便说一句:end
方法在 docs
中有描述
var MyGenerator = module.exports = yeoman.generators.Base.extend({
constructor: function (args, options, config) {
yeoman.generators.Base.apply(this, arguments);
this.appName = this.options.appName;
},
prompting : function () {
// asking user
},
writing : function () {
// copying files
},
end: function () {
var that = this;
if (this.useLocalDb) {
var done = this.async();
process.chdir('App_Data');
this.spawnCommand('createdb.cmd').on('close', function () {
that._sayGoodbay();
done();
});
process.chdir('..');
} else {
this._sayGoodbay();
}
},
_sayGoodbay: funciton () {
// final words to user
}
});
切勿在 end
事件中触发任何操作。此事件将由实现者使用,而不是生成器本身。
你的情况:
module.exports = generators.Base({
end: function () {
var done = this.async();
this.spawnCommand('createdb.cmd').on('close', done);
}
});
我正在编写一个自耕农生成器。
复制所有文件后,我需要 运行 一些 shell 脚本。
生成器被称为子生成器,因此它应该等到脚本完成。
该脚本是一些命令文件 运行 通过 spawn
:
that.spawnCommand('createdb.cmd');
由于脚本依赖于生成器创建的文件,因此它不能 运行 就在生成器的方法内部,因为所有 copy/template 操作都是异步的并且尚未执行:
MyGenerator.prototype.serverApp = function serverApp() {
if (this.useLocalDb) {
this.copy('App_Data/createdb.cmd', 'App_Data/createdb.cmd');
// here I cannot run spawn with createdb.cmd as it doesn't exist
}
}
所以我找到的唯一可以 运行 生成的地方是 'end' 事件处理程序:
var MyGenerator = module.exports = function MyGenerator (args, options, config) {
this.on('end', function () {
if (that.useLocalDb) {
that.spawnCommand('createdb.cmd')
}
}
}
脚本 运行 成功但生成器比子进程早完成。我需要告诉 Yeoman 等待我的子进程。 像这样:
this.on('end', function (done) {
this.spawnCommand('createdb.cmd')
.on('close', function () {
done();
});
}.bind(this));
但是 'end' 处理程序没有 'dine' 回调的参数。
如何操作?
更新:
感谢@SimonBoudrias 我让它工作了。
完整的工作代码如下。
顺便说一句:end
方法在 docs
var MyGenerator = module.exports = yeoman.generators.Base.extend({
constructor: function (args, options, config) {
yeoman.generators.Base.apply(this, arguments);
this.appName = this.options.appName;
},
prompting : function () {
// asking user
},
writing : function () {
// copying files
},
end: function () {
var that = this;
if (this.useLocalDb) {
var done = this.async();
process.chdir('App_Data');
this.spawnCommand('createdb.cmd').on('close', function () {
that._sayGoodbay();
done();
});
process.chdir('..');
} else {
this._sayGoodbay();
}
},
_sayGoodbay: funciton () {
// final words to user
}
});
切勿在 end
事件中触发任何操作。此事件将由实现者使用,而不是生成器本身。
你的情况:
module.exports = generators.Base({
end: function () {
var done = this.async();
this.spawnCommand('createdb.cmd').on('close', done);
}
});