-
Notifications
You must be signed in to change notification settings - Fork 105
/
getConfig.js
63 lines (48 loc) · 1.33 KB
/
getConfig.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/* eslint-disable global-require, import/no-dynamic-require */
const path = require('path');
const fs = require('fs');
const signale = require('signale');
const defaults = require('./defaults');
const configFiles = [
'.git-cz.json',
'changelog.config.js',
'changelog.config.cjs',
'changelog.config.json'
];
const findOverrides = (root) => {
const dir = root || process.cwd();
for (const file of configFiles) {
const filename = path.resolve(dir, file);
if (fs.existsSync(filename) && fs.statSync(filename).isFile()) {
return require(filename);
}
}
const parent = path.resolve(dir, '..');
const pkgFilename = path.join(dir, 'package.json');
if (fs.existsSync(pkgFilename)) {
try {
const changelog = require(pkgFilename).config.commitizen.changelog;
if (changelog) {
return changelog;
}
// eslint-disable-next-line no-empty
} catch (error) {}
}
if (parent !== dir) {
return findOverrides(parent);
}
return {};
};
const getConfig = (root) => {
const overrides = findOverrides(root);
if (typeof overrides !== 'object') {
signale.fatal(new TypeError('Expected changelog config to be an object.'));
// eslint-disable-next-line no-process-exit
process.exit(1);
}
return {
...defaults,
...overrides
};
};
module.exports = getConfig;