Skip to content

Commit

Permalink
✨ Add config cli command
Browse files Browse the repository at this point in the history
  • Loading branch information
hirsch committed Jun 4, 2019
1 parent 1f372a8 commit f1bbdbf
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
2 changes: 2 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
import 'reflect-metadata'
import * as yargs from 'yargs'
import { SeedCommand } from './commands/seed.command'
import { ConfigCommand } from './commands/config.command';

yargs
.usage('Usage: $0 <command> [options]')
.command(new ConfigCommand())
.command(new SeedCommand())
.recommendCommands()
.demandCommand(1)
Expand Down
32 changes: 32 additions & 0 deletions src/commands/config.command.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import * as yargs from 'yargs'
import chalk from 'chalk'
import { getConnectionOptions } from '../typeorm-seeding'
import * as pkg from '../../package.json'
import { printError } from '../utils/log.util'

export class ConfigCommand implements yargs.CommandModule {
command = 'config'
describe = 'Show the TypeORM config'

builder(args: yargs.Argv) {
return args
.option('c', {
alias: 'config',
default: 'ormconfig.js',
describe: 'Path to the typeorm config file (json or js).',
})
}

async handler(args: yargs.Arguments) {
const log = console.log
log(chalk.bold(`typeorm-seeding v${(pkg as any).version}`))
try {
const options = await getConnectionOptions(args.config as string)
log(options)
} catch (error) {
printError('Could not find the orm config file', error)
process.exit(1)
}
process.exit(0)
}
}
5 changes: 0 additions & 5 deletions src/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,11 @@ import * as path from 'path'
import {
Connection,
createConnection as createTypeORMConnection,
getConnectionOptions as getTypeORMConnectionOptions,
ConnectionOptions,
} from 'typeorm'

export const getConnectionOptions = async (configPath: string): Promise<ConnectionOptions> => {
try {
return await getTypeORMConnectionOptions()
} catch (_) {
return require(path.join(process.cwd(), configPath))
}
}

export const createConnection = async (configPath: string): Promise<Connection> => {
Expand Down

0 comments on commit f1bbdbf

Please sign in to comment.