diff --git a/src/cat.js b/src/cat.js index 5469cd0..3d271ce 100644 --- a/src/cat.js +++ b/src/cat.js @@ -1,7 +1,11 @@ var common = require('./common'); var fs = require('fs'); -common.register('cat', _cat, {globStart: 1, canReceivePipe: true}); +common.register('cat', _cat, { + globStart: 1, + canReceivePipe: true, + wrapOutput: true, +}); //@ //@ ### cat(file [, file ...]) @@ -33,6 +37,6 @@ function _cat(options, files) { cat += fs.readFileSync(file, 'utf8'); }); - return new common.ShellString(cat, common.state.error, common.state.errorCode); + return cat; } module.exports = _cat; diff --git a/src/cd.js b/src/cd.js index 0b6ef53..50694a8 100644 --- a/src/cd.js +++ b/src/cd.js @@ -1,7 +1,10 @@ var fs = require('fs'); var common = require('./common'); -common.register('cd', _cd, {globStart: 1}); +common.register('cd', _cd, { + globStart: 1, + wrapOutput: true, +}); //@ //@ ### cd([dir]) @@ -33,6 +36,6 @@ function _cd(options, dir) { } if (err) common.error(err); } - return new common.ShellString('', common.state.error, common.state.errorCode); + return ''; } module.exports = _cd; diff --git a/src/chmod.js b/src/chmod.js index 16588f7..7072bfe 100644 --- a/src/chmod.js +++ b/src/chmod.js @@ -30,7 +30,10 @@ var PERMS = (function (base) { READ : 4 }); -common.register('chmod', _chmod, {globStart: 1}); +common.register('chmod', _chmod, { + globStart: 1, + wrapOutput: true, +}); //@ //@ ### chmod(octal_mode || octal_string, file) @@ -210,6 +213,6 @@ function _chmod(options, mode, filePattern) { fs.chmodSync(file, newPerms); } }); - return new common.ShellString('', common.state.error, common.state.errorCode); + return ''; } module.exports = _chmod; diff --git a/src/common.js b/src/common.js index a154431..6b2255a 100644 --- a/src/common.js +++ b/src/common.js @@ -343,6 +343,11 @@ function wrap(cmd, fn, options) { throw e; } + if (options.wrapOutput && + (typeof retValue === 'string' || Array.isArray(retValue))) { + retValue = new ShellString(retValue, state.error, state.errorCode); + } + state.currentCmd = 'shell.js'; return retValue; }; diff --git a/src/echo.js b/src/echo.js index 0f53ea8..28066f6 100644 --- a/src/echo.js +++ b/src/echo.js @@ -1,6 +1,6 @@ var common = require('./common'); -common.register('echo', _echo); +common.register('echo', _echo, {wrapOutput: true}); //@ //@ ### echo(string [, string ...]) @@ -18,6 +18,6 @@ function _echo(opts, messages) { // allow strings starting with '-', see issue #20 messages = [].slice.call(arguments, opts ? 0 : 1); console.log.apply(console, messages); - return new common.ShellString(messages.join(' '), '', 0); + return messages.join(' '); } module.exports = _echo; diff --git a/src/find.js b/src/find.js index a13e23e..8e9bad0 100644 --- a/src/find.js +++ b/src/find.js @@ -3,7 +3,10 @@ var path = require('path'); var common = require('./common'); var _ls = require('./ls'); -common.register('find', _find, {globStart: 1}); +common.register('find', _find, { + globStart: 1, + wrapOutput: true, +}); //@ //@ ### find(path [, path ...]) @@ -48,6 +51,6 @@ function _find(options, paths) { } }); - return new common.ShellString(list, common.state.error, common.state.errorCode); + return list; } module.exports = _find; diff --git a/src/grep.js b/src/grep.js index 09787e6..e9987b8 100644 --- a/src/grep.js +++ b/src/grep.js @@ -8,6 +8,7 @@ common.register('grep', _grep, { 'v': 'inverse', 'l': 'nameOnly', }, + wrapOutput: true, }); //@ @@ -60,6 +61,6 @@ function _grep(options, regex, files) { } }); - return new common.ShellString(grep.join('\n')+'\n', common.state.error, common.state.errorCode); + return grep.join('\n')+'\n'; } module.exports = _grep; diff --git a/src/head.js b/src/head.js index baf7972..37c92a3 100644 --- a/src/head.js +++ b/src/head.js @@ -7,6 +7,7 @@ common.register('head', _head, { cmdOptions: { 'n': 'numLines', }, + wrapOutput: true, }); // This reads n or more lines, or the entire file, whichever is less. @@ -98,6 +99,6 @@ function _head(options, files) { if (shouldAppendNewline) head.push(''); // to add a trailing newline once we join - return new common.ShellString(head.join('\n'), common.state.error, common.state.errorCode); + return head.join('\n'); } module.exports = _head; diff --git a/src/ln.js b/src/ln.js index 6b11fc4..6dc1a26 100644 --- a/src/ln.js +++ b/src/ln.js @@ -8,6 +8,7 @@ common.register('ln', _ln, { 's': 'symlink', 'f': 'force', }, + wrapOutput: true, }); //@ @@ -68,6 +69,6 @@ function _ln(options, source, dest) { common.error(err.message); } } - return new common.ShellString('', common.state.error, common.state.errorCode); + return ''; } module.exports = _ln; diff --git a/src/ls.js b/src/ls.js index d39506a..e9b1137 100644 --- a/src/ls.js +++ b/src/ls.js @@ -14,9 +14,9 @@ common.register('ls', _ls, { 'd': 'directory', 'l': 'long', }, + wrapOutput: true, }); - //@ //@ ### ls([options,] [path, ...]) //@ ### ls([options,] path_array) @@ -104,7 +104,7 @@ function _ls(options, paths) { }); // Add methods, to make this more compatible with ShellStrings - return new common.ShellString(list, common.state.error, common.state.errorCode); + return list; } function addLsAttributes(path, stats) { diff --git a/src/mkdir.js b/src/mkdir.js index 7a8479c..9fa30a4 100644 --- a/src/mkdir.js +++ b/src/mkdir.js @@ -7,6 +7,7 @@ common.register('mkdir', _mkdir, { cmdOptions: { 'p': 'fullpath', }, + wrapOutput: true, }); // Recursively creates 'dir' @@ -85,6 +86,6 @@ function _mkdir(options, dirs) { throw e; } }); - return new common.ShellString('', common.state.error, common.state.errorCode); + return ''; } // mkdir module.exports = _mkdir; diff --git a/src/mv.js b/src/mv.js index 48f8451..38244c8 100644 --- a/src/mv.js +++ b/src/mv.js @@ -10,6 +10,7 @@ common.register('mv', _mv, { 'f': '!no_force', 'n': 'no_force', }, + wrapOutput: true, }); //@ @@ -88,6 +89,6 @@ function _mv(options, sources, dest) { } } }); // forEach(src) - return new common.ShellString('', common.state.error, common.state.errorCode); + return ''; } // mv module.exports = _mv; diff --git a/src/pwd.js b/src/pwd.js index f94373b..c10355e 100644 --- a/src/pwd.js +++ b/src/pwd.js @@ -1,13 +1,13 @@ var path = require('path'); var common = require('./common'); -common.register('pwd', _pwd); +common.register('pwd', _pwd, {wrapOutput: true}); //@ //@ ### pwd() //@ Returns the current directory. function _pwd() { var pwd = path.resolve(process.cwd()); - return new common.ShellString(pwd, '', common.state.errorCode); + return pwd; } module.exports = _pwd; diff --git a/src/rm.js b/src/rm.js index b4929c4..68f571f 100644 --- a/src/rm.js +++ b/src/rm.js @@ -8,6 +8,7 @@ common.register('rm', _rm, { 'r': 'recursive', 'R': 'recursive', }, + wrapOutput: true, }); // Recursively removes 'dir' @@ -149,6 +150,6 @@ function _rm(options, files) { rmdirSyncRecursive(file, options.force); } }); // forEach(file) - return new common.ShellString('', common.state.error, common.state.errorCode); + return ''; } // rm module.exports = _rm; diff --git a/src/sed.js b/src/sed.js index 7a55bc2..d78481f 100644 --- a/src/sed.js +++ b/src/sed.js @@ -7,6 +7,7 @@ common.register('sed', _sed, { cmdOptions: { 'i': 'inplace', }, + wrapOutput: true, }); //@ @@ -67,6 +68,6 @@ function _sed(options, regex, replacement, files) { fs.writeFileSync(file, result, 'utf8'); }); - return new common.ShellString(sed.join('\n'), common.state.error, common.state.errorCode); + return sed.join('\n'); } module.exports = _sed; diff --git a/src/sort.js b/src/sort.js index a67742d..761f477 100644 --- a/src/sort.js +++ b/src/sort.js @@ -7,10 +7,10 @@ common.register('sort', _sort, { cmdOptions: { 'r': 'reverse', 'n': 'numerical', - } + }, + wrapOutput: true, }); - // parse out the number prefix of a line function parseNumber (str) { var match = str.match(/^\s*(\d*)\s*(.*)$/); @@ -86,7 +86,7 @@ function _sort(options, files) { if (options.reverse) sorted = sorted.reverse(); - return new common.ShellString(sorted.join('\n')+'\n', common.state.error, common.state.errorCode); + return sorted.join('\n')+'\n'; } module.exports = _sort; diff --git a/src/tail.js b/src/tail.js index e6a5948..84697d1 100644 --- a/src/tail.js +++ b/src/tail.js @@ -7,6 +7,7 @@ common.register('tail', _tail, { cmdOptions: { 'n': 'numLines', }, + wrapOutput: true, }); //@ @@ -67,6 +68,6 @@ function _tail(options, files) { if (shouldAppendNewline) tail.push(''); // to add a trailing newline once we join - return new common.ShellString(tail.join('\n'), common.state.error, common.state.errorCode); + return tail.join('\n'); } module.exports = _tail; diff --git a/src/touch.js b/src/touch.js index 0eab448..2440507 100644 --- a/src/touch.js +++ b/src/touch.js @@ -10,6 +10,7 @@ common.register('touch', _touch, { 'm': 'mtime_only', 'r': 'reference', }, + wrapOutput: true, }); //@ @@ -45,7 +46,7 @@ function _touch(opts, files) { files.forEach(function(f) { touchFile(opts, f); }); - return new common.ShellString('', common.state.error, common.state.errorCode); + return ''; } function touchFile(opts, file) { diff --git a/src/uniq.js b/src/uniq.js index 930c6b9..3072946 100644 --- a/src/uniq.js +++ b/src/uniq.js @@ -18,6 +18,7 @@ common.register('uniq', _uniq, { 'c': 'count', 'd': 'duplicates', }, + wrapOutput: true, }); //@ @@ -67,13 +68,12 @@ function _uniq(options, input, output) { return (options.count ? (lpad(7,obj.count) + ' ') : '') + obj.ln; }).join('\n') + '\n'; - var res = new common.ShellString(uniqed, common.state.error, common.state.errorCode); if(output){ - res.to(output); + (new common.ShellString(uniqed)).to(output); //if uniq writes to output, nothing is passed to the next command in the pipeline (if any) - return new common.ShellString('', common.state.error, common.state.errorCode); + return ''; }else{ - return res; + return uniqed; } } diff --git a/src/which.js b/src/which.js index e7234e3..bfd5cd3 100644 --- a/src/which.js +++ b/src/which.js @@ -2,7 +2,7 @@ var common = require('./common'); var fs = require('fs'); var path = require('path'); -common.register('which', _which); +common.register('which', _which, {wrapOutput: true}); // XP's system default value for PATHEXT system variable, just in case it's not // set on Windows. @@ -95,6 +95,6 @@ function _which(options, cmd) { where = where || path.resolve(cmd); - return new common.ShellString(where, '', common.state.errorCode); + return where; } module.exports = _which;