netlify-plugin-contextual-env
Advanced tools
Comparing version 0.2.0 to 0.3.0
@@ -10,2 +10,13 @@ # Changelog | ||
## [v0.3.0](https://github.com/cball/netlify-plugin-contextual-env/compare/v0.2.0...v0.3.0) - 2020-04-24 | ||
### Merged | ||
- feat: set process.env directly now that Netlify supports it [`#4`](https://github.com/cball/netlify-plugin-contextual-env/pull/4) | ||
### Commits | ||
- docs: Update README [`114f744`](https://github.com/cball/netlify-plugin-contextual-env/commit/114f7444eaca83888dc80add8f3b0b02a2e65550) | ||
- docs: update README [`fffaab6`](https://github.com/cball/netlify-plugin-contextual-env/commit/fffaab63f1da5dbe9b87ad29b0d2f5a077ee1cd6) | ||
## [v0.2.0](https://github.com/cball/netlify-plugin-contextual-env/compare/v0.1.0...v0.2.0) - 2020-04-18 | ||
@@ -12,0 +23,0 @@ |
{ | ||
"name": "netlify-plugin-contextual-env", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"description": "A Netlify plugin to override ENV vars based on a branch or context", | ||
@@ -5,0 +5,0 @@ "main": "src/index", |
@@ -13,3 +13,3 @@ # netlify-plugin-contextual-env | ||
This allows you to have per-environment or per-context environment variables, without exposing those variables in your `netlify.toml` config. | ||
This allows you to have per-environment or per-context environment variables, **without exposing those variables in your `netlify.toml` config**. | ||
@@ -38,16 +38,1 @@ If you'd rather use a suffix rather than the default prefix configuration, pass suffix to the inputs below. | ||
| `mode` | The way to append the context or branch name (`prefix` or `suffix`) | `prefix` | | ||
## Update your build command | ||
Update your build command to "source" the `.env` file that gets created for you. **Note: your `.env` should always be in `.gitignore`!** This script writes a temporary .env file for you at build time, so updated values can be used during the build process. | ||
You can do this in `netlify.toml`: | ||
```toml | ||
[build] | ||
command = ". ./.env && yarn build" | ||
``` | ||
Or through the Netlify UI if you don't have a `build` section defined in `netlify.toml`: | ||
![image](https://user-images.githubusercontent.com/14339/79069048-45bbd680-7c99-11ea-816b-fec8ee851672.png) |
const fs = require('fs'); | ||
const util = require('util'); | ||
const pWriteFile = util.promisify(fs.writeFile); | ||
@@ -19,6 +18,4 @@ /** | ||
console.log(`Exporting ${key}=${process.env[envVar]}.`); | ||
process.env[key] = process.env[envVar]; | ||
// Renable this once setting process.env is supported in Netlify builds | ||
// See: https://github.com/netlify/build/issues/1129 | ||
// process.env[key] = process.env[envVar]; | ||
return `${key}=${process.env[envVar]}\n`; | ||
@@ -31,6 +28,7 @@ } | ||
const branch = `${process.env.BRANCH}`.toUpperCase().replace(/-/g, '_'); | ||
const { mode } = inputs; | ||
const envOverrides = Object.keys(process.env).map((key) => [ | ||
setEnvWithValue(key, context, inputs.mode), | ||
setEnvWithValue(key, branch, inputs.mode), | ||
setEnvWithValue(key, context, mode), | ||
setEnvWithValue(key, branch, mode), | ||
]); | ||
@@ -41,6 +39,3 @@ | ||
if (replaced.length) { | ||
// Write an env file so we can source it during build | ||
await pWriteFile('.env', replaced.join('')); | ||
console.log(`Replaced ${replaced.length} ENVs and wrote .env file`); | ||
console.log(`Replaced ${replaced.length} ENVs`); | ||
} else { | ||
@@ -47,0 +42,0 @@ console.log(`Nothing found... keeping default ENVs`); |
@@ -1,8 +0,1 @@ | ||
jest.mock('fs'); | ||
const mockWriteFile = jest.fn(() => Promise.resolve()); | ||
jest.mock('util', () => ({ | ||
promisify: () => mockWriteFile, | ||
})); | ||
const onPreBuild = require('./index').onPreBuild; | ||
@@ -14,3 +7,2 @@ | ||
beforeEach(() => { | ||
mockWriteFile.mockReset(); | ||
jest.resetModules(); | ||
@@ -25,3 +17,3 @@ process.env = { ...OLD_ENV }; | ||
describe('with context prefix ENV overrides', () => { | ||
it('writes an env file with updated values', async () => { | ||
it('sets ENV vars to the correct values', async () => { | ||
process.env.DATABASE_URL = 'https://dev.com'; | ||
@@ -32,3 +24,3 @@ process.env.STAGING_DATABASE_URL = 'https://stage.com'; | ||
expect(mockWriteFile).toHaveBeenCalledWith('.env', 'DATABASE_URL=https://stage.com\n'); | ||
expect(process.env.DATABASE_URL).toBe(process.env.STAGING_DATABASE_URL); | ||
}); | ||
@@ -38,3 +30,3 @@ }); | ||
describe('with suffix context prefix ENV overrides', () => { | ||
it('writes an env file with updated values', async () => { | ||
it('sets ENV vars to the correct values', async () => { | ||
process.env.DATABASE_URL = 'https://dev.com'; | ||
@@ -45,3 +37,3 @@ process.env.DATABASE_URL_STAGING = 'https://stage.com'; | ||
expect(mockWriteFile).toHaveBeenCalledWith('.env', 'DATABASE_URL=https://stage.com\n'); | ||
expect(process.env.DATABASE_URL).toBe(process.env.DATABASE_URL_STAGING); | ||
}); | ||
@@ -51,3 +43,3 @@ }); | ||
describe('with branch ENV overrides', () => { | ||
it('writes an env file with updated values', async () => { | ||
it('sets ENV vars to the correct values', async () => { | ||
process.env.DATABASE_URL = 'https://dev.com'; | ||
@@ -58,3 +50,3 @@ process.env.HELLO_DATABASE_URL = 'https://stage.com'; | ||
expect(mockWriteFile).toHaveBeenCalledWith('.env', 'DATABASE_URL=https://stage.com\n'); | ||
expect(process.env.DATABASE_URL).toBe(process.env.HELLO_DATABASE_URL); | ||
}); | ||
@@ -64,3 +56,3 @@ }); | ||
describe('with suffix branch ENV overrides', () => { | ||
it('writes an env file with updated values', async () => { | ||
it('sets ENV vars to the correct values', async () => { | ||
process.env.DATABASE_URL = 'https://dev.com'; | ||
@@ -71,3 +63,3 @@ process.env.DATABASE_URL_HELLO = 'https://stage.com'; | ||
expect(mockWriteFile).toHaveBeenCalledWith('.env', 'DATABASE_URL=https://stage.com\n'); | ||
expect(process.env.DATABASE_URL).toBe(process.env.DATABASE_URL_HELLO); | ||
}); | ||
@@ -77,10 +69,11 @@ }); | ||
describe('without ENV overrides', () => { | ||
it('writes an env file with updated values', async () => { | ||
it('does not change ENV vars', async () => { | ||
process.env.DATABASE_URL = 'https://dev.com'; | ||
process.env.DATABASE_URL_HELLO = 'https://dontsetme.com'; | ||
process.env.BRANCH = 'hello'; | ||
await onPreBuild({ inputs: { mode: 'prefix' } }); | ||
await onPreBuild({ inputs: { mode: 'suffix' } }); | ||
expect(mockWriteFile).not.toHaveBeenCalled(); | ||
expect(process.env.DATABASE_URL).toBe(process.env.DATABASE_URL_HELLO); | ||
}); | ||
}); | ||
}); |
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 2 instances in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 5 instances in 1 package
10210
90
37
62