【问题标题】:Running CLI commands in Sails.js application在 Sails.js 应用程序中运行 CLI 命令
【发布时间】:2016-04-07 21:53:25
【问题描述】:

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

【问题讨论】:

    标签: sails.js command-line-interface


    【解决方案1】:

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

    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

    【讨论】:

    • 这个问题主要强调的是如何运行CLI。我调整了我的起源问题。
    猜你喜欢
    • 2021-09-20
    • 2015-07-03
    • 2017-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-24
    • 1970-01-01
    相关资源
    最近更新 更多