projm 0.6.1

Project organizer and fuzzy navigator for developers — classify, group, and jump to projects from the terminal
Documentation
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
use colored::Colorize;
use serde::{Deserialize, Serialize};
use std::path::Path;

// ── Category ─────────────────────────────────────────────────────────────────

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub enum Category {
    Apps,
    Services,
    Ui,
    Embedded,
    Ml,
    Tools,
    Labs,
    Content,
}

impl Category {
    pub fn dir_name(&self) -> &'static str {
        match self {
            Self::Apps     => "apps",
            Self::Services => "services",
            Self::Ui       => "ui",
            Self::Embedded => "embedded",
            Self::Ml       => "ml",
            Self::Tools    => "tools",
            Self::Labs     => "labs",
            Self::Content  => "content",
        }
    }

    /// Fixed-width colored label for aligned display
    pub fn label(&self) -> String {
        let s = format!("{:<8}", self.dir_name());
        match self {
            Self::Apps     => s.blue().bold().to_string(),
            Self::Services => s.cyan().bold().to_string(),
            Self::Ui       => s.magenta().bold().to_string(),
            Self::Embedded => s.yellow().bold().to_string(),
            Self::Ml       => s.green().bold().to_string(),
            Self::Tools    => s.white().bold().to_string(),
            Self::Labs     => s.truecolor(255, 100, 50).bold().to_string(),
            Self::Content  => s.truecolor(255, 105, 180).bold().to_string(),
        }
    }

    pub fn all() -> &'static [Self] {
        &[
            Self::Apps,
            Self::Services,
            Self::Ui,
            Self::Embedded,
            Self::Ml,
            Self::Tools,
            Self::Labs,
            Self::Content,
        ]
    }
}

// ── Known project suffixes ────────────────────────────────────────────────────
// Recognised separators: `-` or `_`

pub const KNOWN_SUFFIXES: &[&str] = &[
    "api", "web", "mob", "mobile", "desk", "desktop",
    "mono", "cli", "fw", "lib", "core", "ui", "website",
    // common real-world names people actually use
    "backend", "frontend", "server", "client",
    "app", "apps", "bot", "worker", "jobs",
    "admin", "dashboard", "landing", "docs",
];

/// Split `DriveTrack-Backend` -> `Some(("DriveTrack", "backend"))`.
/// Matching is case-insensitive on the suffix; the returned prefix slice
/// preserves the original casing of `name`.
/// Returns `None` if the name has no recognised suffix.
pub fn split_suffix(name: &str) -> Option<(&str, &str)> {
    for sep in ['-', '_'] {
        if let Some(pos) = name.rfind(sep) {
            let suffix_raw = &name[pos + 1..];
            let suffix_low = suffix_raw.to_lowercase();
            if KNOWN_SUFFIXES.contains(&suffix_low.as_str()) {
                return Some((&name[..pos], suffix_raw));
            }
        }
    }
    None
}

/// Normalised group key: both `DriveTrack-Api` and `drivetrack-web`
/// map to `"drivetrack"`.
#[allow(dead_code)]
pub fn prefix_key(name: &str) -> Option<String> {
    split_suffix(name).map(|(prefix, _)| prefix.to_lowercase())
}

// ── Classification logic ──────────────────────────────────────────────────────

