Skip to content

Commit

Permalink
Add custom env file path (#759)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
vanpana committed Mar 18, 2021
1 parent e8fd3b7 commit db3f901
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 17 deletions.
45 changes: 29 additions & 16 deletions bin/node-pg-migrate
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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]')
Expand Down Expand Up @@ -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',
})

Expand Down Expand Up @@ -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]
Expand Down
3 changes: 2 additions & 1 deletion docs/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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.)
Expand Down

0 comments on commit db3f901

Please sign in to comment.