【发布时间】:2016-04-07 21:53:25
【问题描述】:
我需要在提升我的应用程序后立即运行 PostgreSQL 脚本。例如,我需要从应用程序执行此命令:psql -d DOGHOUZ -a -f script.sql。有没有办法做到这一点?
【问题讨论】:
标签: sails.js command-line-interface
我需要在提升我的应用程序后立即运行 PostgreSQL 脚本。例如,我需要从应用程序执行此命令:psql -d DOGHOUZ -a -f script.sql。有没有办法做到这一点?
【问题讨论】:
标签: sails.js command-line-interface
这取决于您提升应用的方式。如果你使用:
node app.js
你可以添加
sails.on('lifted', function yourEventHandler () {
console.log('lifted')
});
在您的 app.js 文件中 Sails.lift(rc('sails')); 之前的行中
否则您需要将其添加到配置中。最好的方法是在 /config 中创建新文件,例如 /config/eventhooks.js,内容如下:
module.exports.eventhooks = function(cb) {
sails.on('lifted', function yourEventHandler () {
console.log('lifted')
});
}
您可以在这里阅读更多内容:
编辑 1
要执行 CLI 命令,只需:
var exec = require('child_process').exec;
var cmd = 'psql -d DOGHOUZ -a -f script.sql';
exec(cmd, function(error, stdout, stderr){
// command output is in stdout
});
更多关于在 CLI 中执行命令的信息你可以read here
【讨论】: