tidyup 0.1.0

Free up ports and disk space from your dev projects: kill port hogs, delete node_modules/target folders, start your dev stack
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
//! Finding folders inside projects that can be regenerated: dependencies, build output, caches.
//!
//! A folder only counts when the project file that creates it sits next to it, so a
//! `target/` or `build/` folder you made yourself is never touched.

use std::{
    collections::HashMap,
    env, fs, io,
    io::IsTerminal,
    os::unix::fs::MetadataExt,
    path::{Path, PathBuf},
    sync::{
        Arc, Mutex,
        atomic::{AtomicU64, AtomicUsize, Ordering},
    },
    time::{Duration, SystemTime},
};

use anyhow::{Result, bail};
use dialoguer::{Confirm, MultiSelect, theme::ColorfulTheme};
use rayon::prelude::*;
use walkdir::WalkDir;

use crate::{
    demo,
    fmt::{self, Paint},
    progress::{self, Progress},
};

/// A deletable folder `dir`, recognised by one of `markers` in the same parent folder.
pub struct Rule {
    pub dir: &'static str,
    pub markers: &'static [&'static str],
    pub kind: &'static str,
}

const NODE: &[&str] = &["package.json"];
const GRADLE: &[&str] = &[
    "build.gradle",
    "build.gradle.kts",
    "settings.gradle",
    "settings.gradle.kts",
];
const PYTHON: &[&str] = &["pyproject.toml", "requirements.txt", "setup.py", "Pipfile"];

pub const RULES: &[Rule] = &[
    Rule {
        dir: "node_modules",
        markers: NODE,
        kind: "Node",
    },
    Rule {
        dir: ".next",
        markers: NODE,
        kind: "Next.js",
    },
    Rule {
        dir: ".nuxt",
        markers: NODE,
        kind: "Nuxt",
    },
    Rule {
        dir: ".svelte-kit",
        markers: NODE,
        kind: "SvelteKit",
    },
    Rule {
        dir: ".turbo",
        markers: NODE,
        kind: "Turborepo",
    },
    Rule {
        dir: ".parcel-cache",
        markers: NODE,
        kind: "Parcel",
    },
    Rule {
        dir: ".angular",
        markers: NODE,
        kind: "Angular",
    },
    Rule {
        dir: "target",
        markers: &["Cargo.toml"],
        kind: "Rust",
    },
    Rule {
        dir: "target",
        markers: &["pom.xml"],
        kind: "Maven",
    },
    Rule {
        dir: "build",
        markers: GRADLE,
        kind: "Gradle",
    },
    Rule {
        dir: ".gradle",
        markers: GRADLE,
        kind: "Gradle",
    },
    Rule {
        dir: ".venv",
        markers: PYTHON,
        kind: "Python",
    },
    Rule {
        dir: "venv",
        markers: PYTHON,
        kind: "Python",
    },
    Rule {
        dir: "Pods",
        markers: &["Podfile"],
        kind: "CocoaPods",
    },
    Rule {
        dir: ".dart_tool",
        markers: &["pubspec.yaml"],
        kind: "Dart",
    },
    Rule {
        dir: "build",
        markers: &["pubspec.yaml"],
        kind: "Flutter",
    },
    Rule {
        dir: "vendor",
        markers: &["composer.json"],
        kind: "Composer",
    },
    Rule {
        dir: "_build",
        markers: &["mix.exs"],
        kind: "Elixir",
    },
    Rule {
        dir: "deps",
        markers: &["mix.exs"],
        kind: "Elixir",
    },
    Rule {
        dir: ".zig-cache",
        markers: &["build.zig"],
        kind: "Zig",
    },
    Rule {
        dir: "zig-out",
        markers: &["build.zig"],
        kind: "Zig",
    },
];

/// Big folders in the home directory that never contain projects worth scanning.
const HOME_SKIP: &[&str] = &["Library", "Applications", "Movies", "Music", "Pictures"];

/// A deletable folder that was found.
#[derive(Debug, Clone)]
pub struct Junk {
    pub path: PathBuf,
    pub kind: &'static str,
    /// Bytes freed by deleting it.
    pub size: u64,
    /// Last change to the project the folder belongs to.
    pub last_used: SystemTime,
}

