/
config.js
128 lines (107 loc) Β· 2.95 KB
/
config.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import util from 'node:util';
import { cosmiconfigSync } from 'cosmiconfig';
import parseToml from '@iarna/toml/parse-string.js';
import _ from 'lodash';
import isCI from 'is-ci';
import { readJSON, getSystemInfo } from './util.js';
const debug = util.debug('release-it:config');
const defaultConfig = readJSON(new URL('../config/release-it.json', import.meta.url));
const searchPlaces = [
'package.json',
'.release-it.json',
'.release-it.js',
'.release-it.cjs',
'.release-it.yaml',
'.release-it.yml',
'.release-it.toml'
];
const loaders = {
'.toml': (_, content) => parseToml(content)
};
const getLocalConfig = ({ file, dir = process.cwd() }) => {
let localConfig = {};
if (file === false) return localConfig;
const explorer = cosmiconfigSync('release-it', {
searchPlaces,
loaders
});
const result = file ? explorer.load(file) : explorer.search(dir);
if (result && typeof result.config === 'string') {
throw new Error(`Invalid configuration file at ${result.filepath}`);
}
debug({ cosmiconfig: result });
return result && _.isPlainObject(result.config) ? result.config : localConfig;
};
class Config {
constructor(config = {}) {
this.constructorConfig = config;
this.localConfig = getLocalConfig({ file: config.config, dir: config.configDir });
this.options = this.mergeOptions();
this.options = this.expandPreReleaseShorthand(this.options);
this.contextOptions = {};
debug({ system: getSystemInfo() });
debug(this.options);
}
expandPreReleaseShorthand(options) {
const { increment, preRelease, preReleaseId } = options;
options.version = {
increment,
isPreRelease: Boolean(preRelease),
preReleaseId: typeof preRelease === 'string' ? preRelease : preReleaseId
};
return options;
}
mergeOptions() {
return _.defaultsDeep(
{},
this.constructorConfig,
{
ci: isCI
},
this.localConfig,
this.defaultConfig
);
}
getContext(path) {
const context = _.merge({}, this.options, this.contextOptions);
return path ? _.get(context, path) : context;
}
setContext(options) {
debug(options);
_.merge(this.contextOptions, options);
}
setCI(value = true) {
this.options.ci = value;
}
get defaultConfig() {
return defaultConfig;
}
get isDryRun() {
return Boolean(this.options['dry-run']);
}
get isIncrement() {
return this.options.increment !== false;
}
get isVerbose() {
return Boolean(this.options.verbose);
}
get verbosityLevel() {
return this.options.verbose;
}
get isDebug() {
return debug.enabled;
}
get isCI() {
return Boolean(this.options.ci) || this.isReleaseVersion || this.isChangelog;
}
get isPromptOnlyVersion() {
return this.options['only-version'];
}
get isReleaseVersion() {
return Boolean(this.options['release-version']);
}
get isChangelog() {
return Boolean(this.options['changelog']);
}
}
export default Config;