pub fn classify(path: &Path, rules: &[crate::rules::ValidatedRule]) -> Category {
    // --- 1. Custom rules.toml (First match wins) ---
    for rule in rules {
        if rule.matches(path) {
            return rule.category.clone();
        }
    }

    let has = |f: &str| path.join(f).exists();

    // ── doc-lab.md is the explicit labs marker — highest priority ──────────
    if has("doc-lab.md") {
        return Category::Labs;
    }

    // ── Monorepos (Turborepo, pnpm workspaces, etc.) ──────────────────────────
    if is_monorepo(path) {
        return Category::Apps;
    }

    let has_cargo   = has("Cargo.toml");
    let has_pkg     = has("package.json");
    let has_tauri   = has("src-tauri");
    // uv: uv.lock or .python-version are definitive uv markers
    let has_uv      = has("uv.lock") || has(".python-version");
    let has_py      = has_uv || has("requirements.txt") || has("pyproject.toml") || has("setup.py");
    let has_nb      = has_ext(path, "ipynb");
    let has_mem_x   = has("memory.x");           // embedded linker script
    let has_openocd = has("openocd.cfg") || has(".probe-rs");

    // ── Suffix gives a strong hint for mixed stacks ──────────────────────────
    // Bind to a variable so the temporary String lives long enough for split_suffix
    let name_lower = path
        .file_name()
        .map(|n| n.to_string_lossy().to_lowercase());
    if let Some((_prefix, suffix)) = name_lower.as_deref().and_then(split_suffix) {
        // suffix is already lowercase because name_lower is lowercased
        match suffix {
            "fw"                        => return Category::Embedded,
            "mob" | "mobile"            => return Category::Ui,
            "web" | "ui"                => return Category::Ui,
            "api" | "core" | "backend"
            | "server"                  => {
                if !has_tauri && !(has_cargo && has_pkg) {
                    return Category::Services;
                }
            }
            "frontend" | "client"
            | "landing" | "dashboard"   => return Category::Ui,
            "mono" | "desktop" | "desk" => return Category::Apps,
            _ => {}
        }
    }

    // ── Embedded: memory.x, openocd, Cargo cross-compile target, or C/C++ embedded ──────────
    if has_mem_x || has_openocd || is_embedded_cargo(path) || is_c_cpp_embedded(path) {
        return Category::Embedded;
    }

    // ── Full-stack / Tauri desktop app ───────────────────────────────────────
    if has_tauri || (has_cargo && has_pkg) {
        return Category::Apps;
    }

    // ── Flutter / Dart ───────────────────────────────────────────────────────
    if has("pubspec.yaml") {
        if path.join("android").exists() || path.join("ios").exists() {
            return Category::Apps;
        }
        return Category::Ui;
    }

    // ── Kotlin / Android & Spring Boot ───────────────────────────────────────
    if has("build.gradle") || has("build.gradle.kts") {
        if has_android_manifest(path) {
            return Category::Apps;
        }
        return Category::Services;
    }

    // ── Java / Maven ─────────────────────────────────────────────────────────
    if has("pom.xml") {
        return Category::Services;
    }

    // ── Swift / iOS / macOS ──────────────────────────────────────────────────
    if has_ext(path, "xcodeproj") || has("Package.swift") {
        return Category::Apps;
    }

    // ── Go ───────────────────────────────────────────────────────────────────
    if has("go.mod") {
        let name = path
            .file_name()
            .unwrap_or_default()
            .to_string_lossy()
            .to_lowercase();
        if name.contains("cli") || name.contains("tool") || name.contains("util") {
            return Category::Tools;
        }
        return Category::Services;
    }

    // ── Ruby on Rails ────────────────────────────────────────────────────────
    if has("Gemfile") && path.join("config/routes.rb").exists() {
        return Category::Services;
    }

    // ── Laravel / PHP ────────────────────────────────────────────────────────
    if has("composer.json") && has("artisan") {
        return Category::Services;
    }

    // ── Elixir / Phoenix ─────────────────────────────────────────────────────
    if has("mix.exs") {
        return Category::Services;
    }

    // ── C# / .NET ────────────────────────────────────────────────────────────
    if has_ext(path, "csproj") || has_ext(path, "sln") {
        let name = path
            .file_name()
            .unwrap_or_default()
            .to_string_lossy()
            .to_lowercase();
        if name.contains("app") || name.contains("ui") || name.contains("desktop") || name.contains("mobile") {
            return Category::Apps;
        }
        return Category::Services;
    }

    // ── C / C++ Native (Non-embedded) ────────────────────────────────────────
    if has("CMakeLists.txt") {
        return Category::Tools;
    }

    // ── Python: ML pipeline or tool ─────────────────────────────────────────
    if has_py || has_nb {
        let ml_markers = [
            "train.py", "model.py", "dataset.py",
            "notebooks", "data", "models", "checkpoints",
        ];
        if ml_markers.iter().any(|m| path.join(m).exists()) {
            return Category::Ml;
        }
        return Category::Tools;
    }


    // ── Pure JS/TS: read package.json to know the truth ─────────────────────
    if has_pkg && !has_cargo {
        match read_pkg_kind(path) {
            PkgKind::Frontend  => return Category::Ui,
            PkgKind::Backend   => return Category::Services,
            PkgKind::Fullstack => return Category::Apps,
            PkgKind::Unknown   => {
                // fall back to dir-name heuristic
                if has("server") || has("backend") || has("api") {
                    return Category::Apps;
                }
                return Category::Ui;
            }
        }
    }

    // ── Pure Rust ────────────────────────────────────────────────────────────
    if has_cargo && !has_pkg {
        let name = path
            .file_name()
            .unwrap_or_default()
            .to_string_lossy()
            .to_lowercase();
        if name.contains("cli") || name.contains("tool") || name.contains("util") {
            return Category::Tools;
        }
        return Category::Services;
    }

    Category::Labs
}

