Skip to content

Commit

Permalink
Merge pull request #293 from nfischer/feat-home-directory-tilde
Browse files Browse the repository at this point in the history
feat: add tilde expansion to expand()
  • Loading branch information
ariporad committed Jan 24, 2016
2 parents 12c47e3 + b3f2664 commit c422069
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
20 changes: 20 additions & 0 deletions src/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,18 @@ function ShellString(str) {
}
exports.ShellString = ShellString;

// Return the home directory in a platform-agnostic way, with consideration for
// older versions of node
function getUserHome() {
var result;
if (os.homedir)
result = os.homedir(); // node 3+
else
result = process.env[(process.platform === 'win32') ? 'USERPROFILE' : 'HOME'];
return result;
}
exports.getUserHome = getUserHome;

// Returns {'alice': true, 'bob': false} when passed a dictionary, e.g.:
// parseOptions('-a', {'a':'alice', 'b':'bob'});
function parseOptions(str, map) {
Expand Down Expand Up @@ -188,6 +200,14 @@ function wrap(cmd, fn, options) {
} else {
if (args.length === 0 || typeof args[0] !== 'string' || args[0].length <= 1 || args[0][0] !== '-')
args.unshift(''); // only add dummy option if '-option' not already present
// Expand the '~' if appropriate
var homeDir = getUserHome();
args = args.map(function(arg) {
if (typeof arg === 'string' && arg.slice(0, 2) === '~/' || arg === '~')
return arg.replace(/^~/, homeDir);
else
return arg;
});
retValue = fn.apply(this, args);
}
} catch (e) {
Expand Down
11 changes: 10 additions & 1 deletion test/cd.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ var shell = require('..');

var assert = require('assert'),
path = require('path'),
fs = require('fs');
fs = require('fs'),
common = require('../src/common');

shell.config.silent = true;

Expand Down Expand Up @@ -63,4 +64,12 @@ shell.cd('../tmp');
assert.equal(shell.error(), null);
assert.equal(fs.existsSync('file1'), true);

// Test tilde expansion

shell.cd('~');
assert.equal(process.cwd(), common.getUserHome());
shell.cd('..');
shell.cd('~'); // Change back to home
assert.equal(process.cwd(), common.getUserHome());

shell.exit(123);

0 comments on commit c422069

Please sign in to comment.