-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(cli): introduce the migration-runner helper for less code du…
…plication and fewer return paths Thanks to the migration-runner the "up" and "list" commands are now very similar code wise
- Loading branch information
1 parent
5307e87
commit 8cc43a8
Showing
9 changed files
with
334 additions
and
296 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/** | ||
* This is a simple polyfill for [Array.fromAsync()](https://github.com/tc39/proposal-array-from-async) | ||
* | ||
* It converts an async iterable to an array. | ||
*/ | ||
export const arrayFromAsync = async <T>(iterable: AsyncIterable<T>): Promise<T[]> => { | ||
const array: T[] = []; | ||
|
||
for await (const item of iterable) { | ||
array.push(item); | ||
} | ||
|
||
return array; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.