// ── package.json classifier ──────────────────────────────────────────────────

#[derive(Debug)]
enum PkgKind { Frontend, Backend, Fullstack, Unknown }

/// Known frontend deps/devDeps (frameworks, bundlers, ui libs)
const FRONTEND_DEPS: &[&str] = &[
    "react", "react-dom", "vue", "svelte", "solid-js",
    "next", "nuxt", "sveltekit", "@sveltejs/kit",
    "vite", "webpack", "parcel", "rollup", "esbuild",
    "astro", "remix", "@remix-run/react",
    "gatsby", "angular", "@angular/core",
    "tailwindcss", "@shadcn/ui", "radix-ui",
    "react-router", "react-router-dom", "wouter",
];

/// Known backend deps
const BACKEND_DEPS: &[&str] = &[
    "hono", "express", "fastify", "koa", "restify",
    "nestjs", "@nestjs/core", "@nestjs/common",
    "elysia", "h3", "nitro",
    "better-sqlite3", "pg", "mysql2", "mongoose", "prisma",
    "@prisma/client", "drizzle-orm",
    "jsonwebtoken", "passport", "bcrypt", "bcryptjs",
    "ws", "socket.io",
];

fn read_pkg_kind(dir: &Path) -> PkgKind {
    let raw = match std::fs::read_to_string(dir.join("package.json")) {
        Ok(s)  => s,
        Err(_) => return PkgKind::Unknown,
    };

    // Collect all dependency keys (deps + devDeps) without a full JSON parser
    let all_deps = extract_dep_keys(&raw);

    let has_fe = all_deps.iter().any(|d| FRONTEND_DEPS.contains(&d.as_str()));
    let has_be = all_deps.iter().any(|d| BACKEND_DEPS.contains(&d.as_str()));

    match (has_fe, has_be) {
        (true,  true)  => PkgKind::Fullstack,
        (true,  false) => PkgKind::Frontend,
        (false, true)  => PkgKind::Backend,
        (false, false) => PkgKind::Unknown,
    }
}

