|
| 1 | +// jshint -W053 |
| 2 | +// Ignore warning about 'new String()' |
| 3 | +'use strict'; |
| 4 | + |
1 | 5 | var os = require('os'); |
2 | 6 | var fs = require('fs'); |
3 | 7 | var glob = require('glob'); |
| 8 | +var _to = require('./to'); |
| 9 | +var _toEnd = require('./toEnd'); |
4 | 10 |
|
5 | 11 | // Module globals |
6 | 12 | var config = { |
@@ -46,11 +52,26 @@ function error(msg, _continue) { |
46 | 52 | } |
47 | 53 | exports.error = error; |
48 | 54 |
|
49 | | -// In the future, when Proxies are default, we can add methods like `.to()` to primitive strings. |
50 | | -// For now, this is a dummy function to bookmark places we need such strings |
51 | | -function ShellString(str) { |
52 | | - return str; |
53 | | -} |
| 55 | +//@ |
| 56 | +//@ ### ShellString(str) |
| 57 | +//@ |
| 58 | +//@ Examples: |
| 59 | +//@ |
| 60 | +//@ ``` |
| 61 | +//@ var foo = ShellString('hello world'); |
| 62 | +//@ ``` |
| 63 | +//@ |
| 64 | +//@ Turns a regular string into a string-like object similar to what each |
| 65 | +//@ command returns. This has special methods, like `.to()` and `.toEnd()` |
| 66 | +var ShellString = function (str, stderr) { |
| 67 | + var that = new String(str); |
| 68 | + that.to = wrap('to', _to, {idx: 1}); |
| 69 | + that.toEnd = wrap('toEnd', _toEnd, {idx: 1}); |
| 70 | + that.stdout = str; |
| 71 | + that.stderr = stderr; |
| 72 | + return that; |
| 73 | +}; |
| 74 | + |
54 | 75 | exports.ShellString = ShellString; |
55 | 76 |
|
56 | 77 | // Return the home directory in a platform-agnostic way, with consideration for |
@@ -225,6 +246,13 @@ function wrap(cmd, fn, options) { |
225 | 246 | return accum; |
226 | 247 | } |
227 | 248 | }, []); |
| 249 | + // Convert ShellStrings to regular strings |
| 250 | + args = args.map(function(arg) { |
| 251 | + if (arg.constructor.name === 'String') { |
| 252 | + return arg.toString(); |
| 253 | + } else |
| 254 | + return arg; |
| 255 | + }); |
228 | 256 | // Expand the '~' if appropriate |
229 | 257 | var homeDir = getUserHome(); |
230 | 258 | args = args.map(function(arg) { |
|
0 commit comments