summaryrefslogtreecommitdiff
path: root/cli/lockfile.rs
diff options
context:
space:
mode:
authorKitson Kelly <me@kitsonkelly.com>2021-10-11 08:26:22 +1100
committerGitHub <noreply@github.com>2021-10-11 08:26:22 +1100
commita7baf5f2bbb50dc0cb571de141b800b9155faca7 (patch)
tree4bebaabd1d3ed4595e8a388e0fae559bb5558974 /cli/lockfile.rs
parent5a8a989b7815023f33a1e3183a55cc8999af5d98 (diff)
refactor: integrate deno_graph into CLI (#12369)
Diffstat (limited to 'cli/lockfile.rs')
-rw-r--r--cli/lockfile.rs42
1 files changed, 42 insertions, 0 deletions
diff --git a/cli/lockfile.rs b/cli/lockfile.rs
index 0b52f5ee7..6b553bd15 100644
--- a/cli/lockfile.rs
+++ b/cli/lockfile.rs
@@ -1,11 +1,16 @@
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
+use deno_core::parking_lot::Mutex;
use deno_core::serde_json;
use deno_core::serde_json::json;
+use deno_core::ModuleSpecifier;
use log::debug;
+use std::cell::RefCell;
use std::collections::BTreeMap;
use std::io::Result;
use std::path::PathBuf;
+use std::rc::Rc;
+use std::sync::Arc;
#[derive(Debug, Clone)]
pub struct Lockfile {
@@ -81,6 +86,43 @@ impl Lockfile {
}
}
+#[derive(Debug)]
+pub(crate) struct Locker(Option<Arc<Mutex<Lockfile>>>);
+
+impl deno_graph::source::Locker for Locker {
+ fn check_or_insert(
+ &mut self,
+ specifier: &ModuleSpecifier,
+ source: &str,
+ ) -> bool {
+ if let Some(lock_file) = &self.0 {
+ let mut lock_file = lock_file.lock();
+ lock_file.check_or_insert(specifier.as_str(), source)
+ } else {
+ true
+ }
+ }
+
+ fn get_checksum(&self, content: &str) -> String {
+ crate::checksum::gen(&[content.as_bytes()])
+ }
+
+ fn get_filename(&self) -> Option<String> {
+ let lock_file = self.0.as_ref()?.lock();
+ lock_file.filename.to_str().map(|s| s.to_string())
+ }
+}
+
+pub(crate) fn as_maybe_locker(
+ lockfile: Option<Arc<Mutex<Lockfile>>>,
+) -> Option<Rc<RefCell<Box<dyn deno_graph::source::Locker>>>> {
+ lockfile.as_ref().map(|lf| {
+ Rc::new(RefCell::new(
+ Box::new(Locker(Some(lf.clone()))) as Box<dyn deno_graph::source::Locker>
+ ))
+ })
+}
+
#[cfg(test)]
mod tests {
use super::*;