Skip to content

Commit 1de336c

Browse files
authored
fix(command-dev): pass dev context to '@netlify/config' (#1878)
1 parent 0b589f7 commit 1de336c

File tree

6 files changed

+70
-4
lines changed

6 files changed

+70
-4
lines changed

src/commands/dev/exec.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ const Command = require('../../utils/command')
44
const { injectEnvVariables } = require('../../utils/dev')
55

66
class ExecCommand extends Command {
7+
async init() {
8+
this.commandContext = 'dev'
9+
await super.init()
10+
}
11+
712
async run() {
813
const { log, warn, netlify } = this
914
const { cachedConfig, site } = netlify

src/commands/dev/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,11 @@ const printBanner = ({ url, log }) => {
203203
}
204204

205205
class DevCommand extends Command {
206+
async init() {
207+
this.commandContext = 'dev'
208+
await super.init()
209+
}
210+
206211
async run() {
207212
this.log(`${NETLIFYDEV}`)
208213
const { error: errorExit, log, warn, exit } = this

src/commands/dev/trace.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ const Command = require('../../utils/command')
44
const { runProcess } = require('../../utils/traffic-mesh')
55

66
class TraceCommand extends Command {
7+
async init() {
8+
this.commandContext = 'dev'
9+
await super.init()
10+
}
11+
712
async run() {
813
this.parse(TraceCommand)
914

src/utils/command.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ class BaseCommand extends Command {
142142
return await resolveConfig({
143143
config: argv.config,
144144
cwd,
145-
context: argv.context,
145+
context: argv.context || this.commandContext,
146146
debug: argv.debug,
147147
siteId: argv.siteId || (typeof argv.site === 'string' && argv.site) || state.get('siteId'),
148148
token,

src/utils/dev.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,6 @@ const injectEnvVariables = async ({ env, log, site, warn }) => {
153153
}
154154
}
155155

156-
process.env.CONTEXT = 'dev'
157156
process.env.NETLIFY_DEV = 'true'
158157
}
159158

tests/command.dev.test.js

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,58 @@ testMatrix.forEach(({ args }) => {
256256
})
257257
})
258258

259+
test(testName('[context.dev.environment] should override [build.environment]', args), async (t) => {
260+
await withSiteBuilder('site-with-build-environment', async (builder) => {
261+
builder
262+
.withNetlifyToml({
263+
config: {
264+
build: { environment: { TEST: 'DEFAULT_CONTEXT' }, functions: 'functions' },
265+
context: { dev: { environment: { TEST: 'DEV_CONTEXT' } } },
266+
},
267+
})
268+
.withFunction({
269+
path: 'env.js',
270+
handler: async () => ({
271+
statusCode: 200,
272+
body: `${process.env.TEST}`,
273+
}),
274+
})
275+
276+
await builder.buildAsync()
277+
278+
await withDevServer({ cwd: builder.directory, args }, async (server) => {
279+
const response = await got(`${server.url}/.netlify/functions/env`).text()
280+
t.is(response, 'DEV_CONTEXT')
281+
})
282+
})
283+
})
284+
285+
test(testName('should use [build.environment] and not [context.production.environment]', args), async (t) => {
286+
await withSiteBuilder('site-with-build-environment', async (builder) => {
287+
builder
288+
.withNetlifyToml({
289+
config: {
290+
build: { environment: { TEST: 'DEFAULT_CONTEXT' }, functions: 'functions' },
291+
context: { production: { environment: { TEST: 'PRODUCTION_CONTEXT' } } },
292+
},
293+
})
294+
.withFunction({
295+
path: 'env.js',
296+
handler: async () => ({
297+
statusCode: 200,
298+
body: `${process.env.TEST}`,
299+
}),
300+
})
301+
302+
await builder.buildAsync()
303+
304+
await withDevServer({ cwd: builder.directory, args }, async (server) => {
305+
const response = await got(`${server.url}/.netlify/functions/env`).text()
306+
t.is(response, 'DEFAULT_CONTEXT')
307+
})
308+
})
309+
})
310+
259311
test(testName('should override .env.development with process env', args), async (t) => {
260312
await withSiteBuilder('site-with-override', async (builder) => {
261313
builder
@@ -321,7 +373,7 @@ testMatrix.forEach(({ args }) => {
321373
})
322374
})
323375

324-
test(testName('should override value of the CONTEXT env variable', args), async (t) => {
376+
test(testName('should set value of the CONTEXT env variable', args), async (t) => {
325377
await withSiteBuilder('site-with-context-override', async (builder) => {
326378
builder.withNetlifyToml({ config: { build: { functions: 'functions' } } }).withFunction({
327379
path: 'env.js',
@@ -333,7 +385,7 @@ testMatrix.forEach(({ args }) => {
333385

334386
await builder.buildAsync()
335387

336-
await withDevServer({ cwd: builder.directory, env: { CONTEXT: 'production' }, args }, async (server) => {
388+
await withDevServer({ cwd: builder.directory, args }, async (server) => {
337389
const response = await got(`${server.url}/.netlify/functions/env`).text()
338390
t.is(response, 'dev')
339391
})

0 commit comments

Comments
 (0)