11import * as path from 'path' ;
22import * as execa from 'execa' ;
3+ import * as shell from 'shelljs' ;
34import { INodeManager } from '../types' ;
45import log from '../utils/log' ;
5- import formatWhitespaceInPath from '../utils/formatWhitespaceInPath' ;
66import formatNodeVersion from '../utils/formatNodeVersion' ;
77import { NOT_REINSTALL_PACKAGES } from '../constants' ;
88import getNpmRegistry from '../utils/getNpmRegistry' ;
9- // eslint-disable-next-line import/order
10- import stripAnsi = require( 'strip-ansi' ) ;
119
1210class NvmManager implements INodeManager {
1311 channel : string ;
1412
1513 std : string ;
1614
15+ previousNpmPath : string ;
16+
1717 globalNpmPackages : string [ ] ;
1818
1919 nodePath : string ;
2020
2121 constructor ( channel = '' ) {
2222 this . channel = channel ;
2323 this . std = '' ;
24+ this . previousNpmPath = '' ;
2425 this . globalNpmPackages = [ ] ;
2526 this . nodePath = '' ;
2627 }
2728
2829 installNode = ( version : string , reinstallGlobalDeps = true ) => {
2930 if ( reinstallGlobalDeps ) {
30- // collect the global npm packages
31- const args : string [ ] = [ 'list' , '-g' , '--depth=0' , '--json' ] ;
32- const { stdout } = execa . sync ( 'npm' , args ) ;
33- if ( stdout ) {
34- const { dependencies = { } } = JSON . parse ( stdout ) ;
35- const depNames = Object . keys ( dependencies ) . filter ( ( dep : string ) => ! NOT_REINSTALL_PACKAGES . includes ( dep ) ) || [ ] ;
36-
37- depNames . forEach ( ( dep : string ) => {
38- const { version : depVersion } = dependencies [ dep ] ;
39- this . globalNpmPackages . push ( `${ dep } @${ depVersion } ` ) ;
40- } ) ;
41- }
31+ // get the previous npm path
32+ const { stdout } = shell . which ( 'npm' ) ;
33+ this . previousNpmPath = stdout ;
4234 }
4335 const formattedVersion = formatNodeVersion ( version ) ;
4436 const shFilePath = path . resolve ( __dirname , '../data/shells' , 'nvm-install-node.sh' ) ;
@@ -66,18 +58,9 @@ class NvmManager implements INodeManager {
6658 } ) ;
6759 } ;
6860
69- async getNodeVersionsList ( ) {
70- const shFilePath = formatWhitespaceInPath ( path . resolve ( __dirname , '../data/shells' , 'nvm-node-version.sh' ) ) ;
71- const { stdout } = await execa . command ( `sh ${ shFilePath } ` ) ;
72-
73- return stdout
74- . split ( '\n' )
75- . reverse ( )
76- . map ( ( version : string ) => stripAnsi ( version ) . trim ( ) ) ;
77- }
78-
7961 reinstallPackages = ( ) => {
80- return getNpmRegistry ( )
62+ return this . getGlobalNpmPackages ( )
63+ . then ( ( ) => getNpmRegistry ( ) )
8164 . then ( ( npmRegistry ) => {
8265 return new Promise ( ( resolve , reject ) => {
8366 const args = [ 'i' , '-g' , ...this . globalNpmPackages , '--registry' , npmRegistry ] ;
@@ -88,6 +71,7 @@ class NvmManager implements INodeManager {
8871 // Don't extend env because it will not use the current(new) npm to install package
8972 extendEnv : false ,
9073 } ) ;
74+
9175 cp . stdout . on ( 'data' , this . listenFunc ) ;
9276
9377 cp . stderr . on ( 'data' , this . listenFunc ) ;
@@ -126,6 +110,22 @@ class NvmManager implements INodeManager {
126110 }
127111 return undefined ;
128112 }
113+
114+ private async getGlobalNpmPackages ( ) {
115+ if ( ! this . previousNpmPath ) {
116+ throw new Error ( 'Npm command was not Found.' ) ;
117+ }
118+ const { stdout } = await execa ( this . previousNpmPath , [ 'list' , '-g' , '--depth=0' , '--json' ] ) ;
119+ if ( stdout ) {
120+ const { dependencies = { } } = JSON . parse ( stdout ) ;
121+ const depNames = Object . keys ( dependencies ) . filter ( ( dep : string ) => ! NOT_REINSTALL_PACKAGES . includes ( dep ) ) || [ ] ;
122+
123+ depNames . forEach ( ( dep : string ) => {
124+ const { version : depVersion } = dependencies [ dep ] ;
125+ this . globalNpmPackages . push ( `${ dep } @${ depVersion } ` ) ;
126+ } ) ;
127+ }
128+ }
129129}
130130
131131export default NvmManager ;
0 commit comments