UNPKG

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