Skip to content

Commit

Permalink
working with readline
Browse files Browse the repository at this point in the history
  • Loading branch information
gilamran committed Feb 25, 2020
1 parent 35f011d commit 8641cc4
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 38 deletions.
51 changes: 20 additions & 31 deletions lib/stdout-manipulator.js
Expand Up @@ -22,17 +22,17 @@ const newAdditionToSyntax = [

function color(line, noClear) {
// coloring errors:
line = line.replace(typescriptErrorRegex, m => `\u001B[36m${m}\u001B[39m`); // Cyan
line = line.replace(typescriptPrettyErrorRegex, m => `\u001B[36m${m}\u001B[39m`); // Cyan
line = line.replace(typescriptErrorRegex, m => `\u001B[36m${m}\u001B[39m`); // Cyan
line = line.replace(typescriptPrettyErrorRegex, m => `\u001B[36m${m}\u001B[39m`); // Cyan

// completed with error:
line = line.replace(compilationCompleteWithErrorRegex, m => `\u001B[31m${m}\u001B[39m`); // Red
line = line.replace(compilationCompleteWithErrorRegex, m => `\u001B[31m${m}\u001B[39m`); // Red

// completed without error:
line = line.replace(compilationCompleteWithoutErrorRegex, m => `\u001B[32m${m}\u001B[39m`); // Green
line = line.replace(compilationCompleteWithoutErrorRegex, m => `\u001B[32m${m}\u001B[39m`); // Green

// usage
line = line.replace(tscUsageSyntaxRegex, m => `\u001B[33m${m}\u001B[39m`); // Yellow
line = line.replace(tscUsageSyntaxRegex, m => `\u001B[33m${m}\u001B[39m`); // Yellow

if (noClear && newCompilationRegex.test(line)) {
return '\n\n----------------------\n' + line;
Expand All @@ -41,38 +41,27 @@ function color(line, noClear) {
return line;
}

function print(noColors, noClear, lines) {
return lines.forEach(line => console.log(noColors ? line : color(line, noClear)));
function print(noColors, noClear, line) {
return console.log(noColors ? line : color(line, noClear));
}

function removeColors(lines) {
return lines.map(line => stripAnsi(line));
function isClear(line) {
const buffer = Buffer.from(line);
return buffer.length >= 2 && buffer[0] === 0x1b && buffer[1] === 0x63;
}

function isClear(buffer) {
return buffer && buffer.length === 2 && buffer[0] === 0x1b && buffer[1] === 0x63;
function manipulate(line) {
return line.replace(tscUsageSyntaxRegex, newAdditionToSyntax);
}

function manipulate(buffer) {
const lines = buffer
.toString()
.split('\n')
.filter(a => a.length > 0)
.map(a => a.replace(tscUsageSyntaxRegex, newAdditionToSyntax));

return lines;
}

function detectState(lines) {
const clearLines = removeColors(lines);
const newCompilation = clearLines.some(line => newCompilationRegex.test(line));
const compilationError = clearLines.some(
line =>
compilationCompleteWithErrorRegex.test(line) ||
typescriptErrorRegex.test(line) ||
typescriptPrettyErrorRegex.test(line)
);
const compilationComplete = clearLines.some(line => compilationCompleteRegex.test(line));
function detectState(line) {
const clearLine = stripAnsi(line);
const newCompilation = newCompilationRegex.test(clearLine);
const compilationError =
compilationCompleteWithErrorRegex.test(clearLine) ||
typescriptErrorRegex.test(clearLine) ||
typescriptPrettyErrorRegex.test(clearLine);
const compilationComplete = compilationCompleteRegex.test(clearLine);

return {
newCompilation: newCompilation,
Expand Down
15 changes: 10 additions & 5 deletions lib/tsc-watch.js
Expand Up @@ -6,6 +6,7 @@ const spawn = require('cross-spawn');
const run = require('./runner');
const { extractArgs } = require('./args-manager');
const { manipulate, detectState, isClear, print } = require('./stdout-manipulator');
const readline = require('readline');

let firstTime = true;
let firstSuccessKiller = null;
Expand Down Expand Up @@ -46,14 +47,18 @@ try {

let compilationErrorSinceStart = false;
const tscProcess = spawn(bin, [...args]);
tscProcess.stdout.on('data', buffer => {
if (noClear && isClear(buffer)) {
const rl = readline.createInterface({
input: tscProcess.stdout,
});

rl.on('line', function(input) {
if (noClear && isClear(input)) {
return;
}

const lines = manipulate(buffer);
print(noColors, noClear, lines);
const state = detectState(lines);
const line = manipulate(input);
print(noColors, noClear, line);
const state = detectState(line);
const newCompilation = state.newCompilation;
const compilationError = state.compilationError;
const compilationComplete = state.compilationComplete;
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "tsc-watch",
"version": "4.1.1",
"version": "4.2.0-rc1",
"description": "The TypeScript compiler with onSuccess command",
"scripts": {
"publish": "node release.js",
Expand Down

0 comments on commit 8641cc4

Please sign in to comment.