UNPKG

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