(target: UpdateTarget, currentBin: string)
| 147 | } |
| 148 | |
| 149 | export async function applySelfUpdate(target: UpdateTarget, currentBin: string): Promise<void> { |
| 150 | const tmp = join(tmpdir(), `mmx-update-${Date.now()}`); |
| 151 | |
| 152 | process.stderr.write(`Downloading ${target.version}...\n`); |
| 153 | let lastPct = -1; |
| 154 | await downloadFile(target.downloadUrl, tmp, (pct) => { |
| 155 | if (pct !== lastPct && pct % 10 === 0) { |
| 156 | process.stderr.write(` ${pct}%\r`); |
| 157 | lastPct = pct; |
| 158 | } |
| 159 | }); |
| 160 | process.stderr.write(' \r'); |
| 161 | |
| 162 | process.stderr.write('Verifying checksum...\n'); |
| 163 | await verifySha256(tmp, target.checksum); |
| 164 | |
| 165 | chmodSync(tmp, 0o755); |
| 166 | |
| 167 | // Atomic replace: rename works on same filesystem |
| 168 | // If cross-device, fall back to copy+rename |
| 169 | try { |
| 170 | renameSync(tmp, currentBin); |
| 171 | } catch { |
| 172 | const { copyFileSync, unlinkSync } = await import('fs'); |
| 173 | const backup = `${currentBin}.bak`; |
| 174 | copyFileSync(currentBin, backup); |
| 175 | try { |
| 176 | copyFileSync(tmp, currentBin); |
| 177 | chmodSync(currentBin, 0o755); |
| 178 | unlinkSync(tmp); |
| 179 | if (existsSync(backup)) unlinkSync(backup); |
| 180 | } catch (e) { |
| 181 | // Restore backup |
| 182 | if (existsSync(backup)) renameSync(backup, currentBin); |
| 183 | throw e; |
| 184 | } |
| 185 | } |
| 186 | } |
nothing calls this directly
no test coverage detected