(fn: () => Promise<T>)
| 205 | * @returns Wrapped function which overrides `TEMP` environment variable, before cleaning up afterwards. |
| 206 | */ |
| 207 | export function withLocalTmpDir<T>(fn: () => Promise<T>): () => Promise<T> { |
| 208 | if (os.platform() !== 'win32') { |
| 209 | return fn; |
| 210 | } |
| 211 | |
| 212 | return async () => { |
| 213 | const originalTmpDir = process.env['TEMP']; |
| 214 | const localPath = path.join(pluginWorkDir(LIGHTHOUSE_PLUGIN_SLUG), 'tmp'); |
| 215 | |
| 216 | // eslint-disable-next-line functional/immutable-data |
| 217 | process.env['TEMP'] = localPath; |
| 218 | logger.debug( |
| 219 | `Temporarily overwriting TEMP environment variable with ${localPath} to prevent permissions error on cleanup`, |
| 220 | ); |
| 221 | |
| 222 | try { |
| 223 | return await fn(); |
| 224 | } finally { |
| 225 | // eslint-disable-next-line functional/immutable-data |
| 226 | process.env['TEMP'] = originalTmpDir; |
| 227 | logger.debug( |
| 228 | `Restored TEMP environment variable to original value ${originalTmpDir}`, |
| 229 | ); |
| 230 | } |
| 231 | }; |
| 232 | } |
no test coverage detected