(version)
| 248 | } |
| 249 | |
| 250 | async function writePDFJSVersion(version) { |
| 251 | if (!version) { |
| 252 | throw new Error(`Could not determine ${pdfJSVersionEnvName}`) |
| 253 | } |
| 254 | |
| 255 | const nextLine = `${pdfJSVersionEnvName}=${trimVersionTag(version)}` |
| 256 | const envText = await fs.readFile(envPath, 'utf8').catch(error => { |
| 257 | if (error && error.code === 'ENOENT') { |
| 258 | return '' |
| 259 | } |
| 260 | |
| 261 | throw error |
| 262 | }) |
| 263 | const lineEnding = envText.includes('\r\n') ? '\r\n' : '\n' |
| 264 | const lines = envText.split(/\r?\n/) |
| 265 | const envLinePattern = new RegExp( |
| 266 | `^\\s*(?:export\\s+)?${escapeRegExp(pdfJSVersionEnvName)}\\s*=` |
| 267 | ) |
| 268 | let updated = false |
| 269 | |
| 270 | for (let i = 0; i < lines.length; i++) { |
| 271 | if (envLinePattern.test(lines[i])) { |
| 272 | lines[i] = nextLine |
| 273 | updated = true |
| 274 | break |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | if (updated) { |
| 279 | await fs.outputFile(envPath, lines.join(lineEnding)) |
| 280 | return |
| 281 | } |
| 282 | |
| 283 | const separator = |
| 284 | envText && !envText.endsWith('\n') && !envText.endsWith('\r') |
| 285 | ? lineEnding |
| 286 | : '' |
| 287 | const sectionBreak = envText ? lineEnding : '' |
| 288 | |
| 289 | await fs.outputFile( |
| 290 | envPath, |
| 291 | `${envText}${separator}${sectionBreak}${nextLine}${lineEnding}` |
| 292 | ) |
| 293 | } |
| 294 | |
| 295 | function escapeRegExp(text) { |
| 296 | return text.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') |
no test coverage detected