1
- // import proc from 'process';
1
+ import fs from 'fs' ;
2
+ import path from 'path' ;
2
3
import unpdateNotifier from 'update-notifier' ;
3
- import isInstalledGlobally from 'is-installed-globally' ;
4
4
import { exec } from '@tunnckocore/execa' ;
5
+ import globalDirs from 'global-dirs' ;
5
6
6
7
export default function updater ( options ) {
7
8
const opts = Object . assign ( { } , options ) ;
@@ -14,33 +15,44 @@ export default function updater(options) {
14
15
if ( err ) {
15
16
throw err ;
16
17
}
17
- if ( info . type !== 'latest' ) {
18
+ if ( isInstalledGlobally ( opts . pkg ) && info . type !== 'latest' ) {
18
19
autoupdate ( opts . pkg , opts . manager ) ;
19
20
}
20
21
} ;
21
22
22
23
return unpdateNotifier ( opts ) ;
23
24
}
24
25
26
+ function isInstalledGlobally ( { name } ) {
27
+ /* eslint-disable no-restricted-syntax */
28
+
29
+ let exists = false ;
30
+ for ( const [ , dirs ] of Object . entries ( globalDirs ) ) {
31
+ for ( const [ , globalPath ] of Object . entries ( dirs ) ) {
32
+ const fp = path . join ( globalPath , name ) ;
33
+ if ( fs . existsSync ( fp ) ) {
34
+ exists = true ;
35
+ break ;
36
+ }
37
+ }
38
+ }
39
+ return exists ;
40
+ }
41
+
25
42
async function autoupdate ( pkg , manager ) {
26
43
const isNpm = manager === 'npm' ;
27
44
28
45
if ( isNpm || manager === 'pnpm' ) {
29
- await exec (
30
- `${ isNpm ? 'npm' : 'pnpm' } install ${
31
- isInstalledGlobally ? '--global' : ''
32
- } ${ pkg . name } `,
33
- ) ;
46
+ await exec ( `${ isNpm ? 'npm' : 'pnpm' } install --global ${ pkg . name } ` ) ;
34
47
}
35
48
36
49
if ( manager === 'yarn' ) {
37
- const g = isInstalledGlobally ? 'global' : '' ;
38
50
await exec ( [
39
51
// ensure it is cleanest one
40
- `yarn ${ g } remove ${ pkg . name } ` ,
52
+ `yarn global remove ${ pkg . name } ` ,
41
53
42
54
// install it after that ensurance
43
- `yarn ${ g } add ${ pkg . name } ` ,
55
+ `yarn global add ${ pkg . name } ` ,
44
56
] ) ;
45
57
}
46
58
}
0 commit comments