gpt4 book ai didi

node.js - 如何在 node.js 中处理标准输出

转载 作者:搜寻专家 更新时间:2023-10-31 23:54:56 25 4
gpt4 key购买 nike

每次在服务器上测试我的应用程序和网站时,我都在尝试使我经历的过程自动化。我目前正在运行 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 文件,让我的应用程序部署到暂存环境并且我的文件基本上保持不变。

最佳答案

我没有在这里设置运行 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 变量中的字符串中,并针对 那个 因为如果有大量输出,则不能保证提示会在单个 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,提示第二次,退出。

关于node.js - 如何在 node.js 中处理标准输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23143704/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com