Skip to content

Commit

Permalink
Merge pull request #5 from acro5piano/feature/cli
Browse files Browse the repository at this point in the history
feat: able to run the CLI without the script file
  • Loading branch information
acro5piano authored May 25, 2023
2 parents 52024ed + 5465173 commit e9202aa
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 11 deletions.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,27 @@ Commands:
time as the version
help [command] display help for command
```

# Experimental: CLI without a script file

Important NOTEs:

- This is an experimental feature and the behaviour might change in the future.
- Currently it works with Postgres only.

You can run `kysely-migration-cli` without a script file.

```
export DATABASE_URL=postgres://postgres:[email protected]:27253/postgres
npm run kysely-migration-cli
```

If you place `.env` file which contains `DATABASE_URL=postgres://...`, the CLI automatically loads it before execution.

To compile typescript, `kysely-migration-cli` should register Node's hook api. Currently it supports the following transpiers:

- `esbuild-register`
- `ts-node/register/transpile-only`
- `@swc-node/register`

If you need more, please create a pull request.
21 changes: 21 additions & 0 deletions bin/kysely-migration-cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env node

let registered = false

const tryRegister = (pkg) => {
try {
require(pkg)
registered = true
console.log(`registered ${pkg}`)
} catch (err) {}
}

tryRegister('esbuild-register')
tryRegister('ts-node/register/transpile-only')
tryRegister('@swc-node/register')

if (!registered) {
console.log('Warning: all of supported typescript transpiler registration failed')
}

require('../dist/cli').main()
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
"repository": "[email protected]:acro5piano/kysely-migration-cli",
"author": "Kay Gosho <[email protected]>",
"license": "MIT",
"bin": {
"kysely-migration-cli": "./bin/kysely-migration-cli.js"
},
"files": [
"dist/*.js",
"dist/*.map",
Expand Down
17 changes: 6 additions & 11 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
import { run } from '.'
import {
FileMigrationProvider,
Kysely,
Migrator,
PostgresDialect,
} from 'kysely'
import { FileMigrationProvider, Kysely, Migrator, PostgresDialect } from 'kysely'
import path from 'path'
import { promises as fs } from 'fs';
import {
Pool
} from 'pg';
import { promises as fs } from 'fs'
import { Pool } from 'pg'

/**
* Load Dotenv if the module exists.
Expand Down Expand Up @@ -48,4 +41,6 @@ const migrator = new Migrator({
}),
})

run(db, migrator)
export function main() {
run(db, migrator)
}

0 comments on commit e9202aa

Please sign in to comment.