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
use crate::map::{ExportsField, Field, ImportsField, PathTreeNode};
use crate::{AliasMap, RResult, Resolver};
use std::collections::HashMap;
use std::fs::read_to_string;
use std::path::{Path, PathBuf};
use std::sync::Arc;
#[derive(Clone, Debug)]
pub enum SideEffects {
Bool(bool),
Array(Vec<String>),
}
#[derive(Clone, Debug)]
pub struct PkgFileInfo {
pub abs_dir_path: PathBuf,
pub name: Option<String>,
pub main_fields: Vec<String>,
pub alias_fields: HashMap<String, AliasMap>,
pub exports_field_tree: Option<PathTreeNode>,
pub imports_field_tree: Option<PathTreeNode>,
pub side_effects: Option<SideEffects>,
}
impl Resolver {
#[tracing::instrument]
fn parse_description_file(
&self,
dir: &Path,
description_file_name: &str,
) -> RResult<PkgFileInfo> {
let location = dir.join(description_file_name);
let str = tracing::debug_span!("read_to_string").in_scope(|| {
read_to_string(&location).map_err(|_| format!("Open {} failed", location.display()))
})?;
let json: serde_json::Value =
tracing::debug_span!("serde_json_from_str").in_scope(|| {
serde_json::from_str(&str)
.map_err(|_| format!("Parse {} failed", location.display()))
})?;
let main_fields = self
.options
.main_fields
.iter()
.fold(vec![], |mut acc, main_filed| {
if let Some(value) = json.get(main_filed) {
if let Some(s) = value.as_str() {
acc.push(s.to_string());
}
}
acc
});
let mut alias_fields = HashMap::new();
if let Some(value) = json.get("browser") {
if let Some(map) = value.as_object() {
for (key, value) in map {
if let Some(b) = value.as_bool() {
assert!(!b);
alias_fields.insert(key.to_string(), AliasMap::Ignored);
} else if let Some(s) = value.as_str() {
alias_fields.insert(key.to_string(), AliasMap::Target(s.to_string()));
}
}
}
}
let exports_field_tree = if let Some(value) = json.get("exports") {
Some(ExportsField::build_field_path_tree(value)?)
} else {
None
};
let imports_field_tree = if let Some(value) = json.get("imports") {
Some(ImportsField::build_field_path_tree(value)?)
} else {
None
};
let name = json
.get("name")
.and_then(|v| v.as_str())
.map(|s| s.to_string());
let side_effects: Option<SideEffects> =
json.get("sideEffects").map_or(Ok(None), |value| {
if let Some(b) = value.as_bool() {
Ok(Some(SideEffects::Bool(b)))
} else if let Some(vec) = value.as_array() {
let mut ans = vec![];
for value in vec {
if let Some(str) = value.as_str() {
ans.push(str.to_string());
} else {
return Err(format!(
"sideEffects in {} had unexpected value {}",
location.display(),
value
));
}
}
Ok(Some(SideEffects::Array(ans)))
} else {
Err(format!(
"sideEffects in {} had unexpected value {}",
location.display(),
value
))
}
})?;
Ok(PkgFileInfo {
name,
abs_dir_path: dir.to_path_buf(),
main_fields,
alias_fields,
exports_field_tree,
imports_field_tree,
side_effects,
})
}
pub fn load_sideeffects(&self, path: &Path) -> RResult<Option<(PathBuf, Option<SideEffects>)>> {
Ok(self.load_pkg_file(path)?.map(|pkg_info| {
(
pkg_info
.abs_dir_path
.join(self.options.description_file.as_ref().unwrap()),
pkg_info.side_effects.clone(),
)
}))
}
#[tracing::instrument]
pub(crate) fn load_pkg_file(&self, path: &Path) -> RResult<Option<Arc<PkgFileInfo>>> {
if self.options.description_file.is_none() {
return Ok(None);
}
if !path.is_dir() {
return match path.parent() {
Some(dir) => self.load_pkg_file(dir),
None => Err(Resolver::raise_tag()),
};
}
let pkg_info = if let Some(r#ref) = self
.unsafe_cache
.as_ref()
.and_then(|cache| cache.pkg_info.get(path))
{
r#ref.clone()
} else {
let description_file_name = self.options.description_file.as_ref().unwrap();
let (pkg_info, target_dir) =
if let Some(target_dir) = Self::find_up(path, description_file_name) {
if let Some(r#ref) = self
.unsafe_cache
.as_ref()
.and_then(|cache| cache.pkg_info.get(&target_dir))
{
return Ok(r#ref.clone());
}
let parsed =
Arc::new(self.parse_description_file(&target_dir, description_file_name)?);
(Some(parsed), Some(target_dir))
} else {
(None, None)
};
if let Some(cache) = self.unsafe_cache.as_ref() {
let mut temp_dir = path.to_path_buf();
let target_dir = if let Some(target_dir) = target_dir {
target_dir
} else {
PathBuf::from("/")
};
loop {
let info = pkg_info.clone();
cache.pkg_info.insert(temp_dir.clone(), info);
if temp_dir.eq(&target_dir) || !temp_dir.pop() {
break;
}
}
}
pkg_info
};
Ok(pkg_info)
}
}