dotenv-assert
Advanced tools
71
index.js
'use strict'; | ||
module.exports = function(options) { | ||
module.exports = function(options, callback) { | ||
var fs = require('fs'); | ||
@@ -12,2 +12,3 @@ var findParentDir = require('find-parent-dir'); | ||
options = options || {}; | ||
callback = callback || function() {}; | ||
@@ -56,2 +57,7 @@ function getSettingsFromFile(options) { | ||
function CallbackError(message) { | ||
this.message = message; | ||
this.name = ' Callback Error'; | ||
} | ||
function FileNotFoundError(message) { | ||
@@ -64,3 +70,3 @@ this.message = message; | ||
if (typeof options !== 'object' || Object.prototype.toString.call(options) === '[object Array]') { | ||
errorMessage = '\n As of version 2.0.0, you can only pass an Options Object ' + | ||
errorMessage = ' As of version 2.1.0, you can only pass an Options Object and Callback Function ' + | ||
'or empty arguments to dotenv-assert. Please see the official README.md for details.'; | ||
@@ -70,45 +76,46 @@ throw new OptionsError(errorMessage); | ||
if (!options.filePath) { | ||
errorMessage = '\n An Options Object must contain a "filePath" property, ' + | ||
'e.g { filepath: "../settings/assert.env" }'; | ||
throw new OptionsError(errorMessage); | ||
assertFilePath = options.filePath || assertFilePath; | ||
if (typeof callback !== 'function') { | ||
errorMessage = ' The callback is not a function'; | ||
throw new CallbackError(errorMessage); | ||
} | ||
assertFilePath = options.filePath; | ||
} | ||
// look for explicit file location | ||
if (assertFilePath.indexOf('/') > -1) { | ||
// look for explicit file location | ||
fs.exists(assertFilePath, function(exists) { | ||
if (exists) { | ||
settings = getSettingsFromFile({ | ||
'assertFilePath': assertFilePath | ||
}); | ||
assertSettings(settings); | ||
return true; | ||
if (!exists) { | ||
errorMessage = assertFilePath + ' does not exist.'; | ||
throw new FileNotFoundError(errorMessage); | ||
} | ||
errorMessage = assertFilePath + ' does not exist.'; | ||
throw new FileNotFoundError(errorMessage); | ||
settings = getSettingsFromFile({ | ||
'assertFilePath': assertFilePath | ||
}); | ||
assertSettings(settings); | ||
callback(); | ||
}); | ||
} | ||
} else { | ||
// find the file in $CWD or the nearest parent directory | ||
findParentDir(CURRENT_WORKING_DIRECTORY, assertFilePath, function(error, directory) { | ||
if (error || directory === null) { | ||
errorMessage = assertFilePath + ' cannot be found.'; | ||
throw new FileNotFoundError(errorMessage); | ||
} | ||
// find the file in $CWD or the nearest parent directory | ||
findParentDir(CURRENT_WORKING_DIRECTORY, assertFilePath, function(error, directory) { | ||
if (error || directory === null) { | ||
errorMessage = assertFilePath + ' cannot be found.'; | ||
throw new FileNotFoundError(errorMessage); | ||
} | ||
assertFilePath = directory + '/' + assertFilePath; | ||
assertFilePath = directory + '/' + assertFilePath; | ||
settings = getSettingsFromFile({ | ||
'assertFilePath': assertFilePath | ||
}); | ||
settings = getSettingsFromFile({ | ||
'assertFilePath': assertFilePath | ||
}); | ||
assertSettings(settings); | ||
assertSettings(settings); | ||
return true; | ||
}); | ||
callback(); | ||
}); | ||
} | ||
}; |
{ | ||
"name": "dotenv-assert", | ||
"version": "2.0.0", | ||
"version": "2.1.0", | ||
"description": "Requires specified environment settings to exist", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -5,4 +5,5 @@ # dotenv-assert | ||
## Version 2.0.0 | ||
- This module version no longer accepts an Array of settings to assert. Instead, an Options Object: `{ filePath: 'somefile.ext' }` or empty arguments are required. | ||
## Version 2.1.0 | ||
- This module version now accepts an optional callback function. | ||
- It is recommended that you wrap your application in the callback, to insure that the async functions used by dotenv-assert complete before attempting to start. | ||
@@ -30,5 +31,13 @@ ## Prerequisites | ||
* from the nearest parent directory where assert.env is found | ||
* (without a specified callback, not recommended) | ||
*/ | ||
require('dotenv-assert')(); | ||
// or, same as above (with a callback, recommended) | ||
require('dotenv-assert')({}, function() { | ||
console.log('Environment Settings Asserted!'); | ||
}); | ||
// you may include a callback on all further examples | ||
/** | ||
@@ -74,12 +83,12 @@ * or, specify a custom file location | ||
require('dotenv-assert')(); | ||
require('dotenv-assert')({}, function() { | ||
var http = require('http'); | ||
var http = require('http'); | ||
http.createServer(function (request, response) { | ||
response.writeHead(200, {'Content-Type': 'text/plain'}); | ||
response.end('Hello World\n'); | ||
}).listen(process.env.PORT, process.env.IP); | ||
http.createServer(function (request, response) { | ||
response.writeHead(200, {'Content-Type': 'text/plain'}); | ||
response.end('Hello World\n'); | ||
}).listen(process.env.PORT, process.env.IP); | ||
console.log('Server running at http://' + process.env.IP + ':' + process.env.PORT + '/'); | ||
console.log('Server running at http://' + process.env.IP + ':' + process.env.PORT + '/'); | ||
}); | ||
``` | ||
@@ -94,2 +103,7 @@ | ||
## CHANGELOG | ||
- 2.0.0 | ||
No longer accepts an Array of settings to assert. Instead, an Options Object: `{ filePath: 'somefile.ext' }` or empty arguments are required. | ||
## LICENSE | ||
@@ -96,0 +110,0 @@ |
@@ -0,10 +1,22 @@ | ||
'use strict'; | ||
process.env.HELLO = 'WORLD'; | ||
process.env.GOODBYE = 'WORLD'; | ||
require('../../../../index')(); | ||
console.log('✓ Found default assert.env file in a parent directory.'); | ||
try { | ||
require('../../../../index')(); | ||
} catch (error) { | ||
console.log('UNEXPECTED ERROR'); | ||
throw error; | ||
} finally { | ||
console.log('_ No Options and No Callback, find assert file in parent directory'); | ||
} | ||
require('../../../../index')({}, function() { | ||
console.log('✓ Found default assert.env file in a parent directory.'); | ||
}); | ||
require('../../../../index')({ | ||
filePath: 'assert.env.settings' | ||
}); | ||
console.log('✓ Found custom filename: assert.env.settings in a parent directory'); | ||
}, function() { | ||
console.log('✓ Found custom filename: assert.env.settings in a parent directory'); | ||
}); |
@@ -0,3 +1,7 @@ | ||
'use strict'; | ||
process.env.HELLO = 'WORLD'; | ||
process.env.GOODBYE = 'WORLD'; | ||
// exceptions expected | ||
try { | ||
@@ -17,26 +21,43 @@ require('../index')('nada'); | ||
// no exceptions expected | ||
try { | ||
require('../index')({}); | ||
} catch (error) { | ||
console.log('✓ CAUGHT ERROR'); | ||
console.log(['error', error]); | ||
console.log('UNEXPECTED ERROR'); | ||
// console.log(['error', error]); | ||
throw error; | ||
} finally { | ||
console.log('_ Empty Options and No Callback'); | ||
} | ||
process.env.GOODBYE = 'WORLD'; | ||
try { | ||
require('../index')(); | ||
} catch (error) { | ||
console.log('UNEXPECTED ERROR'); | ||
// console.log(['error', error]); | ||
throw error; | ||
} finally { | ||
console.log('_ No Options and No Callback'); | ||
} | ||
require('../index')({ | ||
filePath: './assert.env.settings' | ||
}, function() { | ||
console.log('✓ Found custom ./assert.env.settings file.'); | ||
}); | ||
console.log('✓ Found custom ./assert.env.settings file.'); | ||
require('../index')(); | ||
console.log('✓ Found default assert.env file in either the CWD or a parent directory.'); | ||
require('../index')({}, function() { | ||
console.log('✓ Found default assert.env file in either the CWD or a parent directory.'); | ||
}); | ||
// Dunno how to gracefully and automatically test for an unhandledException; | ||
// so, I'm not for now. | ||
// try{ | ||
// require('../index')({filePath: './assert.xxx'}); | ||
// } catch(error) { | ||
// try { | ||
// require('../index')({ | ||
// filePath: './assert.xxx' | ||
// }); | ||
// } catch (error) { | ||
// console.log('✓ CAUGHT ERROR'); | ||
// console.log(['error', error]); | ||
// } |
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
9585
16.1%180
24.14%114
14%