UNPKG

cosmiconfig

Version:

Find and load configuration from a package.json property, rc file, or CommonJS module

32 lines (27 loc) 779 B
'use strict'; var fs = require('graceful-fs'); var Promise = require('pinkie-promise'); var yaml = require('js-yaml'); var parseJson = require('parse-json'); module.exports = function(filepath, options) { return new Promise(function(resolve, reject) { fs.readFile(filepath, 'utf8', function(fileError, content) { if (fileError) { if (fileError.code === 'ENOENT') return resolve(null); return reject(fileError); } resolve(content); }); }).then(function(content) { if (!content) return null; var parsedConfig = (options.strictJson) ? parseJson(content, filepath) : yaml.safeLoad(content, { filename: filepath, }); return { config: parsedConfig, filepath: filepath, }; }); };