UNPKG

@emigrate/cli

Version:

Emigrate is a tool for managing database migrations. It is designed to be simple yet support advanced setups, modular and extensible.

36 lines 1.33 kB
import path from 'node:path'; import fs from 'node:fs/promises'; import { withLeadingPeriod } from './with-leading-period.js'; import { BadOptionError } from './errors.js'; import { arrayFromAsync } from './array-from-async.js'; async function* tryReadDirectory(directoryPath) { try { for await (const entry of await fs.opendir(directoryPath)) { if (entry.isFile() && !entry.name.startsWith('.') && !entry.name.startsWith('_') && path.extname(entry.name) !== '') { yield entry.name; } } } catch { throw BadOptionError.fromOption('directory', `Couldn't read directory: ${directoryPath}`); } } export const getMigrations = async (cwd, directory) => { const directoryPath = path.resolve(cwd, directory); const allFilesInMigrationDirectory = await arrayFromAsync(tryReadDirectory(directoryPath)); return allFilesInMigrationDirectory.sort().map((name) => { const filePath = path.join(directoryPath, name); return { name, filePath, relativeFilePath: path.relative(cwd, filePath), extension: withLeadingPeriod(path.extname(name)), directory, cwd, }; }); }; //# sourceMappingURL=get-migrations.js.map