refactor(cli): introduce the migration-runner helper for less code duplication and fewer return paths
Thanks to the migration-runner the "up" and "list" commands are now very similar code wise
This commit is contained in:
parent
5307e87242
commit
8cc43a8f83
9 changed files with 332 additions and 294 deletions
33
packages/cli/src/collect-migrations.ts
Normal file
33
packages/cli/src/collect-migrations.ts
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
import {
|
||||
type MigrationHistoryEntry,
|
||||
type MigrationMetadata,
|
||||
type MigrationMetadataFinished,
|
||||
} from '@emigrate/plugin-tools/types';
|
||||
import { toMigrationMetadata } from './to-migration-metadata.js';
|
||||
import { getMigrations as getMigrationsOriginal } from './get-migrations.js';
|
||||
|
||||
export async function* collectMigrations(
|
||||
cwd: string,
|
||||
directory: string,
|
||||
history: AsyncIterable<MigrationHistoryEntry>,
|
||||
getMigrations = getMigrationsOriginal,
|
||||
): AsyncIterable<MigrationMetadata | MigrationMetadataFinished> {
|
||||
const allMigrations = await getMigrations(cwd, directory);
|
||||
const seen = new Set<string>();
|
||||
|
||||
for await (const entry of history) {
|
||||
const index = allMigrations.findIndex((migrationFile) => migrationFile.name === entry.name);
|
||||
|
||||
if (index === -1) {
|
||||
continue;
|
||||
}
|
||||
|
||||
yield toMigrationMetadata(entry, { cwd, directory });
|
||||
|
||||
seen.add(entry.name);
|
||||
}
|
||||
|
||||
yield* allMigrations.filter((migration) => !seen.has(migration.name));
|
||||
|
||||
seen.clear();
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue