(passthrough: string[])
| 9 | } |
| 10 | |
| 11 | export const runUpdate = async (passthrough: string[]): Promise<number> => { |
| 12 | const spinner = ora('Updating relay...').start() |
| 13 | |
| 14 | let code = await runStop({ all: true }, []) |
| 15 | if (code !== 0) { |
| 16 | spinner.fail('Update failed while stopping relay') |
| 17 | return code |
| 18 | } |
| 19 | |
| 20 | const stashResult = await runCommandWithOutput('git', ['stash', 'push', '-u', '-m', 'nostream-cli-update']) |
| 21 | if (!stashResult.ok) { |
| 22 | spinner.fail(stashResult.ok === false && stashResult.reason === 'not-found' ? 'Update failed: git is not installed' : 'Update failed while stashing local changes') |
| 23 | return 1 |
| 24 | } |
| 25 | if (stashResult.code !== 0) { |
| 26 | spinner.fail('Update failed while stashing local changes') |
| 27 | return stashResult.code |
| 28 | } |
| 29 | |
| 30 | const stashOutput = `${stashResult.stdout}\n${stashResult.stderr}` |
| 31 | const stashCreated = wasStashCreated(stashOutput) |
| 32 | |
| 33 | code = await runCommand('git', ['pull']) |
| 34 | if (code !== 0) { |
| 35 | if (stashCreated) { |
| 36 | const restoreCode = await runCommand('git', ['stash', 'pop']) |
| 37 | if (restoreCode === 0) { |
| 38 | spinner.fail('Update failed while pulling latest changes. Restored stashed local changes.') |
| 39 | return code |
| 40 | } |
| 41 | |
| 42 | spinner.fail( |
| 43 | 'Update failed while pulling latest changes, and restoring stashed local changes also failed. Run `git stash list` then `git stash pop` manually.', |
| 44 | ) |
| 45 | return restoreCode |
| 46 | } |
| 47 | |
| 48 | spinner.fail('Update failed while pulling latest changes.') |
| 49 | return code |
| 50 | } |
| 51 | |
| 52 | if (stashCreated) { |
| 53 | code = await runCommand('git', ['stash', 'pop']) |
| 54 | if (code !== 0) { |
| 55 | spinner.fail('Update pulled latest changes, but reapplying stashed changes failed') |
| 56 | return code |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | code = await runStart({}, passthrough) |
| 61 | if (code !== 0) { |
| 62 | spinner.fail('Update finished pull, but restart failed') |
| 63 | return code |
| 64 | } |
| 65 | |
| 66 | spinner.succeed('Relay updated and restarted') |
| 67 | return 0 |
| 68 | } |
no test coverage detected