feat(storage): add "end" method to storage for cleaning up resources when commands are finished

This commit is contained in:
Joakim Carlstein 2023-12-08 13:01:19 +01:00
parent 334e2099bb
commit 703e6f028a
15 changed files with 150 additions and 77 deletions

View file

@ -2,10 +2,11 @@ import process from 'node:process';
import path from 'node:path';
import { getOrLoadReporter, getOrLoadStorage } from '@emigrate/plugin-tools';
import { type MigrationMetadataFinished } from '@emigrate/plugin-tools/types';
import { BadOptionError, MigrationHistoryError, MissingOptionError } from '../errors.js';
import { BadOptionError, MigrationHistoryError, MissingOptionError, StorageInitError } from '../errors.js';
import { type Config } from '../types.js';
import { withLeadingPeriod } from '../with-leading-period.js';
import { getMigrations } from '../get-migrations.js';
import { exec } from '../exec.js';
const lazyDefaultReporter = async () => import('../reporters/default.js');
@ -21,7 +22,6 @@ export default async function listCommand({ directory, reporter: reporterConfig,
throw new BadOptionError('storage', 'No storage found, please specify a storage using the storage option');
}
const storage = await storagePlugin.initializeStorage();
const reporter = await getOrLoadReporter([reporterConfig ?? lazyDefaultReporter]);
if (!reporter) {
@ -33,6 +33,14 @@ export default async function listCommand({ directory, reporter: reporterConfig,
await reporter.onInit?.({ command: 'list', cwd, dry: false, directory });
const [storage, storageError] = await exec(async () => storagePlugin.initializeStorage());
if (storageError) {
await reporter.onFinished?.([], new StorageInitError('Could not initialize storage', { cause: storageError }));
return 1;
}
const migrationFiles = await getMigrations(cwd, directory);
let migrationHistoryError: MigrationHistoryError | undefined;
@ -82,4 +90,8 @@ export default async function listCommand({ directory, reporter: reporterConfig,
}
await reporter.onFinished?.(finishedMigrations, migrationHistoryError);
await storage.end();
return migrationHistoryError ? 1 : 0;
}