angular/packages/localize/tools/src/diagnostics.ts
Paul Gschwendtner a18981ab7b refactor: move localize/src/tools into localize/tools folder (#43431)
Moves the `src/tools` folder of the `@angular/localize` package into the
top-level of the package. This is in preparation of actually exposing an
entry-point for the tools that can be accessed using
`@angular/localize/tools`.

We want to expose such an entry-point because the CLI currently
deep-imports into various places of the tools, but this will not
work well with strict ESM because the localize tool depends on the
v13 strict ESM packages like the `@angular/compiler` or
`@angular/compiler-cli`.

PR Close #43431
2021-10-01 18:28:44 +00:00

50 lines
1.4 KiB
TypeScript

/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
/**
* How to handle potential diagnostics.
*/
export type DiagnosticHandlingStrategy = 'error'|'warning'|'ignore';
/**
* This class is used to collect and then report warnings and errors that occur during the execution
* of the tools.
*
* @publicApi used by CLI
*/
export class Diagnostics {
readonly messages: {type: 'warning'|'error', message: string}[] = [];
get hasErrors() {
return this.messages.some(m => m.type === 'error');
}
add(type: DiagnosticHandlingStrategy, message: string) {
if (type !== 'ignore') {
this.messages.push({type, message});
}
}
warn(message: string) {
this.messages.push({type: 'warning', message});
}
error(message: string) {
this.messages.push({type: 'error', message});
}
merge(other: Diagnostics) {
this.messages.push(...other.messages);
}
formatDiagnostics(message: string): string {
const errors = this.messages.filter(d => d.type === 'error').map(d => ' - ' + d.message);
const warnings = this.messages.filter(d => d.type === 'warning').map(d => ' - ' + d.message);
if (errors.length) {
message += '\nERRORS:\n' + errors.join('\n');
}
if (warnings.length) {
message += '\nWARNINGS:\n' + warnings.join('\n');
}
return message;
}
}