fn rule_for(dir: &Path) -> Option<&'static Rule> {
    let name = dir.file_name()?.to_str()?;
    let parent = dir.parent()?;
    RULES
        .iter()
        .find(|rule| rule.dir == name && rule.markers.iter().any(|m| parent.join(m).is_file()))
}

/// Hidden folders (`.git`, `.vscode`, `.cargo`, …), app bundles and big media folders.
/// Skipping hidden folders also keeps editor extensions' `node_modules` safe.
fn should_skip(dir: &Path, home: Option<&Path>) -> bool {
    let Some(name) = dir.file_name().and_then(|n| n.to_str()) else {
        return true;
    };
    name.starts_with('.')
        || name.ends_with(".app")
        || (dir.parent() == home && HOME_SKIP.contains(&name))
}

/// Walks `root` and returns every deletable folder, without measuring them.
fn find(root: &Path) -> Vec<(PathBuf, &'static Rule)> {
    let home = env::var_os("HOME").map(PathBuf::from);
    let mut found = Vec::new();
    let mut walk = WalkDir::new(root).into_iter();

    while let Some(entry) = walk.next() {
        let Ok(entry) = entry else { continue };
        if entry.depth() == 0 || !entry.file_type().is_dir() {
            continue;
        }
        if let Some(rule) = rule_for(entry.path()) {
            found.push((entry.into_path(), rule));
            walk.skip_current_dir();
        } else if should_skip(entry.path(), home.as_deref()) {
            walk.skip_current_dir();
        }
    }
    found
}

fn measure(path: PathBuf, rule: &'static Rule) -> Junk {
    Junk {
        size: disk_usage(&path),
        last_used: last_used(&path),
        kind: rule.kind,
        path,
    }
}

/// Space on disk that deleting `path` gives back. A hard-linked file counts only when all
/// of its links are inside `path` (Cargo links outputs within `target/`); one linked from
/// elsewhere, like pnpm's store, stays on disk after the delete.
fn disk_usage(path: &Path) -> u64 {
    let mut total = 0;
    // (device, inode) → (links seen inside `path`, size)
    let mut linked: HashMap<(u64, u64), (u64, u64)> = HashMap::new();

    for meta in WalkDir::new(path)
        .into_iter()
        .filter_map(Result::ok)
        .filter_map(|entry| entry.metadata().ok())
    {
        let size = meta.blocks() * 512;
        if meta.is_dir() || meta.nlink() == 1 {
            total += size;
            continue;
        }
        let (seen, _) = linked.entry((meta.dev(), meta.ino())).or_insert((0, size));
        *seen += 1;
        if *seen == meta.nlink() {
            total += size;
        }
    }
    total
}

/// Newest modification time among the project's other top-level entries.
fn last_used(junk: &Path) -> SystemTime {
    let project = junk.parent().unwrap_or(junk);
    fs::read_dir(project)
        .into_iter()
        .flatten()
        .flatten()
        .filter(|entry| entry.path() != junk && entry.file_name() != ".DS_Store")
        .filter_map(|entry| entry.metadata().ok()?.modified().ok())
        .max()
        .unwrap_or(SystemTime::UNIX_EPOCH)
}

/// Scans `root`, calling `on_found` from worker threads as each folder is measured.
pub fn scan(root: &Path, on_found: impl Fn(Junk) + Sync) {
    if demo::enabled() {
        return demo::scan(root, on_found);
    }
    find(root)
        .into_par_iter()
        .for_each(|(path, rule)| on_found(measure(path, rule)));
}

pub fn delete(path: &Path) -> io::Result<()> {
    delete_counting(path, &AtomicU64::new(0))
}

/// Deletes `path`, adding each removed entry's size to `freed` as it goes. A hard-linked
/// file counts once its last link inside `path` is removed, matching `disk_usage`.
pub fn delete_counting(path: &Path, freed: &AtomicU64) -> io::Result<()> {
    if demo::enabled() {
        demo::delete(path, freed);
        return Ok(());
    }
    remove_tree(path, freed)
}

