Skip to content

Commit

Permalink
feat(console): hack in process.stdout and process.stderr
Browse files Browse the repository at this point in the history
  • Loading branch information
Yanjiie authored and mapleeit committed Apr 20, 2020
1 parent 9e06d56 commit 56b095a
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 1 deletion.
17 changes: 17 additions & 0 deletions lib/core/__test__/runtime/console.hack.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ afterAll(() => {
consoleRestore();
});

afterEach(() => {
jest.clearAllMocks();
});

describe("console hack test", () => {
test("ensure console contains all origin functions", () => {
expect(typeof console.originDebug).toBe("function");
Expand Down Expand Up @@ -44,6 +48,19 @@ describe("console hack test", () => {
(getCurrentContext as jest.Mock).mockClear();
});

test("console.debug should be logged by log111ger", () => {
(getCurrentContext as jest.Mock).mockImplementation(() => true);
const mockedWriteLog = logger.writeLog as jest.Mock;

expect(mockedWriteLog.mock.calls.length).toEqual(0);
process.stdout.write("test_log");
expect(mockedWriteLog.mock.calls.length).toEqual(1);
process.stderr.write("test_log");
expect(mockedWriteLog.mock.calls.length).toEqual(2);

(getCurrentContext as jest.Mock).mockClear();
});

test("multi consoleRestore() should not have side effect", () => {
consoleRestore();
consoleRestore();
Expand Down
4 changes: 3 additions & 1 deletion lib/core/logger/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,9 @@ export class Logger {
}

private static fillStdout(str: string): void {
process.stdout.write(`${str}\n`);
// console hacking origin write, so use originWrite
const stdout = (process.stdout as any).originWrite || process.stdout.write;
stdout.call(process.stdout, `${str}\n`);
}
}

Expand Down
44 changes: 44 additions & 0 deletions lib/core/runtime/console.hack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,44 @@ export const consoleHack = (): void => {
`${util.format(message, ...optionalParams)}`
);
};

// hack process._stdout
(process.stdout as any).originWrite = process.stdout.write;
process.stdout.write = (
data: Buffer | string,
encodingOrCallback?: string | ((err?: Error) => void) | undefined
): boolean => {
let encoding: BufferEncoding;
if (typeof encodingOrCallback !== "function") {
encoding = encodingOrCallback as BufferEncoding;
}

logger.writeLog(
"DEBUG",
data.toString(encoding).replace(/\n$/, "") // 去掉换行符
);

return true;
};

// hack process._stderr
(process.stderr as any).originWrite = process.stderr.write;
process.stderr.write = (
data: Buffer | string,
encodingOrCallback?: string | ((err?: Error) => void) | undefined
): boolean => {
let encoding: BufferEncoding;
if (typeof encodingOrCallback !== "function") {
encoding = encodingOrCallback as BufferEncoding;
}

logger.writeLog(
"ERROR",
data.toString(encoding).replace(/\n$/, "") // 去掉换行符
);

return true;
};
}
};

Expand All @@ -124,11 +162,17 @@ export const consoleRestore = (): void => {
console.error = console.originError;
console.dir = console.originDir;

process.stdout.write = (process.stdout as any).originWrite;
process.stderr.write = (process.stderr as any).originWrite;

delete console.originDebug;
delete console.originInfo;
delete console.originLog;
delete console.originWarn;
delete console.originError;
delete console.originDir;

delete (process.stdout as any).originWrite;
delete (process.stderr as any).originWrite;
}
};

0 comments on commit 56b095a

Please sign in to comment.