Skip to content

Commit d0b7d09

Browse files
committed
Merge pull request shelljs#360 from shelljs/refactor-shellstring
refactor(ShellString): refactor shellstring
2 parents 14d518c + 2978855 commit d0b7d09

28 files changed

+195
-93
lines changed

README.md

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -331,8 +331,9 @@ Examples:
331331
cat('input.txt').to('output.txt');
332332
```
333333

334-
Analogous to the redirection operator `>` in Unix, but works with JavaScript strings (such as
335-
those returned by `cat`, `grep`, etc). _Like Unix redirections, `to()` will overwrite any existing file!_
334+
Analogous to the redirection operator `>` in Unix, but works with
335+
ShellStrings (such as those returned by `cat`, `grep`, etc). _Like Unix
336+
redirections, `to()` will overwrite any existing file!_
336337

337338

338339
### 'string'.toEnd(file)
@@ -343,8 +344,8 @@ Examples:
343344
cat('input.txt').toEnd('output.txt');
344345
```
345346

346-
Analogous to the redirect-and-append operator `>>` in Unix, but works with JavaScript strings (such as
347-
those returned by `cat`, `grep`, etc).
347+
Analogous to the redirect-and-append operator `>>` in Unix, but works with
348+
ShellStrings (such as those returned by `cat`, `grep`, etc).
348349

349350

350351
### sed([options,] search_regex, replacement, file [, file ...])
@@ -614,6 +615,18 @@ Tests if error occurred in the last command. Returns `null` if no error occurred
614615
otherwise returns string explaining the error
615616

616617

618+
### ShellString(str)
619+
620+
Examples:
621+
622+
```
623+
var foo = ShellString('hello world');
624+
```
625+
626+
Turns a regular string into a string-like object similar to what each
627+
command returns. This has special methods, like `.to()` and `.toEnd()`
628+
629+
617630
## Configuration
618631

619632

scripts/generate-docs.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/usr/bin/env node
2-
/* globals cat, cd, echo, grep, sed */
2+
/* globals cat, cd, echo, grep, sed, ShellString */
33
require('../global');
44