/// Deletes a folder tree with its subfolders handled in parallel. Unlinking is limited by
/// filesystem latency rather than CPU, so many deletes in flight beat one at a time.
fn remove_tree(dir: &Path, freed: &AtomicU64) -> io::Result<()> {
    let entries: Vec<fs::DirEntry> = fs::read_dir(dir)?.collect::<io::Result<_>>()?;
    entries.into_par_iter().try_for_each(|entry| {
        let meta = entry.metadata()?;
        if meta.is_dir() {
            return remove_tree(&entry.path(), freed);
        }
        fs::remove_file(entry.path())?;
        if meta.nlink() == 1 {
            freed.fetch_add(meta.blocks() * 512, Ordering::Relaxed);
        }
        Ok(())
    })?;
    let size = fs::symlink_metadata(dir).map_or(0, |meta| meta.blocks() * 512);
    fs::remove_dir(dir)?;
    freed.fetch_add(size, Ordering::Relaxed);
    Ok(())
}

/// Running totals shared with a progress line.
#[derive(Default)]
struct Tally {
    count: AtomicUsize,
    bytes: AtomicU64,
}

/// Displays `path` relative to `root` when it's inside it.
pub fn display(path: &Path, root: &Path) -> String {
    match path.strip_prefix(root) {
        Ok(relative) => relative.display().to_string(),
        Err(_) => fmt::path(path),
    }
}

pub struct Options {
    pub root: PathBuf,
    pub older_than: Option<u64>,
    pub dry_run: bool,
    pub yes: bool,
}

/// `tidyup clean`
pub fn run(options: Options) -> Result<()> {
    let Options {
        root,
        older_than,
        dry_run,
        yes,
    } = options;
    if !dry_run && !yes && !std::io::stdin().is_terminal() {
        bail!("not running in a terminal: pass --dry-run to list or --yes to delete everything");
    }

    let scanned = Arc::new(Tally::default());
    let progress = Progress::start({
        let (scanned, shown) = (scanned.clone(), fmt::path(&root));
        move |spinner| {
            format!(
                "{spinner} Scanning {shown}… {} found, {}",
                scanned.count.load(Ordering::Relaxed),
                fmt::size(scanned.bytes.load(Ordering::Relaxed))
            )
        }
    });
    let found = Mutex::new(Vec::new());
    scan(&root, |junk| {
        scanned.count.fetch_add(1, Ordering::Relaxed);
        scanned.bytes.fetch_add(junk.size, Ordering::Relaxed);
        found.lock().unwrap().push(junk);
    });
    drop(progress);
    let mut junk = found.into_inner().unwrap();

    if let Some(days) = older_than {
        let cutoff = SystemTime::now() - Duration::from_secs(days * 86_400);
        junk.retain(|j| j.last_used < cutoff);
    }
    junk.retain(|j| j.size > 0);
    junk.sort_by_key(|j| std::cmp::Reverse(j.size));

    if junk.is_empty() {
        println!("Nothing to clean.");
        return Ok(());
    }

    let total: u64 = junk.iter().map(|j| j.size).sum();
    let lines: Vec<String> = junk
        .iter()
        .map(|j| {
            format!(
                "{:>8}  {:>5}  {:<10} {}",
                fmt::size(j.size),
                fmt::age(j.last_used),
                j.kind,
                display(&j.path, &root)
            )
        })
        .collect();
    let summary = format!(
        "{} folder{}, {}",
        junk.len(),
        plural(junk.len()),
        fmt::size(total)
    );

    let chosen: Vec<&Junk> = if dry_run || yes {
        println!(
            "{}",
            format!("{:>8}  {:>5}  {:<10} {}", "SIZE", "AGE", "KIND", "PATH").dim()
        );
        for line in &lines {
            println!("{line}");
        }
        println!("{}", summary.as_str().bold());
        if dry_run {
            return Ok(());
        }
        junk.iter().collect()
    } else {
        let picked = MultiSelect::with_theme(&ColorfulTheme::default())
            .with_prompt(format!(
                "Found {summary}. Space to pick, a for all, enter to continue"
            ))
            .items(&lines)
            .max_length(20)
            .report(false)
            .interact_opt()?;
        let Some(picked) = picked else { return Ok(()) };
        picked.into_iter().map(|i| &junk[i]).collect()
    };

    if chosen.is_empty() {
        println!("Nothing picked.");
        return Ok(());
    }

    let chosen_size: u64 = chosen.iter().map(|j| j.size).sum();
    if !yes
        && !Confirm::with_theme(&ColorfulTheme::default())
            .with_prompt(format!(
                "Delete {} folder{} ({})?",
                chosen.len(),
                plural(chosen.len()),
                fmt::size(chosen_size)
            ))
            .default(false)
            .interact()?
    {
        return Ok(());
    }

    let deleted = Arc::new(Tally::default());
    let progress = Progress::start({
        let (deleted, folders) = (deleted.clone(), chosen.len());
        move |spinner| {
            let bytes = deleted.bytes.load(Ordering::Relaxed);
            format!(
                "{spinner} Deleting {} {} of {} · {}/{folders} folders",
                progress::bar(bytes as f64 / chosen_size.max(1) as f64, 24),
                fmt::size(bytes),
                fmt::size(chosen_size),
                deleted.count.load(Ordering::Relaxed),
            )
        }
    });
    let results: Vec<(&Junk, io::Result<()>)> = chosen
        .into_par_iter()
        .map(|j| {
            let result = delete_counting(&j.path, &deleted.bytes);
            deleted.count.fetch_add(1, Ordering::Relaxed);
            (j, result)
        })
        .collect();
    drop(progress);
    let mut freed = 0;
    for (junk, result) in results {
        match result {
            Ok(()) => freed += junk.size,
            Err(err) => eprintln!("{} {}: {err}", "✘".red(), fmt::path(&junk.path)),
        }
    }
    println!("{} Freed {}", "✔".green(), fmt::size(freed).bold());
    Ok(())
}

