-
Notifications
You must be signed in to change notification settings - Fork 331
Expand file tree
/
Copy pathmod.rs
More file actions
206 lines (174 loc) · 5.92 KB
/
mod.rs
File metadata and controls
206 lines (174 loc) · 5.92 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
use std::fmt::{self, Display};
use super::node::load_default_npm_version;
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::inventory::npm_available;
use crate::session::Session;
use crate::style::{success_prefix, tool_version};
use crate::sync::VoltaLock;
use log::info;
use node_semver::Version;
mod fetch;
mod resolve;
pub use resolve::resolve;
/// The Tool implementation for fetching and installing npm
pub struct Npm {
pub(super) version: Version,
}
impl Npm {
pub fn new(version: Version) -> Self {
Npm { version }
}
pub fn archive_basename(version: &str) -> String {
format!("npm-{}", version)
}
pub fn archive_filename(version: &str) -> String {
format!("{}.tgz", Npm::archive_basename(version))
}
pub(crate) fn ensure_fetched(&self, session: &mut Session) -> Fallible<()> {
match check_fetched(|| npm_available(&self.version))? {
FetchStatus::AlreadyFetched => {
debug_already_fetched(self);
Ok(())
}
FetchStatus::FetchNeeded(_lock) => fetch::fetch(&self.version, session.hooks()?.npm()),
}
}
}
impl Tool for Npm {
fn fetch(self: Box<Self>, session: &mut Session) -> Fallible<()> {
self.ensure_fetched(session)?;
info_fetched(self);
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();
self.ensure_fetched(session)?;
session
.toolchain_mut()?
.set_active_npm(Some(self.version.clone()))?;
info_installed(&self);
check_shim_reachable("npm");
if let Ok(Some(project)) = session.project_platform() {
if let Some(npm) = &project.npm {
info_project_version(tool_version("npm", npm), &self);
}
}
Ok(())
}
fn pin(self: Box<Self>, session: &mut Session) -> Fallible<()> {
if session.project()?.is_some() {
self.ensure_fetched(session)?;
// Note: We know this will succeed, since we checked above
let project = session.project_mut()?.unwrap();
project.pin_npm(Some(self.version.clone()))?;
info_pinned(self);
Ok(())
} else {
Err(ErrorKind::NotInPackage.into())
}
}
fn uninstall(self: Box<Self>, _session: &mut Session) -> Fallible<()> {
Err(ErrorKind::Unimplemented {
feature: "Uninstalling npm".into(),
}
.into())
}
}
impl Display for Npm {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&tool_version("npm", &self.version))
}
}
/// The Tool implementation for setting npm to the version bundled with Node
pub struct BundledNpm;
impl Tool for BundledNpm {
fn fetch(self: Box<Self>, _session: &mut Session) -> Fallible<()> {
info!("Bundled npm is included with Node, use `volta fetch node` to fetch Node");
Ok(())
}
fn install(self: Box<Self>, session: &mut Session) -> Fallible<()> {
let toolchain = session.toolchain_mut()?;
toolchain.set_active_npm(None)?;
let bundled_version = match toolchain.platform() {
Some(platform) => {
let version = load_default_npm_version(&platform.node).with_context(|| {
ErrorKind::NoBundledNpm {
command: "install".into(),
}
})?;
version.to_string()
}
None => {
return Err(ErrorKind::NoBundledNpm {
command: "install".into(),
}
.into());
}
};
info!(
"{} set bundled npm (currently {}) as default",
success_prefix(),
bundled_version
);
Ok(())
}
fn pin(self: Box<Self>, session: &mut Session) -> Fallible<()> {
match session.project_mut()? {
Some(project) => {
project.pin_npm(None)?;
let bundled_version = match project.platform() {
Some(platform) => {
let version =
load_default_npm_version(&platform.node).with_context(|| {
ErrorKind::NoBundledNpm {
command: "pin".into(),
}
})?;
version.to_string()
}
None => {
return Err(ErrorKind::NoBundledNpm {
command: "pin".into(),
}
.into());
}
};
info!(
"{} set package.json to use bundled npm (currently {})",
success_prefix(),
bundled_version
);
Ok(())
}
None => Err(ErrorKind::NotInPackage.into()),
}
}
fn uninstall(self: Box<Self>, _session: &mut Session) -> Fallible<()> {
Err(ErrorKind::Unimplemented {
feature: "Uninstalling bundled npm".into(),
}
.into())
}
}
impl Display for BundledNpm {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&tool_version("npm", "bundled"))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_npm_archive_basename() {
assert_eq!(Npm::archive_basename("1.2.3"), "npm-1.2.3");
}
#[test]
fn test_npm_archive_filename() {
assert_eq!(Npm::archive_filename("1.2.3"), "npm-1.2.3.tgz");
}
}