From e4f7d8bf50b33fb7d343ebb02516aa0ebcd56923 Mon Sep 17 00:00:00 2001 From: Matteo Collina Date: Fri, 8 Mar 2024 15:37:06 +0100 Subject: [PATCH] support default export Signed-off-by: Matteo Collina --- src/lib/semgrator.ts | 28 +++++++++++++++++++--------- src/test/fixtures/plt/0.17.x.ts | 4 +++- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/src/lib/semgrator.ts b/src/lib/semgrator.ts index c4b6ae3..c5ec7f0 100644 --- a/src/lib/semgrator.ts +++ b/src/lib/semgrator.ts @@ -59,15 +59,25 @@ async function* loadMigrationsFromPath( file.match(/\.(c|m)?js$/), ) - const migrations = await Promise.all( - files.map(async file => { - const module = await import( - pathToFileURL(join(path, file)).toString() - ) - // Casted, there is nothing type safe here - return module.migration as Migration - }), - ) + const migrations = ( + await Promise.all( + files.map(async file => { + const module = await import( + pathToFileURL(join(path, file)).toString() + ) + if (module.migration) { + // Casted, there is nothing type safe here + return module.migration as Migration + } else if ( + module.default?.version && + typeof module.default?.up === 'function' + ) { + // Casted, there is nothing type safe here + return module.default as Migration + } + }), + ) + ).filter(migration => !!migration) as Migration[] migrations.sort((a, b) => semver.compare(a.version, b.version)) diff --git a/src/test/fixtures/plt/0.17.x.ts b/src/test/fixtures/plt/0.17.x.ts index 2b7c653..12f4485 100644 --- a/src/test/fixtures/plt/0.17.x.ts +++ b/src/test/fixtures/plt/0.17.x.ts @@ -1,10 +1,12 @@ import type { Migration } from '../../../lib/semgrator.js' import type { Order } from '../order.js' -export const migration: Migration = { +const migration: Migration = { version: '0.17.0', up: (input: Order) => { input.order.push('0.17.0') return input }, } + +export default migration