fn plural(count: usize) -> &'static str {
    if count == 1 { "" } else { "s" }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn temp_dir(name: &str) -> PathBuf {
        let dir = env::temp_dir().join(format!("tidyup-{}-{name}", std::process::id()));
        let _ = fs::remove_dir_all(&dir);
        fs::create_dir_all(&dir).unwrap();
        dir
    }

    fn touch(path: &Path) {
        fs::create_dir_all(path.parent().unwrap()).unwrap();
        fs::write(path, "x").unwrap();
    }

    #[test]
    fn finds_only_folders_next_to_their_project_file() {
        let root = temp_dir("find");
        touch(&root.join("web/package.json"));
        touch(&root.join("web/node_modules/react/index.js"));
        touch(&root.join("web/node_modules/nested/node_modules/x.js"));
        touch(&root.join("cli/Cargo.toml"));
        touch(&root.join("cli/target/debug/cli"));
        touch(&root.join("notes/target/keep-me.txt"));
        touch(&root.join(".hidden/package.json"));
        touch(&root.join(".hidden/node_modules/x.js"));

        let mut found: Vec<_> = find(&root)
            .into_iter()
            .map(|(path, rule)| (display(&path, &root), rule.kind))
            .collect();
        found.sort();
        assert_eq!(
            found,
            [
                ("cli/target".to_string(), "Rust"),
                ("web/node_modules".to_string(), "Node")
            ]
        );

        fs::remove_dir_all(root).unwrap();
    }

    #[test]
    fn measures_and_deletes() {
        let root = temp_dir("measure");
        touch(&root.join("package.json"));
        fs::create_dir_all(root.join("node_modules")).unwrap();
        fs::write(root.join("node_modules/big.bin"), vec![0u8; 100_000]).unwrap();

        let junk = std::sync::Mutex::new(Vec::new());
        scan(&root, |j| junk.lock().unwrap().push(j));
        let junk = junk.into_inner().unwrap();
        assert_eq!(junk.len(), 1);
        assert!(junk[0].size >= 100_000);

        delete(&junk[0].path).unwrap();
        assert!(!root.join("node_modules").exists());
        assert!(root.join("package.json").exists());

        fs::remove_dir_all(root).unwrap();
    }

    #[test]
    fn counts_hard_links_only_when_all_are_inside() {
        let root = temp_dir("links");
        let inside = root.join("target");
        fs::create_dir_all(&inside).unwrap();
        fs::write(inside.join("a"), vec![1u8; 200_000]).unwrap();
        fs::hard_link(inside.join("a"), inside.join("a-link")).unwrap();
        fs::write(inside.join("b"), vec![2u8; 300_000]).unwrap();
        fs::hard_link(inside.join("b"), root.join("b-outside")).unwrap();

        let size = disk_usage(&inside);
        assert!((200_000..300_000).contains(&size), "got {size}");

        fs::remove_dir_all(root).unwrap();
    }
}