siteone-crawler 2.5.1

Website crawler and QA toolkit in Rust for security, performance, SEO, and accessibility audits, offline cloning, markdown export, sitemap generation, cache warming, and CI/CD gating — one dependency-free binary for all major platforms, 10 tools in one.
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
// SiteOne Crawler - Interactive wizard for no-args invocation
// (c) Jan Reges <jan.reges@siteone.cz>

mod form;
mod presets;

use colored::Colorize;
use inquire::ui::{Color, RenderConfig, StyleSheet, Styled};
use inquire::validator::Validation;
use inquire::{Confirm, InquireError, Select, Text};
use std::io::IsTerminal;

use crate::version;
use presets::{PRESETS, WizardState};

// ── Public API ──────────────────────────────────────────────────────────────

/// Returns true when stdin AND stdout are interactive TTYs.
pub fn is_interactive_tty() -> bool {
    std::io::stdin().is_terminal() && std::io::stdout().is_terminal()
}

/// After an offline/markdown export crawl (wizard mode), offer to immediately
/// serve the exported content via HTTP.
/// Returns `Some((dir_path, "offline"|"markdown"))` if the user confirms, else `None`.
pub fn offer_serve_after_export(crawl_argv: &[String]) -> Option<(String, String)> {
    let (dir, kind) = if let Some(arg) = crawl_argv.iter().find(|a| a.starts_with("--offline-export-dir=")) {
        let raw = arg.trim_start_matches("--offline-export-dir=");
        (raw.trim_matches('\'').to_string(), "offline")
    } else if let Some(arg) = crawl_argv.iter().find(|a| a.starts_with("--markdown-export-dir=")) {
        let raw = arg.trim_start_matches("--markdown-export-dir=");
        (raw.trim_matches('\'').to_string(), "markdown")
    } else {
        return None;
    };

    println!();
    let confirmed = Confirm::new(&format!("Serve the {} export via HTTP?", kind))
        .with_default(true)
        .prompt()
        .unwrap_or(false);

    if confirmed { Some((dir, kind.to_string())) } else { None }
}

/// Block until the user presses Enter. Used after a wizard-launched crawl
/// so the terminal window stays open (especially on Windows double-click).
pub fn press_enter_to_exit() {
    println!();
    println!("{}", "Press Enter to exit...".dimmed());
    let mut buf = String::new();
    let _ = std::io::stdin().read_line(&mut buf);
}

/// Run the interactive wizard. Returns a synthetic argv `Vec<String>` ready
/// to be fed into `Initiator::new()` / `parse_argv()`.
pub fn run_wizard() -> Result<Vec<String>, WizardError> {
    // Set inquire theme: yellow accents instead of default cyan, gray help text
    let mut render_config = RenderConfig::default_colored();
    render_config.help_message = StyleSheet::new().with_fg(Color::DarkGrey);
    render_config.highlighted_option_prefix = Styled::new("❯").with_fg(Color::DarkYellow);
    render_config.answer = StyleSheet::new().with_fg(Color::DarkYellow);
    inquire::set_global_render_config(render_config);

    print_banner();

    // Step 1: Preset selection (+ dynamic serve items if exports exist)
    let choice = prompt_preset_or_serve()?;
    match choice {
        PresetChoice::Serve(argv) => Ok(argv),
        PresetChoice::Preset(preset_idx) => {
            let preset = &PRESETS[preset_idx];
            let mut state = WizardState::from_preset(preset);

            // Step 2: URL (required)
            state.url = prompt_url()?;

            // Resolve {domain} and {date} placeholders in export paths
            resolve_export_paths(&mut state);

            // Step 3: Interactive settings form (arrow-key navigation + value cycling)
            let mut settings = form::build_form_settings(&state);
            println!();

            // Show warning for Stress Test preset
            if preset.name == "Stress Test" {
                println!(
                    "  {} {}",
                    "WARNING:".yellow().bold(),
                    "Stress testing generates high-concurrency load with cache-busting".yellow()
                );
                println!(
                    "           {}",
                    "random query params. This can overload a server and cause downtime.".yellow()
                );
                println!(
                    "           {}",
                    "Only run this against your own websites or with explicit permission!"
                        .yellow()
                        .bold()
                );
                println!();
            }

            let confirmed = form::run_form(&mut settings, preset.name)?;
            if !confirmed {
                return Err(WizardError::Cancelled);
            }
            form::apply_form_to_state(&settings, &mut state);

            // Re-resolve export paths — apply_form_to_state may have reset them to templates
            resolve_export_paths(&mut state);

            // Step 4: Summary & confirm
            let argv = state.build_argv();
            print_summary(&state, &argv);

            let run = Confirm::new("Start the crawl?").with_default(true).prompt()?;

            if run {
                println!();
                Ok(argv)
            } else {
                Err(WizardError::Cancelled)
            }
        }
    }
}

