loadRc.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. //
  2. 'use strict';
  3. const yaml = require('js-yaml');
  4. const requireFromString = require('require-from-string');
  5. const readFile = require('./readFile');
  6. const parseJson = require('./parseJson');
  7. const funcRunner = require('./funcRunner');
  8. module.exports = function loadRc(
  9. filepath ,
  10. options
  11. ) {
  12. if (!options.sync) {
  13. return readFile(filepath)
  14. .then(parseExtensionlessRcFile)
  15. .then(checkExtensionlessRcResult);
  16. } else {
  17. return checkExtensionlessRcResult(
  18. parseExtensionlessRcFile(readFile.sync(filepath))
  19. );
  20. }
  21. function checkExtensionlessRcResult(result) {
  22. if (result) return result;
  23. if (options.rcExtensions) return loadRcWithExtensions();
  24. return null;
  25. }
  26. function parseExtensionlessRcFile(content ) {
  27. if (!content) return null;
  28. const pasedConfig = options.rcStrictJson
  29. ? parseJson(content, filepath)
  30. : yaml.safeLoad(content, { filename: filepath });
  31. return {
  32. config: pasedConfig,
  33. filepath,
  34. };
  35. }
  36. function loadRcWithExtensions() {
  37. let foundConfig = null;
  38. return funcRunner(readRcFile('json'), [
  39. (jsonContent ) => {
  40. // Since this is the first try, config cannot have been found, so don't
  41. // check `if (foundConfig)`.
  42. if (jsonContent) {
  43. const successFilepath = `${filepath}.json`;
  44. foundConfig = {
  45. config: parseJson(jsonContent, successFilepath),
  46. filepath: successFilepath,
  47. };
  48. } else {
  49. return readRcFile('yaml');
  50. }
  51. },
  52. (yamlContent ) => {
  53. if (foundConfig) {
  54. return;
  55. } else if (yamlContent) {
  56. const successFilepath = `${filepath}.yaml`;
  57. foundConfig = {
  58. config: yaml.safeLoad(yamlContent, { filename: successFilepath }),
  59. filepath: successFilepath,
  60. };
  61. } else {
  62. return readRcFile('yml');
  63. }
  64. },
  65. (ymlContent ) => {
  66. if (foundConfig) {
  67. return;
  68. } else if (ymlContent) {
  69. const successFilepath = `${filepath}.yml`;
  70. foundConfig = {
  71. config: yaml.safeLoad(ymlContent, { filename: successFilepath }),
  72. filepath: successFilepath,
  73. };
  74. } else {
  75. return readRcFile('js');
  76. }
  77. },
  78. (jsContent ) => {
  79. if (foundConfig) {
  80. return;
  81. } else if (jsContent) {
  82. const successFilepath = `${filepath}.js`;
  83. foundConfig = {
  84. config: requireFromString(jsContent, successFilepath),
  85. filepath: successFilepath,
  86. };
  87. } else {
  88. return;
  89. }
  90. },
  91. () => foundConfig,
  92. ]);
  93. }
  94. function readRcFile(extension ) {
  95. const filepathWithExtension = `${filepath}.${extension}`;
  96. return !options.sync
  97. ? readFile(filepathWithExtension)
  98. : readFile.sync(filepathWithExtension);
  99. }
  100. };