-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
172 lines (154 loc) · 4.81 KB
/
index.ts
File metadata and controls
172 lines (154 loc) · 4.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import { Command, Option } from "@commander-js/extra-typings";
import path from "path";
import YAML from "yaml";
import { ZodError } from "zod";
import { resolveConfig } from "./src/config";
import { style, LogLevel, logger } from "./src/logging";
import { resolveOutputPrinter } from "./src/output";
import { allPlatformTypes, resolvePlatform } from "./src/platform";
import { resolveVersion } from "./src/version/versionResolver";
import { resolveStrategies } from "./src/version/versionStrategy";
declare const __GTS_VERSION__: string;
const gtsVersion =
typeof __GTS_VERSION__ !== "undefined" ? __GTS_VERSION__ : "dev";
const program = new Command("git-that-semver")
.version(gtsVersion)
.addOption(
new Option(
"-f, --config-file <config-file>",
"Config file (git-that-semver.yaml)",
)
.env("GTS_CONFIG_FILE")
.default("git-that-semver.yaml"),
)
.addOption(
new Option(
"-c, --config-value <config-values...>",
"Override config values (e.g. output.json.indent=2)",
).default([]),
)
.addOption(
new Option("--log-level <level>", "Log level")
.env("GTS_LOG_LEVEL")
.default("INFO" as LogLevel)
.choices(["TRACE", "DEBUG", "INFO", "WARN", "ERROR", "SILENT"] as const),
)
.addOption(
new Option(
"-e, --enable-strategy <strategies...>",
"Enable strategies by name",
).default([]),
)
.addOption(
new Option(
"-d, --disable-strategy <strategies...>",
"Disable strategies by name",
).default([]),
)
.addOption(
new Option("-o, --output-format <format>", "Output format")
.env("GTS_OUTPUT_FORMAT")
.default("env")
.choices(["env", "json", "yaml"] as const),
)
.addOption(
new Option("--platform <platform>", "Platform type")
.env("GTS_PLATFORM")
.choices(["auto", ...allPlatformTypes] as const),
)
.addOption(
new Option("--commit-sha <sha>", "Commit SHA (manual platform)").env(
"GTS_COMMIT_SHA",
),
)
.addOption(
new Option("--ref-name <name>", "Branch/tag name (manual platform)").env(
"GTS_REF_NAME",
),
)
.addOption(
new Option("--git-tag <tag>", "Git tag (manual platform)").env(
"GTS_GIT_TAG",
),
)
.addOption(
new Option(
"--change-request-id <id>",
"Change request identifier (manual platform)",
).env("GTS_CHANGE_REQUEST_ID"),
)
.option("--dump-config", "Dump configuration for debug purposes")
.configureOutput({
writeErr: (str) =>
process.stderr.write(`${style.red.bold("[ERROR]")} ${str}`),
})
.parse();
logger.setLevel(LogLevel[program.opts().logLevel]);
try {
const config = await resolveConfig(
path.resolve(program.opts().configFile),
[...program.opts().enableStrategy],
[...program.opts().disableStrategy],
program.opts().outputFormat,
[...program.opts().configValue],
);
if (program.opts().dumpConfig) {
logger.info("Dumping resolved config file as --dump-config was passed");
let configOutputFormat = program.opts().outputFormat;
if (configOutputFormat === "env") {
logger.info(
"Selected output format is 'env' which is not supported for config dump, using YAML instead",
);
configOutputFormat = "yaml";
}
if (configOutputFormat === "json") {
console.log(JSON.stringify(config, null, 2));
} else if (configOutputFormat === "yaml") {
console.log(YAML.stringify(config));
}
process.exit(0);
}
const opts = program.opts();
const platformType = opts.platform ?? config.platform;
const hasManualOpts = !!(
opts.commitSha ||
opts.refName ||
opts.gitTag ||
opts.changeRequestId
);
const manualOpts = hasManualOpts
? {
sha: opts.commitSha ?? "",
refName: opts.refName ?? "",
tag: opts.gitTag,
changeRequestId: opts.changeRequestId,
}
: undefined;
const platform = resolvePlatform(platformType, manualOpts);
const strategies = resolveStrategies(config.strategies);
const result = resolveVersion(config, platform, strategies);
const outputPrinter = resolveOutputPrinter(config);
outputPrinter.printResult(config, result);
} catch (e) {
logger.debug("Encountered exception", e);
let exitCode = 2;
let errorMessage = style.white.bold("An unexpected error occurred.");
if (e instanceof ZodError) {
exitCode = 3;
errorMessage = style.white.bold("Failed to parse configuration:") + "\n\n";
errorMessage += e.issues
.map(
(err) =>
style.red.bold(" •") +
" " +
style.white.bold(err.path.join(".") + ": ") +
err.message,
)
.join("\n");
} else if (e instanceof Error) {
errorMessage = style.white.bold(e.message);
} else if (typeof e === "string") {
errorMessage = style.white.bold(e);
}
program.error(errorMessage, { exitCode: exitCode });
}