deno/cli/deno_dir.rs

44 lines
1.4 KiB
Rust
Raw Normal View History

2020-01-02 20:13:47 +00:00
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
use crate::disk_cache::DiskCache;
2019-01-14 06:30:38 +00:00
use dirs;
2018-07-26 21:54:22 +00:00
use std;
use std::path::PathBuf;
/// `DenoDir` serves as coordinator for multiple `DiskCache`s containing them
/// in single directory that can be controlled with `$DENO_DIR` env variable.
#[derive(Clone)]
2018-07-26 21:54:22 +00:00
pub struct DenoDir {
// Example: /Users/rld/.deno/
pub root: PathBuf,
/// Used by SourceFileFetcher to cache remote modules.
pub deps_cache: DiskCache,
/// Used by TsCompiler to cache compiler output.
pub gen_cache: DiskCache,
2018-07-26 21:54:22 +00:00
}
impl DenoDir {
pub fn new(custom_root: Option<PathBuf>) -> std::io::Result<Self> {
2018-07-26 21:54:22 +00:00
// Only setup once.
let home_dir = dirs::home_dir().expect("Could not get home directory.");
let fallback = home_dir.join(".deno");
// We use the OS cache dir because all files deno writes are cache files
// Once that changes we need to start using different roots if DENO_DIR
// is not set, and keep a single one if it is.
let default = dirs::cache_dir()
.map(|d| d.join("deno"))
.unwrap_or(fallback);
2018-07-26 21:54:22 +00:00
2018-11-30 03:03:00 +00:00
let root: PathBuf = custom_root.unwrap_or(default);
let deps_path = root.join("deps");
let gen_path = root.join("gen");
let deno_dir = Self {
root,
deps_cache: DiskCache::new(&deps_path),
gen_cache: DiskCache::new(&gen_path),
};
2018-07-26 21:54:22 +00:00
Ok(deno_dir)
}
}