find_config.js 827 B

12345678910111213141516171819202122232425
  1. const fs = require('fs');
  2. const path = require('path');
  3. const 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. };