File tree Expand file tree Collapse file tree 6 files changed +32
-10
lines changed Expand file tree Collapse file tree 6 files changed +32
-10
lines changed Original file line number Diff line number Diff line change 1+ ---
2+ " fnm " : patch
3+ ---
4+
5+ Make ` fnm default ` with no trailing positional argument to return the default version
Original file line number Diff line number Diff line change @@ -14,7 +14,7 @@ Commands:
1414 completions Print shell completions to stdout
1515 alias Alias a version to a common name
1616 unalias Remove an alias definition
17- default Set a version as the default version
17+ default Set a version as the default version or get the current default version
1818 current Print the current Node.js version
1919 exec Run a command within fnm context
2020 uninstall Uninstall a Node.js version [aliases: uni]
@@ -652,14 +652,14 @@ Options:
652652# ` fnm default `
653653
654654```
655- Set a version as the default version
655+ Set a version as the default version or get the current default version.
656656
657657This is a shorthand for `fnm alias VERSION default`
658658
659- Usage: fnm default [OPTIONS] < VERSION>
659+ Usage: fnm default [OPTIONS] [ VERSION]
660660
661661Arguments:
662- < VERSION>
662+ [ VERSION]
663663
664664
665665Options:
Original file line number Diff line number Diff line change @@ -30,6 +30,11 @@ pub fn list_aliases(config: &FnmConfig) -> std::io::Result<Vec<StoredAlias>> {
3030 Ok ( vec)
3131}
3232
33+ pub fn get_alias_by_name ( config : & FnmConfig , alias_name : & str ) -> Option < StoredAlias > {
34+ let alias_path = config. aliases_dir ( ) . join ( alias_name) ;
35+ TryInto :: < StoredAlias > :: try_into ( alias_path. as_path ( ) ) . ok ( )
36+ }
37+
3338#[ derive( Debug ) ]
3439pub struct StoredAlias {
3540 alias_path : PathBuf ,
Original file line number Diff line number Diff line change @@ -44,7 +44,7 @@ pub enum SubCommand {
4444 #[ clap( name = "unalias" , bin_name = "unalias" ) ]
4545 Unalias ( commands:: unalias:: Unalias ) ,
4646
47- /// Set a version as the default version
47+ /// Set a version as the default version or get the current default version.
4848 ///
4949 /// This is a shorthand for `fnm alias VERSION default`
5050 #[ clap( name = "default" , bin_name = "default" ) ]
Original file line number Diff line number Diff line change @@ -38,4 +38,6 @@ pub enum Error {
3838 VersionNotFound { version : UserVersion } ,
3939 #[ error( transparent) ]
4040 CantUnderstandVersion { source : ApplicableVersionError } ,
41+ #[ error( "A default version has not been set." ) ]
42+ DefaultAliasDoesNotExist ,
4143}
Original file line number Diff line number Diff line change 11use super :: alias:: Alias ;
22use super :: command:: Command ;
3+ use crate :: alias:: get_alias_by_name;
34use crate :: config:: FnmConfig ;
45use crate :: user_version:: UserVersion ;
56
67#[ derive( clap:: Parser , Debug ) ]
78pub struct Default {
8- version : UserVersion ,
9+ version : Option < UserVersion > ,
910}
1011
1112impl Command for Default {
1213 type Error = super :: alias:: Error ;
1314
1415 fn apply ( self , config : & FnmConfig ) -> Result < ( ) , Self :: Error > {
15- Alias {
16- name : "default" . into ( ) ,
17- to_version : self . version ,
16+ match self . version {
17+ Some ( version) => Alias {
18+ name : "default" . into ( ) ,
19+ to_version : version,
20+ }
21+ . apply ( config) ,
22+ None => match get_alias_by_name ( config, "default" ) {
23+ Some ( alias) => {
24+ println ! ( "{}" , alias. s_ver( ) ) ;
25+ Ok ( ( ) )
26+ }
27+ None => Err ( Self :: Error :: DefaultAliasDoesNotExist ) ,
28+ } ,
1829 }
19- . apply ( config)
2030 }
2131
2232 fn handle_error ( err : Self :: Error , config : & FnmConfig ) {
You can’t perform that action at this time.
0 commit comments