1 | const { cosmiconfigSync } = require('cosmiconfig');
|
2 | const parseJson = require('parse-json');
|
3 | const parseToml = require('@iarna/toml/parse-string');
|
4 | const yaml = require('yaml');
|
5 | const _ = require('lodash');
|
6 | const isCI = require('is-ci');
|
7 | const debug = require('debug')('release-it:config');
|
8 | const defaultConfig = require('../config/release-it.json');
|
9 | const { InvalidConfigurationError } = require('./errors');
|
10 | const { getSystemInfo } = require('./util');
|
11 |
|
12 | const searchPlaces = [
|
13 | 'package.json',
|
14 | '.release-it.json',
|
15 | '.release-it.js',
|
16 | '.release-it.yaml',
|
17 | '.release-it.yml',
|
18 | '.release-it.toml'
|
19 | ];
|
20 |
|
21 | const loaders = {
|
22 | '.json': (_, content) => parseJson(content),
|
23 | '.toml': (_, content) => parseToml(content),
|
24 | '.yaml': (_, content) => yaml.parse(content)
|
25 | };
|
26 |
|
27 | const getLocalConfig = localConfigFile => {
|
28 | let localConfig = {};
|
29 | if (localConfigFile === false) return localConfig;
|
30 | const explorer = cosmiconfigSync('release-it', {
|
31 | searchPlaces,
|
32 | loaders
|
33 | });
|
34 | const result = localConfigFile ? explorer.load(localConfigFile) : explorer.search();
|
35 | if (result && typeof result.config === 'string') {
|
36 | throw new InvalidConfigurationError(result.filepath);
|
37 | }
|
38 | debug({ cosmiconfig: result });
|
39 | return result && _.isPlainObject(result.config) ? result.config : localConfig;
|
40 | };
|
41 |
|
42 | class Config {
|
43 | constructor(config = {}) {
|
44 | this.constructorConfig = config;
|
45 | this.localConfig = getLocalConfig(config.config);
|
46 | this.options = this.mergeOptions();
|
47 | this.options = this.expandPreReleaseShorthand(this.options);
|
48 | this.contextOptions = {};
|
49 | debug({ system: getSystemInfo() });
|
50 | debug(this.options);
|
51 | }
|
52 |
|
53 | expandPreReleaseShorthand(options) {
|
54 | const { increment, preRelease, preReleaseId } = options;
|
55 | options.version = {
|
56 | increment,
|
57 | isPreRelease: Boolean(preRelease),
|
58 | preReleaseId: typeof preRelease === 'string' ? preRelease : preReleaseId
|
59 | };
|
60 | return options;
|
61 | }
|
62 |
|
63 | mergeOptions() {
|
64 | return _.defaultsDeep(
|
65 | {},
|
66 | this.constructorConfig,
|
67 | {
|
68 | ci: isCI || undefined
|
69 | },
|
70 | this.localConfig,
|
71 | this.defaultConfig
|
72 | );
|
73 | }
|
74 |
|
75 | getContext(path) {
|
76 | const context = _.merge({}, this.options, this.contextOptions);
|
77 | return path ? _.get(context, path) : context;
|
78 | }
|
79 |
|
80 | setContext(options) {
|
81 | debug(options);
|
82 | _.merge(this.contextOptions, options);
|
83 | }
|
84 |
|
85 | get defaultConfig() {
|
86 | return defaultConfig;
|
87 | }
|
88 |
|
89 | get isDryRun() {
|
90 | return Boolean(this.options['dry-run']);
|
91 | }
|
92 |
|
93 | get isVerbose() {
|
94 | return Boolean(this.options.verbose);
|
95 | }
|
96 |
|
97 | get verbosityLevel() {
|
98 | return this.options.verbose;
|
99 | }
|
100 |
|
101 | get isDebug() {
|
102 | return debug.enabled;
|
103 | }
|
104 |
|
105 | get isCI() {
|
106 | return Boolean(this.options.ci);
|
107 | }
|
108 |
|
109 | get isCollectMetrics() {
|
110 | return !this.options['disable-metrics'];
|
111 | }
|
112 | }
|
113 |
|
114 | module.exports = Config;
|