UNPKG

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