* Get a file. * * @param {Readonly } localUrl * URL to file system. * @param {Readonly } remoteUrl * URL to remote. * @returns {Promise } * Content, if local or remote.
(localUrl, remoteUrl)
| 250 | * Content, if local or remote. |
| 251 | */ |
| 252 | async function readOrFetch(localUrl, remoteUrl) { |
| 253 | try { |
| 254 | return String(await fs.readFile(localUrl)) |
| 255 | } catch (error) { |
| 256 | const exception = /** @type {NodeJS.ErrnoException} */ (error) |
| 257 | if (exception.code !== 'ENOENT') { |
| 258 | throw exception |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | try { |
| 263 | const response = await fetch(remoteUrl, { |
| 264 | headers: {Authorization: 'bearer ' + ghKey} |
| 265 | }) |
| 266 | const responseText = await response.text() |
| 267 | await fs.writeFile(localUrl, responseText) |
| 268 | return responseText |
| 269 | } catch (error) { |
| 270 | console.error('Could not fetch `%s`', remoteUrl, error) |
| 271 | } |
| 272 | } |