Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement isTTY property on FakeStream #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {Chunk, ChunkType} from './chunk';
import {PassThrough, Readable, Transform, TransformOptions, Writable} from 'stream';
import {EventEmitter} from 'events';
import * as fs from 'fs';
import * as tty from 'tty';

export interface Ref {
ref(): void
Expand All @@ -16,16 +17,25 @@ export interface CommandContext {

// get-stdin maintains reference to process.stdin, so it must be replaced with a permemant value
class FakeStream extends PassThrough {
constructor(private readonly options?: TransformOptions) {
constructor(private readonly fd: number, private readonly options?: TransformOptions) {
super(options);
}

reset() {
this.removeAllListeners();
PassThrough.call(this, this.options);
}

get isTTY(): boolean {
return +(process.env[`NAILGUN_TTY_${this.fd}`] as any) > 0;
}
}

// @ts-ignore
FakeStream.prototype.getColorDepth = tty.WriteStream.prototype.getColorDepth;
// @ts-ignore
FakeStream.prototype.hasColors = tty.WriteStream.prototype.hasColors;

export namespace real {
export const stderrWrite = process.stderr.write.bind(process.stderr);
export const stdoutWrite = process.stdout.write.bind(process.stdout);
Expand Down Expand Up @@ -54,12 +64,12 @@ function install() {
Object.defineProperty(process, 'stderr', {
configurable: true,
enumerable: true,
get: (stderr => () => stderr)(new FakeStream),
get: (stderr => () => stderr)(new FakeStream(2)),
});
Object.defineProperty(process, 'stdout', {
configurable: true,
enumerable: true,
get: (stdout => () => stdout)(new FakeStream),
get: (stdout => () => stdout)(new FakeStream(1)),
});

process.stderr.write = function() {
Expand All @@ -75,7 +85,7 @@ function install() {
Object.defineProperty(process, 'stdin', {
configurable: true,
enumerable: true,
get: (stdin => () => stdin)(new FakeStream),
get: (stdin => () => stdin)(new FakeStream(0)),
});
}

Expand Down