aboutsummaryrefslogtreecommitdiff
path: root/node_modules/cosmiconfig/lib
diff options
context:
space:
mode:
Diffstat (limited to 'node_modules/cosmiconfig/lib')
-rw-r--r--node_modules/cosmiconfig/lib/createExplorer.js113
-rw-r--r--node_modules/cosmiconfig/lib/loadDefinedFile.js66
-rw-r--r--node_modules/cosmiconfig/lib/loadJs.js15
-rw-r--r--node_modules/cosmiconfig/lib/loadPackageProp.js21
-rw-r--r--node_modules/cosmiconfig/lib/loadRc.js85
-rw-r--r--node_modules/cosmiconfig/lib/parseJson.js12
-rw-r--r--node_modules/cosmiconfig/lib/readFile.js20
7 files changed, 332 insertions, 0 deletions
diff --git a/node_modules/cosmiconfig/lib/createExplorer.js b/node_modules/cosmiconfig/lib/createExplorer.js
new file mode 100644
index 0000000..a79beab
--- /dev/null
+++ b/node_modules/cosmiconfig/lib/createExplorer.js
@@ -0,0 +1,113 @@
+'use strict';
+
+var path = require('path');
+var isDir = require('is-directory');
+var loadPackageProp = require('./loadPackageProp');
+var loadRc = require('./loadRc');
+var loadJs = require('./loadJs');
+var loadDefinedFile = require('./loadDefinedFile');
+
+module.exports = function (options) {
+ // These cache Promises that resolve with results, not the results themselves
+ var fileCache = (options.cache) ? new Map() : null;
+ var directoryCache = (options.cache) ? new Map() : null;
+ var transform = options.transform || identityPromise;
+
+ function clearFileCache() {
+ if (fileCache) fileCache.clear();
+ }
+
+ function clearDirectoryCache() {
+ if (directoryCache) directoryCache.clear();
+ }
+
+ function clearCaches() {
+ clearFileCache();
+ clearDirectoryCache();
+ }
+
+ function load(searchPath, configPath) {
+ if (!configPath && options.configPath) {
+ configPath = options.configPath;
+ }
+
+ if (configPath) {
+ var absoluteConfigPath = path.resolve(process.cwd(), configPath);
+ if (fileCache && fileCache.has(absoluteConfigPath)) {
+ return fileCache.get(absoluteConfigPath);
+ }
+ var result = loadDefinedFile(absoluteConfigPath, options)
+ .then(transform);
+ if (fileCache) fileCache.set(absoluteConfigPath, result);
+ return result;
+ }
+
+ if (!searchPath) return Promise.resolve(null);
+
+ var absoluteSearchPath = path.resolve(process.cwd(), searchPath);
+
+ return isDirectory(absoluteSearchPath)
+ .then(function (searchPathIsDirectory) {
+ var directory = (searchPathIsDirectory)
+ ? absoluteSearchPath
+ : path.dirname(absoluteSearchPath);
+ return searchDirectory(directory);
+ });
+ }
+
+ function searchDirectory(directory) {
+ if (directoryCache && directoryCache.has(directory)) {
+ return directoryCache.get(directory);
+ }
+
+ var result = Promise.resolve()
+ .then(function () {
+ if (!options.packageProp) return;
+ return loadPackageProp(directory, options);
+ })
+ .then(function (result) {
+ if (result || !options.rc) return result;
+ return loadRc(path.join(directory, options.rc), options);
+ })
+ .then(function (result) {
+ if (result || !options.js) return result;
+ return loadJs(path.join(directory, options.js));
+ })
+ .then(function (result) {
+ if (result) return result;
+
+ var splitPath = directory.split(path.sep);
+ var nextDirectory = (splitPath.length > 1)
+ ? splitPath.slice(0, -1).join(path.sep)
+ : null;
+
+ if (!nextDirectory || directory === options.stopDir) return null;
+
+ return searchDirectory(nextDirectory);
+ })
+ .then(transform);
+
+ if (directoryCache) directoryCache.set(directory, result);
+ return result;
+ }
+
+ return {
+ load: load,
+ clearFileCache: clearFileCache,
+ clearDirectoryCache: clearDirectoryCache,
+ clearCaches: clearCaches,
+ };
+};
+
+function isDirectory(filepath) {
+ return new Promise(function (resolve, reject) {
+ return isDir(filepath, function (err, dir) {
+ if (err) return reject(err);
+ return resolve(dir);
+ });
+ });
+}
+
+function identityPromise(x) {
+ return Promise.resolve(x);
+}
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();
+ }
+}
diff --git a/node_modules/cosmiconfig/lib/loadJs.js b/node_modules/cosmiconfig/lib/loadJs.js
new file mode 100644
index 0000000..c08d524
--- /dev/null
+++ b/node_modules/cosmiconfig/lib/loadJs.js
@@ -0,0 +1,15 @@
+'use strict';
+
+var requireFromString = require('require-from-string');
+var readFile = require('./readFile');
+
+module.exports = function (filepath) {
+ return readFile(filepath).then(function (content) {
+ if (!content) return null;
+
+ return {
+ config: requireFromString(content, filepath),
+ filepath: filepath,
+ };
+ });
+};
diff --git a/node_modules/cosmiconfig/lib/loadPackageProp.js b/node_modules/cosmiconfig/lib/loadPackageProp.js
new file mode 100644
index 0000000..01e759f
--- /dev/null
+++ b/node_modules/cosmiconfig/lib/loadPackageProp.js
@@ -0,0 +1,21 @@
+'use strict';
+
+var path = require('path');
+var readFile = require('./readFile');
+var parseJson = require('./parseJson');
+
+module.exports = function (packageDir, options) {
+ var packagePath = path.join(packageDir, 'package.json');
+
+ return readFile(packagePath).then(function (content) {
+ if (!content) return null;
+ var parsedContent = parseJson(content, packagePath);
+ var packagePropValue = parsedContent[options.packageProp];
+ if (!packagePropValue) return null;
+
+ return {
+ config: packagePropValue,
+ filepath: packagePath,
+ };
+ });
+};
diff --git a/node_modules/cosmiconfig/lib/loadRc.js b/node_modules/cosmiconfig/lib/loadRc.js
new file mode 100644
index 0000000..cbe1d67
--- /dev/null
+++ b/node_modules/cosmiconfig/lib/loadRc.js
@@ -0,0 +1,85 @@
+'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 loadExtensionlessRc().then(function (result) {
+ if (result) return result;
+ if (options.rcExtensions) return loadRcWithExtensions();
+ return null;
+ });
+
+ function loadExtensionlessRc() {
+ return readRcFile().then(function (content) {
+ if (!content) return null;
+
+ var pasedConfig = (options.rcStrictJson)
+ ? parseJson(content, filepath)
+ : yaml.safeLoad(content, {
+ filename: filepath,
+ });
+ return {
+ config: pasedConfig,
+ filepath: filepath,
+ };
+ });
+ }
+
+ function loadRcWithExtensions() {
+ return readRcFile('json').then(function (content) {
+ if (content) {
+ var successFilepath = filepath + '.json';
+ return {
+ config: parseJson(content, successFilepath),
+ filepath: successFilepath,
+ };
+ }
+ // If not content was found in the file with extension,
+ // try the next possible extension
+ return readRcFile('yaml');
+ }).then(function (content) {
+ if (content) {
+ // If the previous check returned an object with a config
+ // property, then it succeeded and this step can be skipped
+ if (content.config) return content;
+ // If it just returned a string, then *this* check succeeded
+ var successFilepath = filepath + '.yaml';
+ return {
+ config: yaml.safeLoad(content, { filename: successFilepath }),
+ filepath: successFilepath,
+ };
+ }
+ return readRcFile('yml');
+ }).then(function (content) {
+ if (content) {
+ if (content.config) return content;
+ var successFilepath = filepath + '.yml';
+ return {
+ config: yaml.safeLoad(content, { filename: successFilepath }),
+ filepath: successFilepath,
+ };
+ }
+ return readRcFile('js');
+ }).then(function (content) {
+ if (content) {
+ if (content.config) return content;
+ var successFilepath = filepath + '.js';
+ return {
+ config: requireFromString(content, successFilepath),
+ filepath: successFilepath,
+ };
+ }
+ return null;
+ });
+ }
+
+ function readRcFile(extension) {
+ var filepathWithExtension = (extension)
+ ? filepath + '.' + extension
+ : filepath;
+ return readFile(filepathWithExtension);
+ }
+};
diff --git a/node_modules/cosmiconfig/lib/parseJson.js b/node_modules/cosmiconfig/lib/parseJson.js
new file mode 100644
index 0000000..5541daf
--- /dev/null
+++ b/node_modules/cosmiconfig/lib/parseJson.js
@@ -0,0 +1,12 @@
+'use strict';
+
+var parseJson = require('parse-json');
+
+module.exports = function (json, filepath) {
+ try {
+ return parseJson(json);
+ } catch (err) {
+ err.message = 'JSON Error in ' + filepath + ':\n' + err.message;
+ throw err;
+ }
+};
diff --git a/node_modules/cosmiconfig/lib/readFile.js b/node_modules/cosmiconfig/lib/readFile.js
new file mode 100644
index 0000000..95e1e2e
--- /dev/null
+++ b/node_modules/cosmiconfig/lib/readFile.js
@@ -0,0 +1,20 @@
+'use strict';
+
+var fs = require('fs');
+
+module.exports = function (filepath, options) {
+ options = options || {};
+ options.throwNotFound = options.throwNotFound || false;
+
+ return new Promise(function (resolve, reject) {
+ fs.readFile(filepath, 'utf8', function (err, content) {
+ if (err && err.code === 'ENOENT' && !options.throwNotFound) {
+ return resolve(null);
+ }
+
+ if (err) return reject(err);
+
+ resolve(content);
+ });
+ });
+};