This repository has been archived by the owner on Mar 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
496 additions
and
49 deletions.
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
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
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
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
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 |
---|---|---|
@@ -0,0 +1,48 @@ | ||
"use strict"; | ||
|
||
var fs = require("fs"); | ||
var path = require("path"); | ||
var _ = require("lodash"); | ||
|
||
/** | ||
* Parse environment string and/or JSON file path into a full JS object. | ||
* | ||
* For use with environment variable specification. | ||
* | ||
* **Note**: Does synchronous file read. | ||
* | ||
* Parsing logic: | ||
* | ||
* 1. Try `opts.str` string if set | ||
* 2. If not, try `opts.path` file | ||
* | ||
* @param {Object} opts Options | ||
* @param {String} opts.str Stringified JSON object | ||
* @param {String} opts.path Path to JSON file | ||
* @returns {Object} Data object. | ||
*/ | ||
module.exports = { | ||
parse: function (opts) { | ||
var jsonStr = _.isUndefined(opts.str) ? null : opts.str; | ||
var jsonPath = _.isUndefined(opts.path) ? null : opts.path; | ||
|
||
// Validation: Programming error if thrown. | ||
if (jsonStr === null && jsonPath === null) { | ||
throw new Error("Must specify JSON string or path."); | ||
} | ||
|
||
// Try string first. | ||
if (jsonStr) { | ||
return JSON.parse(jsonStr); | ||
} | ||
|
||
// Try JSON file path next. | ||
if (jsonPath) { | ||
var data = fs.readFileSync(path.resolve(jsonPath)); | ||
return JSON.parse(data); | ||
} | ||
|
||
// Programming error. | ||
throw new Error("Invalid parsing path."); | ||
} | ||
}; |
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
Oops, something went wrong.