-
Notifications
You must be signed in to change notification settings - Fork 15
read from .env by default in inject() #70
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,10 +31,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) { | ||
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 = { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,55 @@ | ||
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', () => { | ||
describe.only('read env', () => { | ||
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. Should we remove 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. oh good catch. that was just for me, since I didn't want to run the whole suite every time. Will remove. |
||
beforeEach(() => { | ||
env = Object.assign({}, process.env); | ||
}); | ||
|
||
it('should should only read .env', done => { | ||
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'); | ||
should.not.exist(process.env.CITY); | ||
done(); | ||
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. The tests are synchronous code, so I guess we don't need |
||
}); | ||
|
||
// this is temporary in v6, removed for v7 | ||
it('should read .environment if .env is missing', done => { | ||
mock({ | ||
'.environment': 'CITY="boulder"\nNAME="david"\n' | ||
}); | ||
|
||
tools.env.inject(); | ||
process.env.CITY.should.equal('boulder'); | ||
should.not.exist(process.env.PIZZA); | ||
done(); | ||
}); | ||
|
||
it('should parse a with filename', done => { | ||
it('should parse a with filename, ignore defaults', done => { | ||
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'); | ||
should.not.exist(process.env.CITY); | ||
should.not.exist(process.env.PIZZA); | ||
done(); | ||
}); | ||
|
||
afterEach(() => { | ||
mock.restore(); | ||
process.env = env; | ||
}); | ||
}); |
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).