nativeview-cli 0.1.0-rc.1

Developer CLI (`nv`) for Native View Engine: dev hot-reload, builds, pairing, SDK management, app scaffolding, and docs.
Documentation
//! `cargo nv docs` subcommand (`check`, `build`, `dev`).
//!
//! Keeps `nativeview-cli` free of compile-time dependencies on `examples/*`, `nativeview-docs`, and `nativeview-ui`:
//! standalone MDX validation runs directly via `nativeview-mdx`, while site-specific SSR builds and
//! playground checks delegate to the site crate's native binary (`--site-package`, defaulting to
//! auto-detecting `nativeview-docs-site` when present in the active Cargo workspace).

pub(crate) mod build;
pub(crate) mod check;
pub(crate) mod dev;

use std::path::{Path, PathBuf};

use clap::Subcommand;

use crate::manifest::NvManifest;

const DEFAULT_DOCS_CONTENT: &str = "docs/content";

fn apply_manifest_docs_defaults(
    content: PathBuf,
    site_package: Option<String>,
) -> (PathBuf, Option<String>) {
    let Ok(manifest) = NvManifest::load_for_dir(Path::new(".")) else {
        return (content, site_package);
    };
    let Some(docs) = manifest.docs else {
        return (content, site_package);
    };
    let resolved_content = if content == Path::new(DEFAULT_DOCS_CONTENT)
        && let Some(configured) = docs.content
    {
        PathBuf::from(configured)
    } else {
        content
    };
    let resolved_pkg = site_package.or(docs.site_package);
    (resolved_content, resolved_pkg)
}

#[derive(Subcommand, Debug, Clone)]
pub(crate) enum DocsAction {
    /// Validate frontmatter, internal links, heading anchors, _meta.toml manifests, and LiveDemo IDs.
    #[command(alias = "validate")]
    Check {
        /// Path to the documentation content directory.
        #[arg(long, default_value = "docs/content")]
        content: PathBuf,
        /// Optional Cargo site package exposing a `check` binary (auto-detects `nativeview-docs-site` in workspace).
        #[arg(long)]
        site_package: Option<String>,
        /// Force standalone generic `nativeview-mdx` validation without delegating to a site package.
        #[arg(long, default_value_t = false, conflicts_with = "site_package")]
        generic: bool,
    },
    /// Build static semantic HTML5 + Option 5 CSS pages, search.nv-idx, sitemap.xml, and WASM hydration bundle.
    Build {
        /// Path to the documentation content directory.
        #[arg(long, default_value = "docs/content")]
        content: PathBuf,
        /// Output directory for the generated static site.
        #[arg(long, default_value = "dist/docs")]
        out: PathBuf,
        /// Optional deployment base path (e.g. `""` or `"/nativeview"`).
        #[arg(long, default_value = "")]
        base_path: String,
        /// Canonical public site origin + base URL used in sitemap.xml and OpenGraph tags.
        #[arg(
            long,
            default_value = "https://nativeview.github.io/nativeview"
        )]
        site_url: String,
        /// Skip compiling the wasm32 progressive hydration bundle (emit static SSR HTML/CSS only).
        #[arg(long, default_value_t = false)]
        skip_wasm: bool,
        /// Optional path to a prebuilt `hosts/web-shell` bundle directory (supports both unified
        /// `index.html` + `pkg/` and per-app `studio/` + `kitchen-sink/` layouts).
        #[arg(long)]
        web_shell_dist: Option<PathBuf>,
        /// Automatically build `hosts/web-shell` bundles if no prebuilt bundle is found at `--web-shell-dist`.
        #[arg(long, default_value_t = false)]
        with_examples: bool,
        /// Cargo site package exposing the static site builder binary (auto-detects `nativeview-docs-site` in workspace).
        #[arg(long)]
        site_package: Option<String>,
    },
    /// Build once, serve over HTTP, watch docs/content/, and incrementally rebuild on change.
    #[command(alias = "serve")]
    Dev {
        /// Path to the documentation content directory.
        #[arg(long, default_value = "docs/content")]
        content: PathBuf,
        /// Local HTTP port to serve the docs site on.
        #[arg(long, default_value_t = 4173)]
        port: u16,
        /// Skip compiling the wasm32 progressive hydration bundle.
        #[arg(long, default_value_t = false)]
        skip_wasm: bool,
        /// Cargo site package exposing the static site builder binary (auto-detects `nativeview-docs-site` in workspace).
        #[arg(long)]
        site_package: Option<String>,
    },
}

pub(crate) fn cmd_docs(action: DocsAction) {
    let result = match action {
        DocsAction::Check {
            content,
            site_package,
            generic,
        } => {
            let (content, site_package) = apply_manifest_docs_defaults(content, site_package);
            check::run_check(&content, site_package.as_deref(), generic)
        }
        DocsAction::Build {
            content,
            out,
            base_path,
            site_url,
            skip_wasm,
            web_shell_dist,
            with_examples,
            site_package,
        } => {
            let (content, site_package) = apply_manifest_docs_defaults(content, site_package);
            build::run_build(&build::BuildOptions {
                content,
                out,
                base_path,
                site_url,
                skip_wasm,
                web_shell_dist,
                with_examples,
                site_package,
            })
        }
        DocsAction::Dev {
            content,
            port,
            skip_wasm,
            site_package,
        } => {
            let (content, site_package) = apply_manifest_docs_defaults(content, site_package);
            dev::run_dev(&content, port, skip_wasm, site_package)
        }
    };

    if let Err(err) = result {
        eprintln!("error: {err}");
        std::process::exit(1);
    }
}