Skip to content

Commit

Permalink
Better environment variables loading
Browse files Browse the repository at this point in the history
  • Loading branch information
elegos committed Feb 21, 2018
1 parent 240f259 commit 9b51ebf
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
18 changes: 11 additions & 7 deletions lib/load-dotenv-variables.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { CompositeDisposable } from 'atom'
import env from 'node-env-file'
import fs from 'fs'

const loadEnvFile = (filePath) => {
const loadEnvFile = (filePath, notify) => {
try {
var file = fs.openSync(filePath, 'r')
var stats = fs.fstatSync(file)
Expand All @@ -21,7 +21,9 @@ const loadEnvFile = (filePath) => {
}

env(filePath)
atom.notifications.addSuccess('Load .env Variables: file correctly loaded', { detail: filePath })
if (notify) {
atom.notifications.addSuccess('Load .env Variables: file correctly loaded', { detail: filePath })
}

return true
}
Expand All @@ -44,16 +46,18 @@ export default {
'load-dotenv-variables:reload': () => this.reload()
}));

this.subscriptions.add(atom.project.onDidChangePaths(this.reload))
this.subscriptions.add(atom.whenShellEnvironmentLoaded(() => this.reload(false)))
this.subscriptions.add(atom.workspace.onDidOpen(() => this.reload(false)))
this.subscriptions.add(atom.project.onDidChangePaths(() => this.reload(false)))

this.reload()
this.reload(false)
},

deactivate() {
this.subscriptions.dispose();
},

reload() {
reload(notify) {
const dotEnvFileName = atom.config.get('load-dotenv-variables.envFile')

const dotEnvFiles = atom.project.getDirectories().map(dir => {
Expand All @@ -62,10 +66,10 @@ export default {

let loaded = false
dotEnvFiles.forEach(file => {
loaded = loaded || loadEnvFile(file)
loaded = loaded || loadEnvFile(file, notify)
})

if (!loaded) {
if (!loaded && notify) {
atom.notifications.addWarning('Load .env Variables: no dotenv file was loaded', { detail: `Searching for: ${dotEnvFileName}` })
}
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "load-dotenv-variables",
"main": "./lib/load-dotenv-variables",
"version": "1.0.0",
"version": "1.0.1",
"description": "Load dotenv variables from the project's root .env file",
"keywords": [],
"activationCommands": {},
Expand Down

0 comments on commit 9b51ebf

Please sign in to comment.