resq_cli/commands/tree_shake.rs
1/*
2 * Copyright 2026 ResQ
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17//! Tree-shaking command for removing unused code.
18//!
19//! Analyzes JavaScript/TypeScript bundles to identify and report
20//! unused code that can be removed to reduce bundle size.
21
22use anyhow::{Context, Result};
23use std::process::Command;
24
25/// CLI arguments for the tree-shake command.
26#[derive(clap::Args, Debug)]
27pub struct TreeShakeArgs {}
28
29/// Run the tree-shake command.
30pub async fn run(_args: TreeShakeArgs) -> Result<()> {
31 println!("Running tree-shake (tsr)...");
32
33 let root = crate::utils::find_project_root();
34
35 let status = Command::new("bunx")
36 .args([
37 "tsr",
38 "--write",
39 "--recursive",
40 r"^src/(main|index)\.ts$",
41 r"^src/app/.*\.(ts|tsx)$",
42 ])
43 .current_dir(&root)
44 .status()
45 .context("Failed to execute tsr")?;
46
47 if !status.success() {
48 anyhow::bail!("Tree-shake failed");
49 }
50
51 println!("Tree-shake completed successfully.");
52 Ok(())
53}