UNPKG

10.4 kBJavaScriptView Raw
1"use strict";
2var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3 if (k2 === undefined) k2 = k;
4 var desc = Object.getOwnPropertyDescriptor(m, k);
5 if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6 desc = { enumerable: true, get: function() { return m[k]; } };
7 }
8 Object.defineProperty(o, k2, desc);
9}) : (function(o, m, k, k2) {
10 if (k2 === undefined) k2 = k;
11 o[k2] = m[k];
12}));
13var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14 Object.defineProperty(o, "default", { enumerable: true, value: v });
15}) : function(o, v) {
16 o["default"] = v;
17});
18var __importStar = (this && this.__importStar) || function (mod) {
19 if (mod && mod.__esModule) return mod;
20 var result = {};
21 if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22 __setModuleDefault(result, mod);
23 return result;
24};
25var __importDefault = (this && this.__importDefault) || function (mod) {
26 return (mod && mod.__esModule) ? mod : { "default": mod };
27};
28Object.defineProperty(exports, "__esModule", { value: true });
29exports.main = void 0;
30const release_script_core_1 = require("@alcalzone/release-script-core");
31const arrays_1 = require("alcalzone-shared/arrays");
32const enquirer_1 = require("enquirer");
33const picocolors_1 = __importDefault(require("picocolors"));
34const yargs_1 = __importDefault(require("yargs"));
35function colorizeTextAndTags(textWithTags, textColor, bgColor) {
36 return textColor(textWithTags.replace(/\[(.*?)\]/g, (match, group1) => bgColor("[") + picocolors_1.default.inverse(group1) + bgColor("]")));
37}
38const prefixColors = [
39 picocolors_1.default.magenta,
40 picocolors_1.default.cyan,
41 picocolors_1.default.yellow,
42 picocolors_1.default.red,
43 picocolors_1.default.green,
44 picocolors_1.default.blue,
45 picocolors_1.default.white,
46];
47const usedPrefixes = [];
48function colorizePrefix(prefix) {
49 if (!prefix.includes(":"))
50 return picocolors_1.default.white(prefix);
51 const prefixShort = prefix.split(":").slice(-1)[0];
52 let prefixIndex = usedPrefixes.indexOf(prefixShort);
53 if (prefixIndex === -1) {
54 usedPrefixes.push(prefixShort);
55 prefixIndex = usedPrefixes.length - 1;
56 }
57 return prefixColors[prefixIndex % prefixColors.length](prefix);
58}
59function prependPrefix(prefix, str) {
60 if (!prefix)
61 return str;
62 return picocolors_1.default.bold(colorizePrefix(prefix)) + " " + str;
63}
64class CLI {
65 constructor(context) {
66 this.context = context;
67 // eslint-disable-next-line @typescript-eslint/no-inferrable-types
68 this.prefix = "";
69 this.colors = picocolors_1.default;
70 this.stripColors = release_script_core_1.stripColors;
71 }
72 log(msg) {
73 console.log(prependPrefix(this.context.cli.prefix, msg));
74 }
75 warn(msg) {
76 console.warn(prependPrefix(this.context.cli.prefix, colorizeTextAndTags(`[WARN] ${msg}`, picocolors_1.default.yellow, picocolors_1.default.bgYellow)));
77 this.context.warnings.push(msg);
78 }
79 error(msg) {
80 console.error(prependPrefix(this.context.cli.prefix, colorizeTextAndTags(`[ERR] ${msg}`, picocolors_1.default.red, picocolors_1.default.bgRed)));
81 this.context.errors.push(msg);
82 }
83 fatal(msg, code) {
84 throw new release_script_core_1.ReleaseError(msg, true, code);
85 }
86 logCommand(command, args) {
87 if (args === null || args === void 0 ? void 0 : args.length) {
88 command += ` ${args.join(" ")}`;
89 }
90 this.log(`$ ${command}`);
91 }
92 clearLines(lines) {
93 process.stdout.moveCursor(0, -lines);
94 process.stdout.clearScreenDown();
95 }
96 async select(question, options) {
97 try {
98 const result = await (0, enquirer_1.prompt)({
99 name: "default",
100 message: question,
101 type: "select",
102 choices: options.map((o) => ({
103 name: o.value,
104 message: o.label,
105 hint: o.hint ? this.colors.gray(${o.hint}`) : undefined,
106 })),
107 });
108 return result.default;
109 }
110 catch (e) {
111 // Strg+C
112 if (e === "")
113 this.fatal("Aborted by user");
114 throw e;
115 }
116 }
117 async ask(question, placeholder) {
118 try {
119 const result = await (0, enquirer_1.prompt)({
120 name: "default",
121 message: question,
122 type: "input",
123 initial: placeholder,
124 });
125 return result.default;
126 }
127 catch (e) {
128 // Strg+C
129 if (e === "")
130 this.fatal("Aborted by user");
131 throw e;
132 }
133 }
134}
135async function main() {
136 var _a, _b, _c, _d;
137 let argv = yargs_1.default
138 .env("RELEASE_SCRIPT")
139 .usage("$0 [<bump> [<preid>]] [options]", "AlCalzone's release script", (yargs) => yargs
140 .positional("bump", {
141 describe: "The version bump to do.",
142 choices: [
143 "major",
144 "premajor",
145 "minor",
146 "preminor",
147 "patch",
148 "prepatch",
149 "prerelease",
150 ],
151 required: false,
152 })
153 .positional("preid", {
154 describe: "The prerelease identifier. Only for pre* bumps.",
155 required: false,
156 }))
157 .wrap(yargs_1.default.terminalWidth())
158 // Delay showing help until the second parsing pass
159 .help(false)
160 .alias("v", "version")
161 .options({
162 config: {
163 alias: "c",
164 describe: "Path to the release config file",
165 config: true,
166 default: ".releaseconfig.json",
167 },
168 plugins: {
169 alias: "p",
170 describe: "Additional plugins to load",
171 string: true,
172 array: true,
173 },
174 verbose: {
175 alias: "V",
176 type: "boolean",
177 description: "Enable debug output",
178 default: false,
179 },
180 yes: {
181 alias: "y",
182 type: "boolean",
183 description: "Answer all (applicable) yes/no prompts with yes",
184 default: false,
185 },
186 publishAll: {
187 type: "boolean",
188 description: `Bump and publish all non-private packages in monorepos, even if they didn't change`,
189 default: false,
190 },
191 });
192 // We do two-pass parsing:
193 // 1. parse the config file and plugins (non-strict)
194 // 2. parse all options (strict)
195 let parsedArgv = (await argv.parseAsync());
196 const chosenPlugins = (0, arrays_1.distinct)([
197 // These plugins must always be loaded
198 "git",
199 "package",
200 "exec",
201 "version",
202 "changelog",
203 // These are provided by the user
204 ...(parsedArgv.plugins || []),
205 ]);
206 const allPlugins = await Promise.all(chosenPlugins.map(async (plugin) => new (await Promise.resolve().then(() => __importStar(require(`@alcalzone/release-script-plugin-${plugin}`)))).default()));
207 const plugins = (0, release_script_core_1.resolvePlugins)(allPlugins, chosenPlugins);
208 argv = argv
209 .strict()
210 .help(true)
211 .alias("h", "help")
212 .options({
213 dryRun: {
214 alias: "dry",
215 type: "boolean",
216 description: "Perform a dry-run: check status, describe changes without changing anything",
217 default: false,
218 },
219 });
220 // Let plugins hook into the CLI options
221 for (const plugin of plugins) {
222 if (typeof plugin.defineCLIOptions === "function") {
223 argv = plugin.defineCLIOptions(argv);
224 }
225 }
226 parsedArgv = (await argv.parseAsync());
227 const data = new Map();
228 const context = {
229 cwd: process.cwd(),
230 cli: undefined,
231 sys: {
232 exec: release_script_core_1.exec,
233 execRaw: release_script_core_1.execRaw,
234 },
235 argv: parsedArgv,
236 plugins,
237 warnings: [],
238 errors: [],
239 getData: (key) => {
240 if (!data.has(key)) {
241 throw new release_script_core_1.ReleaseError(`A plugin tried to access non-existent data with key "${key}"`, true);
242 }
243 else {
244 return data.get(key);
245 }
246 },
247 hasData: (key) => data.has(key),
248 setData: (key, value) => {
249 data.set(key, value);
250 },
251 };
252 context.cli = new CLI(context);
253 try {
254 // Initialize plugins
255 for (const plugin of plugins) {
256 await ((_a = plugin.init) === null || _a === void 0 ? void 0 : _a.call(plugin, context));
257 }
258 // Execute stages
259 await (0, release_script_core_1.execute)(context);
260 const numWarnings = context.warnings.length;
261 const numErrors = context.errors.length;
262 if (numErrors > 0) {
263 let message = `Release did not complete. There ${numErrors + numWarnings !== 1 ? "were" : "was"} ${picocolors_1.default.red(`${numErrors} error${numErrors !== 1 ? "s" : ""}`)}`;
264 if (numWarnings > 0) {
265 message += ` and ${picocolors_1.default.yellow(`${numWarnings} warning${numWarnings !== 1 ? "s" : ""}`)}`;
266 }
267 message += "!";
268 console.error();
269 console.error(message);
270 process.exit(1);
271 }
272 }
273 catch (e) {
274 if ((0, release_script_core_1.isReleaseError)(e)) {
275 console.error(prependPrefix(context.cli.prefix, colorizeTextAndTags(`[FATAL] ${e.message.replace("ReleaseError: ", "")}`, picocolors_1.default.red, picocolors_1.default.bgRed)));
276 }
277 else {
278 const msg = (_c = (_b = e.stack) !== null && _b !== void 0 ? _b : e.message) !== null && _c !== void 0 ? _c : String(e);
279 console.error(prependPrefix(context.cli.prefix, colorizeTextAndTags(`[FATAL] ${msg}`, picocolors_1.default.red, picocolors_1.default.bgRed)));
280 }
281 process.exit((_d = e.code) !== null && _d !== void 0 ? _d : 1);
282 }
283}
284exports.main = main;
285void main();
286//# sourceMappingURL=index.js.map
\No newline at end of file