Skip to content

Commit

Permalink
Added changed field in response
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 f54de96 commit 86e21ad
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ const res = await semgrator<MyConfig, MyConfig>({
})

console.log(res.result)
console.log(res.changed)
console.log(res.version)
```

### Getting intermediate results
Expand Down
5 changes: 4 additions & 1 deletion src/lib/semgrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ interface SemgratorParamsWithPath<Input>

interface SemgratorResult<Output> {
version: string
changed: boolean
result: Output
}

Expand Down Expand Up @@ -81,14 +82,16 @@ async function* processMigrations<Input, Output>(
): AsyncGenerator<SemgratorResult<Output>> {
let result = input as unknown
let lastVersion = version
let changed = false

for await (const migration of migrations) {
if (semver.gt(migration.version, lastVersion)) {
// @ts-expect-error
result = await migration.up(result)
lastVersion = migration.toVersion || migration.version
changed = true
}
yield { version: lastVersion, result: result as Output }
yield { version: lastVersion, result: result as Output, changed }
}
}

Expand Down
6 changes: 4 additions & 2 deletions src/test/semgrator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ type SeenBy = {
}

test('apply all migrations', async t => {
const plan = tspl(t, { plan: 3 })
const plan = tspl(t, { plan: 4 })
const m1: Migration<SeenBy> = {
version: '2.0.0',
async up(input) {
Expand Down Expand Up @@ -43,6 +43,7 @@ test('apply all migrations', async t => {
})

plan.equal(res.version, '2.4.0')
plan.equal(res.changed, true)
plan.deepEqual(res.result, {
foo: 'bar',
seenBy: '2.4.0',
Expand Down Expand Up @@ -91,7 +92,7 @@ test('apply only needed', async t => {
})

test('apply no migrations', async t => {
const plan = tspl(t, { plan: 2 })
const plan = tspl(t, { plan: 3 })
const m1: Migration<SeenBy> = {
version: '2.0.0',
async up(input) {
Expand Down Expand Up @@ -123,6 +124,7 @@ test('apply no migrations', async t => {
})

plan.equal(res.version, '2.30.1')
plan.equal(res.changed, false)
plan.deepEqual(res.result, {
foo: 'bar',
})
Expand Down

0 comments on commit 86e21ad

Please sign in to comment.