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

feat: envFiles in netlify.toml #4321

Merged
merged 30 commits into from
Feb 22, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
3d7f48a
fix: npm run docs on Windows (#3973)
iib0011 Feb 16, 2022
8b6c546
feat: env_priority in netlify.toml (#4272)
iib0011 Feb 17, 2022
c8d207b
feat: dev --env flag (#4272)
iib0011 Feb 17, 2022
eb51778
Merge branch 'netlify:main' into envfix
iib0011 Feb 17, 2022
672903d
chore: rollback docs
iib0011 Feb 17, 2022
db6ca39
refactor: envPriority-> envFiles
iib0011 Feb 17, 2022
5dd6dc6
chore: comma separated list for env option
iib0011 Feb 17, 2022
316e367
chore: comma separated list for env option
iib0011 Feb 17, 2022
390f44a
Merge branch 'main' into envfix
erezrokah Feb 17, 2022
1064933
Merge branch 'main' into envfix
iib0011 Feb 17, 2022
3611233
chore: env flag removal
iib0011 Feb 18, 2022
ea28bb2
chore: env flag removal
iib0011 Feb 18, 2022
bd18271
Merge branch 'envfix' of https://github.com/iib0011/cli into envfix
iib0011 Feb 18, 2022
152b49f
chore: env files test and docs
iib0011 Feb 18, 2022
c86384a
chore: env_files test
iib0011 Feb 18, 2022
876a618
chore: env_files test
iib0011 Feb 18, 2022
132c6e6
chore: env_files test title
iib0011 Feb 18, 2022
27f1825
chore: env_files test fix
iib0011 Feb 18, 2022
fd07a35
Merge branch 'envfix' of https://github.com/iib0011/cli into envfix
iib0011 Feb 18, 2022
ce2f68f
refactor: env_files -> envFiles
iib0011 Feb 18, 2022
e849722
chore: env_files in dev block
iib0011 Feb 18, 2022
1a90081
Merge branch 'main' into envfix
erezrokah Feb 21, 2022
aae03ad
Merge branch 'main' into envfix
erezrokah Feb 21, 2022
3c295d8
fix(docs): move `envFiles` to the general section
erezrokah Feb 21, 2022
a239de2
chore: use `dev` block in test
erezrokah Feb 21, 2022
efde8de
Merge branch 'envfix' of https://github.com/iib0011/cli into envfix
iib0011 Feb 21, 2022
070ffb3
fix: env_files test
iib0011 Feb 21, 2022
97380fa
fix: apply config envFiles from left (highest) to right (lowest)
erezrokah Feb 22, 2022
12a29c4
Merge branch 'main' into envfix
erezrokah Feb 22, 2022
e9ca564
chore: remove redundant doc line
erezrokah Feb 22, 2022
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
8 changes: 6 additions & 2 deletions site/scripts/docs.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @ts-check
import { basename, join } from 'path'
import { env } from 'process'
import { env, platform } from 'process'
import { fileURLToPath } from 'url'

import markdownMagic from 'markdown-magic'
Expand All @@ -9,7 +9,11 @@ import stripAnsi from 'strip-ansi'
import { generateCommandData } from './generate-command-data.mjs'

const rootDir = fileURLToPath(new URL('../..', import.meta.url))
const markdownFiles = [join(rootDir, 'README.md'), join(rootDir, 'docs/**/**.md')]
const isWindows = platform === 'win32'
erezrokah marked this conversation as resolved.
Show resolved Hide resolved
const markdownFiles = [
isWindows ? join(rootDir, 'README.md').replace(/\\/g, '/') : join(rootDir, 'README.md'),
isWindows ? join(rootDir, 'docs/**/**.md').replace(/\\/g, '/') : join(rootDir, 'docs/**/**.md'),
]

env.DOCS_GEN = 'TRUE'

Expand Down
4 changes: 2 additions & 2 deletions src/commands/dev/dev-exec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ const { injectEnvVariables } = require('../../utils')
* @param {import('../base-command').BaseCommand} command
*/
const devExec = async (cmd, options, command) => {
const { cachedConfig, site } = command.netlify
await injectEnvVariables({ env: cachedConfig.env, site })
const { cachedConfig, config, site } = command.netlify
await injectEnvVariables({ devConfig: { ...config.dev }, env: cachedConfig.env, site })

await execa(cmd, command.args.slice(1), {
stdio: 'inherit',
Expand Down
7 changes: 5 additions & 2 deletions src/commands/dev/dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -325,8 +325,10 @@ const dev = async (options, command) => {
'--trafficMesh and -t are deprecated and will be removed in the near future. Please use --edgeHandlers or -e instead.',
)
}

await injectEnvVariables({ env: command.netlify.cachedConfig.env, site })
if (options.env) {
devConfig.envPriority = options.env.split(' ')
erezrokah marked this conversation as resolved.
Show resolved Hide resolved
}
await injectEnvVariables({ devConfig, env: command.netlify.cachedConfig.env, site })

const { addonsUrls, capabilities, siteUrl, timeouts } = await getSiteInformation({
// inherited from base command --offline
Expand Down Expand Up @@ -463,6 +465,7 @@ const createDevCommand = (program) => {
.option('-o ,--offline', 'disables any features that require network access')
.option('-l, --live', 'start a public live session', false)
.option('--functionsPort <port>', 'port of functions server', (value) => Number.parseInt(value))
.option('-e, --env <env>', 'set the env files priority', false)
iib0011 marked this conversation as resolved.
Show resolved Hide resolved
.addOption(
new Option('--staticServerPort <port>', 'port of the static app server used when no framework is detected')
.argParser((value) => Number.parseInt(value))
Expand Down
3 changes: 2 additions & 1 deletion src/commands/dev/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ export type DevConfig = {
https?: {
keyFile: string
certFile: string
}
},
envPriority?:string[]
}
16 changes: 14 additions & 2 deletions src/commands/functions/functions-create.js
Original file line number Diff line number Diff line change
Expand Up @@ -477,8 +477,14 @@ const createFunctionAddon = async function ({ addonName, addons, api, siteData,
* @param {(command: import('../base-command').BaseCommand) => any} config.onComplete
*/
const handleOnComplete = async ({ command, onComplete }) => {
const { config } = command.netlify

if (onComplete) {
await injectEnvVariables({ env: command.netlify.cachedConfig.env, site: command.netlify.site })
await injectEnvVariables({
devConfig: { ...config.dev },
env: command.netlify.cachedConfig.env,
site: command.netlify.site,
})
await onComplete.call(command)
}
}
Expand All @@ -491,6 +497,8 @@ const handleOnComplete = async ({ command, onComplete }) => {
* @param {string} config.fnPath
*/
const handleAddonDidInstall = async ({ addonCreated, addonDidInstall, command, fnPath }) => {
const { config } = command.netlify

if (!addonCreated || !addonDidInstall) {
return
}
Expand All @@ -508,7 +516,11 @@ const handleAddonDidInstall = async ({ addonCreated, addonDidInstall, command, f
return
}

await injectEnvVariables({ env: command.netlify.cachedConfig.env, site: command.netlify.site })
await injectEnvVariables({
devConfig: { ...config.dev },
env: command.netlify.cachedConfig.env,
site: command.netlify.site,
})
addonDidInstall(fnPath)
}

Expand Down
2 changes: 1 addition & 1 deletion src/commands/functions/functions-serve.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const functionsServe = async (options, command) => {

const functionsDir = getFunctionsDir({ options, config }, join('netlify', 'functions'))

await injectEnvVariables({ env: command.netlify.cachedConfig.env, site })
await injectEnvVariables({ devConfig: { ...config.dev }, env: command.netlify.cachedConfig.env, site })

const { capabilities, siteUrl, timeouts } = await getSiteInformation({
offline: options.offline,
Expand Down
4 changes: 2 additions & 2 deletions src/utils/dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,9 @@ const getEnvSourceName = (source) => {

// Takes a set of environment variables in the format provided by @netlify/config, augments it with variables from both
// dot-env files and the process itself, and injects into `process.env`.
const injectEnvVariables = async ({ env, site }) => {
const injectEnvVariables = async ({ devConfig, env, site }) => {
const environment = new Map(Object.entries(env))
const dotEnvFiles = await loadDotEnvFiles({ projectDir: site.root })
const dotEnvFiles = await loadDotEnvFiles({ envPriority: devConfig.envPriority, projectDir: site.root })

dotEnvFiles.forEach(({ env: fileEnv, file }) => {
Object.keys(fileEnv).forEach((key) => {
Expand Down
9 changes: 5 additions & 4 deletions src/utils/dot-env.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ const { isFileAsync } = require('../lib/fs')

const { warn } = require('./command-helpers')

const loadDotEnvFiles = async function ({ projectDir }) {
const response = await tryLoadDotEnvFiles({ projectDir })
const loadDotEnvFiles = async function ({ envPriority, projectDir }) {
const response = await tryLoadDotEnvFiles({ projectDir, dotenvFiles: envPriority })

const filesWithWarning = response.filter((el) => el.warning)
filesWithWarning.forEach((el) => {
Expand All @@ -19,8 +19,9 @@ const loadDotEnvFiles = async function ({ projectDir }) {
return response.filter((el) => el.file && el.env)
}

const tryLoadDotEnvFiles = async ({ projectDir }) => {
const dotenvFiles = ['.env', '.env.development']
const defaultEnvPriority = ['.env', '.env.development', '.env.local', '.env.development.local']
erezrokah marked this conversation as resolved.
Show resolved Hide resolved

const tryLoadDotEnvFiles = async ({ projectDir, dotenvFiles = defaultEnvPriority }) => {
const results = await Promise.all(
dotenvFiles.map(async (file) => {
const filepath = path.resolve(projectDir, file)
Expand Down
1 change: 1 addition & 0 deletions src/utils/init/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ const getNetlifyToml = ({
# command = "yarn start" # Command to start your dev server
# port = 3000 # Port that the dev server will be listening on
# publish = "dist" # Folder with the static content for _redirect file
# env_priority = [ ".env", ".env.local", ".env.development", ".env.development.local" ]
erezrokah marked this conversation as resolved.
Show resolved Hide resolved

## more info on configuring this file: https://www.netlify.com/docs/netlify-toml-reference/
`
Expand Down