mirror of
https://github.com/tauri-apps/tauri.git
synced 2024-12-01 11:13:40 +03:00
fix(cli.js): remove cli file if the download fails or process is killed (#1592)
This commit is contained in:
parent
ae156e77e0
commit
8a32d0ec39
5
.changes/cli-js-download-stop.md
Normal file
5
.changes/cli-js-download-stop.md
Normal file
@ -0,0 +1,5 @@
|
||||
---
|
||||
"cli.js": patch
|
||||
---
|
||||
|
||||
Remove Rust CLI download 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')
|
||||
|
Loading…
Reference in New Issue
Block a user