diff --git a/src/commands/dev/exec.js b/src/commands/dev/exec.js index 1703a19dd67..35040d04a4a 100644 --- a/src/commands/dev/exec.js +++ b/src/commands/dev/exec.js @@ -4,6 +4,11 @@ const Command = require('../../utils/command') const { injectEnvVariables } = require('../../utils/dev') class ExecCommand extends Command { + async init() { + this.commandContext = 'dev' + await super.init() + } + async run() { const { log, warn, netlify } = this const { cachedConfig, site } = netlify diff --git a/src/commands/dev/index.js b/src/commands/dev/index.js index e485b3a5805..7ad45c5778a 100644 --- a/src/commands/dev/index.js +++ b/src/commands/dev/index.js @@ -203,6 +203,11 @@ const printBanner = ({ url, log }) => { } class DevCommand extends Command { + async init() { + this.commandContext = 'dev' + await super.init() + } + async run() { this.log(`${NETLIFYDEV}`) const { error: errorExit, log, warn, exit } = this diff --git a/src/commands/dev/trace.js b/src/commands/dev/trace.js index b526f419b16..866d8c6f60e 100644 --- a/src/commands/dev/trace.js +++ b/src/commands/dev/trace.js @@ -4,6 +4,11 @@ const Command = require('../../utils/command') const { runProcess } = require('../../utils/traffic-mesh') class TraceCommand extends Command { + async init() { + this.commandContext = 'dev' + await super.init() + } + async run() { this.parse(TraceCommand) diff --git a/src/utils/command.js b/src/utils/command.js index f3a30a5778d..e4129e3d0e1 100644 --- a/src/utils/command.js +++ b/src/utils/command.js @@ -142,7 +142,7 @@ class BaseCommand extends Command { return await resolveConfig({ config: argv.config, cwd, - context: argv.context, + context: argv.context || this.commandContext, debug: argv.debug, siteId: argv.siteId || (typeof argv.site === 'string' && argv.site) || state.get('siteId'), token, diff --git a/src/utils/dev.js b/src/utils/dev.js index 23fb90daf7a..535e97de403 100644 --- a/src/utils/dev.js +++ b/src/utils/dev.js @@ -153,7 +153,6 @@ const injectEnvVariables = async ({ env, log, site, warn }) => { } } - process.env.CONTEXT = 'dev' process.env.NETLIFY_DEV = 'true' } diff --git a/tests/command.dev.test.js b/tests/command.dev.test.js index bc8f544fbb4..137b6f097fc 100644 --- a/tests/command.dev.test.js +++ b/tests/command.dev.test.js @@ -256,6 +256,58 @@ testMatrix.forEach(({ args }) => { }) }) + test(testName('[context.dev.environment] should override [build.environment]', args), async (t) => { + await withSiteBuilder('site-with-build-environment', async (builder) => { + builder + .withNetlifyToml({ + config: { + build: { environment: { TEST: 'DEFAULT_CONTEXT' }, functions: 'functions' }, + context: { dev: { environment: { TEST: 'DEV_CONTEXT' } } }, + }, + }) + .withFunction({ + path: 'env.js', + handler: async () => ({ + statusCode: 200, + body: `${process.env.TEST}`, + }), + }) + + await builder.buildAsync() + + await withDevServer({ cwd: builder.directory, args }, async (server) => { + const response = await got(`${server.url}/.netlify/functions/env`).text() + t.is(response, 'DEV_CONTEXT') + }) + }) + }) + + test(testName('should use [build.environment] and not [context.production.environment]', args), async (t) => { + await withSiteBuilder('site-with-build-environment', async (builder) => { + builder + .withNetlifyToml({ + config: { + build: { environment: { TEST: 'DEFAULT_CONTEXT' }, functions: 'functions' }, + context: { production: { environment: { TEST: 'PRODUCTION_CONTEXT' } } }, + }, + }) + .withFunction({ + path: 'env.js', + handler: async () => ({ + statusCode: 200, + body: `${process.env.TEST}`, + }), + }) + + await builder.buildAsync() + + await withDevServer({ cwd: builder.directory, args }, async (server) => { + const response = await got(`${server.url}/.netlify/functions/env`).text() + t.is(response, 'DEFAULT_CONTEXT') + }) + }) + }) + test(testName('should override .env.development with process env', args), async (t) => { await withSiteBuilder('site-with-override', async (builder) => { builder @@ -321,7 +373,7 @@ testMatrix.forEach(({ args }) => { }) }) - test(testName('should override value of the CONTEXT env variable', args), async (t) => { + test(testName('should set value of the CONTEXT env variable', args), async (t) => { await withSiteBuilder('site-with-context-override', async (builder) => { builder.withNetlifyToml({ config: { build: { functions: 'functions' } } }).withFunction({ path: 'env.js', @@ -333,7 +385,7 @@ testMatrix.forEach(({ args }) => { await builder.buildAsync() - await withDevServer({ cwd: builder.directory, env: { CONTEXT: 'production' }, args }, async (server) => { + await withDevServer({ cwd: builder.directory, args }, async (server) => { const response = await got(`${server.url}/.netlify/functions/env`).text() t.is(response, 'dev') })