Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions examples/7-bundling-codegen/codegen.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import {readdirSync, writeFileSync} from 'node:fs'

const migrationsDir = new URL('./migrations/', import.meta.url)

const migrations = readdirSync(migrationsDir)
.filter(file => file.endsWith('.ts') && file !== 'barrel.ts')
.sort()
.map((file, index) => {
const modulePath = `./${file.slice(0, -'.ts'.length)}`
return {
identifier: `migration${index}_${toIdentifierSuffix(modulePath)}`,
modulePath,
}
})

const imports = migrations.map(migration => {
return `import * as ${migration.identifier} from '${migration.modulePath}'`
})

const entries = migrations.map(migration => {
return ` "${migration.modulePath}": ${migration.identifier}`
})

writeFileSync(
new URL('./barrel.ts', migrationsDir),
[
'// This file is generated by `npm run codegen`.',
...imports,
'',
'export const migrations = {',
entries.join(',\n'),
'}',
'',
].join('\n'),
)

function toIdentifierSuffix(modulePath) {
return modulePath
.replace(/[^a-zA-Z0-9]+/g, ' ')
.trim()
.split(' ')
.map(part => `${part[0].toUpperCase()}${part.slice(1)}`)
.join('')
}
10 changes: 0 additions & 10 deletions examples/7-bundling-codegen/eslint.config.js

This file was deleted.

16 changes: 5 additions & 11 deletions examples/7-bundling-codegen/migrations/barrel.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
// The content in this file is autogenerated by eslint. So any IDE with an eslint extension will automatically
// sync it, or a lint task such as `yarn lint --fix`.
// Ideally there should be a CI job that runs `yarn lint` - and will fail if it ever does get out of date.
// See https://npmjs.com/package/eslint-plugin-codegen for more details.

// codegen:start {preset: barrel, include: './*.ts', import: star, export: {name: migrations, keys: path}}
import * as _20201209T192431UsersTable from './2020.12.09T19.24.31.users-table'
import * as _20201209T192509RolesTable from './2020.12.09T19.25.09.roles-table'
// This file is generated by `npm run codegen`.
import * as migration0_20201209T192431UsersTable from './2020.12.09T19.24.31.users-table'
import * as migration1_20201209T192509RolesTable from './2020.12.09T19.25.09.roles-table'

export const migrations = {
"./2020.12.09T19.24.31.users-table": _20201209T192431UsersTable,
"./2020.12.09T19.25.09.roles-table": _20201209T192509RolesTable
"./2020.12.09T19.24.31.users-table": migration0_20201209T192431UsersTable,
"./2020.12.09T19.25.09.roles-table": migration1_20201209T192509RolesTable
}
// codegen:end
9 changes: 3 additions & 6 deletions examples/7-bundling-codegen/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@
"name": "umzug-bundling-example",
"version": "0.0.0",
"scripts": {
"build": "tsup umzug.ts --external pg-hstore",
"lint": "eslint ."
"build": "npm run codegen && esbuild umzug.ts --bundle --platform=node --outfile=dist/umzug.js --external:pg-hstore",
"codegen": "node codegen.mjs"
},
"devDependencies": {
"eslint": "8.57.0",
"eslint-plugin-codegen": "0.28.0",
"tsup": "8.0.2",
"typescript-eslint": "7.3.1"
"esbuild": "0.28.0"
}
}
14 changes: 7 additions & 7 deletions examples/7-bundling-codegen/readme.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
This example shows how Umzug can be used with a bundler like tsup, webpack, parcel, turbopack, microbundle, ncc, bun, esbuild, swc, etc. This scenario isn't really what Umzug was designed for, so it depends on the help of codegen tool.
This example shows how Umzug can be used with a bundler like esbuild, webpack, parcel, turbopack, microbundle, ncc, bun, swc, etc. This scenario isn't really what Umzug was designed for, so it depends on the help of a codegen tool.

Since we're going to be bundling this package, (maybe with the purpose of running migrations in another environment), we can't rely on "globbing" the filesystem. Here, [eslint-plugin-codegen](https://npmjs.com/package/eslint-plugin-codegen) is used to glob for the files when the linter runs, and barrel them into an object (see [barrel.ts](./barrel.ts)). That object can then be passed to the Umzug constructor directly as a list of migrations (see [umzug.ts](./umzug.ts)).
Since we're going to be bundling this package, (maybe with the purpose of running migrations in another environment), we can't rely on "globbing" the filesystem. Here, [a small codegen script](./codegen.mjs) is used to glob for the files before bundling, and barrel them into an object (see [barrel.ts](./barrel.ts)). That object can then be passed to the Umzug constructor directly as a list of migrations (see [umzug.ts](./umzug.ts)).

When a new migration file is added, the linter can ensure it is added to the barrel by running `eslint . --fix`.
When a new migration file is added, the codegen script can ensure it is added to the barrel by running `npm run codegen`.

To try out this example, which uses `tsup`, first install dependencies, then bundle and run the migrator:
To try out this example, which uses `esbuild`, first install dependencies, then bundle and run the migrator:

```bash
npm install
npm run lint -- --fix # makes sure barrel is up to date
npm run codegen # makes sure barrel is up to date
npm run build

node dist/umzug up # apply migrations

node dist/umzug create --name new-migration.ts --skip-verify # create a new migration file
npm run lint -- --fix # makes sure barrel is up to date
npm run codegen # makes sure barrel is up to date
```

Since the codegen lint plugin just creates a simple JavaScript object using regular imports, the same technique can be used with any other bundling library (e.g. webpack, pkg etc.).
Since the codegen script just creates a simple JavaScript object using regular imports, the same technique can be used with any other bundling library (e.g. webpack, pkg etc.).
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
],
"packageManager": "pnpm@8.10.2",
"dependencies": {
"@rushstack/ts-command-line": "^4.12.2",
"@rushstack/ts-command-line": "4.19.1",
"emittery": "^0.13.0",
"pony-cause": "^2.1.4",
"tinyglobby": "^0.2.13",
Expand Down
2 changes: 1 addition & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions test/__snapshots__/7-bundling-codegen.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

...

`npm run lint -- --fix` output:
`npm run codegen` output:

...

Expand Down Expand Up @@ -30,6 +30,6 @@

{ event: 'created', path: '<<timestamp>>.new-migration.ts' }

`npm run lint -- --fix` output:
`npm run codegen` output:

...
2 changes: 1 addition & 1 deletion vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export default defineConfig({
test: {
globals: true,
environment: 'node',
testTimeout: 10_000,
testTimeout: 30_000,
coverage: {
include: ['src/**'],
},
Expand Down
Loading