(targetVersion?: string)
| 104 | * reported without relying on whichever binary happens to be on `PATH`. |
| 105 | */ |
| 106 | export async function executeUpgrade(targetVersion?: string): Promise<number> { |
| 107 | const totalSteps = targetVersion ? 2 : 3; |
| 108 | renderUpgradeProgress(0, totalSteps, 'Resolving installer…'); |
| 109 | |
| 110 | const { command: updateCommand, global } = await getUpdateCommandInfo().catch( |
| 111 | error => { |
| 112 | output.stopSpinner(); |
| 113 | throw error; |
| 114 | } |
| 115 | ); |
| 116 | const [command, ...args] = updateCommand.split(' '); |
| 117 | |
| 118 | const cwd = global ? tmpdir() : process.cwd(); |
| 119 | |
| 120 | // The version currently running, captured before the install overwrites it. |
| 121 | // This is what `vc --version` reports, for both Node.js and native binary. |
| 122 | const versionBefore = pkg.version; |
| 123 | |
| 124 | let resolvedTargetVersion = targetVersion; |
| 125 | if (!resolvedTargetVersion) { |
| 126 | renderUpgradeProgress(1, totalSteps, 'Checking for updates…'); |
| 127 | resolvedTargetVersion = await getLatestPackageVersion(command, args); |
| 128 | } |
| 129 | |
| 130 | if ( |
| 131 | resolvedTargetVersion && |
| 132 | isVersionCurrent(versionBefore, resolvedTargetVersion) |
| 133 | ) { |
| 134 | renderUpgradeProgress(totalSteps, totalSteps); |
| 135 | output.stopSpinner(); |
| 136 | output.log( |
| 137 | `No upgrade available. Vercel CLI is already up to date (v${versionBefore}).` |
| 138 | ); |
| 139 | return 0; |
| 140 | } |
| 141 | |
| 142 | output.debug(`Executing: ${updateCommand} (cwd: ${cwd})`); |
| 143 | renderUpgradeProgress(targetVersion ? 1 : 2, totalSteps, 'Installing…'); |
| 144 | |
| 145 | return new Promise<number>(resolve => { |
| 146 | const stdout: Uint8Array[] = []; |
| 147 | const stderr: Uint8Array[] = []; |
| 148 | |
| 149 | const upgradeProcess = spawn(command, args, { |
| 150 | cwd, |
| 151 | stdio: ['inherit', 'pipe', 'pipe'], |
| 152 | shell: false, |
| 153 | }); |
| 154 | |
| 155 | upgradeProcess.stdout?.on('data', (data: Buffer) => { |
| 156 | stdout.push(Uint8Array.from(data)); |
| 157 | }); |
| 158 | |
| 159 | upgradeProcess.stderr?.on('data', (data: Buffer) => { |
| 160 | stderr.push(Uint8Array.from(data)); |
| 161 | }); |
| 162 | |
| 163 | upgradeProcess.on('error', (err: Error) => { |
no test coverage detected