wasm-pkg-core 0.16.0

Wasm Package Tools core libraries for wkg
Documentation
use std::path::{Path, PathBuf};

use tempfile::TempDir;
use wasm_pkg_client::{
    Client, Config,
    caching::{CachingClient, FileCache},
};
use wasm_pkg_core::wit::WIT_DEPS_DIR;

pub fn fixture_dir() -> PathBuf {
    PathBuf::from(env!("CARGO_MANIFEST_DIR"))
        .join("tests")
        .join("fixtures")
}

pub async fn get_client() -> anyhow::Result<(TempDir, CachingClient<FileCache>)> {
    // NOTE: `Client::with_global_defaults()` may pick up a user's redirect of `wasi` to a private registry
    let client = Client::new(Config::default());
    let cache_temp_dir = tempfile::tempdir()?;
    let cache = FileCache::new(cache_temp_dir.path()).await?;

    Ok((cache_temp_dir, CachingClient::new(Some(client), cache)))
}

/// Loads the fixture with the given name into a temporary directory. This will copy the fixture from the tests/fixtures directory into a temporary directory and return the tempdir containing that directory (and its path)
pub async fn load_fixture(fixture: &str) -> anyhow::Result<(TempDir, PathBuf)> {
    let temp_dir = tempfile::tempdir()?;
    let fixture_path = fixture_dir().join(fixture);
    // This will error if it doesn't exist, which is what we want
    tokio::fs::metadata(&fixture_path).await?;
    let copied_path = temp_dir.path().join(fixture_path.file_name().unwrap());
    copy_dir(&fixture_path, &copied_path).await?;
    Ok((temp_dir, copied_path))
}

async fn copy_dir(source: impl AsRef<Path>, destination: impl AsRef<Path>) -> anyhow::Result<()> {
    tokio::fs::create_dir_all(&destination).await?;
    let mut entries = tokio::fs::read_dir(source).await?;
    while let Some(entry) = entries.next_entry().await? {
        let filetype = entry.file_type().await?;
        if filetype.is_dir() {
            // Skip the deps directory in case it is there from debugging
            if entry.path().file_name().unwrap_or_default() == WIT_DEPS_DIR {
                continue;
            }
            Box::pin(copy_dir(
                entry.path(),
                destination.as_ref().join(entry.file_name()),
            ))
            .await?;
        } else {
            let path = entry.path();
            let extension = path.extension().unwrap_or_default();
            // Skip any .lock or .wasm files that might be there from debugging
            if extension == "lock" || extension == "wasm" {
                continue;
            }
            tokio::fs::copy(path, destination.as_ref().join(entry.file_name())).await?;
        }
    }
    Ok(())
}