child_process.spawn을 실행할 때 색상 유지
child_process.spawn을 사용하여 node.js의 cmd.exe를 통해 Windows 명령을 실행하려고합니다. 올바르게 실행되지만 기본 텍스트 색상으로 만 표시됩니다. 색상을 어떻게 보존합니까? 가능합니까?
var spawn = require('child_process').spawn,
cmd = spawn('cmd', ['/s', '/c', 'C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\\MSBuild c:\\test.sln']);
cmd.stdout.on('data', function(data){
process.stdout.write(data);
});
cmd.stderr.on('data', function(data){
process.stderr.write(data);
});
cmd.on('exit', function(code){
console.log(code);
});
노드를 통해 실행할 때 색상이 유지되지 않습니다.
cmd.exe를 통해 직접 실행하면 색상이 표시됩니다. (이것은 예상되는 동작입니다). 노드를 통해 실행할 때이 동작을 어떻게 얻습니까?
대신 이것을 시도하십시오.
var spawn = require('child_process').spawn
, command = 'C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\\MSBuild c:\\test.sln'
, cmd = spawn('cmd', ['/s', '/c', command], { customFds: [0,1,2] });
cmd.on('exit', function(code){
console.log(code);
});
customFds
Windows에서 작동 하는지 여부는 확실하지 않습니다 . 이전 사용되지 않는 기능이 작동하지 않는다는 것을 알고 있지만 [0,1,2]
fd 로만 전달 하면 특별한 경우가 있다고 생각합니다.
I've been doing something similar here, but I've only ran that command on Unix machines. So let me know if that works on Windows.
There are new 'stdio' option for child_process.spawn(). Try following:
spawn("path to executable", ["params"], {stdio: "inherit"});
"Inherit" means [0, 1, 2] or [process.stdin, process.stdout, process.stderr].
crossplatform solution that worked for me was to use both shell: true
and stdio: 'inherit'
:
const spawn = require('child_process').spawn;
spawn('node', ['./child.js'], { shell: true, stdio: 'inherit' });
thanks @59naga https://github.com/nodejs/node/issues/2333
If you are getting error:
Cannot call method 'on' of null
Try this:
spawn("command", ["args"], { env : { FORCE_COLOR: true }});
works with mocha
This doesn't fix the underlying issue (lack of a proper TTY stream) but it should help get around it.
If the sub-process you're running uses supports-color (https://www.npmjs.com/package/supports-color) like chalk, then you can set an environmental variable FORCE_COLOR
to any value and it will skip the rest of the checks. This will allow you to continue to use pipes (and capture/modify the returned data) unlike the inherit
fix.
There is also a node-pty (https://www.npmjs.com/package/node-pty) module that provides a .spawn
with the ability to pass a pty (pseudo tty) that may be a more holistic answer. I haven't played with it yet, and I'm not sure if it's cross platform or not.
If you want to either keep the color or add some notation for the output, you can try the code below:
var spawn = require('child_process').spawn,
var stream = require('stream');
cmd = spawn('cmd', ['/s', '/c', 'C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\\MSBuild c:\\test.sln'], { stdio: [process.stdin, process.stdout, 'pipe'] });
var customStream = new stream.Writable();
customStream._write = function (data, ...argv) {
console.log('your notation');
process.stderr._write(data, ...argv);
};
cmd.stderr.pipe(customStream);
note that the code use es6
참고 URL : https://stackoverflow.com/questions/7725809/preserve-color-when-executing-child-process-spawn
'IT Share you' 카테고리의 다른 글
Node js 파일에서 폴더 경로 가져 오기 (0) | 2020.11.20 |
---|---|
대소 문자를 구분하지 않고 모두 교체 (0) | 2020.11.19 |
터치를 통해 UIViews 아래에 전달 (0) | 2020.11.19 |
다른 사용자의 힘내 풀 명령 (0) | 2020.11.19 |
OkHttp 요청 인터셉터에 헤더를 추가하는 방법은 무엇입니까? (0) | 2020.11.19 |