-
Notifications
You must be signed in to change notification settings - Fork 117
Expand file tree
/
Copy pathproject_cache.rs
More file actions
35 lines (29 loc) · 1.09 KB
/
project_cache.rs
File metadata and controls
35 lines (29 loc) · 1.09 KB
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
//! Disk persistence for ruborist's project-level manifest cache.
//!
//! Stored at `<root>/node_modules/.utoo-manifest.json`. Used to warm the
//! demand resolver's in-memory manifest cache across installs.
use std::path::{Path, PathBuf};
use anyhow::{Context, Result};
use utoo_ruborist::service::ProjectCacheData;
use crate::util::json::read_json_file;
pub fn path_for(root: &Path) -> PathBuf {
root.join("node_modules/.utoo-manifest.json")
}
pub async fn load(root: &Path) -> ProjectCacheData {
read_json_file(&path_for(root)).await.unwrap_or_default()
}
pub async fn save(root: &Path, data: &ProjectCacheData) -> Result<()> {
if data.cache.is_empty() {
return Ok(());
}
let path = path_for(root);
if let Some(parent) = path.parent() {
crate::fs::create_dir_all(parent)
.await
.with_context(|| format!("Failed to create {parent:?}"))?;
}
let bytes = serde_json::to_vec(data).context("Failed to serialize project cache")?;
crate::fs::write(&path, &bytes)
.await
.with_context(|| format!("Failed to write {path:?}"))
}