feat(reporter): print Emigrate CLI's version number and relative paths to migrations (#39)

* feat(reporter-default): print CLI version number

* feat(reporter-default): print relative paths to migrations instead of only the file names

This makes the output clickable in most shells

* feat(reporter-pino): include the Emigrate CLI version in each log
This commit is contained in:
Joakim Carlstein 2023-12-14 13:45:02 +01:00 committed by GitHub
parent 480796e95b
commit 1434be5d5e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 28 additions and 9 deletions

View file

@ -0,0 +1,5 @@
---
'@emigrate/reporter-pino': minor
---
Include the Emigrate CLI's version number in each log

View file

@ -0,0 +1,5 @@
---
'@emigrate/cli': minor
---
The default reporter now prints the relative path instead of only the migration file name when logging migrations. Thanks to this most shells supports opening the corresponding migration file by clicking it.

View file

@ -0,0 +1,5 @@
---
'@emigrate/cli': minor
---
Print Emigrate CLI version when using the default reporter

View file

@ -1,4 +1,3 @@
import path from 'node:path';
import { black, blueBright, bold, cyan, dim, faint, gray, green, red, redBright, yellow } from 'ansis'; import { black, blueBright, bold, cyan, dim, faint, gray, green, red, redBright, yellow } from 'ansis';
import logUpdate from 'log-update'; import logUpdate from 'log-update';
import elegantSpinner from 'elegant-spinner'; import elegantSpinner from 'elegant-spinner';
@ -25,10 +24,10 @@ const formatDuration = (duration: number): string => {
return yellow(pretty.replaceAll(/([^\s\d]+)/g, dim('$1'))); return yellow(pretty.replaceAll(/([^\s\d]+)/g, dim('$1')));
}; };
const getTitle = ({ command, directory, dry, cwd }: ReporterInitParameters) => { const getTitle = ({ command, version, dry, cwd }: ReporterInitParameters) => {
return `${black.bgBlueBright(' Emigrate ').trim()} ${blueBright.bold(command)} ${gray(cwd + path.sep)}${directory}${ return `${black.bgBlueBright(' Emigrate ').trim()} ${blueBright.bold(command)} ${blueBright(`v${version}`)} ${gray(
dry ? yellow` (dry run)` : '' cwd,
}`; )}${dry ? yellow` (dry run)` : ''}`;
}; };
const getMigrationStatus = ( const getMigrationStatus = (
@ -94,11 +93,12 @@ const getMigrationText = (
migration: MigrationMetadata | MigrationMetadataFinished, migration: MigrationMetadata | MigrationMetadataFinished,
activeMigration?: MigrationMetadata, activeMigration?: MigrationMetadata,
) => { ) => {
const pathWithoutName = migration.relativeFilePath.slice(0, -migration.name.length);
const nameWithoutExtension = migration.name.slice(0, -migration.extension.length); const nameWithoutExtension = migration.name.slice(0, -migration.extension.length);
const status = getMigrationStatus(migration, activeMigration); const status = getMigrationStatus(migration, activeMigration);
const parts = [' ', getIcon(status)]; const parts = [' ', getIcon(status)];
parts.push(`${getName(nameWithoutExtension, status)}${dim(migration.extension)}`); parts.push(`${dim(pathWithoutName)}${getName(nameWithoutExtension, status)}${dim(migration.extension)}`);
if ('status' in migration) { if ('status' in migration) {
parts.push(gray(`(${migration.status})`)); parts.push(gray(`(${migration.status})`));

View file

@ -42,7 +42,7 @@ class PinoReporter implements Required<EmigrateReporter> {
return this.options.errorKey ?? 'error'; return this.options.errorKey ?? 'error';
} }
onInit({ command, ...parameters }: ReporterInitParameters): Awaitable<void> { onInit({ command, version, ...parameters }: ReporterInitParameters): Awaitable<void> {
this.#command = command; this.#command = command;
this.#logger = pino({ this.#logger = pino({
name: 'emigrate', name: 'emigrate',
@ -50,6 +50,7 @@ class PinoReporter implements Required<EmigrateReporter> {
errorKey: this.errorKey, errorKey: this.errorKey,
base: { base: {
scope: command, scope: command,
version,
}, },
}); });
@ -171,11 +172,14 @@ class PinoReporter implements Required<EmigrateReporter> {
if (error) { if (error) {
this.#logger.error( this.#logger.error(
{ failed, done, skipped, pending, total, [this.errorKey]: error }, { result: { failed, done, skipped, pending, total }, [this.errorKey]: error },
`Emigrate "${this.#command}" failed`, `Emigrate "${this.#command}" failed`,
); );
} else { } else {
this.#logger.info({ failed, done, skipped, pending, total }, `Emigrate "${this.#command}" finished successfully`); this.#logger.info(
{ result: { failed, done, skipped, pending, total } },
`Emigrate "${this.#command}" finished successfully`,
);
} }
} }
} }