diff --git a/examples/7-bundling-codegen/codegen.mjs b/examples/7-bundling-codegen/codegen.mjs new file mode 100644 index 00000000..b3bbeb77 --- /dev/null +++ b/examples/7-bundling-codegen/codegen.mjs @@ -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('') +} diff --git a/examples/7-bundling-codegen/eslint.config.js b/examples/7-bundling-codegen/eslint.config.js deleted file mode 100644 index ab5b811e..00000000 --- a/examples/7-bundling-codegen/eslint.config.js +++ /dev/null @@ -1,10 +0,0 @@ -const tseslint = require('typescript-eslint') -const codegen = require('eslint-plugin-codegen') - -module.exports = [ - tseslint.configs.base, - {plugins: {codegen}}, - {rules: {'codegen/codegen': 'error'}}, - {files: ['migrations/*.ts']}, - {ignores: ['dist/**']}, -] diff --git a/examples/7-bundling-codegen/migrations/barrel.ts b/examples/7-bundling-codegen/migrations/barrel.ts index 98ee6ef0..95db5da6 100644 --- a/examples/7-bundling-codegen/migrations/barrel.ts +++ b/examples/7-bundling-codegen/migrations/barrel.ts @@ -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 diff --git a/examples/7-bundling-codegen/package.json b/examples/7-bundling-codegen/package.json index 2be3de83..32e61379 100644 --- a/examples/7-bundling-codegen/package.json +++ b/examples/7-bundling-codegen/package.json @@ -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" } } diff --git a/examples/7-bundling-codegen/readme.md b/examples/7-bundling-codegen/readme.md index 1328700a..0f58d55f 100644 --- a/examples/7-bundling-codegen/readme.md +++ b/examples/7-bundling-codegen/readme.md @@ -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.). diff --git a/package.json b/package.json index 3b0358d9..c402ed3c 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 450f7cc7..7c493f00 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -6,7 +6,7 @@ settings: dependencies: '@rushstack/ts-command-line': - specifier: ^4.12.2 + specifier: 4.19.1 version: 4.19.1(@types/node@20.12.7) emittery: specifier: ^0.13.0 diff --git a/test/__snapshots__/7-bundling-codegen.snap b/test/__snapshots__/7-bundling-codegen.snap index cff76e3c..72ae57b1 100644 --- a/test/__snapshots__/7-bundling-codegen.snap +++ b/test/__snapshots__/7-bundling-codegen.snap @@ -2,7 +2,7 @@ ... -`npm run lint -- --fix` output: +`npm run codegen` output: ... @@ -30,6 +30,6 @@ { event: 'created', path: '<>.new-migration.ts' } -`npm run lint -- --fix` output: +`npm run codegen` output: ... \ No newline at end of file diff --git a/vite.config.ts b/vite.config.ts index f43c5d5d..af4b3da3 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -4,7 +4,7 @@ export default defineConfig({ test: { globals: true, environment: 'node', - testTimeout: 10_000, + testTimeout: 30_000, coverage: { include: ['src/**'], },