From bf387cd5e0ac01d1430fab536c07651753f8d4d7 Mon Sep 17 00:00:00 2001 From: Hugues Pouillot Date: Tue, 17 Jun 2025 12:01:27 +0200 Subject: [PATCH] feat(cli): delete sourcemaps after upload (#33758) --- cli/src/commands/mod.rs | 6 ++++++ cli/src/commands/sourcemap/upload.rs | 22 ++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/cli/src/commands/mod.rs b/cli/src/commands/mod.rs index 961ebf91b1..3583db7b27 100644 --- a/cli/src/commands/mod.rs +++ b/cli/src/commands/mod.rs @@ -62,6 +62,10 @@ pub enum SourcemapCommand { /// disk if not provided. #[arg(long)] version: Option, + + /// Whether to delete the source map files after uploading them + #[arg(long, default_value = "false")] + delete_after: bool, }, } @@ -81,12 +85,14 @@ impl Cli { directory, project, version, + delete_after, } => { sourcemap::upload::upload( command.host, directory, project.clone(), version.clone(), + *delete_after, )?; } }, diff --git a/cli/src/commands/sourcemap/upload.rs b/cli/src/commands/sourcemap/upload.rs index 9f87e9330b..c4ccd5c42f 100644 --- a/cli/src/commands/sourcemap/upload.rs +++ b/cli/src/commands/sourcemap/upload.rs @@ -15,6 +15,7 @@ pub fn upload( directory: &PathBuf, project: Option, version: Option, + delete_after: bool, ) -> Result<()> { let token = load_token().context("While starting upload command")?; let host = token.get_host(host.as_deref()); @@ -27,6 +28,10 @@ pub fn upload( ); let pairs = read_pairs(directory)?; + let sourcemap_paths = pairs + .iter() + .map(|pair| pair.sourcemap.path.clone()) + .collect::>(); let uploads = collect_uploads(pairs).context("While preparing files for upload")?; info!("Found {} chunks to upload", uploads.len()); @@ -48,6 +53,10 @@ pub fn upload( upload_chunks(&url, &token.token, uploads, release.as_ref())?; + if delete_after { + delete_files(sourcemap_paths).context("While deleting sourcemaps")?; + } + let _ = capture_handle.join(); Ok(()) @@ -105,3 +114,16 @@ fn content_hash(uploads: &[ChunkUpload]) -> String { } format!("{:x}", hasher.finalize()) } + +fn delete_files(paths: Vec) -> Result<()> { + // Delete local sourcemaps files from the sourcepair + for path in paths { + if path.exists() { + std::fs::remove_file(&path).context(format!( + "Failed to delete sourcemaps file: {}", + path.display() + ))?; + } + } + Ok(()) +}