Skip to content

Commit 75f0fd7

Browse files
committed
feat: only download zipfile if its outdated or does not exist
1 parent 4295440 commit 75f0fd7

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

packages/simulator/streams/gtfs.js

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,21 +36,30 @@ const downloadIfNotExists = (operator) => {
3636
reject(err)
3737
})
3838
)
39-
.catch((err) => error('Error fetching GTFS', err) || reject())
39+
.catch((err) => error('Error fetching GTFS', err) || reject(err))
4040
} else {
41-
resolve()
41+
resolve(zipFile)
4242
}
4343
})
4444
}
4545

4646
const downloadAndExtractIfNotExists = (operator) => {
47-
return downloadIfNotExists(operator).then((zipFile) => {
48-
const outPath = path.join(__dirname, `../.cache/${operator}`)
49-
const zip = new AdmZip(zipFile)
50-
if (!fs.existsSync(outPath)) fs.mkdirSync(outPath, true)
51-
zip.extractAllTo(outPath, true)
52-
return zipFile
53-
})
47+
return downloadIfNotExists(operator)
48+
.then((zipFile) => {
49+
try {
50+
const outPath = path.join(__dirname, `../.cache/${operator}`)
51+
const zip = new AdmZip(zipFile)
52+
if (!fs.existsSync(outPath)) fs.mkdirSync(outPath, true)
53+
zip.extractAllTo(outPath, true)
54+
return zipFile
55+
} catch (err) {
56+
error('Error unpacking', err)
57+
fs.rmSync(zipFile) // try again next time
58+
}
59+
})
60+
.catch((err) => {
61+
error('Error when unpacking GTFS file', err)
62+
})
5463
}
5564

5665
function gtfs(operator) {

0 commit comments

Comments
 (0)