Skip to content

Commit

Permalink
feat: only download zipfile if its outdated or does not exist
Browse files Browse the repository at this point in the history
  • Loading branch information
irony committed Sep 29, 2023
1 parent 4295440 commit 75f0fd7
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions packages/simulator/streams/gtfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,30 @@ const downloadIfNotExists = (operator) => {
reject(err)
})
)
.catch((err) => error('Error fetching GTFS', err) || reject())
.catch((err) => error('Error fetching GTFS', err) || reject(err))
} else {
resolve()
resolve(zipFile)
}
})
}

const downloadAndExtractIfNotExists = (operator) => {
return downloadIfNotExists(operator).then((zipFile) => {
const outPath = path.join(__dirname, `../.cache/${operator}`)
const zip = new AdmZip(zipFile)
if (!fs.existsSync(outPath)) fs.mkdirSync(outPath, true)
zip.extractAllTo(outPath, true)
return zipFile
})
return downloadIfNotExists(operator)
.then((zipFile) => {
try {
const outPath = path.join(__dirname, `../.cache/${operator}`)
const zip = new AdmZip(zipFile)
if (!fs.existsSync(outPath)) fs.mkdirSync(outPath, true)
zip.extractAllTo(outPath, true)
return zipFile
} catch (err) {
error('Error unpacking', err)
fs.rmSync(zipFile) // try again next time
}
})
.catch((err) => {
error('Error when unpacking GTFS file', err)
})
}

function gtfs(operator) {
Expand Down

0 comments on commit 75f0fd7

Please sign in to comment.