// ── Preset or Serve choice ──────────────────────────────────────────────────

enum PresetChoice {
    Preset(usize),
    Serve(Vec<String>),
}

/// Separator label used in the menu to visually separate serve items.
const SERVE_SEPARATOR: &str = "──────────────────────────────────────";

fn prompt_preset_or_serve() -> Result<PresetChoice, WizardError> {
    let mut labels: Vec<String> = PRESETS.iter().map(|p| p.to_string()).collect();

    // Detect existing exports in ./tmp/
    let offline_dirs = find_export_dirs("offline");
    let markdown_dirs = find_export_dirs("markdown");

    let has_serve_items = !offline_dirs.is_empty() || !markdown_dirs.is_empty();
    let serve_offline_label = "Browse offline export     Serve a previously exported offline site via HTTP";
    let serve_markdown_label = "Browse markdown export    Serve a previously exported markdown site via HTTP";

    if has_serve_items {
        labels.push(SERVE_SEPARATOR.to_string());
        if !offline_dirs.is_empty() {
            labels.push(serve_offline_label.to_string());
        }
        if !markdown_dirs.is_empty() {
            labels.push(serve_markdown_label.to_string());
        }
    }

    let choice = Select::new("Choose a crawl mode:", labels.clone())
        .with_page_size(labels.len())
        .prompt()?;

    // Check if it's a serve option
    if choice == serve_offline_label {
        return prompt_serve_export(&offline_dirs, "offline");
    }
    if choice == serve_markdown_label {
        return prompt_serve_export(&markdown_dirs, "markdown");
    }
    if choice == SERVE_SEPARATOR {
        // User selected the separator — re-prompt
        return prompt_preset_or_serve();
    }

    // It's a preset
    let preset_idx = PRESETS.iter().position(|p| choice.starts_with(p.name)).unwrap_or(0);
    Ok(PresetChoice::Preset(preset_idx))
}

/// Prompt the user to select from available exports, then return serve argv.
fn prompt_serve_export(dirs: &[ExportDir], kind: &str) -> Result<PresetChoice, WizardError> {
    let labels: Vec<String> = dirs.iter().map(|d| format!("{:40} {}", d.name, d.date_label)).collect();

    let choice = Select::new(&format!("Select {} export to serve:", kind), labels).prompt()?;

    // Find matching dir
    let selected = dirs.iter().find(|d| choice.starts_with(&d.name)).unwrap();

    let serve_flag = match kind {
        "offline" => format!("--serve-offline={}", selected.path),
        _ => format!("--serve-markdown={}", selected.path),
    };

    Ok(PresetChoice::Serve(vec!["siteone-crawler".to_string(), serve_flag]))
}

// ── Export directory detection ───────────────────────────────────────────────

struct ExportDir {
    name: String,
    path: String,
    date_label: String,
}

/// Find export directories matching `./tmp/{kind}-*/` pattern, sorted newest first.
fn find_export_dirs(kind: &str) -> Vec<ExportDir> {
    let tmp_path = std::path::Path::new("./tmp");
    if !tmp_path.is_dir() {
        return Vec::new();
    }

    let prefix = format!("{}-", kind);
    let mut dirs: Vec<ExportDir> = Vec::new();

    if let Ok(entries) = std::fs::read_dir(tmp_path) {
        for entry in entries.flatten() {
            let path = entry.path();
            if !path.is_dir() {
                continue;
            }
            let name = entry.file_name().to_string_lossy().to_string();
            if !name.starts_with(&prefix) {
                continue;
            }

            // Extract a human-readable date from metadata
            let date_label = std::fs::metadata(&path)
                .and_then(|m| m.modified())
                .ok()
                .map(|t| {
                    let dt: chrono::DateTime<chrono::Local> = t.into();
                    dt.format("%Y-%m-%d %H:%M").to_string()
                })
                .unwrap_or_default();

            dirs.push(ExportDir {
                name,
                path: path.to_string_lossy().to_string(),
                date_label,
            });
        }
    }

    // Sort newest first (by name descending — names contain date)
    dirs.sort_by(|a, b| b.name.cmp(&a.name));
    dirs
}

// ── Export path resolution ──────────────────────────────────────────────────

/// Replace `{domain}` and `{date}` placeholders in export dir paths after URL is known.
fn resolve_export_paths(state: &mut WizardState) {
    let url = &state.url;
    if let Some(ref dir) = state.offline_export_dir
        && (dir.contains("{domain}") || dir.contains("{date}"))
    {
        state.offline_export_dir = Some(presets::resolve_export_path(dir, url));
    }
    if let Some(ref dir) = state.markdown_export_dir
        && (dir.contains("{domain}") || dir.contains("{date}"))
    {
        state.markdown_export_dir = Some(presets::resolve_export_path(dir, url));
    }
}

// ── Error type ──────────────────────────────────────────────────────────────

#[derive(Debug)]
pub enum WizardError {
    Cancelled,
    IoError(String),
}

