This commit is contained in:
Artur Adib 2013-08-25 16:18:21 -04:00
parent 305dff0df2
commit 27978b8f9a
2 changed files with 19 additions and 13 deletions

View File

@ -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);
//@

18
src/to.js Normal file
View File

@ -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;