GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Merge pull request #293 from nfischer/feat-home-directory-tilde · shelljs/shelljs@c422069

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • src
    • common.js
  • test
    • cd.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/common.js‎

Copy file name to clipboardExpand all lines: src/common.js
+20Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,18 @@ function ShellString(str) {
5454
}
5555
exports.ShellString = ShellString;
5656

57+
// Return the home directory in a platform-agnostic way, with consideration for
58+
// older versions of node
59+
function getUserHome() {
60+
var result;
61+
if (os.homedir)
62+
result = os.homedir(); // node 3+
63+
else
64+
result = process.env[(process.platform === 'win32') ? 'USERPROFILE' : 'HOME'];
65+
return result;
66+
}
67+
exports.getUserHome = getUserHome;
68+
5769
// Returns {'alice': true, 'bob': false} when passed a dictionary, e.g.:
5870
// parseOptions('-a', {'a':'alice', 'b':'bob'});
5971
function parseOptions(str, map) {
@@ -188,6 +200,14 @@ function wrap(cmd, fn, options) {
188200
} else {
189201
if (args.length === 0 || typeof args[0] !== 'string' || args[0].length <= 1 || args[0][0] !== '-')
190202
args.unshift(''); // only add dummy option if '-option' not already present
203+
// Expand the '~' if appropriate
204+
var homeDir = getUserHome();
205+
args = args.map(function(arg) {
206+
if (typeof arg === 'string' && arg.slice(0, 2) === '~/' || arg === '~')
207+
return arg.replace(/^~/, homeDir);
208+
else
209+
return arg;
210+
});
191211
retValue = fn.apply(this, args);
192212
}
193213
} catch (e) {
Collapse file

‎test/cd.js‎

Copy file name to clipboardExpand all lines: test/cd.js
+10-1Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ var shell = require('..');
22

33
var assert = require('assert'),
44
path = require('path'),
5-
fs = require('fs');
5+
fs = require('fs'),
6+
common = require('../src/common');
67

78
shell.config.silent = true;
89

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

67+
// Test tilde expansion
68+
69+
shell.cd('~');
70+
assert.equal(process.cwd(), common.getUserHome());
71+
shell.cd('..');
72+
shell.cd('~'); // Change back to home
73+
assert.equal(process.cwd(), common.getUserHome());
74+
6675
shell.exit(123);

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.