mirror of
https://github.com/snachodog/just-the-docs.git
synced 2025-09-13 05:13:33 -06:00
Initial commit
This commit is contained in:
109
node_modules/cosmiconfig/lib/createExplorer.js
generated
vendored
Normal file
109
node_modules/cosmiconfig/lib/createExplorer.js
generated
vendored
Normal file
@@ -0,0 +1,109 @@
|
||||
'use strict';
|
||||
|
||||
var path = require('path');
|
||||
var fs = require('fs');
|
||||
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) {
|
||||
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) {
|
||||
fs.stat(filepath, function (err, stats) {
|
||||
if (err) return reject(err);
|
||||
return resolve(stats.isDirectory());
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function identityPromise(x) {
|
||||
return Promise.resolve(x);
|
||||
}
|
66
node_modules/cosmiconfig/lib/loadDefinedFile.js
generated
vendored
Normal file
66
node_modules/cosmiconfig/lib/loadDefinedFile.js
generated
vendored
Normal file
@@ -0,0 +1,66 @@
|
||||
'use strict';
|
||||
|
||||
var yaml = require('js-yaml');
|
||||
var parseJson = require('parse-json');
|
||||
var requireFromString = require('require-from-string');
|
||||
var readFile = require('./readFile');
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
15
node_modules/cosmiconfig/lib/loadJs.js
generated
vendored
Normal file
15
node_modules/cosmiconfig/lib/loadJs.js
generated
vendored
Normal file
@@ -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,
|
||||
};
|
||||
});
|
||||
};
|
21
node_modules/cosmiconfig/lib/loadPackageProp.js
generated
vendored
Normal file
21
node_modules/cosmiconfig/lib/loadPackageProp.js
generated
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
'use strict';
|
||||
|
||||
var path = require('path');
|
||||
var parseJson = require('parse-json');
|
||||
var readFile = require('./readFile');
|
||||
|
||||
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,
|
||||
};
|
||||
});
|
||||
};
|
85
node_modules/cosmiconfig/lib/loadRc.js
generated
vendored
Normal file
85
node_modules/cosmiconfig/lib/loadRc.js
generated
vendored
Normal file
@@ -0,0 +1,85 @@
|
||||
'use strict';
|
||||
|
||||
var yaml = require('js-yaml');
|
||||
var parseJson = require('parse-json');
|
||||
var requireFromString = require('require-from-string');
|
||||
var readFile = require('./readFile');
|
||||
|
||||
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);
|
||||
}
|
||||
};
|
20
node_modules/cosmiconfig/lib/readFile.js
generated
vendored
Normal file
20
node_modules/cosmiconfig/lib/readFile.js
generated
vendored
Normal file
@@ -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);
|
||||
});
|
||||
});
|
||||
};
|
Reference in New Issue
Block a user