aboutsummaryrefslogtreecommitdiff
path: root/node_modules/cosmiconfig/lib/loadDefinedFile.js
diff options
context:
space:
mode:
authorDimitri Staessens <[email protected]>2019-10-06 21:37:45 +0200
committerDimitri Staessens <[email protected]>2019-10-06 21:37:45 +0200
commit3c51c3be85bb0d1bdb87ea0d6632f1c256912f27 (patch)
treec7ccc8279b12c4f7bdbbb4270d617e48f51722e4 /node_modules/cosmiconfig/lib/loadDefinedFile.js
parent412c104bebc507bea9c94fd53b5bdc4b64cbfe31 (diff)
downloadwebsite-3c51c3be85bb0d1bdb87ea0d6632f1c256912f27.tar.gz
website-3c51c3be85bb0d1bdb87ea0d6632f1c256912f27.zip
build: Add some required modules for node
Diffstat (limited to 'node_modules/cosmiconfig/lib/loadDefinedFile.js')
-rw-r--r--node_modules/cosmiconfig/lib/loadDefinedFile.js66
1 files changed, 66 insertions, 0 deletions
diff --git a/node_modules/cosmiconfig/lib/loadDefinedFile.js b/node_modules/cosmiconfig/lib/loadDefinedFile.js
new file mode 100644
index 0000000..433d83c
--- /dev/null
+++ b/node_modules/cosmiconfig/lib/loadDefinedFile.js
@@ -0,0 +1,66 @@
+'use strict';
+
+var yaml = require('js-yaml');
+var requireFromString = require('require-from-string');
+var readFile = require('./readFile');
+var parseJson = require('./parseJson');
+
+module.exports = function (filepath, options) {
+ return readFile(filepath, { throwNotFound: true }).then(function (content) {
+ var parsedConfig = (function () {
+ switch (options.format) {
+ case 'json':
+ return parseJson(content, filepath);
+ case 'yaml':
+ return yaml.safeLoad(content, {
+ filename: filepath,
+ });
+ case 'js':
+ return requireFromString(content, filepath);
+ default:
+ return tryAllParsing(content, filepath);
+ }
+ })();
+
+ if (!parsedConfig) {
+ throw new Error(
+ 'Failed to parse "' + filepath + '" as JSON, JS, or YAML.'
+ );
+ }
+
+ return {
+ config: parsedConfig,
+ filepath: filepath,
+ };
+ });
+};
+
+function tryAllParsing(content, filepath) {
+ return tryYaml(content, filepath, function () {
+ return tryRequire(content, filepath, function () {
+ return null;
+ });
+ });
+}
+
+function tryYaml(content, filepath, cb) {
+ try {
+ var result = yaml.safeLoad(content, {
+ filename: filepath,
+ });
+ if (typeof result === 'string') {
+ return cb();
+ }
+ return result;
+ } catch (e) {
+ return cb();
+ }
+}
+
+function tryRequire(content, filepath, cb) {
+ try {
+ return requireFromString(content, filepath);
+ } catch (e) {
+ return cb();
+ }
+}