Browse Source

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

Lucas Fernandes Nogueira 4 years ago
parent
commit
8a32d0ec39
2 changed files with 30 additions and 1 deletions
  1. 5 0
      .changes/cli-js-download-stop.md
  2. 25 1
      tooling/cli.js/src/helpers/download-cli.ts

+ 5 - 0
.changes/cli-js-download-stop.md

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

+ 25 - 1
tooling/cli.js/src/helpers/download-cli.ts

@@ -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')