【问题标题】:How to handle stdout in node.js如何在 node.js 中处理标准输出
【发布时间】:2014-04-17 21:22:54
【问题描述】:

我正在尝试自动化我每次在服务器上测试我的应用和网站时所经历的过程。我目前正在 nodejitsu 上运行。当我测试了某些东西并且它可以在我的本地机器上运行时,我要做的下一件事是......

  1. 打开我的 package.json 文件
  2. 删除域字段并将名称和子域更改为暂存。 (更改版本号也可能有意义)
  3. 那我jitsu deploy
  4. 确认任何提示(例如批准增加版本号)
  5. 应用启动后,我会检查我的应用在服务器上的运行情况、进行更改等

完成后,我的应用程序已准备就绪,我将撤消 package.json 文件中的更改。我想自动化这个过程。我想用一个很小的 ​​node.js 文件来做这件事。到这里为止……

/*
 * Use this file to deploy an app to the staging server on nodejitsu
 */
var bash = require('child_process').spawn('bash');
var colors = require('colors');
var fs = require('fs');

// stdout setup
bash.stdout.on('data', function (data) {
  console.log('stdout: ' + data);
});
bash.stdout.on('error', function (err) {
  console.log('stdout error: '.red, err);
});

// on bash exit
bash.on('exit', function (code) {
  console.log('Exiting... ', code);
});

// grab package.json
var package = '';
fs.readFile('package.json', {encoding: 'utf-8'}, function (err, data) { // grab the package.json file contents
  if (err) throw err;
  package = JSON.parse(data);
  fs.rename('package.json', 'rename-me-before-deploying.json'); // rename the package.json file
  package.name = 'stajing'; // alter json
  package.subdomain = 'stajing'; // alter json
  package.domains = []; // alter json
  fs.writeFile('package.json', JSON.stringify(package, null, 2), function(err) { // write the new package to package.json
    if (err) throw err;
    bash.stdin.write('jitsu deploy\n'); // Deploy to staging app on nodejitsu.
    setTimeout(function () { // this needs to be replaced
      bash.stdin.write('yes\n');
    }, 5000);
    console.log("All done : )");
    // bash.stdin.end(); // close out
  });
});

我有几个问题。我很确定要完成它,我需要知道的只是当 nodejitsu 提示我增加版本号 prompt: Is this ok?: (yes) 时触发的事件,以便我可以确认,如果发生这种情况,以及在整个过程中触发的事件完成,以便我可以恢复对我的 package.json 文件的更改,将我的应用程序部署到一个暂存环境,并且我的文件基本上保持不变。

【问题讨论】:

  • undefined 似乎来自'exit' 事件colors doesn't appear to define orange
  • 你摇滚!!!!!! (感叹号由 Stackoverflow 的 cmets 最小字符数提供)
  • 您的问题到底是什么?顺便说一句,为什么你必须操纵你的 package.json?你不应该这样做。处理特定于环境的配置有不同的可能性。
  • 我最终发布了另一个问题(这里:stackoverflow.com/q/23209413/1027004),最终我需要这样做:gist.github.com/CostaMichailidis/11181252
  • 我同意您不需要操作 package.json 文件,但我想不出另一种在服务器上测试我的应用程序的方法,因为它将用于生产。我可以在本地进行测试,但是一旦我部署,它就会上线(据我所知,无论 NODE_ENV 是什么)。所以,我用一架无人机设置了一个单独的“应用程序”来测试我的网站/应用程序。

标签: node.js bash stdout std stdin


【解决方案1】:

我没有设置在这里运行jitsu deploy。但是,这里有一些代码说明了如何处理提示:

var command = require('child_process').spawn('./command.js');
require('colors');

var stdout = "";
var prompt_re = /Is it okay \(yes\)\?.*?$/m;
command.stdout.on('data', function (data) {
    console.log("stdout data: ".green + data);
    stdout += data;
    if (prompt_re.test(stdout)) {
        command.stdin.write("yes\n");
        // Flush the current buffer.
        stdout = "";
    }
});

command.stdout.on('error', function (err) {
    console.log('stdout error: '.red, err);
});

var exit_msg = 'Exited with code... ';
command.on('exit', function (code) {
    if (code != 0) {
        console.log(exit_msg.red, code);
        process.exit(1); // Or whatever you want to handle errors.
    }

    console.log(exit_msg.green, code);
    // The code you want to execute once your command is done goes here.
});

一些解释:

  1. 上面的代码将从command.stdout 获取的数据缓冲为存储在stdout 变量中的字符串,并针对that 进行测试,因为如果有大量输出,则无法保证提示将在单个 data 事件中到达。 (例如,它可能出现在一个包含一堆数据 + 字符串 Is it 的事件中,然后下一个 data 事件可能包含提示的其余部分。)

  2. 正则表达式 prompt_re 包含 '.*?$' 因为我用来模拟获取提示的命令使用了颜色的转义码,我懒得去匹配输出的确切代码。

  3. 此代码假定命令将在输出提示后立即停止并在那里等待。这似乎是一个安全的假设。

  4. 它还假定提示文本不能以非提示的形式出现。这是否安全取决于您的具体情况。

我用来模拟正在运行的命令的./command.js文件是:

#!/usr/bin/env node

var prompt = require("prompt");

function ask(cb) {
    prompt.get(["Is it okay (yes)?"], function (err, result) {
        console.log("asked");
        cb();
    });
}

setTimeout(function () {
    ask(function () {
        setTimeout(function () {
            ask(function () { process.exit(0); });
        }, 1000);
    });
}, 5000);

等待5s,提示一次,等待1s,提示第二次,退出。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2022-07-10
  • 2013-06-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多