nav tabs on admin dashboard
This commit is contained in:
+288
-128
@@ -2,158 +2,318 @@
|
||||
'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 get = require('lodash.get');
|
||||
const loaders = require('./loaders');
|
||||
const readFile = require('./readFile');
|
||||
const cacheWrapper = require('./cacheWrapper');
|
||||
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;
|
||||
const MODE_SYNC = 'sync';
|
||||
|
||||
function clearFileCache() {
|
||||
if (fileCache) fileCache.clear();
|
||||
// An object value represents a config object.
|
||||
// null represents that the loader did not find anything relevant.
|
||||
// undefined represents that the loader found something relevant
|
||||
// but it was empty.
|
||||
|
||||
|
||||
class Explorer {
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
constructor(options ) {
|
||||
this.loadCache = options.cache ? new Map() : null;
|
||||
this.loadSyncCache = options.cache ? new Map() : null;
|
||||
this.searchCache = options.cache ? new Map() : null;
|
||||
this.searchSyncCache = options.cache ? new Map() : null;
|
||||
this.config = options;
|
||||
this.validateConfig();
|
||||
}
|
||||
|
||||
function clearDirectoryCache() {
|
||||
if (directoryCache) directoryCache.clear();
|
||||
}
|
||||
|
||||
function clearCaches() {
|
||||
clearFileCache();
|
||||
clearDirectoryCache();
|
||||
}
|
||||
|
||||
function throwError(error) {
|
||||
if (options.sync) {
|
||||
throw error;
|
||||
} else {
|
||||
return Promise.reject(error);
|
||||
clearLoadCache() {
|
||||
if (this.loadCache) {
|
||||
this.loadCache.clear();
|
||||
}
|
||||
if (this.loadSyncCache) {
|
||||
this.loadSyncCache.clear();
|
||||
}
|
||||
}
|
||||
|
||||
function load(
|
||||
searchPath ,
|
||||
configPath
|
||||
) {
|
||||
if (!searchPath) searchPath = process.cwd();
|
||||
if (!configPath && options.configPath) configPath = options.configPath;
|
||||
clearSearchCache() {
|
||||
if (this.searchCache) {
|
||||
this.searchCache.clear();
|
||||
}
|
||||
if (this.searchSyncCache) {
|
||||
this.searchSyncCache.clear();
|
||||
}
|
||||
}
|
||||
|
||||
if (configPath) {
|
||||
const absoluteConfigPath = path.resolve(process.cwd(), configPath);
|
||||
if (fileCache && fileCache.has(absoluteConfigPath)) {
|
||||
return fileCache.get(absoluteConfigPath);
|
||||
clearCaches() {
|
||||
this.clearLoadCache();
|
||||
this.clearSearchCache();
|
||||
}
|
||||
|
||||
validateConfig() {
|
||||
const config = this.config;
|
||||
|
||||
config.searchPlaces.forEach(place => {
|
||||
const loaderKey = path.extname(place) || 'noExt';
|
||||
const loader = config.loaders[loaderKey];
|
||||
if (!loader) {
|
||||
throw new Error(
|
||||
`No loader specified for ${getExtensionDescription(
|
||||
place
|
||||
)}, so searchPlaces item "${place}" is invalid`
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
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.'
|
||||
)
|
||||
);
|
||||
search(searchFrom ) {
|
||||
searchFrom = searchFrom || process.cwd();
|
||||
return getDirectory(searchFrom).then(dir => {
|
||||
return this.searchFromDirectory(dir);
|
||||
});
|
||||
}
|
||||
|
||||
searchFromDirectory(dir ) {
|
||||
const absoluteDir = path.resolve(process.cwd(), dir);
|
||||
const run = () => {
|
||||
return this.searchDirectory(absoluteDir).then(result => {
|
||||
const nextDir = this.nextDirectoryToSearch(absoluteDir, result);
|
||||
if (nextDir) {
|
||||
return this.searchFromDirectory(nextDir);
|
||||
}
|
||||
load = () =>
|
||||
loadPackageProp(path.dirname(absoluteConfigPath), {
|
||||
packageProp,
|
||||
sync: options.sync,
|
||||
});
|
||||
} else {
|
||||
load = () =>
|
||||
loadDefinedFile(absoluteConfigPath, {
|
||||
sync: options.sync,
|
||||
format: options.format,
|
||||
});
|
||||
}
|
||||
return this.config.transform(result);
|
||||
});
|
||||
};
|
||||
|
||||
const loadResult = load();
|
||||
const result =
|
||||
loadResult instanceof Promise
|
||||
? loadResult.then(transform)
|
||||
: transform(loadResult);
|
||||
if (fileCache) fileCache.set(absoluteConfigPath, result);
|
||||
return result;
|
||||
if (this.searchCache) {
|
||||
return cacheWrapper(this.searchCache, absoluteDir, run);
|
||||
}
|
||||
|
||||
const absoluteSearchPath = path.resolve(process.cwd(), searchPath);
|
||||
const searchPathDir = getDirectory(absoluteSearchPath, options.sync);
|
||||
|
||||
return searchPathDir instanceof Promise
|
||||
? searchPathDir.then(searchDirectory)
|
||||
: searchDirectory(searchPathDir);
|
||||
return run();
|
||||
}
|
||||
|
||||
function searchDirectory(
|
||||
directory
|
||||
) {
|
||||
if (directoryCache && directoryCache.has(directory)) {
|
||||
return directoryCache.get(directory);
|
||||
searchSync(searchFrom ) {
|
||||
searchFrom = searchFrom || process.cwd();
|
||||
const dir = getDirectory.sync(searchFrom);
|
||||
return this.searchFromDirectorySync(dir);
|
||||
}
|
||||
|
||||
searchFromDirectorySync(dir ) {
|
||||
const absoluteDir = path.resolve(process.cwd(), dir);
|
||||
const run = () => {
|
||||
const result = this.searchDirectorySync(absoluteDir);
|
||||
const nextDir = this.nextDirectoryToSearch(absoluteDir, result);
|
||||
if (nextDir) {
|
||||
return this.searchFromDirectorySync(nextDir);
|
||||
}
|
||||
return this.config.transform(result);
|
||||
};
|
||||
|
||||
if (this.searchSyncCache) {
|
||||
return cacheWrapper(this.searchSyncCache, absoluteDir, run);
|
||||
}
|
||||
return run();
|
||||
}
|
||||
|
||||
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;
|
||||
searchDirectory(dir ) {
|
||||
return this.config.searchPlaces.reduce((prevResultPromise, place) => {
|
||||
return prevResultPromise.then(prevResult => {
|
||||
if (this.shouldSearchStopWithResult(prevResult)) {
|
||||
return prevResult;
|
||||
}
|
||||
return this.loadSearchPlace(dir, place);
|
||||
});
|
||||
}, Promise.resolve(null));
|
||||
}
|
||||
|
||||
const nextDirectory = path.dirname(directory);
|
||||
|
||||
if (nextDirectory === directory || directory === options.stopDir)
|
||||
return null;
|
||||
|
||||
return searchDirectory(nextDirectory);
|
||||
},
|
||||
transform,
|
||||
]);
|
||||
|
||||
if (directoryCache) directoryCache.set(directory, result);
|
||||
searchDirectorySync(dir ) {
|
||||
let result = null;
|
||||
for (const place of this.config.searchPlaces) {
|
||||
result = this.loadSearchPlaceSync(dir, place);
|
||||
if (this.shouldSearchStopWithResult(result)) break;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
shouldSearchStopWithResult(result ) {
|
||||
if (result === null) return false;
|
||||
if (result.isEmpty && this.config.ignoreEmptySearchPlaces) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
loadSearchPlace(dir , place ) {
|
||||
const filepath = path.join(dir, place);
|
||||
return readFile(filepath).then(content => {
|
||||
return this.createCosmiconfigResult(filepath, content);
|
||||
});
|
||||
}
|
||||
|
||||
loadSearchPlaceSync(dir , place ) {
|
||||
const filepath = path.join(dir, place);
|
||||
const content = readFile.sync(filepath);
|
||||
return this.createCosmiconfigResultSync(filepath, content);
|
||||
}
|
||||
|
||||
nextDirectoryToSearch(
|
||||
currentDir ,
|
||||
currentResult
|
||||
) {
|
||||
if (this.shouldSearchStopWithResult(currentResult)) {
|
||||
return null;
|
||||
}
|
||||
const nextDir = nextDirUp(currentDir);
|
||||
if (nextDir === currentDir || currentDir === this.config.stopDir) {
|
||||
return null;
|
||||
}
|
||||
return nextDir;
|
||||
}
|
||||
|
||||
loadPackageProp(filepath , content ) {
|
||||
const parsedContent = loaders.loadJson(filepath, content);
|
||||
const packagePropValue = get(parsedContent, this.config.packageProp);
|
||||
return packagePropValue || null;
|
||||
}
|
||||
|
||||
getLoaderEntryForFile(filepath ) {
|
||||
if (path.basename(filepath) === 'package.json') {
|
||||
const loader = this.loadPackageProp.bind(this);
|
||||
return { sync: loader, async: loader };
|
||||
}
|
||||
|
||||
const loaderKey = path.extname(filepath) || 'noExt';
|
||||
return this.config.loaders[loaderKey] || {};
|
||||
}
|
||||
|
||||
getSyncLoaderForFile(filepath ) {
|
||||
const entry = this.getLoaderEntryForFile(filepath);
|
||||
if (!entry.sync) {
|
||||
throw new Error(
|
||||
`No sync loader specified for ${getExtensionDescription(filepath)}`
|
||||
);
|
||||
}
|
||||
return entry.sync;
|
||||
}
|
||||
|
||||
getAsyncLoaderForFile(filepath ) {
|
||||
const entry = this.getLoaderEntryForFile(filepath);
|
||||
const loader = entry.async || entry.sync;
|
||||
if (!loader) {
|
||||
throw new Error(
|
||||
`No async loader specified for ${getExtensionDescription(filepath)}`
|
||||
);
|
||||
}
|
||||
return loader;
|
||||
}
|
||||
|
||||
loadFileContent(
|
||||
mode ,
|
||||
filepath ,
|
||||
content
|
||||
) {
|
||||
if (content === null) {
|
||||
return null;
|
||||
}
|
||||
if (content.trim() === '') {
|
||||
return undefined;
|
||||
}
|
||||
const loader =
|
||||
mode === MODE_SYNC
|
||||
? this.getSyncLoaderForFile(filepath)
|
||||
: this.getAsyncLoaderForFile(filepath);
|
||||
return loader(filepath, content);
|
||||
}
|
||||
|
||||
loadedContentToCosmiconfigResult(
|
||||
filepath ,
|
||||
loadedContent
|
||||
) {
|
||||
if (loadedContent === null) {
|
||||
return null;
|
||||
}
|
||||
if (loadedContent === undefined) {
|
||||
return { filepath, config: undefined, isEmpty: true };
|
||||
}
|
||||
return { config: loadedContent, filepath };
|
||||
}
|
||||
|
||||
createCosmiconfigResult(
|
||||
filepath ,
|
||||
content
|
||||
) {
|
||||
return Promise.resolve()
|
||||
.then(() => {
|
||||
return this.loadFileContent('async', filepath, content);
|
||||
})
|
||||
.then(loaderResult => {
|
||||
return this.loadedContentToCosmiconfigResult(filepath, loaderResult);
|
||||
});
|
||||
}
|
||||
|
||||
createCosmiconfigResultSync(
|
||||
filepath ,
|
||||
content
|
||||
) {
|
||||
const loaderResult = this.loadFileContent('sync', filepath, content);
|
||||
return this.loadedContentToCosmiconfigResult(filepath, loaderResult);
|
||||
}
|
||||
|
||||
validateFilePath(filepath ) {
|
||||
if (!filepath) {
|
||||
throw new Error('load and loadSync must pass a non-empty string');
|
||||
}
|
||||
}
|
||||
|
||||
load(filepath ) {
|
||||
return Promise.resolve().then(() => {
|
||||
this.validateFilePath(filepath);
|
||||
const absoluteFilePath = path.resolve(process.cwd(), filepath);
|
||||
return cacheWrapper(this.loadCache, absoluteFilePath, () => {
|
||||
return readFile(absoluteFilePath, { throwNotFound: true })
|
||||
.then(content => {
|
||||
return this.createCosmiconfigResult(absoluteFilePath, content);
|
||||
})
|
||||
.then(this.config.transform);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
loadSync(filepath ) {
|
||||
this.validateFilePath(filepath);
|
||||
const absoluteFilePath = path.resolve(process.cwd(), filepath);
|
||||
return cacheWrapper(this.loadSyncCache, absoluteFilePath, () => {
|
||||
const content = readFile.sync(absoluteFilePath, { throwNotFound: true });
|
||||
const result = this.createCosmiconfigResultSync(
|
||||
absoluteFilePath,
|
||||
content
|
||||
);
|
||||
return this.config.transform(result);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = function createExplorer(options ) {
|
||||
const explorer = new Explorer(options);
|
||||
|
||||
return {
|
||||
load,
|
||||
clearFileCache,
|
||||
clearDirectoryCache,
|
||||
clearCaches,
|
||||
search: explorer.search.bind(explorer),
|
||||
searchSync: explorer.searchSync.bind(explorer),
|
||||
load: explorer.load.bind(explorer),
|
||||
loadSync: explorer.loadSync.bind(explorer),
|
||||
clearLoadCache: explorer.clearLoadCache.bind(explorer),
|
||||
clearSearchCache: explorer.clearSearchCache.bind(explorer),
|
||||
clearCaches: explorer.clearCaches.bind(explorer),
|
||||
};
|
||||
};
|
||||
|
||||
function identity(x) {
|
||||
return x;
|
||||
function nextDirUp(dir ) {
|
||||
return path.dirname(dir);
|
||||
}
|
||||
|
||||
function getExtensionDescription(filepath ) {
|
||||
const ext = path.extname(filepath);
|
||||
return ext ? `extension "${ext}"` : 'files without extensions';
|
||||
}
|
||||
|
||||
-22
@@ -1,22 +0,0 @@
|
||||
//
|
||||
'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
|
||||
);
|
||||
};
|
||||
+7
-8
@@ -4,14 +4,7 @@
|
||||
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);
|
||||
}
|
||||
|
||||
function getDirectory(filepath ) {
|
||||
return new Promise((resolve, reject) => {
|
||||
return isDirectory(filepath, (err, filepathIsDirectory) => {
|
||||
if (err) {
|
||||
@@ -20,4 +13,10 @@ module.exports = function getDirectory(
|
||||
return resolve(filepathIsDirectory ? filepath : path.dirname(filepath));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
getDirectory.sync = function getDirectorySync(filepath ) {
|
||||
return isDirectory.sync(filepath) ? filepath : path.dirname(filepath);
|
||||
};
|
||||
|
||||
module.exports = getDirectory;
|
||||
|
||||
+63
-22
@@ -3,38 +3,79 @@
|
||||
|
||||
const os = require('os');
|
||||
const createExplorer = require('./createExplorer');
|
||||
const loaders = require('./loaders');
|
||||
|
||||
const homedir = os.homedir();
|
||||
module.exports = cosmiconfig;
|
||||
|
||||
module.exports = function cosmiconfig(
|
||||
function cosmiconfig(
|
||||
moduleName ,
|
||||
options
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
) {
|
||||
options = Object.assign(
|
||||
options = options || {};
|
||||
const defaults = {
|
||||
packageProp: moduleName,
|
||||
searchPlaces: [
|
||||
'package.json',
|
||||
`.${moduleName}rc`,
|
||||
`.${moduleName}rc.json`,
|
||||
`.${moduleName}rc.yaml`,
|
||||
`.${moduleName}rc.yml`,
|
||||
`.${moduleName}rc.js`,
|
||||
`${moduleName}.config.js`,
|
||||
],
|
||||
ignoreEmptySearchPlaces: true,
|
||||
stopDir: os.homedir(),
|
||||
cache: true,
|
||||
transform: identity,
|
||||
};
|
||||
const normalizedOptions = Object.assign(
|
||||
{},
|
||||
defaults,
|
||||
options,
|
||||
{
|
||||
packageProp: moduleName,
|
||||
rc: `.${moduleName}rc`,
|
||||
js: `${moduleName}.config.js`,
|
||||
rcStrictJson: false,
|
||||
stopDir: homedir,
|
||||
cache: true,
|
||||
sync: false,
|
||||
},
|
||||
options
|
||||
loaders: normalizeLoaders(options.loaders),
|
||||
}
|
||||
);
|
||||
|
||||
return createExplorer(options);
|
||||
};
|
||||
return createExplorer(normalizedOptions);
|
||||
}
|
||||
|
||||
cosmiconfig.loadJs = loaders.loadJs;
|
||||
cosmiconfig.loadJson = loaders.loadJson;
|
||||
cosmiconfig.loadYaml = loaders.loadYaml;
|
||||
|
||||
function normalizeLoaders(rawLoaders ) {
|
||||
const defaults = {
|
||||
'.js': { sync: loaders.loadJs, async: loaders.loadJs },
|
||||
'.json': { sync: loaders.loadJson, async: loaders.loadJson },
|
||||
'.yaml': { sync: loaders.loadYaml, async: loaders.loadYaml },
|
||||
'.yml': { sync: loaders.loadYaml, async: loaders.loadYaml },
|
||||
noExt: { sync: loaders.loadYaml, async: loaders.loadYaml },
|
||||
};
|
||||
|
||||
if (!rawLoaders) {
|
||||
return defaults;
|
||||
}
|
||||
|
||||
return Object.keys(rawLoaders).reduce((result, ext) => {
|
||||
const entry = rawLoaders && rawLoaders[ext];
|
||||
if (typeof entry === 'function') {
|
||||
result[ext] = { sync: entry, async: entry };
|
||||
} else {
|
||||
result[ext] = entry;
|
||||
}
|
||||
return result;
|
||||
}, defaults);
|
||||
}
|
||||
|
||||
function identity(x) {
|
||||
return x;
|
||||
}
|
||||
|
||||
-98
@@ -1,98 +0,0 @@
|
||||
//
|
||||
'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
@@ -1,23 +0,0 @@
|
||||
//
|
||||
'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
@@ -1,32 +0,0 @@
|
||||
//
|
||||
'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
@@ -1,110 +0,0 @@
|
||||
//
|
||||
'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
@@ -1,16 +0,0 @@
|
||||
//
|
||||
'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;
|
||||
}
|
||||
};
|
||||
+2
-2
@@ -7,7 +7,7 @@ const fs = require('fs');
|
||||
|
||||
|
||||
|
||||
function readFile(filepath , options ) {
|
||||
function readFile(filepath , options ) {
|
||||
options = options || {};
|
||||
const throwNotFound = options.throwNotFound || false;
|
||||
|
||||
@@ -25,7 +25,7 @@ function readFile(filepath , options ) {
|
||||
readFile.sync = function readFileSync(
|
||||
filepath ,
|
||||
options
|
||||
) {
|
||||
) {
|
||||
options = options || {};
|
||||
const throwNotFound = options.throwNotFound || false;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user