UNPKG

6.77 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const tslib_1 = require("tslib");
4const cosmiconfig_1 = require("cosmiconfig");
5const deepmerge_1 = tslib_1.__importDefault(require("deepmerge"));
6const node_fetch_1 = tslib_1.__importDefault(require("node-fetch"));
7const path = tslib_1.__importStar(require("path"));
8const release_1 = require("./release");
9const try_require_1 = tslib_1.__importDefault(require("./utils/try-require"));
10const endent_1 = tslib_1.__importDefault(require("endent"));
11/** Transform all types of label configuration into just 1 shape */
12function normalizeLabel(label) {
13 const baseLabel = release_1.defaultLabels.find(l => (label.releaseType && l.releaseType === label.releaseType) ||
14 (label.name && l.name === label.name)) || {};
15 return Object.assign(Object.assign({}, baseLabel), label);
16}
17exports.normalizeLabel = normalizeLabel;
18/**
19 * Go through all the labels in a config and make them
20 * follow the same format.
21 */
22function normalizeLabels(config) {
23 if (config.labels) {
24 const userLabels = config.labels.map(normalizeLabel);
25 const baseLabels = release_1.defaultLabels.filter(d => !userLabels.some(u => u.releaseType && u.releaseType === d.releaseType && u.overwrite));
26 return [...baseLabels, ...userLabels];
27 }
28 return release_1.defaultLabels;
29}
30exports.normalizeLabels = normalizeLabels;
31/** Load a user's configuration from the system and resolve any extended config */
32class Config {
33 /** Initialize the config loader */
34 constructor(logger) {
35 this.logger = logger;
36 }
37 /**
38 * Load the .autorc from the file system, set up defaults, combine with CLI args
39 * load the extends property, load the plugins and start the git remote interface.
40 */
41 async loadConfig(args) {
42 var _a;
43 const explorer = cosmiconfig_1.cosmiconfig('auto', {
44 searchPlaces: [
45 `.autorc`,
46 `.autorc.json`,
47 `.autorc.yaml`,
48 `.autorc.yml`,
49 'package.json'
50 ]
51 });
52 const result = await explorer.search();
53 let rawConfig = {};
54 if ((_a = result) === null || _a === void 0 ? void 0 : _a.config) {
55 rawConfig = result.config;
56 }
57 if (rawConfig.extends) {
58 rawConfig = deepmerge_1.default(rawConfig, await this.loadExtendConfig(rawConfig.extends));
59 }
60 this.checkDeprecated(rawConfig);
61 const labels = normalizeLabels(rawConfig);
62 const semVerLabels = release_1.getVersionMap(labels);
63 this.logger.verbose.success('Using SEMVER labels:', '\n', semVerLabels);
64 const skipReleaseLabels = rawConfig.skipReleaseLabels || [];
65 if (!skipReleaseLabels.includes(semVerLabels.get('skip'))) {
66 (semVerLabels.get('skip') || []).map(l => skipReleaseLabels.push(l));
67 }
68 return Object.assign(Object.assign(Object.assign({}, rawConfig), args), { labels,
69 skipReleaseLabels, prereleaseBranches: rawConfig.prereleaseBranches || ['next'] });
70 }
71 /**
72 * Loads a config from a path, package name, or special `auto-config` pattern
73 *
74 * ex: auto-config-MY_CONFIG
75 * ex: @MY_CONFIG/auto-config
76 *
77 * @param extend - Path or name of config to find
78 */
79 async loadExtendConfig(extend) {
80 var _a, _b, _c, _d;
81 let config;
82 if (extend.endsWith('.js') || extend.endsWith('.mjs')) {
83 throw new Error('Extended config cannot be a JavaScript file');
84 }
85 if (extend.startsWith('http')) {
86 try {
87 config = (await node_fetch_1.default(extend)).json();
88 this.logger.verbose.note(`${extend} found: ${config}`);
89 }
90 catch (error) {
91 error.message = `Failed to get extended config from ${extend} -- ${error.message}`;
92 throw error;
93 }
94 }
95 else if (extend.startsWith('.')) {
96 config = try_require_1.default(extend);
97 if (extend.endsWith('package.json')) {
98 config = (_a = config) === null || _a === void 0 ? void 0 : _a.auto;
99 }
100 this.logger.verbose.note(`${extend} found: ${config}`);
101 }
102 else {
103 config = try_require_1.default(`${extend}/package.json`);
104 config = (_b = config) === null || _b === void 0 ? void 0 : _b.auto;
105 this.logger.verbose.note(`${extend} found: ${config}`);
106 }
107 if (!config) {
108 const scope = `${extend}/auto-config/package.json`;
109 config = try_require_1.default(scope);
110 config = (_c = config) === null || _c === void 0 ? void 0 : _c.auto;
111 this.logger.verbose.note(`${scope} found: ${config}`);
112 }
113 if (!config) {
114 const scope = `auto-config-${extend}/package.json`;
115 config = try_require_1.default(scope);
116 config = (_d = config) === null || _d === void 0 ? void 0 : _d.auto;
117 this.logger.verbose.note(`${scope} found: ${config}`);
118 }
119 if (!config) {
120 config = try_require_1.default(path.join(process.cwd(), extend));
121 }
122 if (!config) {
123 throw new Error(`Unable to load extended config ${extend}`);
124 }
125 return config;
126 }
127 /** Ensure a user's config is not using deprecated options. */
128 // eslint-disable-next-line @typescript-eslint/no-explicit-any
129 checkDeprecated(config) {
130 if (config.labels && !Array.isArray(config.labels)) {
131 this.logger.log.error(endent_1.default `
132 You're using a deprecated configuration option!
133
134 The "labels" option no longer supports configuration with an object.
135 Instead supply your labels as an array of label objects.
136
137 ex:
138
139 | {
140 | "labels": [
141 | {
142 | "name": "my-label",
143 | "description": "Really big stuff",
144 | "type": "major"
145 | }
146 | ]
147 | }
148 `);
149 process.exit(1);
150 }
151 if (config.skipReleaseLabels) {
152 this.logger.log.error(endent_1.default `
153 You're using a deprecated configuration option!
154
155 The "skipReleaseLabels" option no longer exists.
156 Instead set "type" to "skip" in your label configuration.
157
158 ex:
159
160 | {
161 | "labels": [
162 | {
163 | "name": "my-label",
164 | "description": "Really big stuff",
165 | "type": "skip"
166 | }
167 | ]
168 | }
169 `);
170 process.exit(1);
171 }
172 }
173}
174exports.default = Config;
175//# sourceMappingURL=config.js.map
\No newline at end of file