Skip to content

Commit

Permalink
add DB env var and tsconfig path to config options (#613)
Browse files Browse the repository at this point in the history
* add DB env var and tsconfig path to config options

* fix tsconfig reader
  • Loading branch information
ryands17 authored May 4, 2020
1 parent 5424d6c commit 992e6ba
Showing 1 changed file with 30 additions and 25 deletions.
55 changes: 30 additions & 25 deletions bin/node-pg-migrate
Original file line number Diff line number Diff line change
Expand Up @@ -212,32 +212,34 @@ let MIGRATIONS_FILE_LANGUAGE = argv[migrationFileLanguageArg]
let CHECK_ORDER = argv[checkOrderArg]
let VERBOSE = argv[verboseArg]
let DECAMELIZE = argv[decamelizeArg]

const tsconfigPath = argv[tsconfigArg]
if (tsconfigPath) {
let tsconfig
let tsnode
try {
// eslint-disable-next-line security/detect-non-literal-fs-filename
const config = fs.readFileSync(path.resolve(process.cwd(), tsconfigPath), { encoding: 'utf-8' })
// eslint-disable-next-line global-require,import/no-dynamic-require,security/detect-non-literal-require,import/no-extraneous-dependencies
tsconfig = require('json5').parse(config)
} catch (err) {
console.error(`Can't load tsconfig.json: ${err.stack}`)
}
try {
// eslint-disable-next-line global-require,import/no-extraneous-dependencies
tsnode = require('ts-node')
} catch (err) {
console.error(`Can't load ts-node: ${err.stack}`)
}
if (tsconfig && tsnode) {
tsnode.register(tsconfig)
if (!MIGRATIONS_FILE_LANGUAGE) {
MIGRATIONS_FILE_LANGUAGE = 'ts'
let tsconfigPath = argv[tsconfigArg]

function readTsconfig() {
if (tsconfigPath) {
let tsconfig
let tsnode
try {
// eslint-disable-next-line security/detect-non-literal-fs-filename
const config = fs.readFileSync(path.resolve(process.cwd(), tsconfigPath), { encoding: 'utf-8' })
// eslint-disable-next-line global-require,import/no-dynamic-require,security/detect-non-literal-require,import/no-extraneous-dependencies
tsconfig = require('json5').parse(config)
} catch (err) {
console.error(`Can't load tsconfig.json: ${err.stack}`)
}
try {
// eslint-disable-next-line global-require,import/no-extraneous-dependencies
tsnode = require('ts-node')
} catch (err) {
console.error(`Can't load ts-node: ${err.stack}`)
}
if (tsconfig && tsnode) {
tsnode.register(tsconfig)
if (!MIGRATIONS_FILE_LANGUAGE) {
MIGRATIONS_FILE_LANGUAGE = 'ts'
}
} else {
process.exit(1)
}
} else {
process.exit(1)
}
}

Expand All @@ -256,6 +258,8 @@ function readJson(json) {
CHECK_ORDER = typeof CHECK_ORDER !== 'undefined' ? CHECK_ORDER : json[checkOrderArg]
VERBOSE = typeof VERBOSE !== 'undefined' ? VERBOSE : json[verboseArg]
DECAMELIZE = typeof DECAMELIZE !== 'undefined' ? DECAMELIZE : json[decamelizeArg]
DB_CONNECTION = typeof DB_CONNECTION !== 'undefined' ? DB_CONNECTION : process.env[json[databaseUrlVarArg]]
tsconfigPath = typeof tsconfigPath !== 'undefined' ? tsconfigPath : json[tsconfigArg]
if (json.url) {
DB_CONNECTION = typeof DB_CONNECTION !== 'undefined' ? DB_CONNECTION : json.url
} else if (json.host || json.port || json.name || json.database) {
Expand Down Expand Up @@ -298,6 +302,7 @@ if (configFileName) {
const config = require(path.resolve(configFileName))
readJson(config)
}
readTsconfig()

const action = argv._.shift()

Expand Down

0 comments on commit 992e6ba

Please sign in to comment.