Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

manual loading and parsing of dotenv in watch mode #558 #559

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion lib/watch/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const path = require('path')
const cp = require('child_process')
const chalk = require('chalk')
const { readFileSync, existsSync } = require('fs')
const { arrayToRegExp, logWatchVerbose } = require('./utils')
const { GRACEFUL_SHUT } = require('./constants.js')

Expand Down Expand Up @@ -39,7 +40,15 @@ const watch = function (args, ignoreWatch, verboseWatch) {

const run = (event) => {
const childEvent = { childEvent: event }
const env = Object.assign({}, process.env, childEvent, require('dotenv').config().parsed)

// in order to not override existing env vars, we have to load and parse
// dotenv without modifying process.env
let dotenvParsed = {}
const dotenvPath = path.resolve(process.cwd(), '.env')
if (existsSync(dotenvPath)) {
dotenvParsed = require('dotenv').parse(readFileSync(dotenvPath, { encoding: 'utf-8' }))
}
const env = Object.assign({}, process.env, childEvent, dotenvParsed)

const _child = cp.fork(forkPath, args, {
env,
Expand Down