-
Notifications
You must be signed in to change notification settings - Fork 331
Expand file tree
/
Copy pathmod.rs
More file actions
403 lines (360 loc) · 15.4 KB
/
mod.rs
File metadata and controls
403 lines (360 loc) · 15.4 KB
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
use std::fmt::{self, Display};
use super::{
check_fetched, check_shim_reachable, debug_already_fetched, info_fetched, info_installed,
info_pinned, info_project_version, FetchStatus, Tool,
};
use crate::error::{Context, ErrorKind, Fallible};
use crate::fs::{dir_entry_match, ok_if_not_found, remove_dir_if_exists, remove_file_if_exists};
use crate::inventory::node_available;
use crate::layout::volta_home;
use crate::session::Session;
use crate::style::{note_prefix, success_prefix, tool_version};
use crate::sync::VoltaLock;
use cfg_if::cfg_if;
use log::{info, warn};
use node_semver::Version;
mod fetch;
mod metadata;
mod resolve;
pub use fetch::load_default_npm_version;
pub use resolve::resolve;
cfg_if! {
if #[cfg(all(target_os = "windows", target_arch = "x86"))] {
/// The OS component of a Node distro filename
pub const NODE_DISTRO_OS: &str = "win";
/// The architecture component of a Node distro filename
pub const NODE_DISTRO_ARCH: &str = "x86";
/// The extension for Node distro files
pub const NODE_DISTRO_EXTENSION: &str = "zip";
/// The file identifier in the Node index `files` array
pub const NODE_DISTRO_IDENTIFIER: &str = "win-x86-zip";
} else if #[cfg(all(target_os = "windows", target_arch = "x86_64"))] {
/// The OS component of a Node distro filename
pub const NODE_DISTRO_OS: &str = "win";
/// The architecture component of a Node distro filename
pub const NODE_DISTRO_ARCH: &str = "x64";
/// The extension for Node distro files
pub const NODE_DISTRO_EXTENSION: &str = "zip";
/// The file identifier in the Node index `files` array
pub const NODE_DISTRO_IDENTIFIER: &str = "win-x64-zip";
} else if #[cfg(all(target_os = "windows", target_arch = "aarch64"))] {
/// The OS component of a Node distro filename
pub const NODE_DISTRO_OS: &str = "win";
/// The architecture component of a Node distro filename
pub const NODE_DISTRO_ARCH: &str = "arm64";
/// The extension for Node distro files
pub const NODE_DISTRO_EXTENSION: &str = "zip";
/// The file identifier in the Node index `files` array
pub const NODE_DISTRO_IDENTIFIER: &str = "win-arm64-zip";
// NOTE: Node support for pre-built ARM64 binaries on Windows was added in major version 20
// For versions prior to that, we need to fall back on the x64 binaries via emulator
/// The fallback architecture component of a Node distro filename
pub const NODE_DISTRO_ARCH_FALLBACK: &str = "x64";
/// The fallback file identifier in the Node index `files` array
pub const NODE_DISTRO_IDENTIFIER_FALLBACK: &str = "win-x64-zip";
} else if #[cfg(all(target_os = "macos", target_arch = "x86_64"))] {
/// The OS component of a Node distro filename
pub const NODE_DISTRO_OS: &str = "darwin";
/// The architecture component of a Node distro filename
pub const NODE_DISTRO_ARCH: &str = "x64";
/// The extension for Node distro files
pub const NODE_DISTRO_EXTENSION: &str = "tar.gz";
/// The file identifier in the Node index `files` array
pub const NODE_DISTRO_IDENTIFIER: &str = "osx-x64-tar";
} else if #[cfg(all(target_os = "macos", target_arch = "aarch64"))] {
/// The OS component of a Node distro filename
pub const NODE_DISTRO_OS: &str = "darwin";
/// The architecture component of a Node distro filename
pub const NODE_DISTRO_ARCH: &str = "arm64";
/// The extension for Node distro files
pub const NODE_DISTRO_EXTENSION: &str = "tar.gz";
/// The file identifier in the Node index `files` array
pub const NODE_DISTRO_IDENTIFIER: &str = "osx-arm64-tar";
// NOTE: Node support for pre-built Apple Silicon binaries was added in major version 16
// For versions prior to that, we need to fall back on the x64 binaries via Rosetta 2
/// The fallback architecture component of a Node distro filename
pub const NODE_DISTRO_ARCH_FALLBACK: &str = "x64";
/// The fallback file identifier in the Node index `files` array
pub const NODE_DISTRO_IDENTIFIER_FALLBACK: &str = "osx-x64-tar";
} else if #[cfg(all(target_os = "linux", target_arch = "x86_64"))] {
/// The OS component of a Node distro filename
pub const NODE_DISTRO_OS: &str = "linux";
/// The architecture component of a Node distro filename
pub const NODE_DISTRO_ARCH: &str = "x64";
/// The extension for Node distro files
pub const NODE_DISTRO_EXTENSION: &str = "tar.gz";
/// The file identifier in the Node index `files` array
pub const NODE_DISTRO_IDENTIFIER: &str = "linux-x64";
} else if #[cfg(all(target_os = "linux", target_arch = "aarch64"))] {
/// The OS component of a Node distro filename
pub const NODE_DISTRO_OS: &str = "linux";
/// The architecture component of a Node distro filename
pub const NODE_DISTRO_ARCH: &str = "arm64";
/// The extension for Node distro files
pub const NODE_DISTRO_EXTENSION: &str = "tar.gz";
/// The file identifier in the Node index `files` array
pub const NODE_DISTRO_IDENTIFIER: &str = "linux-arm64";
} else if #[cfg(all(target_os = "linux", target_arch = "arm"))] {
/// The OS component of a Node distro filename
pub const NODE_DISTRO_OS: &str = "linux";
/// The architecture component of a Node distro filename
pub const NODE_DISTRO_ARCH: &str = "armv7l";
/// The extension for Node distro files
pub const NODE_DISTRO_EXTENSION: &str = "tar.gz";
/// The file identifier in the Node index `files` array
pub const NODE_DISTRO_IDENTIFIER: &str = "linux-armv7l";
} else {
compile_error!("Unsuppored operating system + architecture combination");
}
}
/// A full Node version including not just the version of Node itself
/// but also the specific version of npm installed globally with that
/// Node installation.
#[derive(Clone, Debug)]
pub struct NodeVersion {
/// The version of Node itself.
pub runtime: Version,
/// The npm version globally installed with the Node distro.
pub npm: Version,
}
impl Display for NodeVersion {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"{} (with {})",
tool_version("node", &self.runtime),
tool_version("npm", &self.npm)
)
}
}
/// The Tool implementation for fetching and installing Node
pub struct Node {
pub(super) version: Version,
}
impl Node {
pub fn new(version: Version) -> Self {
Node { version }
}
#[cfg(not(any(
all(target_os = "macos", target_arch = "aarch64"),
all(target_os = "windows", target_arch = "aarch64")
)))]
pub fn archive_basename(version: &Version) -> String {
format!("node-v{}-{}-{}", version, NODE_DISTRO_OS, NODE_DISTRO_ARCH)
}
#[cfg(all(target_os = "macos", target_arch = "aarch64"))]
pub fn archive_basename(version: &Version) -> String {
// Note: Node began shipping pre-built binaries for Apple Silicon with Major version 16
// Prior to that, we need to fall back on the x64 binaries
format!(
"node-v{}-{}-{}",
version,
NODE_DISTRO_OS,
if version.major >= 16 {
NODE_DISTRO_ARCH
} else {
NODE_DISTRO_ARCH_FALLBACK
}
)
}
#[cfg(all(target_os = "windows", target_arch = "aarch64"))]
pub fn archive_basename(version: &Version) -> String {
// Note: Node began shipping pre-built binaries for Windows ARM with Major version 20
// Prior to that, we need to fall back on the x64 binaries
format!(
"node-v{}-{}-{}",
version,
NODE_DISTRO_OS,
if version.major >= 20 {
NODE_DISTRO_ARCH
} else {
NODE_DISTRO_ARCH_FALLBACK
}
)
}
pub fn archive_filename(version: &Version) -> String {
format!(
"{}.{}",
Node::archive_basename(version),
NODE_DISTRO_EXTENSION
)
}
pub(crate) fn ensure_fetched(&self, session: &mut Session) -> Fallible<NodeVersion> {
match check_fetched(|| node_available(&self.version))? {
FetchStatus::AlreadyFetched => {
debug_already_fetched(self);
let npm = fetch::load_default_npm_version(&self.version)?;
Ok(NodeVersion {
runtime: self.version.clone(),
npm,
})
}
FetchStatus::FetchNeeded(_lock) => fetch::fetch(&self.version, session.hooks()?.node()),
}
}
}
impl Tool for Node {
fn fetch(self: Box<Self>, session: &mut Session) -> Fallible<()> {
let node_version = self.ensure_fetched(session)?;
info_fetched(node_version);
Ok(())
}
fn install(self: Box<Self>, session: &mut Session) -> Fallible<()> {
// Acquire a lock on the Volta directory, if possible, to prevent concurrent changes
let _lock = VoltaLock::acquire();
let node_version = self.ensure_fetched(session)?;
let default_toolchain = session.toolchain_mut()?;
default_toolchain.set_active_node(&self.version)?;
// If the user has a default version of `npm`, we shouldn't show the "(with npm@X.Y.ZZZ)" text in the success message
// Instead we should check if the bundled version is higher than the default and inform the user
// Note: The previous line ensures that there will be a default platform
if let Some(default_npm) = &default_toolchain.platform().unwrap().npm {
info_installed(&self); // includes node version
if node_version.npm > *default_npm {
info!("{} this version of Node includes {}, which is higher than your default version ({}).
To use the version included with Node, run `volta install npm@bundled`",
note_prefix(),
tool_version("npm", node_version.npm),
default_npm.to_string()
);
}
} else {
info_installed(node_version); // includes node and npm version
}
check_shim_reachable("node");
if let Ok(Some(project)) = session.project_platform() {
info_project_version(tool_version("node", &project.node), &self);
}
Ok(())
}
fn pin(self: Box<Self>, session: &mut Session) -> Fallible<()> {
if session.project()?.is_some() {
let node_version = self.ensure_fetched(session)?;
// Note: We know this will succeed, since we checked above
let project = session.project_mut()?.unwrap();
project.pin_node(self.version.clone())?;
// If the user has a pinned version of `npm`, we shouldn't show the "(with npm@X.Y.ZZZ)" text in the success message
// Instead we should check if the bundled version is higher than the pinned and inform the user
// Note: The pin operation guarantees there will be a platform
if let Some(pinned_npm) = &project.platform().unwrap().npm {
info_pinned(self); // includes node version
if node_version.npm > *pinned_npm {
info!("{} this version of Node includes {}, which is higher than your pinned version ({}).
To use the version included with Node, run `volta pin npm@bundled`",
note_prefix(),
tool_version("npm", node_version.npm),
pinned_npm.to_string()
);
}
} else {
info_pinned(node_version); // includes node and npm version
}
Ok(())
} else {
Err(ErrorKind::NotInPackage.into())
}
}
fn uninstall(self: Box<Self>, _session: &mut Session) -> Fallible<()> {
let home = volta_home()?;
// Acquire a lock on the Volta directory, if possible, to prevent concurrent changes
let _lock: Result<VoltaLock, crate::error::VoltaError> = VoltaLock::acquire();
let node_dir = home.node_image_root_dir().join(self.version.to_string());
dir_entry_match(home.node_inventory_dir(), |entry| {
let path = entry.path();
if path.is_file() {
match path.file_name().and_then(|name| name.to_str()) {
Some(file_name) if file_name.contains(&self.version.to_string()) => Some(path),
_ => None,
}
} else {
None
}
})
.or_else(ok_if_not_found)
.with_context(|| ErrorKind::ReadDirError {
dir: home.node_inventory_dir().to_path_buf(),
})
.map(|files| {
files.iter().for_each(|file| {
remove_file_if_exists(file);
})
});
if node_dir.exists() {
remove_dir_if_exists(&node_dir)?;
info!("{} 'node@{}' uninstalled", success_prefix(), self.version);
} else {
warn!("No version 'node@{}' found to uninstall", self.version);
}
Ok(())
}
}
impl Display for Node {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&tool_version("node", &self.version))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_node_archive_basename() {
assert_eq!(
Node::archive_basename(&Version::parse("20.2.3").unwrap()),
format!("node-v20.2.3-{}-{}", NODE_DISTRO_OS, NODE_DISTRO_ARCH)
);
}
#[test]
fn test_node_archive_filename() {
assert_eq!(
Node::archive_filename(&Version::parse("20.2.3").unwrap()),
format!(
"node-v20.2.3-{}-{}.{}",
NODE_DISTRO_OS, NODE_DISTRO_ARCH, NODE_DISTRO_EXTENSION
)
);
}
#[test]
#[cfg(all(target_os = "macos", target_arch = "aarch64"))]
fn test_fallback_node_archive_basename() {
assert_eq!(
Node::archive_basename(&Version::parse("15.2.3").unwrap()),
format!(
"node-v15.2.3-{}-{}",
NODE_DISTRO_OS, NODE_DISTRO_ARCH_FALLBACK
)
);
}
#[test]
#[cfg(all(target_os = "windows", target_arch = "aarch64"))]
fn test_fallback_node_archive_basename() {
assert_eq!(
Node::archive_basename(&Version::parse("19.2.3").unwrap()),
format!(
"node-v19.2.3-{}-{}",
NODE_DISTRO_OS, NODE_DISTRO_ARCH_FALLBACK
)
);
}
#[test]
#[cfg(all(target_os = "macos", target_arch = "aarch64"))]
fn test_fallback_node_archive_filename() {
assert_eq!(
Node::archive_filename(&Version::parse("15.2.3").unwrap()),
format!(
"node-v15.2.3-{}-{}.{}",
NODE_DISTRO_OS, NODE_DISTRO_ARCH_FALLBACK, NODE_DISTRO_EXTENSION
)
);
}
#[test]
#[cfg(all(target_os = "windows", target_arch = "aarch64"))]
fn test_fallback_node_archive_filename() {
assert_eq!(
Node::archive_filename(&Version::parse("19.2.3").unwrap()),
format!(
"node-v19.2.3-{}-{}.{}",
NODE_DISTRO_OS, NODE_DISTRO_ARCH_FALLBACK, NODE_DISTRO_EXTENSION
)
);
}
}