impl std::fmt::Display for WizardError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            WizardError::Cancelled => write!(f, "Wizard cancelled."),
            WizardError::IoError(msg) => write!(f, "Wizard error: {}", msg),
        }
    }
}

impl From<InquireError> for WizardError {
    fn from(err: InquireError) -> Self {
        match err {
            InquireError::OperationCanceled | InquireError::OperationInterrupted => WizardError::Cancelled,
            other => WizardError::IoError(other.to_string()),
        }
    }
}

// ── Banner ──────────────────────────────────────────────────────────────────

fn print_banner() {
    let separator = "=".repeat(60);
    println!();
    println!("{}", separator.dimmed());
    println!(
        "  {} {}",
        "SiteOne Crawler".bold(),
        format!("v{}", version::CODE).dimmed()
    );
    println!(
        "  {}",
        "Website QA toolkit: audit, clone, export, sitemap, CI/CD".dimmed()
    );
    println!("{}", separator.dimmed());
    println!();
}

// ── URL prompt ──────────────────────────────────────────────────────────────

fn prompt_url() -> Result<String, WizardError> {
    let url = Text::new("Enter the website URL to crawl:")
        .with_placeholder("https://example.com")
        .with_help_message("Enter a domain (e.g. example.com) or full URL (https://...)")
        .with_validator(|input: &str| {
            let trimmed = input.trim();
            if trimmed.is_empty() {
                return Ok(Validation::Invalid("URL is required.".into()));
            }
            let url_str = normalize_url_input(trimmed);
            match url::Url::parse(&url_str) {
                Ok(u) if (u.scheme() == "http" || u.scheme() == "https") && u.host().is_some() => Ok(Validation::Valid),
                _ => Ok(Validation::Invalid(
                    "Invalid URL. Enter a domain name or a valid http(s) address.".into(),
                )),
            }
        })
        .prompt()?;

    Ok(normalize_url_input(url.trim()))
}

fn normalize_url_input(input: &str) -> String {
    let trimmed = input.trim();
    if !trimmed.starts_with("http://") && !trimmed.starts_with("https://") {
        format!("https://{}", trimmed)
    } else {
        trimmed.to_string()
    }
}

// ── Summary ─────────────────────────────────────────────────────────────────

fn print_summary(state: &WizardState, argv: &[String]) {
    println!();
    let separator = "=".repeat(60);
    println!("{}", separator.dimmed());
    println!("  {}", "Configuration Summary".bold());
    println!("{}", separator.dimmed());
    println!();

    let label_width = 22;

    print_row("URL:", &state.url, label_width);
    print_row("Preset:", &state.preset_name, label_width);
    print_row("Workers:", &state.workers.to_string(), label_width);
    print_row("Timeout:", &format!("{}s", state.timeout), label_width);
    let rate_limit = if state.max_reqs_per_sec == 0 {
        "unlimited".to_string()
    } else {
        format!("{}/s", state.max_reqs_per_sec)
    };
    print_row("Rate limit:", &rate_limit, label_width);

    let max_urls = if state.max_visited_urls == 0 {
        "unlimited".to_string()
    } else {
        state.max_visited_urls.to_string()
    };
    print_row("Max URLs:", &max_urls, label_width);
    print_row("Device:", &state.device, label_width);
    print_row("Content types:", &state.content_summary(), label_width);

    if state.single_page {
        print_row("Scope:", "single page", label_width);
    }
    if let Some(ref dir) = state.offline_export_dir {
        print_row("Offline export:", dir, label_width);
    }
    if let Some(ref dir) = state.markdown_export_dir {
        print_row("Markdown export:", dir, label_width);
    }
    if let Some(ref file) = state.sitemap_xml_file {
        print_row("Sitemap XML:", file, label_width);
    }
    if let Some(ref cols) = state.extra_columns {
        print_row("Extra columns:", cols, label_width);
    }
    if !state.http_cache_enabled {
        print_row("HTTP cache:", "disabled", label_width);
    }
    if state.result_storage_file {
        print_row("Storage:", "file", label_width);
    }
    if state.ignore_robots_txt {
        print_row("Robots.txt:", "ignored", label_width);
    }
    if state.http_auth.is_some() {
        print_row("HTTP auth:", "configured", label_width);
    }
    if let Some(ref proxy) = state.proxy {
        print_row("Proxy:", proxy, label_width);
    }

    // Show generated CLI command
    println!();
    println!("  {}", "Equivalent CLI command:".yellow());
    let cmd = argv[1..].join(" \\\n    ");
    println!("  {} {}", "siteone-crawler".yellow(), cmd.yellow());
    println!();
    println!("  {}", "Tip: Copy this command to skip the wizard next time.".dimmed());
    println!();
}

fn print_row(label: &str, value: &str, label_width: usize) {
    println!("  {:<width$} {}", label.dimmed(), value, width = label_width);
}