summaryrefslogtreecommitdiff
path: root/cli/modules.rs
blob: f40f5ca089aa6d0033581dd9009cfad438d693a0 (plain)
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
use crate::ansi;
use crate::deno_dir::DenoDir;
use crate::msg;
use deno::deno_mod;
use std::collections::HashMap;
use std::collections::HashSet;
use std::fmt;

pub struct ModuleInfo {
  name: String,
  children: Vec<deno_mod>,
}

/// A symbolic module entity.
pub enum SymbolicModule {
  /// This module is an alias to another module.
  /// This is useful such that multiple names could point to
  /// the same underlying module (particularly due to redirects).
  Alias(String),
  /// This module associates with a V8 module by id.
  Mod(deno_mod),
}

#[derive(Default)]
/// Alias-able module name map
pub struct ModuleNameMap {
  inner: HashMap<String, SymbolicModule>,
}

impl ModuleNameMap {
  pub fn new() -> Self {
    ModuleNameMap {
      inner: HashMap::new(),
    }
  }

  /// Get the id of a module.
  /// If this module is internally represented as an alias,
  /// follow the alias chain to get the final module id.
  pub fn get(&self, name: &str) -> Option<deno_mod> {
    let mut mod_name = name;
    loop {
      let cond = self.inner.get(mod_name);
      match cond {
        Some(SymbolicModule::Alias(target)) => {
          mod_name = target;
        }
        Some(SymbolicModule::Mod(mod_id)) => {
          return Some(*mod_id);
        }
        _ => {
          return None;
        }
      }
    }
  }

  /// Insert a name assocated module id.
  pub fn insert(&mut self, name: String, id: deno_mod) {
    self.inner.insert(name, SymbolicModule::Mod(id));
  }

  /// Create an alias to another module.
  pub fn alias(&mut self, name: String, target: String) {
    self.inner.insert(name, SymbolicModule::Alias(target));
  }
}

/// A collection of JS modules.
#[derive(Default)]
pub struct Modules {
  pub info: HashMap<deno_mod, ModuleInfo>,
  pub by_name: ModuleNameMap,
}

impl Modules {
  pub fn new() -> Modules {
    Self {
      info: HashMap::new(),
      by_name: ModuleNameMap::new(),
    }
  }

  pub fn get_id(&self, name: &str) -> Option<deno_mod> {
    self.by_name.get(name)
  }

  pub fn get_children(&self, id: deno_mod) -> Option<&Vec<deno_mod>> {
    self.info.get(&id).map(|i| &i.children)
  }

  pub fn get_name(&self, id: deno_mod) -> Option<&String> {
    self.info.get(&id).map(|i| &i.name)
  }

  pub fn is_registered(&self, name: &str) -> bool {
    self.by_name.get(name).is_some()
  }

  pub fn register(&mut self, id: deno_mod, name: &str) {
    let name = String::from(name);
    debug!("register {}", name);
    self.by_name.insert(name.clone(), id);
    self.info.insert(
      id,
      ModuleInfo {
        name,
        children: Vec::new(),
      },
    );
  }

  pub fn alias(&mut self, name: &str, target: &str) {
    self.by_name.alias(name.to_owned(), target.to_owned());
  }

  pub fn resolve_cb(
    &mut self,
    deno_dir: &DenoDir,
    specifier: &str,
    referrer: deno_mod,
  ) -> deno_mod {
    debug!("resolve_cb {}", specifier);

    let maybe_info = self.info.get_mut(&referrer);
    if maybe_info.is_none() {
      debug!("cant find referrer {}", referrer);
      return 0;
    }
    let info = maybe_info.unwrap();
    let referrer_name = &info.name;
    let r = deno_dir.resolve_module(specifier, referrer_name);
    if let Err(err) = r {
      debug!("potentially swallowed err: {}", err);
      return 0;
    }
    let (name, _local_filename) = r.unwrap();

    if let Some(child_id) = self.by_name.get(&name) {
      info.children.push(child_id);
      return child_id;
    } else {
      return 0;
    }
  }

  pub fn print_file_info(&self, deno_dir: &DenoDir, filename: String) {
    // TODO Note the --reload flag is ignored here.
    let maybe_out = deno_dir.fetch_module_meta_data(&filename, ".", true);
    if maybe_out.is_err() {
      println!("{}", maybe_out.unwrap_err());
      return;
    }
    let out = maybe_out.unwrap();

    println!("{} {}", ansi::bold("local:".to_string()), &(out.filename));
    println!(
      "{} {}",
      ansi::bold("type:".to_string()),
      msg::enum_name_media_type(out.media_type)
    );
    if out.maybe_output_code_filename.is_some() {
      println!(
        "{} {}",
        ansi::bold("compiled:".to_string()),
        out.maybe_output_code_filename.as_ref().unwrap(),
      );
    }
    if out.maybe_source_map_filename.is_some() {
      println!(
        "{} {}",
        ansi::bold("map:".to_string()),
        out.maybe_source_map_filename.as_ref().unwrap()
      );
    }

    let deps = Deps::new(self, &out.module_name);
    println!("{}{}", ansi::bold("deps:\n".to_string()), deps.name);
    if let Some(ref depsdeps) = deps.deps {
      for d in depsdeps {
        println!("{}", d);
      }
    }
  }
}

pub struct Deps {
  pub name: String,
  pub deps: Option<Vec<Deps>>,
  prefix: String,
  is_last: bool,
}

impl Deps {
  pub fn new(modules: &Modules, module_name: &str) -> Deps {
    let mut seen = HashSet::new();
    let id = modules.get_id(module_name).unwrap();
    Self::helper(&mut seen, "".to_string(), true, modules, id)
  }

  fn helper(
    seen: &mut HashSet<deno_mod>,
    prefix: String,
    is_last: bool,
    modules: &Modules,
    id: deno_mod,
  ) -> Deps {
    let name = modules.get_name(id).unwrap().to_string();
    if seen.contains(&id) {
      Deps {
        name,
        prefix,
        deps: None,
        is_last,
      }
    } else {
      seen.insert(id);
      let child_ids = modules.get_children(id).unwrap();
      let child_count = child_ids.iter().count();
      let deps = child_ids
        .iter()
        .enumerate()
        .map(|(index, dep_id)| {
          let new_is_last = index == child_count - 1;
          let mut new_prefix = prefix.clone();
          new_prefix.push(if is_last { ' ' } else { '│' });
          new_prefix.push(' ');
          Self::helper(seen, new_prefix, new_is_last, modules, *dep_id)
        }).collect();
      Deps {
        name,
        prefix,
        deps: Some(deps),
        is_last,
      }
    }
  }
}

impl fmt::Display for Deps {
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    let mut has_children = false;
    if let Some(ref deps) = self.deps {
      has_children = !deps.is_empty();
    }
    write!(
      f,
      "{}{}─{} {}",
      self.prefix,
      if self.is_last { "└" } else { "├" },
      if has_children { "┬" } else { "─" },
      self.name
    )?;

    if let Some(ref deps) = self.deps {
      for d in deps {
        write!(f, "\n{}", d)?;
      }
    }
    Ok(())
  }
}