fix: make setup-cpp --version/help immutable

This commit is contained in:
Amin Yahyaabadi 2025-03-01 02:17:33 -08:00
parent 3ce1db2ddb
commit 43d46eff60
6 changed files with 33 additions and 33 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -7,8 +7,6 @@ import type { InstallationInfo } from "./utils/setup/setupBin.js"
export function parseArgs(args: string[]): Opts {
const defaults = Object.fromEntries(inputs.map((inp) => [inp, maybeGetInput(inp)]))
defaults["setup-cpp"] = "true"
return mri<Record<Inputs, string | undefined> & { help: boolean; version: boolean; "setup-cpp": boolean }>(args, {
string: [...inputs, "timeout", "node-package-manager"],
default: defaults,
@ -70,7 +68,7 @@ export type Opts = mri.Argv<
Record<Inputs, string | undefined> & {
help: boolean
version: boolean
"setup-cpp": boolean
"setup-cpp"?: boolean
timeout?: string
"node-package-manager"?: string
}

View File

@ -28,10 +28,6 @@ async function main(args: string[]): Promise<number> {
// parse options using mri or github actions
const opts = parseArgs(args)
const installSetupCppPromise = opts["setup-cpp"]
? installSetupCpp(packageJson.version, opts["node-package-manager"])
: Promise.resolve()
// print help
if (opts.help) {
printHelp()
@ -129,13 +125,20 @@ async function main(args: string[]): Promise<number> {
await finalizeRC(rcOptions)
if (successMessages.length === 0 && errorMessages.length === 0) {
if (!opts.version && !opts.help) {
info("setup-cpp was called without any arguments. Nothing to do.")
}
return 0
const noTool = successMessages.length === 0 && errorMessages.length === 0
// if setup-cpp option is not passed, install setup-cpp by default unless only help or version is passed
// So that --help and --version are immutable
if (opts["setup-cpp"] === undefined) {
opts["setup-cpp"] = !(noTool && (opts.version || opts.help))
}
const installSetupCppPromise = opts["setup-cpp"]
? installSetupCpp(packageJson.version, opts["node-package-manager"])
: Promise.resolve()
await Promise.all([checkUpdatePromise, installSetupCppPromise])
// report the messages in the end
for (const tool of successMessages) {
success(tool)
@ -144,27 +147,26 @@ async function main(args: string[]): Promise<number> {
error(tool)
}
info("setup-cpp finished")
if (successMessages.length !== 0 || errorMessages.length !== 0) {
info("setup-cpp finished")
if (!GITHUB_ACTIONS) {
switch (process.platform) {
case "win32": {
warning("Run `RefreshEnv.cmd` or restart your shell to update the environment.")
break
}
case "linux":
case "darwin": {
warning("Run `source ~/.cpprc` or restart your shell to update the environment.")
break
}
default: {
// nothing
if (!GITHUB_ACTIONS) {
switch (process.platform) {
case "win32": {
warning("Run `RefreshEnv.cmd` or restart your shell to update the environment.")
break
}
case "linux":
case "darwin": {
warning("Run `source ~/.cpprc` or restart your shell to update the environment.")
break
}
default: {
// nothing
}
}
}
}
await Promise.all([checkUpdatePromise, installSetupCppPromise])
return errorMessages.length === 0 ? 0 : 1 // exit with non-zero if any error message
}