Skip to content

Commit

Permalink
support default export
Browse files Browse the repository at this point in the history
Signed-off-by: Matteo Collina <[email protected]>
  • Loading branch information
mcollina committed Mar 8, 2024
1 parent 58819de commit e4f7d8b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
28 changes: 19 additions & 9 deletions src/lib/semgrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,25 @@ async function* loadMigrationsFromPath<Input, Output>(
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<Input, Output>
}),
)
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<Input, Output>
} else if (
module.default?.version &&
typeof module.default?.up === 'function'
) {
// Casted, there is nothing type safe here
return module.default as Migration<Input, Output>
}
}),
)
).filter(migration => !!migration) as Migration<Input, Output>[]

migrations.sort((a, b) => semver.compare(a.version, b.version))

Expand Down
4 changes: 3 additions & 1 deletion src/test/fixtures/plt/0.17.x.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import type { Migration } from '../../../lib/semgrator.js'
import type { Order } from '../order.js'

export const migration: Migration<Order> = {
const migration: Migration<Order> = {
version: '0.17.0',
up: (input: Order) => {
input.order.push('0.17.0')
return input
},
}

export default migration

0 comments on commit e4f7d8b

Please sign in to comment.