This repository has been archived by the owner on Jul 12, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
read from .env by default in inject() #70
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,8 @@ const dotenv = require('dotenv'); | |
|
||
const ensurePath = require('./ensure-path'); | ||
|
||
const { IS_TESTING } = require('../constants'); | ||
|
||
// Copy bundle environment into process.env, and vice versa, | ||
// for convenience and compatibility with native environment vars. | ||
const applyEnvironment = event => { | ||
|
@@ -31,10 +33,29 @@ const cleanEnvironment = () => { | |
} | ||
}; | ||
|
||
const localFilepath = filename => { | ||
return path.join(process.cwd(), filename || ''); | ||
}; | ||
|
||
const injectEnvironmentFile = filename => { | ||
filename = filename || '.environment'; | ||
const filepath = path.join(process.cwd(), filename); | ||
dotenv.load({ path: filepath }); | ||
if (filename) { | ||
filename = localFilepath(filename); | ||
} | ||
// reads ".env" if filename is falsy, needs full path otherwise | ||
let result = dotenv.load({ path: filename }); | ||
if (result.error) { | ||
// backwards compatibility | ||
result = dotenv.load({ path: localFilepath('.environment') }); | ||
if (result.parsed && !IS_TESTING) { | ||
console.log( | ||
[ | ||
'\nWARNING: `.environment` files will no longer be read by default in the next major version.', | ||
'Either rename your file to `.env` or explicitly call this function with a filename:', | ||
'\n zapier.tools.env.inject(".environment");\n\n' | ||
].join('\n') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We may want to hide this when running tests. |
||
); | ||
} | ||
} | ||
}; | ||
|
||
module.exports = { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,52 @@ | ||
require('should'); | ||
const should = require('should'); | ||
const mock = require('mock-fs'); | ||
|
||
const tools = require('../../src/tools/exported'); | ||
const fake_file = 'CITY="boulder"\nNAME="david"\nPIZZA="Blackjack"\n'; | ||
let env; | ||
|
||
describe('read env', () => { | ||
beforeEach(() => { | ||
env = Object.assign({}, process.env); | ||
}); | ||
|
||
it('should should only read .env', () => { | ||
mock({ | ||
'.environment': fake_file, | ||
'.environment': 'CITY="boulder"\nNAME="david"\n', | ||
'.env': 'PIZZA="Blackjack"\n', | ||
secrets: 'SECRET=very_secret_thing' | ||
}); | ||
}); | ||
|
||
it('should parse a config', done => { | ||
tools.env.inject(); | ||
process.env.PIZZA.should.equal('Blackjack'); | ||
done(); | ||
should.not.exist(process.env.CITY); | ||
}); | ||
|
||
it('should parse a with filename', done => { | ||
// this is temporary in v6, removed for v7 | ||
it('should read .environment if .env is missing', () => { | ||
mock({ | ||
'.environment': 'CITY="boulder"\nNAME="david"\n' | ||
}); | ||
|
||
tools.env.inject(); | ||
process.env.CITY.should.equal('boulder'); | ||
should.not.exist(process.env.PIZZA); | ||
}); | ||
|
||
it('should parse a with filename, ignore defaults', () => { | ||
mock({ | ||
'.environment': 'CITY="boulder"\nNAME="david"\n', | ||
'.env': 'PIZZA="Blackjack"\n', | ||
secrets: 'SECRET=very_secret_thing' | ||
}); | ||
|
||
tools.env.inject('secrets'); | ||
process.env.SECRET.should.equal('very_secret_thing'); | ||
done(); | ||
should.not.exist(process.env.CITY); | ||
should.not.exist(process.env.PIZZA); | ||
}); | ||
|
||
afterEach(() => { | ||
mock.restore(); | ||
process.env = env; | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if it's valid use case: Could a user inject an environment file using an absolute path? For example:
If so, we may want to adjust a bit here because
path.join
doesn't seem to handle absolute paths:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
they couldn't, but they also couldn't before. In master, we resolved their filename to the local directory anyway. We could expand that, but that seems like a more disruptive change (instead of renaming, they have to pass the whole path in).