mirror of
https://github.com/shelljs/shelljs.git
synced 2026-01-18 16:03:37 +00:00
to
This commit is contained in:
parent
305dff0df2
commit
27978b8f9a
14
shell.js
14
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);
|
||||
|
||||
//@
|
||||
|
||||
18
src/to.js
Normal file
18
src/to.js
Normal 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;
|
||||
Loading…
x
Reference in New Issue
Block a user