From db3f9012374aa0917f408d12be91f5932ca336b1 Mon Sep 17 00:00:00 2001 From: Dorinel Panaite Date: Thu, 18 Mar 2021 13:02:21 +0100 Subject: [PATCH] Add custom env file path (#759) * Add custom env file path * Tweak dotenv config * Fix linting * Load custom env before accessing process.env * PR review follow-up * Fix linting * Update cli.md --- bin/node-pg-migrate | 45 +++++++++++++++++++++++++++++---------------- docs/cli.md | 3 ++- 2 files changed, 31 insertions(+), 17 deletions(-) diff --git a/bin/node-pg-migrate b/bin/node-pg-migrate index 41c1088c..7278a345 100755 --- a/bin/node-pg-migrate +++ b/bin/node-pg-migrate @@ -15,21 +15,6 @@ process.on('uncaughtException', (err) => { process.exit(1) }) -try { - const myEnv = require('dotenv').config({ silent: true }) // eslint-disable-line global-require,import/no-extraneous-dependencies - try { - require('dotenv-expand')(myEnv) // eslint-disable-line global-require,import/no-extraneous-dependencies - } catch (err) { - if (err.code !== 'MODULE_NOT_FOUND') { - throw err - } - } -} catch (err) { - if (err.code !== 'MODULE_NOT_FOUND') { - throw err - } -} - const schemaArg = 'schema' const createSchemaArg = 'create-schema' const databaseUrlVarArg = 'database-url-var' @@ -53,6 +38,7 @@ const decamelizeArg = 'decamelize' const tsconfigArg = 'tsconfig' const verboseArg = 'verbose' const rejectUnauthorizedArg = 'reject-unauthorized' +const envPathArg = 'envPath' const { argv } = yargs .usage('Usage: $0 [up|down|create|redo] [migrationName] [options]') @@ -161,7 +147,12 @@ const { argv } = yargs }) .option(tsconfigArg, { - describe: 'path to tsconfig.json file', + describe: 'Path to tsconfig.json file', + type: 'string', + }) + + .option(envPathArg, { + describe: 'Path to the .env file that should be used for configuration', type: 'string', }) @@ -221,6 +212,28 @@ if (argv.help || argv._.length === 0) { process.exit(1) } +/* Load env before accessing process.env */ +const envPath = argv[envPathArg] + +// Create default dotenv config +const dotenvConfig = { silent: true } + +// If the path has been configured, add it to the config, otherwise don't change the default dotenv path +if (envPath) { + dotenvConfig.path = envPath +} + +try { + // Load config from ".env" file + const myEnv = require('dotenv').config(dotenvConfig) // eslint-disable-line global-require,import/no-extraneous-dependencies + + require('dotenv-expand')(myEnv) // eslint-disable-line global-require,import/no-extraneous-dependencies +} catch (err) { + if (err.code !== 'MODULE_NOT_FOUND') { + throw err + } +} + let MIGRATIONS_DIR = argv[migrationsDirArg] let DB_CONNECTION = process.env[argv[databaseUrlVarArg]] let IGNORE_PATTERN = argv[ignorePatternArg] diff --git a/docs/cli.md b/docs/cli.md index ef70673c..bd12452c 100644 --- a/docs/cli.md +++ b/docs/cli.md @@ -45,7 +45,7 @@ You can specify custom JSON file with config (format is same as for `db` entry o } ``` -If a .env file exists, it will be loaded using [dotenv](https://www.npmjs.com/package/dotenv) (if installed) when running the node-pg-migrate binary. +If a .env file exists, it will be loaded using [dotenv](https://www.npmjs.com/package/dotenv) (if installed) when running the node-pg-migrate binary. If the .env file is not on the same level where the command has been called, you can use the `--envPath` option to point to the location of your .env file. Depending on your project's setup, it may make sense to write some custom grunt/gulp/whatever tasks that set this env var and run your migration commands. More on that below. @@ -77,6 +77,7 @@ You can adjust defaults by passing arguments to `node-pg-migrate`: - `migration-file-language` (`j`) - Language of the migration file to create (`js` or `ts`) - `template-file-name` - Use your own template file for migrations (language will be determined from the extension of the template). The file must export the `up` method accepting `MigrationBuilder` instance. - `tsconfig` - Path to tsconfig.json. Used to setup transpiling of TS migration files. (Also sets `migration-file-language` to typescript, if not overridden) +- `envPath` - Path to a .env file. The default finds the file on the same level where the command has been called. It might be useful if you have nested projects, but a global .env file that you need to point to. - `timestamp` - Treats number argument to up/down migration as timestamp (running up migrations less or equal to timestamp or down migrations greater or equal to timestamp) - `check-order` - Check order of migrations before running them (defaults to `true`, to switch it off supply `--no-check-order` on the command line). (There should be no migration with timestamp lesser than last run migration.)