外壳到节点时保留输出颜色

时间:2022-02-11 00:06:48

I have a little Grunt task that shells out via node and runs "composer install".

我有一个小Grunt任务,通过节点弹出并运行“composer install”。

var done = this.async();

var exec = require('child_process').exec;
var composer = exec(
    'php bin/composer.phar install',
    function(error, stdout, stderr) {
        done(error===null);
    }
);

composer.stdout.on(
    'data',
    grunt.log.write
);

As you can see, I'm outputting the stdout of this child process to grunt.log. All output is showing up nice and well as expected, except that the output is all in my default console color. If I run "composer install" directly I get highlighting that improves readability.

如您所见,我正在将此子进程的stdout输出到grunt.log。除了输出全部是我的默认控制台颜色之外,所有输出都显示出良好且良好的预期效果。如果我直接运行“composer install”,我会突出显示提高可读性。

Since I'm new to node, Grunt and shelling out in general, I'm unsure about in which part of the system the coloring gets lost, or even how to debug this efficiently.

由于我是节点的新手,Grunt和一般的shelling,我不确定系统的哪个部分着色丢失,甚至不知道如何有效地调试它。

4 个解决方案

#1


6  

In some cases command line programs will prevent a colorized output when not run through a terminal, and thus you need to instruct the program to output the ANSI escape sequences.

在某些情况下,命令行程序在不通过终端运行时会阻止彩色输出,因此您需要指示程序输出ANSI转义序列。

In this case, it's as simple as adding an '--ansi' flag, for example:

在这种情况下,它就像添加' - anansi'标志一样简单,例如:

var done = this.async();

var exec = require('child_process').exec;
var composer = exec(
    'php bin/composer.phar install --ansi',
    function(error, stdout, stderr) {
        done(error===null);
    }
);

composer.stdout.on(
    'data',
    grunt.log.write
);

#2


15  

Using spawn with the option stdio='inherit' worked to include output color.

使用带有stdio ='inherit'选项的spawn可以包含输出颜色。

From the documentation:

从文档:

options (Object)

选项(对象)

  • cwd String Current working directory of the child process
  • cwd String子进程的当前工作目录
  • stdio (Array|String) Child's stdio configuration. (See below)
  • stdio(Array | String)Child的stdio配置。 (见下文)

...

...

As a shorthand, the stdio argument may also be one of the following strings, rather than an array:

作为简写,stdio参数也可以是以下字符串之一,而不是数组:

  • ignore - ['ignore', 'ignore', 'ignore']
  • 忽略 - ['忽略','忽略','忽略']
  • pipe - ['pipe', 'pipe', 'pipe']
  • 管道 - ['管道','管道','管道']
  • inherit - [process.stdin, process.stdout, process.stderr] or [0,1,2]
  • 继承 - [process.stdin,process.stdout,process.stderr]或[0,1,2]

Here is an example of the working code:

以下是工作代码的示例:

require('child_process')
  .spawn('npm', ['install'], {stdio:'inherit'})
  .on('exit', function (error) {

    if(!error){
      console.log('Success!');
    }

    }
  });

I wanted to make exec work but I did not find a way to access the same option.

我想让exec工作,但我没有找到一种方法来访问相同的选项。

#3


4  

The --colors flag worked for me. Node version 6.8.0...

--colors旗帜对我有用。节点版本6.8.0 ...

--colors, -c force enabling of colors [boolean]

--colors,-c强制启用颜色[boolean]

The following generic example would print the colors should any be returned...

以下通用示例将打印颜色,如果有任何返回...

var exec = require('child_process').exec;

exec('node someCommand --colors', function (error, stdout, stderr) {

  console.log(stdout || stderr); // yay colors!
});

#4


3  

If like myself, you are spawning a child node process as opposed to a non-node script, you may find that the --ansi and --color options will give you little success for retaining the colored output of child node processes.

如果像我一样,您正在生成子节点进程而不是非节点脚本,您可能会发现--ansi和--color选项在保留子节点进程的彩色输出方面几乎没有成功。

Instead, you should inherit the instances of stdio of the current process.

相反,您应该继承当前进程的stdio实例。

My particular use-case involved forking a node server as a background task in order to execute an end-to-end test suite against an active HTTP interface. Here was my final solution:

我的特定用例涉及将节点服务器分配为后台任务,以便针对活动HTTP接口执行端到端测试套件。这是我的最终解决方案:

