@@ -10,6 +10,7 @@ use directories::BaseDirs;
1010use log:: debug;
1111#[ cfg( unix) ]
1212use nix:: unistd:: Uid ;
13+ use semver:: Version ;
1314
1415use crate :: executor:: { CommandExt , RunType } ;
1516use crate :: terminal:: print_separator;
@@ -32,23 +33,42 @@ impl NPM {
3233
3334 #[ cfg( target_os = "linux" ) ]
3435 fn root ( & self ) -> Result < PathBuf > {
36+ let version = self . version ( ) ?;
37+ let args = if version < Version :: new ( 8 , 11 , 0 ) {
38+ [ "root" , "-g" ]
39+ } else {
40+ [ "root" , "--location=global" ]
41+ } ;
3542 Command :: new ( & self . command )
36- . args ( & [ "root" , "-g" ] )
43+ . args ( args )
3744 . check_output ( )
3845 . map ( |s| PathBuf :: from ( s. trim ( ) ) )
3946 }
4047
48+ fn version ( & self ) -> Result < Version > {
49+ let version_str = Command :: new ( & self . command )
50+ . args ( & [ "--version" ] )
51+ . check_output ( )
52+ . map ( |s| s. trim ( ) . to_owned ( ) ) ;
53+ Version :: parse ( & version_str?) . map_err ( |err| err. into ( ) )
54+ }
55+
4156 fn upgrade ( & self , run_type : RunType , use_sudo : bool ) -> Result < ( ) > {
4257 print_separator ( "Node Package Manager" ) ;
43-
58+ let version = self . version ( ) ?;
59+ let args = if version < Version :: new ( 8 , 11 , 0 ) || self . pnpm . is_some ( ) {
60+ [ "update" , "-g" ]
61+ } else {
62+ [ "update" , "--location=global" ]
63+ } ;
4464 if use_sudo {
4565 run_type
4666 . execute ( "sudo" )
4767 . arg ( self . pnpm . as_ref ( ) . unwrap_or ( & self . command ) )
48- . args ( & [ "update" , "-g" ] )
68+ . args ( args )
4969 . check_run ( ) ?;
5070 } else {
51- run_type. execute ( & self . command ) . args ( & [ "update" , "-g" ] ) . check_run ( ) ?;
71+ run_type. execute ( & self . command ) . args ( args ) . check_run ( ) ?;
5272 }
5373
5474 Ok ( ( ) )
0 commit comments