Skip to content
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
5 changes: 5 additions & 0 deletions src/commands/dev/exec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions src/commands/dev/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions src/commands/dev/trace.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion src/utils/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 0 additions & 1 deletion src/utils/dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,6 @@ const injectEnvVariables = async ({ env, log, site, warn }) => {
}
}

process.env.CONTEXT = 'dev'
process.env.NETLIFY_DEV = 'true'
}

Expand Down
56 changes: 54 additions & 2 deletions tests/command.dev.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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',
Expand All @@ -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) => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should allow overriding it via process level env, but can go either way

const response = await got(`${server.url}/.netlify/functions/env`).text()
t.is(response, 'dev')
})
Expand Down