@@ -57,8 +57,11 @@ export class AutoCompletionService implements IAutoCompletionService {
57
57
58
58
public removeObsoleteAutoCompletion ( ) : IFuture < void > {
59
59
return ( ( ) => {
60
- // in previous releases we were writing directly in .bash_profile, .bashrc and .zshrc - remove this old code
61
- this . shellProfiles . forEach ( file => {
60
+ // In previous releases we were writing directly in .bash_profile, .bashrc, .zshrc and .profile - remove this old code
61
+ var shellProfilesToBeCleared = this . shellProfiles ;
62
+ // Add .profile only here as we do not want new autocompletion in this file, but we have to remove our old code from it.
63
+ shellProfilesToBeCleared . push ( this . getHomePath ( ".profile" ) ) ;
64
+ shellProfilesToBeCleared . forEach ( file => {
62
65
try {
63
66
var text = this . $fs . readText ( file ) . wait ( ) ;
64
67
var newText = text . replace ( this . getTabTabObsoleteRegex ( this . $staticConfig . CLIENT_NAME ) , "" ) ;
@@ -72,7 +75,7 @@ export class AutoCompletionService implements IAutoCompletionService {
72
75
}
73
76
} catch ( error ) {
74
77
if ( error . code !== "ENOENT" ) {
75
- this . $logger . trace ( "Error while trying to disable autocompletion for '%s' file. Error is:\n%s" , error ) ;
78
+ this . $logger . trace ( "Error while trying to disable autocompletion for '%s' file. Error is:\n%s" , error . toString ( ) ) ;
76
79
}
77
80
}
78
81
} ) ;
@@ -145,9 +148,13 @@ export class AutoCompletionService implements IAutoCompletionService {
145
148
146
149
private isNewAutoCompletionEnabledInFile ( fileName : string ) : IFuture < boolean > {
147
150
return ( ( ) : boolean => {
148
- var data = this . $fs . readText ( fileName ) . wait ( ) ;
149
- if ( data && data . indexOf ( this . completionShellScriptContent ) !== - 1 ) {
150
- return true ;
151
+ try {
152
+ var data = this . $fs . readText ( fileName ) . wait ( ) ;
153
+ if ( data && data . indexOf ( this . completionShellScriptContent ) !== - 1 ) {
154
+ return true ;
155
+ }
156
+ } catch ( err ) {
157
+ this . $logger . trace ( "Error while checking is autocompletion enabled in file %s. Error is: '%s'" , fileName , err . toString ( ) ) ;
151
158
}
152
159
153
160
return false ;
@@ -156,8 +163,12 @@ export class AutoCompletionService implements IAutoCompletionService {
156
163
157
164
private isObsoleteAutoCompletionEnabledInFile ( fileName : string ) : IFuture < boolean > {
158
165
return ( ( ) => {
159
- var text = this . $fs . readText ( fileName ) . wait ( ) ;
160
- return text . match ( this . getTabTabObsoleteRegex ( this . $staticConfig . CLIENT_NAME ) ) || text . match ( this . getTabTabObsoleteRegex ( this . $staticConfig . CLIENT_NAME ) ) ;
166
+ try {
167
+ var text = this . $fs . readText ( fileName ) . wait ( ) ;
168
+ return text . match ( this . getTabTabObsoleteRegex ( this . $staticConfig . CLIENT_NAME ) ) || text . match ( this . getTabTabObsoleteRegex ( this . $staticConfig . CLIENT_NAME ) ) ;
169
+ } catch ( err ) {
170
+ this . $logger . trace ( "Error while checking is obsolete autocompletion enabled in file %s. Error is: '%s'" , fileName , err . toString ( ) ) ;
171
+ }
161
172
} ) . future < boolean > ( ) ( ) ;
162
173
}
163
174
@@ -169,9 +180,15 @@ export class AutoCompletionService implements IAutoCompletionService {
169
180
this . $fs . appendFile ( fileName , this . completionShellScriptContent ) . wait ( ) ;
170
181
this . scriptsUpdated = true ;
171
182
}
172
- } catch ( err ) {
173
- this . $logger . out ( "Failed to update %s. Auto-completion may not work. " , fileName ) ;
174
- this . $logger . out ( err ) ;
183
+ } catch ( err ) {
184
+ this . $logger . out ( "Unable to update %s. Command-line completion might not work." , fileName ) ;
185
+ // When npm is installed with sudo, in some cases the installation cannot write to shell profiles
186
+ // Advise the user how to enable autocompletion after the installation is completed.
187
+ if ( err . code === "EPERM" && ! hostInfo . isWindows ( ) && process . env . SUDO_USER ) {
188
+ this . $logger . out ( "To enable command-line completion, run '$ %s autocomplete enable'." , this . $staticConfig . CLIENT_NAME ) ;
189
+ }
190
+
191
+ this . $logger . trace ( err ) ;
175
192
this . scriptsOk = false ;
176
193
}
177
194
} ) . future < void > ( ) ( ) ;
@@ -223,7 +240,7 @@ export class AutoCompletionService implements IAutoCompletionService {
223
240
}
224
241
} catch ( err ) {
225
242
this . $logger . out ( "Failed to update %s. Auto-completion may not work. " , filePath ) ;
226
- this . $logger . out ( err ) ;
243
+ this . $logger . trace ( err ) ;
227
244
this . scriptsOk = false ;
228
245
}
229
246
} ) . future < void > ( ) ( ) ;
0 commit comments