( info: UpdateObject | null, settings: LoadedSettings, projectRoot: string, spawnFn: typeof spawn = spawnWrapper, )
| 14 | import { spawn } from 'child_process'; |
| 15 | |
| 16 | export async function handleAutoUpdate( |
| 17 | info: UpdateObject | null, |
| 18 | settings: LoadedSettings, |
| 19 | projectRoot: string, |
| 20 | spawnFn: typeof spawn = spawnWrapper, |
| 21 | ) { |
| 22 | if (!info) { |
| 23 | return; |
| 24 | } |
| 25 | |
| 26 | if (settings.merged.disableUpdateNag) { |
| 27 | return; |
| 28 | } |
| 29 | |
| 30 | const installationInfo = await getInstallationInfo( |
| 31 | projectRoot, |
| 32 | settings.merged.disableAutoUpdate ?? false, |
| 33 | ); |
| 34 | |
| 35 | let combinedMessage = info.message; |
| 36 | if (installationInfo.updateMessage) { |
| 37 | combinedMessage += `\n${installationInfo.updateMessage}`; |
| 38 | } |
| 39 | |
| 40 | updateEventEmitter.emit('update-received', { |
| 41 | message: combinedMessage, |
| 42 | }); |
| 43 | |
| 44 | if (!installationInfo.updateCommand || settings.merged.disableAutoUpdate) { |
| 45 | return; |
| 46 | } |
| 47 | const isNightly = info.update.latest.includes('nightly'); |
| 48 | |
| 49 | const updateCommand = installationInfo.updateCommand.replace( |
| 50 | '@latest', |
| 51 | isNightly ? '@nightly' : `@${info.update.latest}`, |
| 52 | ); |
| 53 | const updateProcess = spawnFn(updateCommand, { stdio: 'pipe', shell: true }); |
| 54 | let errorOutput = ''; |
| 55 | updateProcess.stderr.on('data', (data) => { |
| 56 | errorOutput += data.toString(); |
| 57 | }); |
| 58 | |
| 59 | updateProcess.on('close', (code) => { |
| 60 | if (code === 0) { |
| 61 | updateEventEmitter.emit('update-success', { |
| 62 | message: |
| 63 | 'Update successful! The new version will be used on your next run.', |
| 64 | }); |
| 65 | } else { |
| 66 | updateEventEmitter.emit('update-failed', { |
| 67 | message: `Automatic update failed. Please try updating manually. (command: ${updateCommand}, stderr: ${errorOutput.trim()})`, |
| 68 | }); |
| 69 | } |
| 70 | }); |
| 71 | |
| 72 | updateProcess.on('error', (err) => { |
| 73 | updateEventEmitter.emit('update-failed', { |
no test coverage detected