2020-01-02 20:13:47 +00:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2019-07-17 22:15:30 +00:00
|
|
|
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;
|
2019-06-24 16:04:06 +00:00
|
|
|
|
2019-07-17 22:15:30 +00:00
|
|
|
/// `DenoDir` serves as coordinator for multiple `DiskCache`s containing them
|
|
|
|
/// in single directory that can be controlled with `$DENO_DIR` env variable.
|
2019-04-02 01:46:40 +00:00
|
|
|
#[derive(Clone)]
|
2018-07-26 21:54:22 +00:00
|
|
|
pub struct DenoDir {
|
|
|
|
// Example: /Users/rld/.deno/
|
|
|
|
pub root: PathBuf,
|
2019-07-31 11:58:41 +00:00
|
|
|
/// Used by TsCompiler to cache compiler output.
|
|
|
|
pub gen_cache: DiskCache,
|
2018-07-26 21:54:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl DenoDir {
|
2019-07-31 11:58:41 +00:00
|
|
|
pub fn new(custom_root: Option<PathBuf>) -> std::io::Result<Self> {
|
2018-07-26 21:54:22 +00:00
|
|
|
// Only setup once.
|
2018-09-14 06:04:02 +00:00
|
|
|
let home_dir = dirs::home_dir().expect("Could not get home directory.");
|
2019-02-13 13:57:00 +00:00
|
|
|
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);
|
2019-07-31 11:58:41 +00:00
|
|
|
let gen_path = root.join("gen");
|
2019-04-29 14:58:31 +00:00
|
|
|
|
2018-11-05 06:21:21 +00:00
|
|
|
let deno_dir = Self {
|
2018-08-14 20:50:53 +00:00
|
|
|
root,
|
2019-07-31 11:58:41 +00:00
|
|
|
gen_cache: DiskCache::new(&gen_path),
|
2018-08-14 20:50:53 +00:00
|
|
|
};
|
2019-01-18 04:39:06 +00:00
|
|
|
|
2018-07-26 21:54:22 +00:00
|
|
|
Ok(deno_dir)
|
|
|
|
}
|
2019-07-17 22:15:30 +00:00
|
|
|
}
|