Skip to content

Commit 1ebc90d

Browse files
feat(install): add use flag (#1435)
* feat(install): add `--use` flag * add changeset * update the command docs --------- Co-authored-by: Gal Schlezinger <[email protected]>
1 parent ddab191 commit 1ebc90d

File tree

4 files changed

+43
-3
lines changed

4 files changed

+43
-3
lines changed

.changeset/grumpy-dingos-turn.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"fnm": minor
3+
---
4+
5+
add `use` flag to the `install` command

docs/commands.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,9 @@ Options:
262262
[default: info]
263263
[possible values: quiet, error, info]
264264
265+
--use
266+
Use the installed version immediately after installation
267+
265268
--arch <ARCH>
266269
Override the architecture of the installed Node binary. Defaults to arch of fnm binary
267270

src/commands/install.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use super::command::Command;
2+
use super::r#use::Use;
23
use crate::alias::create_alias;
34
use crate::arch::get_safe_arch;
45
use crate::config::FnmConfig;
@@ -8,6 +9,7 @@ use crate::outln;
89
use crate::progress::ProgressConfig;
910
use crate::remote_node_index;
1011
use crate::user_version::UserVersion;
12+
use crate::user_version_reader::UserVersionReader;
1113
use crate::version::Version;
1214
use crate::version_files::get_user_version_for_directory;
1315
use colored::Colorize;
@@ -32,6 +34,10 @@ pub struct Install {
3234
#[clap(long, default_value_t)]
3335
#[arg(value_enum)]
3436
pub progress: ProgressConfig,
37+
38+
/// Use the installed version immediately after installation
39+
#[clap(long)]
40+
pub r#use: bool,
3541
}
3642

3743
impl Install {
@@ -66,6 +72,7 @@ impl Command for Install {
6672
fn apply(self, config: &FnmConfig) -> Result<(), Self::Error> {
6773
let current_dir = std::env::current_dir().unwrap();
6874
let show_progress = self.progress.enabled(config);
75+
let use_installed = self.r#use;
6976

7077
let current_version = self
7178
.version()?
@@ -165,6 +172,10 @@ impl Command for Install {
165172
enable_corepack(&version, config)?;
166173
}
167174

175+
if use_installed {
176+
use_installed_version(&version, config)?;
177+
}
178+
168179
Ok(())
169180
}
170181
}
@@ -194,6 +205,21 @@ fn enable_corepack(version: &Version, config: &FnmConfig) -> Result<(), Error> {
194205
Ok(())
195206
}
196207

208+
fn use_installed_version(version: &Version, config: &FnmConfig) -> Result<(), Error> {
209+
Use {
210+
version: Some(UserVersionReader::Direct(UserVersion::Full(
211+
version.clone(),
212+
))),
213+
install_if_missing: false,
214+
silent_if_unchanged: false,
215+
}
216+
.apply(config)
217+
.map_err(|source| Error::UseError {
218+
source: Box::new(source),
219+
})?;
220+
Ok(())
221+
}
222+
197223
#[derive(Debug, Error)]
198224
pub enum Error {
199225
#[error("Can't download the requested binary: {}", source)]
@@ -208,6 +234,10 @@ pub enum Error {
208234
#[from]
209235
source: super::exec::Error,
210236
},
237+
#[error(transparent)]
238+
UseError {
239+
source: Box<<Use as Command>::Error>,
240+
},
211241
#[error("Can't find version in dotfiles. Please provide a version manually to the command.")]
212242
CantInferVersion,
213243
#[error(transparent)]
@@ -244,6 +274,7 @@ mod tests {
244274
lts: false,
245275
latest: false,
246276
progress: ProgressConfig::Never,
277+
r#use: false,
247278
}
248279
.apply(&config)
249280
.expect("Can't install");
@@ -270,6 +301,7 @@ mod tests {
270301
lts: false,
271302
latest: true,
272303
progress: ProgressConfig::Never,
304+
r#use: false,
273305
}
274306
.apply(&config)
275307
.expect("Can't install");

src/commands/use.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@ use thiserror::Error;
1616

1717
#[derive(clap::Parser, Debug)]
1818
pub struct Use {
19-
version: Option<UserVersionReader>,
19+
pub version: Option<UserVersionReader>,
2020
/// Install the version if it isn't installed yet
2121
#[clap(long)]
22-
install_if_missing: bool,
22+
pub install_if_missing: bool,
2323

2424
/// Don't output a message identifying the version being used
2525
/// if it will not change due to execution of this command
2626
#[clap(long)]
27-
silent_if_unchanged: bool,
27+
pub silent_if_unchanged: bool,
2828
}
2929

3030
impl Command for Use {

0 commit comments

Comments
 (0)