find_config.js 820 B

12345678910111213141516171819202122232425
  1. var fs = require('fs');
  2. var path = require('path');
  3. var fileSearch = require('./file_search');
  4. module.exports = function(opts) {
  5. opts = opts || {};
  6. var configNameSearch = opts.configNameSearch;
  7. var configPath = opts.configPath;
  8. var searchPaths = opts.searchPaths;
  9. // only search for a config if a path to one wasn't explicitly provided
  10. if (!configPath) {
  11. if (!Array.isArray(searchPaths)) {
  12. throw new Error('Please provide an array of paths to search for config in.');
  13. }
  14. if (!configNameSearch) {
  15. throw new Error('Please provide a configNameSearch.');
  16. }
  17. configPath = fileSearch(configNameSearch, searchPaths);
  18. }
  19. // confirm the configPath exists and return an absolute path to it
  20. if (fs.existsSync(configPath)) {
  21. return path.resolve(configPath);
  22. }
  23. return null;
  24. };