forked from conventional-changelog/standard-version
-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathconfiguration.js
41 lines (37 loc) · 1.04 KB
/
configuration.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
const path = require('path');
const findUp = require('find-up');
const { readFileSync } = require('fs');
const CONFIGURATION_FILES = [
'.versionrc',
'.versionrc.cjs',
'.versionrc.json',
'.versionrc.js',
];
module.exports.getConfiguration = function () {
let config = {};
const configPath = findUp.sync(CONFIGURATION_FILES);
if (!configPath) {
return config;
}
const ext = path.extname(configPath);
if (ext === '.js' || ext === '.cjs') {
const jsConfiguration = require(configPath);
if (typeof jsConfiguration === 'function') {
config = jsConfiguration();
} else {
config = jsConfiguration;
}
} else {
config = JSON.parse(readFileSync(configPath));
}
/**
* @todo we could eventually have deeper validation of the configuration (using `ajv`) and
* provide a more helpful error.
*/
if (typeof config !== 'object') {
throw Error(
`[commit-and-tag-version] Invalid configuration in ${configPath} provided. Expected an object but found ${typeof config}.`,
);
}
return config;
};