4 const yaml = require('js-yaml');
5 const requireFromString = require('require-from-string');
6 const readFile = require('./readFile');
7 const parseJson = require('./parseJson');
8 const path = require('path');
10 module.exports = function loadDefinedFile(
17 function parseContent(content ) {
19 throw new Error(`Config file is empty! Filepath - "${filepath}".`);
24 switch (options.format || inferFormat(filepath)) {
26 parsedConfig = parseJson(content, filepath);
29 parsedConfig = yaml.safeLoad(content, {
34 parsedConfig = requireFromString(content, filepath);
37 parsedConfig = tryAllParsing(content, filepath);
41 throw new Error(`Failed to parse "${filepath}" as JSON, JS, or YAML.`);
51 ? readFile(filepath, { throwNotFound: true }).then(parseContent)
52 : parseContent(readFile.sync(filepath, { throwNotFound: true }));
55 function inferFormat(filepath ) {
56 switch (path.extname(filepath)) {
61 // istanbul ignore next
70 function tryAllParsing(content , filepath ) {
71 return tryYaml(content, filepath, () => {
72 return tryRequire(content, filepath, () => {
78 function tryYaml(content , filepath , cb ) {
80 const result = yaml.safeLoad(content, {
83 if (typeof result === 'string') {
92 function tryRequire(content , filepath , cb ) {
94 return requireFromString(content, filepath);