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 { BadOptionError, MissingOptionError, StorageInitError } from '../errors.js';
import { BadOptionError, MissingOptionError, StorageInitError, toError } from '../errors.js';
import { type Config } from '../types.js';
import { exec } from '../exec.js';
import { migrationRunner } from '../migration-runner.js';
@ -12,20 +12,20 @@ const lazyDefaultReporter = async () => import('../reporters/default.js');
export default async function listCommand({ directory, reporter: reporterConfig, storage: storageConfig }: Config) {
if (!directory) {
throw new MissingOptionError('directory');
throw MissingOptionError.fromOption('directory');
}
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',
);
@ -36,25 +36,33 @@ export default async function listCommand({ directory, reporter: reporterConfig,
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;
}
const collectedMigrations = collectMigrations(cwd, directory, storage.getHistory());
try {
const collectedMigrations = collectMigrations(cwd, directory, storage.getHistory());
const error = await migrationRunner({
dry: true,
reporter,
storage,
migrations: await arrayFromAsync(collectedMigrations),
async validate() {
// No-op
},
async execute() {
throw new Error('Unexpected execute call');
},
});
const error = await migrationRunner({
dry: true,
reporter,
storage,
migrations: await arrayFromAsync(collectedMigrations),
async validate() {
// No-op
},
async execute() {
throw new Error('Unexpected execute call');
},
});
return error ? 1 : 0;
return error ? 1 : 0;
} catch (error) {
await reporter.onFinished?.([], toError(error));
return 1;
} finally {
await storage.end();
}
}