sails.js - 在 Sails.js 应用程序中运行 CLI 命令

标签 sails.js command-line-interface

我需要在提升我的应用程序后立即运行 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

关于sails.js - 在 Sails.js 应用程序中运行 CLI 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36469607/

相关文章:

javascript - 所有 Lodash 函数 "Not A Function"

javascript - sailsjs 不选择本地更新的 json

ios - Phonegap 3.1 - 不支持运行 ios

Xcode 等待 OS X 应用程序启动错误 : Lost connection to My Mac

在 Linux 中使用命令行编译 F# 程序

amazon-web-services - AWS CLI : Could not connect to the endpoint URL : "https://sts.amazonaws.com/"

node.js - 风 sails js : dynamic tableName in model

node.js - 加载 View 时 sails 404

python - 如何使用 Python 的 Click 包从装饰器返回参数值?

sails.js - 在 Installable Hook 中定义自定义 API 响应