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

Add .env files support fo netlify dev:exec #895

Merged
merged 5 commits into from
May 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
15 changes: 14 additions & 1 deletion src/commands/dev/exec.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
const path = require('path')
const execa = require('execa')
const chalk = require('chalk')
const Command = require('../../utils/command')
const {
// NETLIFYDEV,
NETLIFYDEVLOG,
// NETLIFYDEVWARN,
NETLIFYDEVERR
} = require('../../utils/logo')
const { getEnvSettings } = require('../../utils/env')

class ExecCommand extends Command {
async run() {
Expand All @@ -20,6 +23,16 @@ class ExecCommand extends Command {
`${NETLIFYDEVERR} No Site ID detected. You probably forgot to run \`netlify link\` or \`netlify init\`. `
)
}

const envSettings = await getEnvSettings(site.root)
if (envSettings.file) {
console.log(
`${NETLIFYDEVLOG} Overriding the following env variables with ${chalk.blue(path.relative(site.root, envSettings.file))} file:`,
chalk.yellow(Object.keys(envSettings.vars))
)
Object.entries(envSettings.vars).forEach(([key, val]) => (process.env[key] = val))
}

execa(this.argv[0], this.argv.slice(1), {
env: process.env,
stdio: 'inherit'
Expand All @@ -37,7 +50,7 @@ ExecCommand.description = `Exec command
Runs a command within the netlify dev environment, e.g. with env variables from any installed addons
`

ExecCommand.examples = ['$ netlify exec npm run bootstrap']
ExecCommand.examples = ['$ netlify dev:exec npm run bootstrap']

ExecCommand.strict = false
ExecCommand.parse = false
Expand Down
15 changes: 6 additions & 9 deletions src/commands/dev/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,14 @@ const Command = require('../../utils/command')
const chalk = require('chalk')
const jwtDecode = require('jwt-decode')
const open = require('open')
const dotenv = require('dotenv')
const { NETLIFYDEV, NETLIFYDEVLOG, NETLIFYDEVWARN, NETLIFYDEVERR } = require('../../utils/logo')
const boxen = require('boxen')
const { createTunnel, connectTunnel } = require('../../utils/live-tunnel')
const { createRewriter } = require('../../utils/rules-proxy')
const { onChanges } = require('../../utils/rules-proxy')
const { parseHeadersFile, objectForPath } = require('../../utils/headers')
const { getEnvFile } = require('../../utils/env')
const { getEnvSettings } = require('../../utils/env')

const readFile = util.promisify(fs.readFile)
const stat = util.promisify(fs.stat)

function isInternal(url) {
Expand Down Expand Up @@ -411,14 +409,13 @@ class DevCommand extends Command {

process.env.NETLIFY_DEV = 'true'
// Override env variables with .env file
const envFile = await getEnvFile(site.root)
if (envFile) {
const vars = dotenv.parse(await readFile(envFile)) || {}
const envSettings = await getEnvSettings(site.root)
if (envSettings.file) {
console.log(
`${NETLIFYDEVLOG} Overriding the following env variables with ${chalk.blue(path.relative(site.root, envFile))} file:`,
chalk.yellow(Object.keys(vars))
`${NETLIFYDEVLOG} Overriding the following env variables with ${chalk.blue(path.relative(site.root, envSettings.file))} file:`,
chalk.yellow(Object.keys(envSettings.vars))
)
Object.entries(vars).forEach(([key, val]) => (process.env[key] = val))
Object.entries(envSettings.vars).forEach(([key, val]) => (process.env[key] = val))
}

let settings = await serverSettings(devConfig, flags, site.root, this.log)
Expand Down
14 changes: 5 additions & 9 deletions src/function-builder-detectors/netlify-lambda.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
const { existsSync, readFileSync, readFile: readFileAsync } = require('fs')
const util = require('util')
const { existsSync, readFileSync } = require('fs')
const execa = require('execa')
const dotenv = require('dotenv')
const { getEnvFile } = require('../utils/env')

const readFile = util.promisify(readFileAsync)
const { getEnvSettings } = require('../utils/env')

module.exports = async function(projectDir) {
if (!existsSync('package.json')) {
Expand All @@ -31,9 +27,9 @@ module.exports = async function(projectDir) {
}

let envConfig = {}
const envFile = await getEnvFile(projectDir)
if (envFile) {
envConfig = dotenv.parse(await readFile(envFile))
const envSettings = await getEnvSettings(projectDir)
if (envSettings.file) {
envConfig = envSettings.vars
}

if (settings.npmScript) {
Expand Down
16 changes: 11 additions & 5 deletions src/utils/env.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,31 @@
const path = require('path')
const fs = require('fs')
const { promisify } = require('util')
const dotenv = require('dotenv')

const fileStat = promisify(fs.stat)
const readFile = promisify(fs.readFile)

async function getEnvFile(projectDir) {
async function getEnvSettings(projectDir) {
const envDevelopmentFile = path.resolve(projectDir, '.env.development')
const envFile = path.resolve(projectDir, '.env')

const settings = {}

try {
if ((await fileStat(envDevelopmentFile)).isFile()) return envDevelopmentFile
if ((await fileStat(envFile)).isFile()) settings.file = envFile
} catch (err) {
// nothing
}
try {
if ((await fileStat(envFile)).isFile()) return envFile
if ((await fileStat(envDevelopmentFile)).isFile()) settings.file = envDevelopmentFile
} catch (err) {
// nothing
}

return undefined
if (settings.file) settings.vars = dotenv.parse(await readFile(settings.file)) || {}

return settings
}

module.exports.getEnvFile = getEnvFile
module.exports.getEnvSettings = getEnvSettings
23 changes: 16 additions & 7 deletions src/utils/env.test.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,31 @@
const test = require('ava')
const path = require('path')
const { getEnvFile } = require('./env')
const { getEnvSettings } = require('./env')

const contextSite = path.join(__dirname, '..', '..', 'tests', 'context-site')
const dummySite = path.join(__dirname, '..', '..', 'tests', 'dummy-site')
const craSite = path.join(__dirname, '..', '..', 'tests', 'site-cra')

test('no .env files', async t => {
const f = await getEnvFile(contextSite)
t.is(f, undefined)
const vars = await getEnvSettings(contextSite)
t.deepEqual(vars, {})
})

test('.env.development file', async t => {
const f = await getEnvFile(dummySite)
t.is(f, path.resolve(dummySite, '.env.development'))
const vars = await getEnvSettings(dummySite)
t.deepEqual(vars, {
file: path.resolve(dummySite, '.env.development'),
vars: {
EASY_VAR: 'true',
DUMMY_VAR: 'false',
},
})
})

test('.env file', async t => {
const f = await getEnvFile(craSite)
t.is(f, path.resolve(craSite, '.env'))
const vars = await getEnvSettings(craSite)
t.deepEqual(vars, {
file: path.resolve(craSite, '.env'),
vars: {},
})
})
Empty file removed tests/dummy-site/.env
Empty file.