diff --git a/.changes/cli-js-download-stop.md b/.changes/cli-js-download-stop.md new file mode 100644 index 000000000..1d0cd7c8e --- /dev/null +++ b/.changes/cli-js-download-stop.md @@ -0,0 +1,5 @@ +--- +"cli.js": patch +--- + +Remove Rust CLI download file \ No newline at end of file diff --git a/tooling/cli.js/src/helpers/download-cli.ts b/tooling/cli.js/src/helpers/download-cli.ts index 6ffedb6c9..ac41170b1 100644 --- a/tooling/cli.js/src/helpers/download-cli.ts +++ b/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 => { const version = tauriCliManifest.package.version let platform: string = process.platform @@ -27,9 +29,31 @@ const downloadCli = async (): Promise => { 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')