diff --git a/shell.js b/shell.js index b615126..b0d165a 100644 --- a/shell.js +++ b/shell.js @@ -216,19 +216,7 @@ exports.cat = wrap('cat', _cat); //@ //@ Analogous to the redirection operator `>` in Unix, but works with JavaScript strings (such as //@ those returned by `cat`, `grep`, etc). _Like Unix redirections, `to()` will overwrite any existing file!_ -function _to(options, file) { - if (!file) - error('wrong arguments'); - - if (!fs.existsSync( path.dirname(file) )) - error('no such file or directory: ' + path.dirname(file)); - - try { - fs.writeFileSync(file, this.toString(), 'utf8'); - } catch(e) { - error('could not write to file (code '+e.code+'): '+file, true); - } -} +var _to = require('./src/to'); String.prototype.to = wrap('to', _to); //@ diff --git a/src/to.js b/src/to.js new file mode 100644 index 0000000..e127d20 --- /dev/null +++ b/src/to.js @@ -0,0 +1,18 @@ +var common = require('./common'); +var fs = require('fs'); +var path = require('path'); + +function _to(options, file) { + if (!file) + common.error('wrong arguments'); + + if (!fs.existsSync( path.dirname(file) )) + common.error('no such file or directory: ' + path.dirname(file)); + + try { + fs.writeFileSync(file, this.toString(), 'utf8'); + } catch(e) { + common.error('could not write to file (code '+e.code+'): '+file, true); + } +} +module.exports = _to;