Skip to content

Commit 9218cc7

Browse files
committed
revamp path checks into own module
1 parent 4523e5f commit 9218cc7

File tree

4 files changed

+39
-20
lines changed

4 files changed

+39
-20
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

+17-16
Original file line numberDiff line numberDiff line change
@@ -20,37 +20,38 @@ const makeDir = require('make-dir')
2020
* respective profiles updated with the source command.
2121
*/
2222
class Profile {
23-
constructor ({ env, runlog }) {
23+
constructor ({ env, runlog, pathChecks }) {
2424
this.env = env
25+
this.pathChecks = pathChecks
2526
this.runlog = runlog
2627
// shells and an appropriate config file to add our source line to
2728
// ref: https://en.wikipedia.org/wiki/Unix_shell#Configuration_files
2829
this.SUPPORTED_SHELLS = {
2930
shellFormat: {
3031
// only support shells that support functions
3132
// ref: https://web.archive.org/web/20160403120601/http://www.unixnote.com/2010/05/different-unix-shell.html
32-
sh: [this.inHome('.profile')],
33-
ksh: [this.inHome('.kshrc')],
34-
zsh: [this.inHome('.zshrc'), this.inHome('.profile'), this.inHome('.zprofile')],
35-
bash: [this.inHome('.bashrc'), this.inHome('.profile'), this.inHome('.bash_profile')]
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+
]
3645
},
3746
powerFormat: {
3847
pwsh: os.platform() !== 'win32' // pwsh stores its profile in different places depending on the OS
39-
? [this.inConfig('powershell', 'Microsoft.PowerShell_profile.ps1')]
40-
: [this.inHome('Documents', 'PowerShell', 'Microsoft.PowerShell_profile.ps1')],
41-
'powershell.exe': [this.inHome('Documents', 'WindowsPowerShell', 'Microsoft.PowerShell_profile.ps1')]
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')]
4251
}
4352
}
4453
}
4554

46-
inHome (...filepaths) {
47-
return path.join(os.homedir(), ...filepaths)
48-
}
49-
50-
inConfig (...filepaths) {
51-
return this.inHome('.config', ...filepaths)
52-
}
53-
5455
async installToProfiles () {
5556
return this._updateProfiles({ install: true })
5657
}

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

+5-3
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@ test.beforeEach((t) => {
1414
record: sinon.stub(),
1515
keys: {}
1616
}
17-
Profile.prototype.inHome = (...args) => args.pop()
18-
Profile.prototype.inConfig = (...args) => args.pop()
19-
t.context.profile = new Profile({ config, runlog })
17+
const pathChecks = {
18+
inHome: (...args) => args.pop(),
19+
inConfig: (...args) => args.pop()
20+
}
21+
t.context.profile = new Profile({ config, runlog, pathChecks })
2022
})
2123

2224
test('writes to all shell and power profiles for system with all shells and power available', async (t) => {

0 commit comments

Comments
 (0)