/// Extract the string keys from `"dependencies"` and `"devDependencies"`
/// blocks using a simple scan — no serde/json crate needed.
fn extract_dep_keys(json: &str) -> Vec<String> {
    let mut keys = Vec::new();
    for section in ["dependencies", "devDependencies"] {
        let start = match json.find(section) {
            Some(i) => i,
            None    => continue,
        };
        // Find the opening `{` of the section
        let brace = match json[start..].find('{') {
            Some(i) => start + i + 1,
            None    => continue,
        };
        // Walk until the matching closing `}`
        let mut depth = 1usize;
        let mut pos   = brace;
        let bytes = json.as_bytes();
        while pos < bytes.len() && depth > 0 {
            match bytes[pos] {
                b'{' => depth += 1,
                b'}' => depth -= 1,
                _ => {}
            }
            pos += 1;
        }
        let block = &json[brace..pos.saturating_sub(1)];
        // Each key is a quoted string before a `:`
        let mut remaining = block;
        while let Some(q1) = remaining.find('"') {
            remaining = &remaining[q1 + 1..];
            let q2 = match remaining.find('"') {
                Some(i) => i,
                None    => break,
            };
            let key = &remaining[..q2];
            remaining = &remaining[q2 + 1..];
            // Only keep it if it's followed by `:` (i.e. it's a key not a value)
            let after = remaining.trim_start();
            if after.starts_with(':') {
                keys.push(key.to_string());
            }
        }
    }
    keys
}

// ── Helpers ───────────────────────────────────────────────────────────────────

fn is_monorepo(path: &Path) -> bool {
    if path.join("turbo.json").exists()
        || path.join("pnpm-workspace.yaml").exists()
        || path.join("lerna.json").exists()
        || path.join("nx.json").exists()
    {
        return true;
    }

    let pkg_json_path = path.join("package.json");
    if pkg_json_path.exists() {
        if let Ok(content) = std::fs::read_to_string(pkg_json_path) {
            if let Ok(val) = serde_json::from_str::<serde_json::Value>(&content) {
                if val.get("workspaces").is_some() {
                    return true;
                }
            }
        }
    }

    false
}

/// Returns true if Cargo.toml is present AND .cargo/config.toml names an
/// embedded target (thumbv*, riscv*, xtensa*)
fn is_embedded_cargo(path: &Path) -> bool {
    if !path.join("Cargo.toml").exists() {
        return false;
    }
    let cfg = path.join(".cargo/config.toml");
    if !cfg.exists() {
        return false;
    }
    std::fs::read_to_string(cfg)
        .map(|s| s.contains("thumbv") || s.contains("riscv") || s.contains("xtensa"))
        .unwrap_or(false)
}

/// Check whether any file directly inside `dir` has the given extension
fn has_ext(dir: &Path, ext: &str) -> bool {
    std::fs::read_dir(dir)
        .map(|rd| {
            rd.filter_map(|e| e.ok())
                .any(|e| e.path().extension().map_or(false, |x| x == ext))
        })
        .unwrap_or(false)
}

pub fn extract_dep_keys_helper(path: &Path) -> Vec<String> {
    if let Ok(raw) = std::fs::read_to_string(path.join("package.json")) {
        extract_dep_keys(&raw)
    } else {
        Vec::new()
    }
}

fn is_c_cpp_embedded(path: &Path) -> bool {
    if !path.join("CMakeLists.txt").exists() {
        return false;
    }
    has_ext(path, "ld")
        || path.join("openocd.cfg").exists()
        || path.join("openocd").exists()
}

fn has_android_manifest(path: &Path) -> bool {
    if path.join("src/main/AndroidManifest.xml").exists()
        || path.join("app/src/main/AndroidManifest.xml").exists()
        || path.join("AndroidManifest.xml").exists()
    {
        return true;
    }
    
    // Depth-limited search (depth <= 4)
    fn find_manifest(dir: &Path, depth: usize) -> bool {
        if depth > 4 {
            return false;
        }
        if let Ok(entries) = std::fs::read_dir(dir) {
            for entry in entries.flatten() {
                let p = entry.path();
                if p.is_dir() {
                    if let Some(name) = p.file_name().and_then(|n| n.to_str()) {
                        if name == "target" || name == "node_modules" || name == ".git" || name == "build" {
                            continue;
                        }
                    }
                    if find_manifest(&p, depth + 1) {
                        return true;
                    }
                } else if p.file_name().map_or(false, |n| n == "AndroidManifest.xml") {
                    return true;
                }
            }
        }
        false
    }
    find_manifest(path, 1)
}