Node.js 中的子进程是否可以保留彩色输出? [英] Is it possible for child processes in Node.js to preserve colored output?

查看:53
本文介绍了Node.js 中的子进程是否可以保留彩色输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Liftoff 编写我的第一个 Node.js 命令行工具.我的应用程序中的重要步骤之一是将一些文件和文件夹复制到用户的 cwd.我正在使用 exeq 来完成此操作.它运行一系列命令:

I am writing my first Node.js command line tool using Liftoff. One of the important steps in my application is to copy some files and folders to the user's cwd. I am using exeq to accomplish this. It runs a series of commands:

  • cd app - cd 到 gulpfile.js 和 package.json 所在的 cwd/app/
  • npm install - 安装依赖
  • gulp - 运行 gulp

在功能上,exeq 完全符合我的要求.它按顺序执行这三个命令并成功执行.我的问题是 exeq 不保留彩色输出,因此 npm installgulp 的日志是纯白色文本,使它们很难解析.

Functionally, exeq does exactly what I want it to do. It executes those three commands in sequence and does so successfully. My problem is that exeq does not preserve colored output, so the logs from npm install and gulp are plain white text, making them very difficult to parse.

到目前为止,我的研究还没有找到可以完成这项工作的替代节点包,也没有找到用我当前的设置保留彩色输出的明确方法.我确实从某人那里得到了线索,这可能是环境问题,我需要一种方法来告诉 exeq 它处于支持彩色输出的环境中.不幸的是,exeq 似乎没有任何选项或参数,所以我不知道如何去做.

My research thus far has not turned up an alternative node package that does the job nor a clear method for preserving colored output with my current setup. I did get a lead from someone that this may be a problem with the environment and that I need a way to tell exeq it's in an environment that supports colored output. Unfortunately, exeq doesn't appear to have any options or arguments, so I have no idea how to go about doing that.

这是节点子进程的限制,还是有办法保留彩色输出?

Is this a limitation of node child processes, or is there a way to preserve the colored output?

推荐答案

例如 gulp使用一个名为chalk 记录格式化输出.chalk 反过来,使用一个名为supports-color 进行实际的终端类型检测.当 chalkrequire()d 时,它会自动使用 supports-color 来确定有多少颜色可用.

So gulp for instance uses a module called chalk to log formatted output. chalk in turn, uses a module called supports-color which does the actual terminal type detection. When chalk is require()d, it automatically uses supports-color to determine how many colors are available.

通常,supports-color 会报告当进程作为子进程使用默认的 stdio 选项执行时没有颜色可用,因为 stdout 是 在那种情况下不是 tty,它是一个管道.幸运的是,supports-colors 提供了几个选项来覆盖该检查:

Ordinarily, supports-color will report that no colors are available when the process is executed as a child process with the default stdio options, since stdout is not a tty in that case, it is a pipe. Fortunately though, supports-colors provides a couple of options to override that check:

  • supports-colors 使用 一个名为 has-flag 的模块,用于查找 process.argv 条目,如 --color--colors 等,强制支持基本 (16) 颜色.您还可以使用 --color=256 强制使用 256 种颜色,使用 --color=full 等参数强制使用真彩色模式(1600 万种颜色).因此,例如,您应该像 gulp --colors 一样调用 gulp 来获得基本的颜色输出.

  • supports-colors uses a module called has-flag to look for process.argv entries like --color, --colors, etc. to force basic (16) color support. You can also use --color=256 to force 256 colors and arguments like --color=full to force true color mode (16 million colors). So for instance you'd supposedly call gulp like gulp --colors to get basic color output.

supports-colors检查 用于名为 FORCE_COLOR 的环境变量,如果检测到不支持颜色,它将强制支持基本颜色.

supports-colors also checks for an environment variable called FORCE_COLOR, which will force basic color support if it is otherwise detected that no colors are supported.

对于 npm,您可以通过几种不同的方式强制颜色输出.附加 --color always 命令行参数或设置 NPM_CONFIG_COLOR=始终(您可以通过在传递给 child_process.exec()/child_process.spawn()<的选项中设置 env/code>).

For npm, you can force color output a couple of different ways. Append the --color always command-line argument or set NPM_CONFIG_COLOR=always in the environment (you can do this by setting env in the options passed to child_process.exec()/child_process.spawn()).

这篇关于Node.js 中的子进程是否可以保留彩色输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