load.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. const path_1 = __importDefault(require("path"));
  7. const merge_1 = __importDefault(require("lodash/merge"));
  8. const mergeWith_1 = __importDefault(require("lodash/mergeWith"));
  9. const pick_1 = __importDefault(require("lodash/pick"));
  10. const union_1 = __importDefault(require("lodash/union"));
  11. const resolve_from_1 = __importDefault(require("resolve-from"));
  12. const execute_rule_1 = __importDefault(require("@commitlint/execute-rule"));
  13. const resolve_extends_1 = __importDefault(require("@commitlint/resolve-extends"));
  14. const load_plugin_1 = __importDefault(require("./utils/load-plugin"));
  15. const load_config_1 = require("./utils/load-config");
  16. const load_parser_opts_1 = require("./utils/load-parser-opts");
  17. const pick_config_1 = require("./utils/pick-config");
  18. const w = (_, b) => Array.isArray(b) ? b : undefined;
  19. async function load(seed = {}, options = {}) {
  20. const cwd = typeof options.cwd === 'undefined' ? process.cwd() : options.cwd;
  21. const loaded = await load_config_1.loadConfig(cwd, options.file);
  22. const base = loaded && loaded.filepath ? path_1.default.dirname(loaded.filepath) : cwd;
  23. // TODO: validate loaded.config against UserConfig type
  24. // Might amount to breaking changes, defer until 9.0.0
  25. // Merge passed config with file based options
  26. const config = pick_config_1.pickConfig(merge_1.default({}, loaded ? loaded.config : null, seed));
  27. const opts = merge_1.default({ extends: [], rules: {}, formatter: '@commitlint/format' }, pick_1.default(config, 'extends', 'plugins', 'ignores', 'defaultIgnores'));
  28. // Resolve parserPreset key
  29. if (typeof config.parserPreset === 'string') {
  30. const resolvedParserPreset = resolve_from_1.default(base, config.parserPreset);
  31. config.parserPreset = {
  32. name: config.parserPreset,
  33. path: resolvedParserPreset,
  34. parserOpts: require(resolvedParserPreset),
  35. };
  36. }
  37. // Resolve extends key
  38. const extended = resolve_extends_1.default(opts, {
  39. prefix: 'commitlint-config',
  40. cwd: base,
  41. parserPreset: config.parserPreset,
  42. });
  43. const preset = pick_config_1.pickConfig(mergeWith_1.default(extended, config, w));
  44. preset.plugins = {};
  45. // TODO: check if this is still necessary with the new factory based conventional changelog parsers
  46. // config.extends = Array.isArray(config.extends) ? config.extends : [];
  47. // Resolve parser-opts from preset
  48. if (typeof preset.parserPreset === 'object') {
  49. preset.parserPreset.parserOpts = await load_parser_opts_1.loadParserOpts(preset.parserPreset.name,
  50. // TODO: fix the types for factory based conventional changelog parsers
  51. preset.parserPreset);
  52. }
  53. // Resolve config-relative formatter module
  54. if (typeof config.formatter === 'string') {
  55. preset.formatter =
  56. resolve_from_1.default.silent(base, config.formatter) || config.formatter;
  57. }
  58. // Read plugins from extends
  59. if (Array.isArray(extended.plugins)) {
  60. config.plugins = union_1.default(config.plugins, extended.plugins || []);
  61. }
  62. // resolve plugins
  63. if (Array.isArray(config.plugins)) {
  64. config.plugins.forEach((plugin) => {
  65. if (typeof plugin === 'string') {
  66. load_plugin_1.default(preset.plugins, plugin, process.env.DEBUG === 'true');
  67. }
  68. else {
  69. preset.plugins.local = plugin;
  70. }
  71. });
  72. }
  73. const rules = preset.rules ? preset.rules : {};
  74. const qualifiedRules = (await Promise.all(Object.entries(rules || {}).map((entry) => execute_rule_1.default(entry)))).reduce((registry, item) => {
  75. const [key, value] = item;
  76. registry[key] = value;
  77. return registry;
  78. }, {});
  79. return {
  80. extends: preset.extends,
  81. formatter: preset.formatter,
  82. parserPreset: preset.parserPreset,
  83. ignores: preset.ignores,
  84. defaultIgnores: preset.defaultIgnores,
  85. plugins: preset.plugins,
  86. rules: qualifiedRules,
  87. };
  88. }
  89. exports.default = load;
  90. //# sourceMappingURL=load.js.map