-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
102 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
'use strict'; | ||
|
||
var homedir = require('homedir-polyfill'); | ||
var isAbsolute = require('is-absolute'); | ||
var removeTrailingSep = require('remove-trailing-separator'); | ||
|
||
function replaceHomedir(filepath, replacement) { | ||
if (typeof filepath !== 'string') { | ||
throw new Error('Path for replace-homedir must be a string.'); | ||
} | ||
|
||
if (!isAbsolute(filepath)) { | ||
return filepath; | ||
} | ||
|
||
var home = removeTrailingSep(homedir()); | ||
var lookupHome = home + '/'; | ||
var lookupPath = removeTrailingSep(filepath) + '/'; | ||
|
||
if (lookupPath.indexOf(lookupHome) !== 0) { | ||
return filepath; | ||
} | ||
|
||
return filepath.replace(home, replacement); | ||
} | ||
|
||
module.exports = replaceHomedir; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
'use strict'; | ||
|
||
var path = require('path'); | ||
|
||
var expect = require('expect'); | ||
var homedir = require('homedir-polyfill'); | ||
|
||
var replaceHomedir = require('../'); | ||
|
||
describe('replace-homedir', function() { | ||
|
||
var home = homedir(); | ||
var here = __dirname.replace(home, ''); | ||
|
||
it('throws if path is not a string', function(done) { | ||
function invalidPath() { | ||
replaceHomedir(null); | ||
} | ||
|
||
expect(invalidPath).toThrow('Path for replace-homedir must be a string.'); | ||
done(); | ||
}); | ||
|
||
it('replaces the homedir with replacement string', function(done) { | ||
var result = replaceHomedir(__dirname, '~'); | ||
var expected = path.join('~', here); | ||
expect(result).toEqual(expected); | ||
done(); | ||
}); | ||
|
||
it('replaces the homedir with replacement function', function(done) { | ||
var result = replaceHomedir(__dirname, function() { | ||
return '~'; | ||
}); | ||
var expected = path.join('~', here); | ||
expect(result).toEqual(expected); | ||
done(); | ||
}); | ||
|
||
it('does not replace the homedir if not at beginning of path', function(done) { | ||
var filepath = path.join('/fake', __dirname); | ||
var result = replaceHomedir(filepath, '~'); | ||
var expected = path.join('/fake', __dirname); | ||
expect(result).toEqual(expected); | ||
done(); | ||
}); | ||
|
||
it('does not replace homedir when it is a subpath', function(done) { | ||
var filepath = path.join(home + '-extended', here); | ||
var result = replaceHomedir(filepath, '~'); | ||
var expected = path.join(home + '-extended', here); | ||
expect(result).toEqual(expected); | ||
done(); | ||
}); | ||
|
||
it('ignores non-absolute paths', function(done) { | ||
var USERPROFILE = process.env.USERPROFILE; | ||
var HOME = process.env.HOME; | ||
process.env.USERPROFILE = process.env.HOME = home.slice(1); | ||
var filepath = __dirname.slice(1); | ||
var result = replaceHomedir(filepath, '~'); | ||
var expected = __dirname.slice(1); | ||
expect(result).toEqual(expected); | ||
done(); | ||
|
||
process.env.USERPROFILE = USERPROFILE; | ||
process.env.HOME = HOME; | ||
}); | ||
|
||
}); |