feat(cli): implement the "list" command for listing migration history and pending migrations

This commit is contained in:
Joakim Carlstein 2023-11-22 14:19:10 +01:00
parent e79dd4bca9
commit 53cdb23237
8 changed files with 199 additions and 38 deletions

View file

@ -0,0 +1,28 @@
import path from 'node:path';
import fs from 'node:fs/promises';
import { type MigrationMetadata } from '@emigrate/plugin-tools/types';
import { withLeadingPeriod } from './with-leading-period.js';
export const getMigrations = async (cwd: string, directory: string): Promise<MigrationMetadata[]> => {
const allFilesInMigrationDirectory = await fs.readdir(path.resolve(cwd, directory), {
withFileTypes: true,
});
const migrationFiles: MigrationMetadata[] = allFilesInMigrationDirectory
.filter((file) => file.isFile() && !file.name.startsWith('.') && !file.name.startsWith('_'))
.sort((a, b) => a.name.localeCompare(b.name))
.map(({ name }) => {
const filePath = path.resolve(cwd, directory, name);
return {
name,
filePath,
relativeFilePath: path.relative(cwd, filePath),
extension: withLeadingPeriod(path.extname(name)),
directory,
cwd,
};
});
return migrationFiles;
};