55
echo('Appending docs to README.md');
@@ -18,7 +18,7 @@ docs = docs.replace(/\/\/\@include (.+)/g, function(match, path) {
1818
docs = docs.replace(/\/\/\@ ?/g, '');
1919

2020
// Wipe out the old docs
21-
cat('README.md').replace(/## Command reference(.|\n)*/, '## Command reference').to('README.md');
21+
ShellString(cat('README.md').replace(/## Command reference(.|\n)*/, '## Command reference')).to('README.md');
2222

2323
// Append new docs to README
2424
sed('-i', /## Command reference/, '## Command reference\n\n' + docs, 'README.md');

shell.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,10 @@ exports.test = common.wrap('test', _test);
5353
var _cat = require('./src/cat');
5454
exports.cat = common.wrap('cat', _cat, {idx: 1});
5555

56+
// The below commands have been moved to common.ShellString(), and are only here
57+
// for generating the docs
5658
//@include ./src/to
57-
var _to = require('./src/to');
58-
String.prototype.to = common.wrap('to', _to, {idx: 1});
59-
6059
//@include ./src/toEnd
61-
var _toEnd = require('./src/toEnd');
62-
String.prototype.toEnd = common.wrap('toEnd', _toEnd, {idx: 1});
6360

6461
//@include ./src/sed
6562
var _sed = require('./src/sed');
@@ -124,11 +121,12 @@ exports.set = common.wrap('set', _set);
124121
var _tempDir = require('./src/tempdir');
125122
exports.tempdir = common.wrap('tempdir', _tempDir);
126123

127-
128124
//@include ./src/error
129125
var _error = require('./src/error');
130126
exports.error = _error;
131127

128+
//@include ./src/common
129+
exports.ShellString = common.ShellString;
132130

133131

134132
//@

src/cat.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,6 @@ function _cat(options, files) {
3333
cat += fs.readFileSync(file, 'utf8');
3434
});
3535

36-
return common.ShellString(cat);
36+
return new common.ShellString(cat, common.state.error);
3737
}
3838
module.exports = _cat;

src/common.js

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1+
// jshint -W053
2+
// Ignore warning about 'new String()'
3+
'use strict';
4+
15
var os = require('os');
26
var fs = require('fs');
37
var glob = require('glob');
8+
var _to = require('./to');
9+
var _toEnd = require('./toEnd');
410

511
// Module globals
612
var config = {
@@ -46,11 +52,26 @@ function error(msg, _continue) {
4652
}
4753
exports.error = error;
4854

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+
5475
exports.ShellString = ShellString;
5576

5677
// Return the home directory in a platform-agnostic way, with consideration for
@@ -225,6 +246,13 @@ function wrap(cmd, fn, options) {
225246
return accum;
226247
}
227248
}, []);
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+
});
228256
// Expand the '~' if appropriate
229257
var homeDir = getUserHome();
230258
args = args.map(function(arg) {

src/echo.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ function _echo(opts, messages) {
1616
// allow strings starting with '-', see issue #20
1717
messages = [].slice.call(arguments, opts ? 0 : 1);
1818
console.log.apply(console, messages);
19-
return common.ShellString(messages.join(' '));
19+
return new common.ShellString(messages.join(' '));
2020
}
2121
module.exports = _echo;

src/exec.js

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ function execSync(cmd, opts) {
2222

2323
opts = common.extend({
2424
silent: common.config.silent,
25-
cwd: _pwd(),
25+
cwd: _pwd().toString(),
2626
env: process.env,
2727
maxBuffer: DEFAULT_MAXBUFFER_SIZE
2828
}, opts);
@@ -155,13 +155,10 @@ function execSync(cmd, opts) {
155155
if (code !== 0) {
156156
common.error('', true);
157157
}
158-
// True if successful, false if not
159-
var obj = {
160-
code: code,
161-
output: stdout, // deprecated
162-
stdout: stdout,
163-
stderr: stderr
164-
};
158+
var obj = common.ShellString(stdout, stderr);
159+
// obj.stdout = stdout;
160+
// obj.stderr = stderr;
161+
obj.code = code;
165162
return obj;
166163
} // execSync()
167164

@@ -172,7 +169,7 @@ function execAsync(cmd, opts, callback) {
172169

173170
opts = common.extend({
174171
silent: common.config.silent,
175-
cwd: _pwd(),
172+
cwd: _pwd().toString(),
176173
env: process.env,
177174
maxBuffer: DEFAULT_MAXBUFFER_SIZE
178175
}, opts);

src/grep.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,6 @@ function _grep(options, regex, files) {
5151
}
5252
});
5353

54-
return common.ShellString(grep.join('\n')+'\n');
54+
return new common.ShellString(grep.join('\n')+'\n', common.state.error);
5555
}
5656
module.exports = _grep;

src/ls.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ var fs = require('fs');
33
var common = require('./common');
44
var _cd = require('./cd');
55
var _pwd = require('./pwd');
6+
var _to = require('./to');
7+
var _toEnd = require('./toEnd');
68

79
//@
810
//@ ### ls([options,] [path, ...])
@@ -104,7 +106,7 @@ function _ls(options, paths) {
104106

105107
// Recursive?
106108
if (options.recursive) {
107-
var oldDir = _pwd();
109+
var oldDir = _pwd().toString();
108110
_cd('', p);
109111
if (fs.statSync(orig_file).isDirectory())
110112
list = list.concat(_ls('-R'+(options.all?'A':''), orig_file+'/*'));
@@ -151,6 +153,15 @@ function _ls(options, paths) {
151153
common.error('no such file or directory: ' + p, true);
152154
});
153155

156+
// Add methods, to make this more compatible with ShellStrings
157+
list.stdout = list.join('\n') + '\n';
158+
list.stderr = common.state.error;
159+
list.to = function (file) {
160+
common.wrap('to', _to, {idx: 1}).call(this.stdout, file);
161+
};
162+
list.toEnd = function(file) {
163+
common.wrap('toEnd', _toEnd, {idx: 1}).call(this.stdout, file);
164+
};
154165
return list;
155166
}
156167
module.exports = _ls;

src/pwd.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ var common = require('./common');
66
//@ Returns the current directory.
77
function _pwd() {
88
var pwd = path.resolve(process.cwd());
9-
return common.ShellString(pwd);
9+
return new common.ShellString(pwd);
1010
}
1111
module.exports = _pwd;

0 commit comments

Comments
 (0)