From 992e6ba2f13ec7ec588726e9a8da32b885a7d0ec Mon Sep 17 00:00:00 2001 From: Ryan Dsouza Date: Mon, 4 May 2020 11:08:10 +0530 Subject: [PATCH] add DB env var and tsconfig path to config options (#613) * add DB env var and tsconfig path to config options * fix tsconfig reader --- bin/node-pg-migrate | 55 ++++++++++++++++++++++++--------------------- 1 file changed, 30 insertions(+), 25 deletions(-) diff --git a/bin/node-pg-migrate b/bin/node-pg-migrate index f4bc75c8..e12ec9fe 100755 --- a/bin/node-pg-migrate +++ b/bin/node-pg-migrate @@ -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) } } @@ -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) { @@ -298,6 +302,7 @@ if (configFileName) { const config = require(path.resolve(configFileName)) readJson(config) } +readTsconfig() const action = argv._.shift()