feat(reporters): add built-in "json" reporter and rename "default" to "pretty"
This commit is contained in:
parent
4e8ac5294d
commit
18382ce961
11 changed files with 102 additions and 16 deletions
14
packages/cli/src/reporters/get.ts
Normal file
14
packages/cli/src/reporters/get.ts
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
import { type Config } from '../types.js';
|
||||
import * as reporters from './index.js';
|
||||
|
||||
export const getStandardReporter = (reporter?: Config['reporter']) => {
|
||||
if (!reporter) {
|
||||
return reporters.pretty;
|
||||
}
|
||||
|
||||
if (typeof reporter === 'string' && reporter in reporters) {
|
||||
return reporters[reporter as keyof typeof reporters];
|
||||
}
|
||||
|
||||
return; // eslint-disable-line no-useless-return
|
||||
};
|
||||
2
packages/cli/src/reporters/index.ts
Normal file
2
packages/cli/src/reporters/index.ts
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
export { default as pretty } from './default.js';
|
||||
export { default as json } from './json.js';
|
||||
60
packages/cli/src/reporters/json.ts
Normal file
60
packages/cli/src/reporters/json.ts
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
import { type ReporterInitParameters, type EmigrateReporter, type MigrationMetadataFinished } from '@emigrate/types';
|
||||
import { toSerializedError } from '../errors.js';
|
||||
|
||||
class JsonReporter implements EmigrateReporter {
|
||||
#parameters!: ReporterInitParameters;
|
||||
#startTime!: number;
|
||||
|
||||
onInit(parameters: ReporterInitParameters): void {
|
||||
this.#startTime = Date.now();
|
||||
this.#parameters = parameters;
|
||||
}
|
||||
|
||||
onFinished(migrations: MigrationMetadataFinished[], error?: Error | undefined): void {
|
||||
const { command, version } = this.#parameters;
|
||||
|
||||
let numberDoneMigrations = 0;
|
||||
let numberSkippedMigrations = 0;
|
||||
let numberFailedMigrations = 0;
|
||||
let numberPendingMigrations = 0;
|
||||
|
||||
for (const migration of migrations) {
|
||||
// eslint-disable-next-line unicorn/prefer-switch
|
||||
if (migration.status === 'done') {
|
||||
numberDoneMigrations++;
|
||||
} else if (migration.status === 'skipped') {
|
||||
numberSkippedMigrations++;
|
||||
} else if (migration.status === 'failed') {
|
||||
numberFailedMigrations++;
|
||||
} else {
|
||||
numberPendingMigrations++;
|
||||
}
|
||||
}
|
||||
|
||||
const result = {
|
||||
command,
|
||||
version,
|
||||
numberTotalMigrations: migrations.length,
|
||||
numberDoneMigrations,
|
||||
numberSkippedMigrations,
|
||||
numberFailedMigrations,
|
||||
numberPendingMigrations,
|
||||
success: !error,
|
||||
startTime: this.#startTime,
|
||||
endTime: Date.now(),
|
||||
error: error ? toSerializedError(error) : undefined,
|
||||
migrations: migrations.map((migration) => ({
|
||||
name: migration.filePath,
|
||||
status: migration.status,
|
||||
duration: 'duration' in migration ? migration.duration : 0,
|
||||
error: 'error' in migration ? toSerializedError(migration.error) : undefined,
|
||||
})),
|
||||
};
|
||||
|
||||
console.log(JSON.stringify(result, undefined, 2));
|
||||
}
|
||||
}
|
||||
|
||||
const jsonReporter = new JsonReporter() as EmigrateReporter;
|
||||
|
||||
export default jsonReporter;
|
||||
Loading…
Add table
Add a link
Reference in a new issue