feat(types): move Emigrate types to separate package and improve types (#41)

* feat(types): move Emigrate types to separate package

Also refactor the types to use discriminating unions for easier error handling and such.
Errors passed to storage plugins should now be serialized and storage plugins are expected to return already serialized errors on failed history entries.

* fix(mysql): handle the new type changes

* fix(storage-fs): handle the new type changes

* feat(cli): better error handling and types

Adapt to the new types from the @emigrate/types package, like discriminating union types and serializing and deserializing errors
This commit is contained in:
Joakim Carlstein 2023-12-15 13:03:35 +01:00 committed by GitHub
parent afe56594c5
commit cae6d11d53
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
38 changed files with 630 additions and 259 deletions

View file

@ -1,6 +1,6 @@
import process from 'node:process';
import { getOrLoadReporter, getOrLoadStorage } from '@emigrate/plugin-tools';
import { type MigrationHistoryEntry, type MigrationMetadataFinished } from '@emigrate/plugin-tools/types';
import { type MigrationHistoryEntry, type MigrationMetadataFinished } from '@emigrate/types';
import {
BadOptionError,
MigrationNotRunError,
@ -26,24 +26,24 @@ export default async function removeCommand(
name: string,
) {
if (!directory) {
throw new MissingOptionError('directory');
throw MissingOptionError.fromOption('directory');
}
if (!name) {
throw new MissingArgumentsError('name');
throw MissingArgumentsError.fromArgument('name');
}
const cwd = process.cwd();
const storagePlugin = await getOrLoadStorage([storageConfig]);
if (!storagePlugin) {
throw new BadOptionError('storage', 'No storage found, please specify a storage using the storage option');
throw BadOptionError.fromOption('storage', 'No storage found, please specify a storage using the storage option');
}
const reporter = await getOrLoadReporter([reporterConfig ?? lazyDefaultReporter]);
if (!reporter) {
throw new BadOptionError(
throw BadOptionError.fromOption(
'reporter',
'No reporter found, please specify an existing reporter using the reporter option',
);
@ -52,14 +52,22 @@ export default async function removeCommand(
const [storage, storageError] = await exec(async () => storagePlugin.initializeStorage());
if (storageError) {
await reporter.onFinished?.([], new StorageInitError('Could not initialize storage', { cause: storageError }));
await reporter.onFinished?.([], StorageInitError.fromError(storageError));
return 1;
}
await reporter.onInit?.({ command: 'remove', version, cwd, dry: false, directory });
const migrationFile = await getMigration(cwd, directory, name, !force);
const [migrationFile, fileError] = await exec(async () => getMigration(cwd, directory, name, !force));
if (fileError) {
await reporter.onFinished?.([], fileError);
await storage.end();
return 1;
}
const finishedMigrations: MigrationMetadataFinished[] = [];
let historyEntry: MigrationHistoryEntry | undefined;
@ -71,7 +79,7 @@ export default async function removeCommand(
}
if (migrationHistoryEntry.status === 'done' && !force) {
removalError = new OptionNeededError(
removalError = OptionNeededError.fromOption(
'force',
`The migration "${migrationFile.name}" is not in a failed state. Use the "force" option to force its removal`,
);
@ -98,10 +106,7 @@ export default async function removeCommand(
removalError = error instanceof Error ? error : new Error(String(error));
}
} else if (!removalError) {
removalError = new MigrationNotRunError(
`Migration "${migrationFile.name}" is not in the migration history`,
migrationFile,
);
removalError = MigrationNotRunError.fromMetadata(migrationFile);
}
if (removalError) {