From 305dff0df26cbcd76493373ec201bc1f0f7e7aa0 Mon Sep 17 00:00:00 2001 From: Artur Adib Date: Sun, 25 Aug 2013 16:16:11 -0400 Subject: [PATCH] cat --- shell.js | 25 +------------------------ src/cat.js | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 24 deletions(-) create mode 100644 src/cat.js diff --git a/shell.js b/shell.js index 4dde6f7..b615126 100644 --- a/shell.js +++ b/shell.js @@ -202,30 +202,7 @@ exports.test = wrap('test', _test); //@ Returns a string containing the given file, or a concatenated string //@ containing the files if more than one file is given (a new line character is //@ introduced between each file). Wildcard `*` accepted. -function _cat(options, files) { - var cat = ''; - - if (!files) - error('no paths given'); - - if (typeof files === 'string') - files = [].slice.call(arguments, 1); - // if it's array leave it as it is - - files = expand(files); - - files.forEach(function(file) { - if (!fs.existsSync(file)) - error('no such file or directory: ' + file); - - cat += fs.readFileSync(file, 'utf8') + '\n'; - }); - - if (cat[cat.length-1] === '\n') - cat = cat.substring(0, cat.length-1); - - return ShellString(cat); -} +var _cat = require('./src/cat'); exports.cat = wrap('cat', _cat); //@ diff --git a/src/cat.js b/src/cat.js new file mode 100644 index 0000000..428062b --- /dev/null +++ b/src/cat.js @@ -0,0 +1,28 @@ +var common = require('./common'); +var fs = require('fs'); + +function _cat(options, files) { + var cat = ''; + + if (!files) + common.error('no paths given'); + + if (typeof files === 'string') + files = [].slice.call(arguments, 1); + // if it's array leave it as it is + + files = common.expand(files); + + files.forEach(function(file) { + if (!fs.existsSync(file)) + common.error('no such file or directory: ' + file); + + cat += fs.readFileSync(file, 'utf8') + '\n'; + }); + + if (cat[cat.length-1] === '\n') + cat = cat.substring(0, cat.length-1); + + return common.ShellString(cat); +} +module.exports = _cat;