posthog_cli/sourcemaps/plain/
upload.rs

1use std::path::PathBuf;
2
3use anyhow::{Context, Result};
4use tracing::{info, warn};
5
6use crate::{
7    api::symbol_sets::{self, SymbolSetUpload},
8    invocation_context::context,
9    sourcemaps::{plain::inject::is_javascript_file, source_pairs::read_pairs},
10    utils::files::delete_files,
11};
12
13#[derive(clap::Args, Clone)]
14pub struct Args {
15    /// The directory containing the bundled chunks
16    #[arg(short, long)]
17    pub directory: PathBuf,
18
19    /// If your bundler adds a public path prefix to sourcemap URLs,
20    /// we need to ignore it while searching for them
21    /// For use alongside e.g. esbuilds "publicPath" config setting.
22    #[arg(short, long)]
23    pub public_path_prefix: Option<String>,
24
25    /// One or more directory glob patterns to ignore
26    #[arg(short, long)]
27    pub ignore: Vec<String>,
28
29    /// Whether to delete the source map files after uploading them
30    #[arg(long, default_value = "false")]
31    pub delete_after: bool,
32
33    /// The maximum number of chunks to upload in a single batch
34    #[arg(long, default_value = "50")]
35    pub batch_size: usize,
36
37    /// DEPRECATED: Does nothing. Set project during `inject` instead
38    #[arg(long)]
39    pub project: Option<String>,
40
41    /// DEPRECATED: Does nothing. Set version during `inject` instead
42    #[arg(long)]
43    pub version: Option<String>,
44
45    /// DEPRECATED - use top-level `--skip-ssl-verification` instead
46    #[arg(long, default_value = "false")]
47    pub skip_ssl_verification: bool,
48}
49
50pub fn upload_cmd(args: &Args) -> Result<()> {
51    if args.project.is_some() || args.version.is_some() {
52        warn!("`--project` and `--version` are deprecated and do nothing. Set project and version during `inject` instead.");
53    }
54
55    context().capture_command_invoked("sourcemap_upload");
56    upload(args)
57}
58
59pub fn upload(args: &Args) -> Result<()> {
60    if args.project.is_some() || args.version.is_some() {
61        warn!("`--project` and `--version` are deprecated and do nothing. Set project and version during `inject` instead.");
62    }
63
64    let pairs = read_pairs(
65        &args.directory,
66        &args.ignore,
67        is_javascript_file,
68        &args.public_path_prefix,
69    )?;
70    let sourcemap_paths = pairs
71        .iter()
72        .map(|pair| pair.sourcemap.inner.path.clone())
73        .collect::<Vec<_>>();
74    info!("Found {} chunks to upload", pairs.len());
75
76    let uploads = pairs
77        .into_iter()
78        .map(TryInto::try_into)
79        .collect::<Result<Vec<SymbolSetUpload>>>()
80        .context("While preparing files for upload")?;
81
82    symbol_sets::upload(&uploads, args.batch_size)?;
83
84    if args.delete_after {
85        delete_files(sourcemap_paths).context("While deleting sourcemaps")?;
86    }
87
88    Ok(())
89}