运行 Sails.js 应用程序中的 CLI 命令

Running CLI commands in Sails.js application

我需要在提升我的应用程序后立即 运行 PostgreSQL 脚本。例如,我需要从应用程序执行此命令:psql -d DOGHOUZ -a -f script.sql。有办法吗?

这取决于您提升应用的方式。如果您使用:

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