var child = spawn('node', ['webserver/server.js'], {
  args: ['--debug'],
  env: _.extend(process.env, {
    MOCK_API: mockApi
  }),

  // use process.stdout to retain ansi color codes
  stdio: [process.stdin, process.stdout, 'pipe']
});

// use custom error buffer in order to throw using grunt.fail()
var errorBuffer = '';
child.stderr.on('data', function(data) {
  errorBuffer += data;
});

child.on('close', function(code) {
  if (code) {
    grunt.fail.fatal(errorBuffer, code);
  } else {
    done();
  }
});

#1


6  

In some cases command line programs will prevent a colorized output when not run through a terminal, and thus you need to instruct the program to output the ANSI escape sequences.

在某些情况下,命令行程序在不通过终端运行时会阻止彩色输出,因此您需要指示程序输出ANSI转义序列。

In this case, it's as simple as adding an '--ansi' flag, for example:

在这种情况下,它就像添加' - anansi'标志一样简单,例如:

var done = this.async();

var exec = require('child_process').exec;
var composer = exec(
    'php bin/composer.phar install --ansi',
    function(error, stdout, stderr) {
        done(error===null);
    }
);

composer.stdout.on(
    'data',
    grunt.log.write
);

#2


15  

Using spawn with the option stdio='inherit' worked to include output color.

使用带有stdio ='inherit'选项的spawn可以包含输出颜色。

From the documentation:

从文档:

options (Object)

选项(对象)

  • cwd String Current working directory of the child process
  • cwd String子进程的当前工作目录
  • stdio (Array|String) Child's stdio configuration. (See below)
  • stdio(Array | String)Child的stdio配置。 (见下文)

...

...

As a shorthand, the stdio argument may also be one of the following strings, rather than an array:

作为简写,stdio参数也可以是以下字符串之一,而不是数组:

  • ignore - ['ignore', 'ignore', 'ignore']
  • 忽略 - ['忽略','忽略','忽略']
  • pipe - ['pipe', 'pipe', 'pipe']
  • 管道 - ['管道','管道','管道']
  • inherit - [process.stdin, process.stdout, process.stderr] or [0,1,2]
  • 继承 - [process.stdin,process.stdout,process.stderr]或[0,1,2]

Here is an example of the working code:

以下是工作代码的示例:

require('child_process')
  .spawn('npm', ['install'], {stdio:'inherit'})
  .on('exit', function (error) {

    if(!error){
      console.log('Success!');
    }

    }
  });

I wanted to make exec work but I did not find a way to access the same option.

我想让exec工作,但我没有找到一种方法来访问相同的选项。

#3


4  

The --colors flag worked for me. Node version 6.8.0...

--colors旗帜对我有用。节点版本6.8.0 ...

--colors, -c force enabling of colors [boolean]

--colors,-c强制启用颜色[boolean]

The following generic example would print the colors should any be returned...

以下通用示例将打印颜色,如果有任何返回...

var exec = require('child_process').exec;

exec('node someCommand --colors', function (error, stdout, stderr) {

  console.log(stdout || stderr); // yay colors!
});

#4


3  

If like myself, you are spawning a child node process as opposed to a non-node script, you may find that the --ansi and --color options will give you little success for retaining the colored output of child node processes.

如果像我一样,您正在生成子节点进程而不是非节点脚本,您可能会发现--ansi和--color选项在保留子节点进程的彩色输出方面几乎没有成功。

Instead, you should inherit the instances of stdio of the current process.

相反,您应该继承当前进程的stdio实例。

My particular use-case involved forking a node server as a background task in order to execute an end-to-end test suite against an active HTTP interface. Here was my final solution:

我的特定用例涉及将节点服务器分配为后台任务,以便针对活动HTTP接口执行端到端测试套件。这是我的最终解决方案:

var child = spawn('node', ['webserver/server.js'], {
  args: ['--debug'],
  env: _.extend(process.env, {
    MOCK_API: mockApi
  }),

  // use process.stdout to retain ansi color codes
  stdio: [process.stdin, process.stdout, 'pipe']
});

// use custom error buffer in order to throw using grunt.fail()
var errorBuffer = '';
child.stderr.on('data', function(data) {
  errorBuffer += data;
});

child.on('close', function(code) {
  if (code) {
    grunt.fail.fatal(errorBuffer, code);
  } else {
    done();
  }
});