load.js 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 execute_rule_1 = __importDefault(require("@commitlint/execute-rule"));
  7. const resolve_extends_1 = __importDefault(require("@commitlint/resolve-extends"));
  8. const isPlainObject_1 = __importDefault(require("lodash/isPlainObject"));
  9. const merge_1 = __importDefault(require("lodash/merge"));
  10. const uniq_1 = __importDefault(require("lodash/uniq"));
  11. const path_1 = __importDefault(require("path"));
  12. const resolve_from_1 = __importDefault(require("resolve-from"));
  13. const load_config_1 = require("./utils/load-config");
  14. const load_parser_opts_1 = require("./utils/load-parser-opts");
  15. const load_plugin_1 = __importDefault(require("./utils/load-plugin"));
  16. const pick_config_1 = require("./utils/pick-config");
  17. async function load(seed = {}, options = {}) {
  18. const cwd = typeof options.cwd === 'undefined' ? process.cwd() : options.cwd;
  19. const loaded = await (0, load_config_1.loadConfig)(cwd, options.file);
  20. const base = loaded && loaded.filepath ? path_1.default.dirname(loaded.filepath) : cwd;
  21. // TODO: validate loaded.config against UserConfig type
  22. // Might amount to breaking changes, defer until 9.0.0
  23. // Merge passed config with file based options
  24. const config = (0, pick_config_1.pickConfig)((0, merge_1.default)({
  25. extends: [],
  26. plugins: [],
  27. rules: {},
  28. }, loaded ? loaded.config : null, seed));
  29. // Resolve parserPreset key
  30. if (typeof config.parserPreset === 'string') {
  31. const resolvedParserPreset = (0, resolve_from_1.default)(base, config.parserPreset);
  32. config.parserPreset = {
  33. name: config.parserPreset,
  34. path: resolvedParserPreset,
  35. parserOpts: require(resolvedParserPreset),
  36. };
  37. }
  38. // Resolve extends key
  39. const extended = (0, resolve_extends_1.default)(config, {
  40. prefix: 'commitlint-config',
  41. cwd: base,
  42. parserPreset: config.parserPreset,
  43. });
  44. if (!extended.formatter || typeof extended.formatter !== 'string') {
  45. extended.formatter = '@commitlint/format';
  46. }
  47. let plugins = {};
  48. if (Array.isArray(extended.plugins)) {
  49. (0, uniq_1.default)(extended.plugins || []).forEach((plugin) => {
  50. if (typeof plugin === 'string') {
  51. plugins = (0, load_plugin_1.default)(plugins, plugin, process.env.DEBUG === 'true');
  52. }
  53. else {
  54. plugins.local = plugin;
  55. }
  56. });
  57. }
  58. const rules = (await Promise.all(Object.entries(extended.rules || {}).map((entry) => (0, execute_rule_1.default)(entry)))).reduce((registry, item) => {
  59. // type of `item` can be null, but Object.entries always returns key pair
  60. const [key, value] = item;
  61. registry[key] = value;
  62. return registry;
  63. }, {});
  64. const helpUrl = typeof extended.helpUrl === 'string'
  65. ? extended.helpUrl
  66. : typeof config.helpUrl === 'string'
  67. ? config.helpUrl
  68. : 'https://github.com/conventional-changelog/commitlint/#what-is-commitlint';
  69. const prompt = extended.prompt && (0, isPlainObject_1.default)(extended.prompt) ? extended.prompt : {};
  70. return {
  71. extends: Array.isArray(extended.extends)
  72. ? extended.extends
  73. : typeof extended.extends === 'string'
  74. ? [extended.extends]
  75. : [],
  76. // Resolve config-relative formatter module
  77. formatter: resolve_from_1.default.silent(base, extended.formatter) || extended.formatter,
  78. // Resolve parser-opts from preset
  79. parserPreset: await (0, load_parser_opts_1.loadParserOpts)(extended.parserPreset),
  80. ignores: extended.ignores,
  81. defaultIgnores: extended.defaultIgnores,
  82. plugins: plugins,
  83. rules: rules,
  84. helpUrl: helpUrl,
  85. prompt,
  86. };
  87. }
  88. exports.default = load;
  89. //# sourceMappingURL=load.js.map