Skip to content

Commit

Permalink
yarn watch: save errors in separate files, make error regex multiline…
Browse files Browse the repository at this point in the history
… aware
  • Loading branch information
aeschli committed Nov 20, 2020
1 parent 1273299 commit 542a827
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 47 deletions.
2 changes: 1 addition & 1 deletion build/gulpfile.editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ function createTscCompileTask(watch) {
// stdio: [null, 'pipe', 'inherit']
});
let errors = [];
let reporter = createReporter();
let reporter = createReporter('monaco');
let report;
// eslint-disable-next-line no-control-regex
let magic = /[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g; // https://stackoverflow.com/questions/25245716/remove-all-ansi-colors-styles-from-strings
Expand Down
2 changes: 1 addition & 1 deletion build/gulpfile.extensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ const tasks = compilations.map(function (tsconfigFile) {
}

function createPipeline(build, emitError) {
const reporter = createReporter();
const reporter = createReporter('extensions');

overrideOptions.inlineSources = Boolean(build);
overrideOptions.base = path.dirname(absolutePath);
Expand Down
107 changes: 62 additions & 45 deletions build/lib/reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,61 +12,76 @@ import * as ansiColors from 'ansi-colors';
import * as fs from 'fs';
import * as path from 'path';

const allErrors: string[][] = [];
let startTime: number | null = null;
let count = 0;
class ErrorLog {
constructor(public id: string) {
}
allErrors: string[][] = [];
startTime: number | null = null;
count = 0;

onStart(): void {
if (this.count++ > 0) {
return;
}

function onStart(): void {
if (count++ > 0) {
return;
this.startTime = new Date().getTime();
fancyLog(`Starting ${ansiColors.green('compilation')}${this.id ? ansiColors.blue(` ${this.id}`) : ''}...`);
}

startTime = new Date().getTime();
fancyLog(`Starting ${ansiColors.green('compilation')}...`);
}
onEnd(): void {
if (--this.count > 0) {
return;
}

function onEnd(): void {
if (--count > 0) {
return;
this.log();
}

log();
}
log(): void {
const errors = _.flatten(this.allErrors);
const seen = new Set<string>();

const buildLogPath = path.join(path.dirname(path.dirname(__dirname)), '.build', 'log');
errors.map(err => {
if (!seen.has(err)) {
seen.add(err);
fancyLog(`${ansiColors.red('Error')}: ${err}`);
}
});

try {
fs.mkdirSync(path.dirname(buildLogPath));
} catch (err) {
// ignore
}
fancyLog(`Finished ${ansiColors.green('compilation')}${this.id ? ansiColors.blue(` ${this.id}`) : ''} with ${errors.length} errors after ${ansiColors.magenta((new Date().getTime() - this.startTime!) + ' ms')}`);

function log(): void {
const errors = _.flatten(allErrors);
const seen = new Set<string>();
const regex = /^([^(]+)\((\d+),(\d+)\): (.*)$/s;
const messages = errors
.map(err => regex.exec(err))
.filter(match => !!match)
.map(x => x as string[])
.map(([, path, line, column, message]) => ({ path, line: parseInt(line), column: parseInt(column), message }));

errors.map(err => {
if (!seen.has(err)) {
seen.add(err);
fancyLog(`${ansiColors.red('Error')}: ${err}`);
try {
const logFileName = 'log' + (this.id ? `_${this.id}` : '');
fs.writeFileSync(path.join(buildLogFolder, logFileName), JSON.stringify(messages));
} catch (err) {
//noop
}
});

const regex = /^([^(]+)\((\d+),(\d+)\): (.*)$/;
const messages = errors
.map(err => regex.exec(err))
.filter(match => !!match)
.map(x => x as string[])
.map(([, path, line, column, message]) => ({ path, line: parseInt(line), column: parseInt(column), message }));
}

try {
}

fs.writeFileSync(buildLogPath, JSON.stringify(messages));
} catch (err) {
//noop
const errorLogsById = new Map<string, ErrorLog>();
function getErrorLog(id: string = '') {
let errorLog = errorLogsById.get(id);
if (!errorLog) {
errorLog = new ErrorLog(id);
errorLogsById.set(id, errorLog);
}
return errorLog;
}

fancyLog(`Finished ${ansiColors.green('compilation')} with ${errors.length} errors after ${ansiColors.magenta((new Date().getTime() - startTime!) + ' ms')}`);
const buildLogFolder = path.join(path.dirname(path.dirname(__dirname)), '.build');

try {
fs.mkdirSync(buildLogFolder);
} catch (err) {
// ignore
}

export interface IReporter {
Expand All @@ -75,24 +90,26 @@ export interface IReporter {
end(emitError: boolean): NodeJS.ReadWriteStream;
}

export function createReporter(): IReporter {
export function createReporter(id?: string): IReporter {
const errorLog = getErrorLog(id);

const errors: string[] = [];
allErrors.push(errors);
errorLog.allErrors.push(errors);

const result = (err: string) => errors.push(err);

result.hasErrors = () => errors.length > 0;

result.end = (emitError: boolean): NodeJS.ReadWriteStream => {
errors.length = 0;
onStart();
errorLog.onStart();

return es.through(undefined, function () {
onEnd();
errorLog.onEnd();

if (emitError && errors.length > 0) {
if (!(errors as any).__logged__) {
log();
errorLog.log();
}

(errors as any).__logged__ = true;
Expand Down

0 comments on commit 542a827

Please sign in to comment.