posthog_cli/sourcemaps/hermes/
mod.rs

1use std::path::PathBuf;
2
3use anyhow::{anyhow, Context, Result};
4use clap::Subcommand;
5use tracing::info;
6
7use crate::sourcemaps::{content::SourceMapFile, inject::InjectArgs, source_pairs::SourcePair};
8
9pub mod clone;
10pub mod inject;
11pub mod upload;
12
13#[derive(Subcommand)]
14pub enum HermesSubcommand {
15    /// Inject your bundled chunk with a posthog chunk ID
16    Inject(InjectArgs),
17    /// Upload the bundled chunk to PostHog
18    Upload(upload::Args),
19    /// Clone chunk_id and release_id metadata from bundle maps to composed maps
20    Clone(clone::CloneArgs),
21}
22
23pub fn get_composed_map(pair: &SourcePair) -> Result<Option<SourceMapFile>> {
24    let sourcemap_path = &pair.sourcemap.inner.path;
25
26    // Look for composed map: change .bundle.map to .bundle.hbc.composed.map
27    let composed_path = sourcemap_path
28        .to_str()
29        .and_then(|s| {
30            if s.ends_with(".bundle.map") {
31                Some(PathBuf::from(
32                    s.replace(".bundle.map", ".bundle.hbc.composed.map"),
33                ))
34            } else if s.ends_with(".jsbundle.map") {
35                Some(PathBuf::from(
36                    s.replace(".jsbundle.map", ".jsbundle.hbc.composed.map"),
37                ))
38            } else {
39                None
40            }
41        })
42        .ok_or_else(|| anyhow!("Could not determine composed map path for {sourcemap_path:?}"))?;
43
44    if !composed_path.exists() {
45        info!(
46            "Skipping {} - no composed map found at {}",
47            sourcemap_path.display(),
48            composed_path.display()
49        );
50        return Ok(None);
51    }
52
53    Ok(Some(SourceMapFile::load(&composed_path).context(
54        format!("reading composed map at {composed_path:?}"),
55    )?))
56}