11import { CommandModule } from 'yargs' ;
22
3- import { defaultMigrator } from '../core/migrator' ;
4- import { BaseCliOptions , baseOptions } from '../core/yargs' ;
3+ import { Migrator } from '../core/migrator' ;
4+ import { BaseCliOptions , baseHandler , baseOptions } from '../core/yargs' ;
55import logger from '../helpers/logger' ;
66
77interface CliOptions extends BaseCliOptions {
@@ -20,26 +20,26 @@ export default {
2020 type : 'string'
2121 } ) ,
2222
23- handler : async function ( args ) {
23+ handler : baseHandler ( async ( args , migrator ) => {
2424 const command = args . _ [ 0 ] ;
2525
2626 switch ( command ) {
2727 case 'migrate' :
28- await migrate ( args ) ;
28+ await migrate ( args , migrator ) ;
2929 break ;
3030 case 'migrate:status' :
31- await migrationStatus ( ) ;
31+ await migrationStatus ( migrator ) ;
3232 break ;
3333 }
3434
3535 process . exit ( 0 ) ;
36- }
36+ } )
3737} as CommandModule < CliOptions , CliOptions > ;
3838
39- async function migrate ( args : CliOptions ) {
39+ async function migrate ( args : CliOptions , migrator : Migrator ) {
4040 try {
41- const migrations = await defaultMigrator . pending ( ) ;
42- const options : Partial < CliOptions > = { } ;
41+ const migrations = await migrator . pending ( ) ;
42+ const options = { } as CliOptions ;
4343
4444 if ( migrations . length === 0 ) {
4545 logger . log ( 'No migrations were executed, database schema was already up to date.' ) ;
@@ -60,21 +60,21 @@ async function migrate(args: CliOptions) {
6060 options . from = args . from ;
6161 }
6262
63- await defaultMigrator . up ( options ) ;
63+ await migrator . up ( options ) ;
6464 } catch ( e ) {
6565 logger . error ( e ) ;
6666 }
6767}
6868
69- async function migrationStatus ( ) {
69+ async function migrationStatus ( migrator : Migrator ) {
7070 try {
71- const executedMigrations = await defaultMigrator . executed ( ) ;
71+ const executedMigrations = await migrator . executed ( ) ;
7272
7373 executedMigrations . forEach ( migration => {
7474 logger . log ( 'up' , migration . file ) ;
7575 } ) ;
7676
77- const pendingMigrations = await defaultMigrator . pending ( ) ;
77+ const pendingMigrations = await migrator . pending ( ) ;
7878
7979 pendingMigrations . forEach ( migration => {
8080 logger . log ( 'down' , migration . file ) ;
0 commit comments