init
This commit is contained in:
159
node_modules/cosmiconfig/dist/createExplorer.js
generated
vendored
Normal file
159
node_modules/cosmiconfig/dist/createExplorer.js
generated
vendored
Normal file
@@ -0,0 +1,159 @@
|
||||
//
|
||||
'use strict';
|
||||
|
||||
const path = require('path');
|
||||
const loadPackageProp = require('./loadPackageProp');
|
||||
const loadRc = require('./loadRc');
|
||||
const loadJs = require('./loadJs');
|
||||
const loadDefinedFile = require('./loadDefinedFile');
|
||||
const funcRunner = require('./funcRunner');
|
||||
const getDirectory = require('./getDirectory');
|
||||
|
||||
module.exports = function createExplorer(options
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
) {
|
||||
// When `options.sync` is `false` (default),
|
||||
// these cache Promises that resolve with results, not the results themselves.
|
||||
const fileCache = options.cache ? new Map() : null;
|
||||
const directoryCache = options.cache ? new Map() : null;
|
||||
const transform = options.transform || identity;
|
||||
const packageProp = options.packageProp;
|
||||
|
||||
function clearFileCache() {
|
||||
if (fileCache) fileCache.clear();
|
||||
}
|
||||
|
||||
function clearDirectoryCache() {
|
||||
if (directoryCache) directoryCache.clear();
|
||||
}
|
||||
|
||||
function clearCaches() {
|
||||
clearFileCache();
|
||||
clearDirectoryCache();
|
||||
}
|
||||
|
||||
function throwError(error) {
|
||||
if (options.sync) {
|
||||
throw error;
|
||||
} else {
|
||||
return Promise.reject(error);
|
||||
}
|
||||
}
|
||||
|
||||
function load(
|
||||
searchPath ,
|
||||
configPath
|
||||
) {
|
||||
if (!searchPath) searchPath = process.cwd();
|
||||
if (!configPath && options.configPath) configPath = options.configPath;
|
||||
|
||||
if (configPath) {
|
||||
const absoluteConfigPath = path.resolve(process.cwd(), configPath);
|
||||
if (fileCache && fileCache.has(absoluteConfigPath)) {
|
||||
return fileCache.get(absoluteConfigPath);
|
||||
}
|
||||
|
||||
let load;
|
||||
if (path.basename(absoluteConfigPath) === 'package.json') {
|
||||
if (!packageProp) {
|
||||
return throwError(
|
||||
new Error(
|
||||
'Please specify the packageProp option. The configPath argument cannot point to a package.json file if packageProp is false.'
|
||||
)
|
||||
);
|
||||
}
|
||||
load = () =>
|
||||
loadPackageProp(path.dirname(absoluteConfigPath), {
|
||||
packageProp,
|
||||
sync: options.sync,
|
||||
});
|
||||
} else {
|
||||
load = () =>
|
||||
loadDefinedFile(absoluteConfigPath, {
|
||||
sync: options.sync,
|
||||
format: options.format,
|
||||
});
|
||||
}
|
||||
|
||||
const loadResult = load();
|
||||
const result =
|
||||
loadResult instanceof Promise
|
||||
? loadResult.then(transform)
|
||||
: transform(loadResult);
|
||||
if (fileCache) fileCache.set(absoluteConfigPath, result);
|
||||
return result;
|
||||
}
|
||||
|
||||
const absoluteSearchPath = path.resolve(process.cwd(), searchPath);
|
||||
const searchPathDir = getDirectory(absoluteSearchPath, options.sync);
|
||||
|
||||
return searchPathDir instanceof Promise
|
||||
? searchPathDir.then(searchDirectory)
|
||||
: searchDirectory(searchPathDir);
|
||||
}
|
||||
|
||||
function searchDirectory(
|
||||
directory
|
||||
) {
|
||||
if (directoryCache && directoryCache.has(directory)) {
|
||||
return directoryCache.get(directory);
|
||||
}
|
||||
|
||||
const result = funcRunner(!options.sync ? Promise.resolve() : undefined, [
|
||||
() => {
|
||||
if (!packageProp) return;
|
||||
return loadPackageProp(directory, {
|
||||
packageProp,
|
||||
sync: options.sync,
|
||||
});
|
||||
},
|
||||
result => {
|
||||
if (result || !options.rc) return result;
|
||||
return loadRc(path.join(directory, options.rc), {
|
||||
sync: options.sync,
|
||||
rcStrictJson: options.rcStrictJson,
|
||||
rcExtensions: options.rcExtensions,
|
||||
});
|
||||
},
|
||||
result => {
|
||||
if (result || !options.js) return result;
|
||||
return loadJs(path.join(directory, options.js), { sync: options.sync });
|
||||
},
|
||||
result => {
|
||||
if (result) return result;
|
||||
|
||||
const nextDirectory = path.dirname(directory);
|
||||
|
||||
if (nextDirectory === directory || directory === options.stopDir)
|
||||
return null;
|
||||
|
||||
return searchDirectory(nextDirectory);
|
||||
},
|
||||
transform,
|
||||
]);
|
||||
|
||||
if (directoryCache) directoryCache.set(directory, result);
|
||||
return result;
|
||||
}
|
||||
|
||||
return {
|
||||
load,
|
||||
clearFileCache,
|
||||
clearDirectoryCache,
|
||||
clearCaches,
|
||||
};
|
||||
};
|
||||
|
||||
function identity(x) {
|
||||
return x;
|
||||
}
|
22
node_modules/cosmiconfig/dist/funcRunner.js
generated
vendored
Normal file
22
node_modules/cosmiconfig/dist/funcRunner.js
generated
vendored
Normal file
@@ -0,0 +1,22 @@
|
||||
//
|
||||
'use strict';
|
||||
|
||||
const chainFuncsAsync = (result, func) => result.then(func);
|
||||
const chainFuncsSync = (result, func) => func(result);
|
||||
|
||||
/**
|
||||
* Runs the given functions sequentially. If the `init` param is a promise,
|
||||
* functions are chained using `p.then()`. Otherwise, functions are chained by passing
|
||||
* the result of each function to the next.
|
||||
*/
|
||||
module.exports = function funcRunner(
|
||||
init ,
|
||||
funcs
|
||||
) {
|
||||
const isAsync = init instanceof Promise;
|
||||
|
||||
return funcs.reduce(
|
||||
isAsync === true ? chainFuncsAsync : chainFuncsSync,
|
||||
init
|
||||
);
|
||||
};
|
23
node_modules/cosmiconfig/dist/getDirectory.js
generated
vendored
Normal file
23
node_modules/cosmiconfig/dist/getDirectory.js
generated
vendored
Normal file
@@ -0,0 +1,23 @@
|
||||
//
|
||||
'use strict';
|
||||
|
||||
const path = require('path');
|
||||
const isDirectory = require('is-directory');
|
||||
|
||||
module.exports = function getDirectory(
|
||||
filepath ,
|
||||
sync
|
||||
) {
|
||||
if (sync === true) {
|
||||
return isDirectory.sync(filepath) ? filepath : path.dirname(filepath);
|
||||
}
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
return isDirectory(filepath, (err, filepathIsDirectory) => {
|
||||
if (err) {
|
||||
return reject(err);
|
||||
}
|
||||
return resolve(filepathIsDirectory ? filepath : path.dirname(filepath));
|
||||
});
|
||||
});
|
||||
};
|
40
node_modules/cosmiconfig/dist/index.js
generated
vendored
Normal file
40
node_modules/cosmiconfig/dist/index.js
generated
vendored
Normal file
@@ -0,0 +1,40 @@
|
||||
//
|
||||
'use strict';
|
||||
|
||||
const os = require('os');
|
||||
const createExplorer = require('./createExplorer');
|
||||
|
||||
const homedir = os.homedir();
|
||||
|
||||
module.exports = function cosmiconfig(
|
||||
moduleName ,
|
||||
options
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
) {
|
||||
options = Object.assign(
|
||||
{},
|
||||
{
|
||||
packageProp: moduleName,
|
||||
rc: `.${moduleName}rc`,
|
||||
js: `${moduleName}.config.js`,
|
||||
rcStrictJson: false,
|
||||
stopDir: homedir,
|
||||
cache: true,
|
||||
sync: false,
|
||||
},
|
||||
options
|
||||
);
|
||||
|
||||
return createExplorer(options);
|
||||
};
|
98
node_modules/cosmiconfig/dist/loadDefinedFile.js
generated
vendored
Normal file
98
node_modules/cosmiconfig/dist/loadDefinedFile.js
generated
vendored
Normal file
@@ -0,0 +1,98 @@
|
||||
//
|
||||
'use strict';
|
||||
|
||||
const yaml = require('js-yaml');
|
||||
const requireFromString = require('require-from-string');
|
||||
const readFile = require('./readFile');
|
||||
const parseJson = require('./parseJson');
|
||||
const path = require('path');
|
||||
|
||||
module.exports = function loadDefinedFile(
|
||||
filepath ,
|
||||
options
|
||||
|
||||
|
||||
|
||||
) {
|
||||
function parseContent(content ) {
|
||||
if (!content) {
|
||||
throw new Error(`Config file is empty! Filepath - "${filepath}".`);
|
||||
}
|
||||
|
||||
let parsedConfig;
|
||||
|
||||
switch (options.format || inferFormat(filepath)) {
|
||||
case 'json':
|
||||
parsedConfig = parseJson(content, filepath);
|
||||
break;
|
||||
case 'yaml':
|
||||
parsedConfig = yaml.safeLoad(content, {
|
||||
filename: filepath,
|
||||
});
|
||||
break;
|
||||
case 'js':
|
||||
parsedConfig = requireFromString(content, filepath);
|
||||
break;
|
||||
default:
|
||||
parsedConfig = tryAllParsing(content, filepath);
|
||||
}
|
||||
|
||||
if (!parsedConfig) {
|
||||
throw new Error(`Failed to parse "${filepath}" as JSON, JS, or YAML.`);
|
||||
}
|
||||
|
||||
return {
|
||||
config: parsedConfig,
|
||||
filepath,
|
||||
};
|
||||
}
|
||||
|
||||
return !options.sync
|
||||
? readFile(filepath, { throwNotFound: true }).then(parseContent)
|
||||
: parseContent(readFile.sync(filepath, { throwNotFound: true }));
|
||||
};
|
||||
|
||||
function inferFormat(filepath ) {
|
||||
switch (path.extname(filepath)) {
|
||||
case '.js':
|
||||
return 'js';
|
||||
case '.json':
|
||||
return 'json';
|
||||
// istanbul ignore next
|
||||
case '.yml':
|
||||
case '.yaml':
|
||||
return 'yaml';
|
||||
default:
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
function tryAllParsing(content , filepath ) {
|
||||
return tryYaml(content, filepath, () => {
|
||||
return tryRequire(content, filepath, () => {
|
||||
return null;
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function tryYaml(content , filepath , cb ) {
|
||||
try {
|
||||
const 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();
|
||||
}
|
||||
}
|
23
node_modules/cosmiconfig/dist/loadJs.js
generated
vendored
Normal file
23
node_modules/cosmiconfig/dist/loadJs.js
generated
vendored
Normal file
@@ -0,0 +1,23 @@
|
||||
//
|
||||
'use strict';
|
||||
|
||||
const requireFromString = require('require-from-string');
|
||||
const readFile = require('./readFile');
|
||||
|
||||
module.exports = function loadJs(
|
||||
filepath ,
|
||||
options
|
||||
) {
|
||||
function parseJsFile(content ) {
|
||||
if (!content) return null;
|
||||
|
||||
return {
|
||||
config: requireFromString(content, filepath),
|
||||
filepath,
|
||||
};
|
||||
}
|
||||
|
||||
return !options.sync
|
||||
? readFile(filepath).then(parseJsFile)
|
||||
: parseJsFile(readFile.sync(filepath));
|
||||
};
|
32
node_modules/cosmiconfig/dist/loadPackageProp.js
generated
vendored
Normal file
32
node_modules/cosmiconfig/dist/loadPackageProp.js
generated
vendored
Normal file
@@ -0,0 +1,32 @@
|
||||
//
|
||||
'use strict';
|
||||
|
||||
const path = require('path');
|
||||
const readFile = require('./readFile');
|
||||
const parseJson = require('./parseJson');
|
||||
|
||||
module.exports = function loadPackageProp(
|
||||
packageDir ,
|
||||
options
|
||||
|
||||
|
||||
|
||||
) {
|
||||
const packagePath = path.join(packageDir, 'package.json');
|
||||
|
||||
function parseContent(content ) {
|
||||
if (!content) return null;
|
||||
const parsedContent = parseJson(content, packagePath);
|
||||
const packagePropValue = parsedContent[options.packageProp];
|
||||
if (!packagePropValue) return null;
|
||||
|
||||
return {
|
||||
config: packagePropValue,
|
||||
filepath: packagePath,
|
||||
};
|
||||
}
|
||||
|
||||
return !options.sync
|
||||
? readFile(packagePath).then(parseContent)
|
||||
: parseContent(readFile.sync(packagePath));
|
||||
};
|
110
node_modules/cosmiconfig/dist/loadRc.js
generated
vendored
Normal file
110
node_modules/cosmiconfig/dist/loadRc.js
generated
vendored
Normal file
@@ -0,0 +1,110 @@
|
||||
//
|
||||
'use strict';
|
||||
|
||||
const yaml = require('js-yaml');
|
||||
const requireFromString = require('require-from-string');
|
||||
const readFile = require('./readFile');
|
||||
const parseJson = require('./parseJson');
|
||||
const funcRunner = require('./funcRunner');
|
||||
|
||||
module.exports = function loadRc(
|
||||
filepath ,
|
||||
options
|
||||
|
||||
|
||||
|
||||
|
||||
) {
|
||||
if (!options.sync) {
|
||||
return readFile(filepath)
|
||||
.then(parseExtensionlessRcFile)
|
||||
.then(checkExtensionlessRcResult);
|
||||
} else {
|
||||
return checkExtensionlessRcResult(
|
||||
parseExtensionlessRcFile(readFile.sync(filepath))
|
||||
);
|
||||
}
|
||||
|
||||
function checkExtensionlessRcResult(result) {
|
||||
if (result) return result;
|
||||
if (options.rcExtensions) return loadRcWithExtensions();
|
||||
return null;
|
||||
}
|
||||
|
||||
function parseExtensionlessRcFile(content ) {
|
||||
if (!content) return null;
|
||||
const pasedConfig = options.rcStrictJson
|
||||
? parseJson(content, filepath)
|
||||
: yaml.safeLoad(content, { filename: filepath });
|
||||
return {
|
||||
config: pasedConfig,
|
||||
filepath,
|
||||
};
|
||||
}
|
||||
|
||||
function loadRcWithExtensions() {
|
||||
let foundConfig = null;
|
||||
return funcRunner(readRcFile('json'), [
|
||||
(jsonContent ) => {
|
||||
// Since this is the first try, config cannot have been found, so don't
|
||||
// check `if (foundConfig)`.
|
||||
if (jsonContent) {
|
||||
const successFilepath = `${filepath}.json`;
|
||||
foundConfig = {
|
||||
config: parseJson(jsonContent, successFilepath),
|
||||
filepath: successFilepath,
|
||||
};
|
||||
} else {
|
||||
return readRcFile('yaml');
|
||||
}
|
||||
},
|
||||
(yamlContent ) => {
|
||||
if (foundConfig) {
|
||||
return;
|
||||
} else if (yamlContent) {
|
||||
const successFilepath = `${filepath}.yaml`;
|
||||
foundConfig = {
|
||||
config: yaml.safeLoad(yamlContent, { filename: successFilepath }),
|
||||
filepath: successFilepath,
|
||||
};
|
||||
} else {
|
||||
return readRcFile('yml');
|
||||
}
|
||||
},
|
||||
(ymlContent ) => {
|
||||
if (foundConfig) {
|
||||
return;
|
||||
} else if (ymlContent) {
|
||||
const successFilepath = `${filepath}.yml`;
|
||||
foundConfig = {
|
||||
config: yaml.safeLoad(ymlContent, { filename: successFilepath }),
|
||||
filepath: successFilepath,
|
||||
};
|
||||
} else {
|
||||
return readRcFile('js');
|
||||
}
|
||||
},
|
||||
(jsContent ) => {
|
||||
if (foundConfig) {
|
||||
return;
|
||||
} else if (jsContent) {
|
||||
const successFilepath = `${filepath}.js`;
|
||||
foundConfig = {
|
||||
config: requireFromString(jsContent, successFilepath),
|
||||
filepath: successFilepath,
|
||||
};
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
},
|
||||
() => foundConfig,
|
||||
]);
|
||||
}
|
||||
|
||||
function readRcFile(extension ) {
|
||||
const filepathWithExtension = `${filepath}.${extension}`;
|
||||
return !options.sync
|
||||
? readFile(filepathWithExtension)
|
||||
: readFile.sync(filepathWithExtension);
|
||||
}
|
||||
};
|
16
node_modules/cosmiconfig/dist/parseJson.js
generated
vendored
Normal file
16
node_modules/cosmiconfig/dist/parseJson.js
generated
vendored
Normal file
@@ -0,0 +1,16 @@
|
||||
//
|
||||
'use strict';
|
||||
|
||||
const parseJson = require('parse-json');
|
||||
|
||||
module.exports = function parseJsonWrapper(
|
||||
json ,
|
||||
filepath
|
||||
) {
|
||||
try {
|
||||
return parseJson(json);
|
||||
} catch (err) {
|
||||
err.message = `JSON Error in ${filepath}:\n${err.message}`;
|
||||
throw err;
|
||||
}
|
||||
};
|
42
node_modules/cosmiconfig/dist/readFile.js
generated
vendored
Normal file
42
node_modules/cosmiconfig/dist/readFile.js
generated
vendored
Normal file
@@ -0,0 +1,42 @@
|
||||
//
|
||||
'use strict';
|
||||
|
||||
const fs = require('fs');
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
function readFile(filepath , options ) {
|
||||
options = options || {};
|
||||
const throwNotFound = options.throwNotFound || false;
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
fs.readFile(filepath, 'utf8', (err, content) => {
|
||||
if (err && err.code === 'ENOENT' && !throwNotFound) {
|
||||
return resolve(null);
|
||||
}
|
||||
if (err) return reject(err);
|
||||
resolve(content);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
readFile.sync = function readFileSync(
|
||||
filepath ,
|
||||
options
|
||||
) {
|
||||
options = options || {};
|
||||
const throwNotFound = options.throwNotFound || false;
|
||||
|
||||
try {
|
||||
return fs.readFileSync(filepath, 'utf8');
|
||||
} catch (err) {
|
||||
if (err.code === 'ENOENT' && !throwNotFound) {
|
||||
return null;
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = readFile;
|
Reference in New Issue
Block a user