( target: string | undefined, options: RollbackOptions, )
| 96 | } |
| 97 | |
| 98 | async function resolveRollbackTarget( |
| 99 | target: string | undefined, |
| 100 | options: RollbackOptions, |
| 101 | ): Promise<string> { |
| 102 | if (options.safe) { |
| 103 | const safeVersion = await getMaxVersion() |
| 104 | if (!safeVersion) { |
| 105 | throw new Error('No safe rollback version is currently configured.') |
| 106 | } |
| 107 | return safeVersion |
| 108 | } |
| 109 | |
| 110 | if (target && /^\d+$/.test(target)) { |
| 111 | const stepsBack = Number.parseInt(target, 10) |
| 112 | const history = await getVersionHistory(TARGET_RESOLUTION_HISTORY_LIMIT) |
| 113 | if (history.length === 0) { |
| 114 | throw new Error('Unable to load version history for rollback.') |
| 115 | } |
| 116 | |
| 117 | const currentIndex = history.indexOf(MACRO.VERSION) |
| 118 | if (currentIndex === -1) { |
| 119 | throw new Error( |
| 120 | `Current version ${MACRO.VERSION} was not found in the recent version history.`, |
| 121 | ) |
| 122 | } |
| 123 | |
| 124 | const targetIndex = currentIndex + stepsBack |
| 125 | if (targetIndex >= history.length) { |
| 126 | throw new Error( |
| 127 | `Only ${history.length - currentIndex - 1} older version(s) are available from the recent history window.`, |
| 128 | ) |
| 129 | } |
| 130 | |
| 131 | return history[targetIndex]! |
| 132 | } |
| 133 | |
| 134 | if (target) { |
| 135 | return target |
| 136 | } |
| 137 | |
| 138 | const history = await getVersionHistory(TARGET_RESOLUTION_HISTORY_LIMIT) |
| 139 | if (history.length === 0) { |
| 140 | throw new Error('Unable to load version history for rollback.') |
| 141 | } |
| 142 | |
| 143 | const currentIndex = history.indexOf(MACRO.VERSION) |
| 144 | if (currentIndex === -1) { |
| 145 | throw new Error( |
| 146 | `Current version ${MACRO.VERSION} was not found in the recent version history.`, |
| 147 | ) |
| 148 | } |
| 149 | |
| 150 | const targetIndex = currentIndex + 1 |
| 151 | if (targetIndex >= history.length) { |
| 152 | throw new Error('No older published version is available to roll back to.') |
| 153 | } |
| 154 | |
| 155 | return history[targetIndex]! |
no test coverage detected