MCPcopy Create free account
hub / github.com/crimx/ext-saladict / writePDFJSVersion

Function writePDFJSVersion

scripts/pdf.js:250–293  ·  view source on GitHub ↗
(version)

Source from the content-addressed store, hash-verified

248}
249
250async 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
295function escapeRegExp(text) {
296 return text.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')

Callers 1

startUpgradeFunction · 0.85

Calls 2

trimVersionTagFunction · 0.85
escapeRegExpFunction · 0.70

Tested by

no test coverage detected