@@ -13,43 +13,43 @@ const os = require('os')
13
13
const path = require ( 'path' )
14
14
const makeDir = require ( 'make-dir' )
15
15
16
- function inHome ( ...filepaths ) {
17
- return path . join ( os . homedir ( ) , ...filepaths )
18
- }
19
-
20
- function inConfig ( ...filepaths ) {
21
- return inHome ( '.config' , ...filepaths )
22
- }
23
-
24
- // shells and an appropriate config file to add our source line to
25
- // ref: https://en.wikipedia.org/wiki/Unix_shell#Configuration_files
26
- const SUPPORTED_SHELLS = {
27
- shellFormat : {
28
- // only support shells that support functions
29
- // ref: https://web.archive.org/web/20160403120601/http://www.unixnote.com/2010/05/different-unix-shell.html
30
- sh : [ inHome ( '.profile' ) ] ,
31
- ksh : [ inHome ( '.kshrc' ) ] ,
32
- zsh : [ inHome ( '.zshrc' ) , inHome ( '.profile' ) , inHome ( '.zprofile' ) ] ,
33
- bash : [ inHome ( '.bashrc' ) , inHome ( '.profile' ) , inHome ( '.bash_profile' ) ]
34
- } ,
35
- powerFormat : {
36
- pwsh : os . platform ( ) !== 'win32' // pwsh stores its profile in different places depending on the OS
37
- ? [ inConfig ( 'powershell' , 'Microsoft.PowerShell_profile.ps1' ) ]
38
- : [ inHome ( 'Documents' , 'PowerShell' , 'Microsoft.PowerShell_profile.ps1' ) ] ,
39
- 'powershell.exe' : [ inHome ( 'Documents' , 'WindowsPowerShell' , 'Microsoft.PowerShell_profile.ps1' ) ]
40
- }
41
- }
42
-
43
16
/**
44
17
* Profile is responsible for detecting appropriate shell profile files and appending/removing the
45
18
* source command to/from those shell profiles. Profile attempts to run many different shell variants
46
19
* to determine the appropriate paths to write to. Shells which are runnable are eligible to have their
47
20
* respective profiles updated with the source command.
48
21
*/
49
22
class Profile {
50
- constructor ( { env, runlog } ) {
23
+ constructor ( { env, runlog, pathChecks } ) {
51
24
this . env = env
25
+ this . pathChecks = pathChecks
52
26
this . runlog = runlog
27
+ // shells and an appropriate config file to add our source line to
28
+ // ref: https://en.wikipedia.org/wiki/Unix_shell#Configuration_files
29
+ this . SUPPORTED_SHELLS = {
30
+ shellFormat : {
31
+ // only support shells that support functions
32
+ // ref: https://web.archive.org/web/20160403120601/http://www.unixnote.com/2010/05/different-unix-shell.html
33
+ sh : [ this . pathChecks . inHome ( '.profile' ) ] ,
34
+ ksh : [ this . pathChecks . inHome ( '.kshrc' ) ] ,
35
+ zsh : [
36
+ this . pathChecks . inHome ( '.zshrc' ) ,
37
+ this . pathChecks . inHome ( '.profile' ) ,
38
+ this . pathChecks . inHome ( '.zprofile' )
39
+ ] ,
40
+ bash : [
41
+ this . pathChecks . inHome ( '.bashrc' ) ,
42
+ this . pathChecks . inHome ( '.profile' ) ,
43
+ this . pathChecks . inHome ( '.bash_profile' )
44
+ ]
45
+ } ,
46
+ powerFormat : {
47
+ pwsh : os . platform ( ) !== 'win32' // pwsh stores its profile in different places depending on the OS
48
+ ? [ this . pathChecks . inConfig ( 'powershell' , 'Microsoft.PowerShell_profile.ps1' ) ]
49
+ : [ this . pathChecks . inHome ( 'Documents' , 'PowerShell' , 'Microsoft.PowerShell_profile.ps1' ) ] ,
50
+ 'powershell.exe' : [ this . pathChecks . inHome ( 'Documents' , 'WindowsPowerShell' , 'Microsoft.PowerShell_profile.ps1' ) ]
51
+ }
52
+ }
53
53
}
54
54
55
55
async installToProfiles ( ) {
@@ -102,28 +102,28 @@ class Profile {
102
102
const detectedShellFormatProfiles = new Set ( )
103
103
const detectedPowerFormatProfiles = new Set ( )
104
104
105
- const shellFormat = Object . keys ( SUPPORTED_SHELLS . shellFormat ) . map ( async shell => {
105
+ const shellFormat = Object . keys ( this . SUPPORTED_SHELLS . shellFormat ) . map ( async shell => {
106
106
const runnable = await this . _isRunnable ( shell )
107
107
return ( {
108
108
shell,
109
109
runnable,
110
- profiles : SUPPORTED_SHELLS . shellFormat [ shell ]
110
+ profiles : this . SUPPORTED_SHELLS . shellFormat [ shell ]
111
111
} )
112
112
} )
113
- const powerFormat = Object . keys ( SUPPORTED_SHELLS . powerFormat ) . map ( async shell => {
113
+ const powerFormat = Object . keys ( this . SUPPORTED_SHELLS . powerFormat ) . map ( async shell => {
114
114
const runnable = await this . _isRunnable ( shell )
115
115
return ( {
116
116
shell,
117
117
runnable,
118
- profiles : SUPPORTED_SHELLS . powerFormat [ shell ]
118
+ profiles : this . SUPPORTED_SHELLS . powerFormat [ shell ]
119
119
} )
120
120
} )
121
121
122
122
; ( await Promise . all ( shellFormat ) ) . filter ( res => res . runnable ) . forEach ( sh => {
123
- detectedShellFormatProfiles . add ( ... sh . profiles )
123
+ sh . profiles . forEach ( profile => detectedShellFormatProfiles . add ( profile ) )
124
124
} )
125
125
; ( await Promise . all ( powerFormat ) ) . filter ( res => res . runnable ) . forEach ( sh => {
126
- detectedPowerFormatProfiles . add ( ... sh . profiles )
126
+ sh . profiles . forEach ( profile => detectedPowerFormatProfiles . add ( profile ) )
127
127
} )
128
128
129
129
return {
0 commit comments