fix(cli.js): remove cli file if the download fails or process is killed (#1592)

This commit is contained in:
Lucas Fernandes Nogueira 2021-04-22 23:10:45 -03:00 committed by GitHub
parent ae156e77e0
commit 8a32d0ec39
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 1 deletions

View File

@ -0,0 +1,5 @@
---
"cli.js": patch
---
Remove Rust CLI download file

View File

@ -10,6 +10,8 @@ const pipeline = promisify(stream.pipeline)
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-var-requires, @typescript-eslint/no-unsafe-member-access
const tauriCliManifest = require('../../../cli.rs/Cargo.toml') as CargoManifest
let downloadedCli = false
const downloadCli = async (): Promise<void> => {
const version = tauriCliManifest.package.version
let platform: string = process.platform
@ -27,9 +29,31 @@ const downloadCli = async (): Promise<void> => {
const outPath = path.join(__dirname, `../../bin/tauri-cli${exe}`)
console.log('Downloading Tauri CLI')
const removeDownloadedCliIfNeeded = (): void => {
try {
if (!downloadedCli) {
// eslint-disable-next-line security/detect-non-literal-fs-filename
fs.unlinkSync(outPath)
}
} finally {
process.exit()
}
}
// on exit, we remove the `tauri-cli` file if the download didn't complete
process.on('exit', removeDownloadedCliIfNeeded)
process.on('SIGINT', removeDownloadedCliIfNeeded)
process.on('SIGTERM', removeDownloadedCliIfNeeded)
process.on('SIGHUP', removeDownloadedCliIfNeeded)
process.on('SIGBREAK', removeDownloadedCliIfNeeded)
// TODO: Check hash of download
// eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access, security/detect-non-literal-fs-filename
await pipeline(got.stream(url), fs.createWriteStream(outPath))
await pipeline(got.stream(url), fs.createWriteStream(outPath)).catch((e) => {
removeDownloadedCliIfNeeded()
throw e
})
downloadedCli = true
// eslint-disable-next-line security/detect-non-literal-fs-filename
fs.chmodSync(outPath, 0o700)
console.log('Download Complete')