Skip to content

Commit defbfe0

Browse files
authored
Merge pull request #78 from flossbank/bashProfile
Bash profile
2 parents 4fa4a1a + 9218cc7 commit defbfe0

File tree

4 files changed

+94
-35
lines changed

4 files changed

+94
-35
lines changed

src/index.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const Profile = require('./profile')
1111
const TempWriter = require('./util/temp')
1212
const UpdateController = require('./update')
1313
const Runlog = require('./util/runlog')
14+
const PathChecks = require('./util/pathChecks')
1415

1516
const app = require('./app')
1617

@@ -33,7 +34,8 @@ function main () {
3334

3435
const alias = new Alias({ config, pm })
3536
const env = new Env({ config, alias })
36-
const profile = new Profile({ env, runlog })
37+
const pathChecks = new PathChecks()
38+
const profile = new Profile({ env, runlog, pathChecks })
3739

3840
const ui = new Ui({ config, runlog, client, stdout: process.stdout })
3941
const args = new Args({ client, update, ui, config, alias, env, profile, runlog })

src/profile/index.js

+34-34
Original file line numberDiff line numberDiff line change
@@ -13,43 +13,43 @@ const os = require('os')
1313
const path = require('path')
1414
const makeDir = require('make-dir')
1515

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-
4316
/**
4417
* Profile is responsible for detecting appropriate shell profile files and appending/removing the
4518
* source command to/from those shell profiles. Profile attempts to run many different shell variants
4619
* to determine the appropriate paths to write to. Shells which are runnable are eligible to have their
4720
* respective profiles updated with the source command.
4821
*/
4922
class Profile {
50-
constructor ({ env, runlog }) {
23+
constructor ({ env, runlog, pathChecks }) {
5124
this.env = env
25+
this.pathChecks = pathChecks
5226
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+
}
5353
}
5454

5555
async installToProfiles () {
@@ -102,28 +102,28 @@ class Profile {
102102
const detectedShellFormatProfiles = new Set()
103103
const detectedPowerFormatProfiles = new Set()
104104

105-
const shellFormat = Object.keys(SUPPORTED_SHELLS.shellFormat).map(async shell => {
105+
const shellFormat = Object.keys(this.SUPPORTED_SHELLS.shellFormat).map(async shell => {
106106
const runnable = await this._isRunnable(shell)
107107
return ({
108108
shell,
109109
runnable,
110-
profiles: SUPPORTED_SHELLS.shellFormat[shell]
110+
profiles: this.SUPPORTED_SHELLS.shellFormat[shell]
111111
})
112112
})
113-
const powerFormat = Object.keys(SUPPORTED_SHELLS.powerFormat).map(async shell => {
113+
const powerFormat = Object.keys(this.SUPPORTED_SHELLS.powerFormat).map(async shell => {
114114
const runnable = await this._isRunnable(shell)
115115
return ({
116116
shell,
117117
runnable,
118-
profiles: SUPPORTED_SHELLS.powerFormat[shell]
118+
profiles: this.SUPPORTED_SHELLS.powerFormat[shell]
119119
})
120120
})
121121

122122
;(await Promise.all(shellFormat)).filter(res => res.runnable).forEach(sh => {
123-
detectedShellFormatProfiles.add(...sh.profiles)
123+
sh.profiles.forEach(profile => detectedShellFormatProfiles.add(profile))
124124
})
125125
;(await Promise.all(powerFormat)).filter(res => res.runnable).forEach(sh => {
126-
detectedPowerFormatProfiles.add(...sh.profiles)
126+
sh.profiles.forEach(profile => detectedPowerFormatProfiles.add(profile))
127127
})
128128

129129
return {

src/util/pathChecks.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const os = require('os')
2+
const path = require('path')
3+
4+
class PathChecks {
5+
inHome (...filepaths) {
6+
return path.join(os.homedir(), ...filepaths)
7+
}
8+
9+
inConfig (...filepaths) {
10+
return this.inHome('.config', ...filepaths)
11+
}
12+
}
13+
14+
module.exports = PathChecks

test/profile/index.test.js

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
2+
const test = require('ava')
3+
const sinon = require('sinon')
4+
const Profile = require('../../src/profile')
5+
6+
test.beforeEach((t) => {
7+
const config = {
8+
getApiHost: () => 'http://localhost',
9+
getApiKey: () => 'api_key'
10+
}
11+
const runlog = {
12+
error: sinon.stub(),
13+
debug: sinon.stub(),
14+
record: sinon.stub(),
15+
keys: {}
16+
}
17+
const pathChecks = {
18+
inHome: (...args) => args.pop(),
19+
inConfig: (...args) => args.pop()
20+
}
21+
t.context.profile = new Profile({ config, runlog, pathChecks })
22+
})
23+
24+
test('writes to all shell and power profiles for system with all shells and power available', async (t) => {
25+
const { profile } = t.context
26+
profile._isRunnable = sinon.stub().returns(true)
27+
28+
const {
29+
detectedShellFormatProfiles,
30+
detectedPowerFormatProfiles
31+
} = await profile._detectProfiles()
32+
t.deepEqual([
33+
'.profile',
34+
'.kshrc',
35+
'.zshrc',
36+
'.zprofile',
37+
'.bashrc',
38+
'.bash_profile'
39+
], detectedShellFormatProfiles)
40+
t.deepEqual([
41+
'Microsoft.PowerShell_profile.ps1'
42+
], detectedPowerFormatProfiles)
43+
})

0 commit comments

Comments
 (0)