mirror of
https://github.com/samsonjs/batteries.git
synced 2026-04-01 10:15:51 +00:00
update file-ext to new format
This commit is contained in:
parent
6ca19ad0cd
commit
144ff713e6
2 changed files with 88 additions and 81 deletions
167
lib/file-ext.js
167
lib/file-ext.js
|
|
@ -2,94 +2,101 @@
|
|||
// Copyright 2010 - 2011 Sami Samhuri <sami@samhuri.net>
|
||||
|
||||
var fs = require('fs')
|
||||
, ArrayExt = require('./array-ext').ArrayExt
|
||||
, ArrayExt = require('./array-ext')
|
||||
, LineEmitter = require('./line-emitter').LineEmitter
|
||||
, ext = require('./ext')
|
||||
, ObjectExt = require('./object-ext')
|
||||
, constants = require('constants')
|
||||
, FileExt
|
||||
;
|
||||
|
||||
exports.extend = function(obj) {
|
||||
ext.extend(obj || fs, FileExt);
|
||||
FileExt =
|
||||
{ eachLine: eachLine
|
||||
, exists: exists
|
||||
, grep: grep
|
||||
, home: home
|
||||
, readLines: readLines
|
||||
}
|
||||
|
||||
var FileExt = exports.FileExt = {
|
||||
|
||||
eachLine: function(f, optionsOrLineFn, endFn) {
|
||||
var lineFn, hasLineFn, hasEndFn;
|
||||
if (typeof optionsOrLineFn === 'object') {
|
||||
lineFn = optionsOrLineFn.line;
|
||||
endFn = optionsOrLineFn.end;
|
||||
}
|
||||
else if (typeof optionsOrLineFn === 'function') {
|
||||
lineFn = optionsOrLineFn;
|
||||
}
|
||||
hasLineFn = typeof lineFn == 'function';
|
||||
hasEndFn = typeof endFn == 'function';
|
||||
if (!hasLineFn && !hasEndFn) throw new Error('bad arguments');
|
||||
var le = new LineEmitter(f);
|
||||
if (hasLineFn) le.on('line', lineFn);
|
||||
if (hasEndFn) le.on('end', endFn);
|
||||
}
|
||||
|
||||
, exists: function(f) {
|
||||
try {
|
||||
fs.statSync(f)
|
||||
return true
|
||||
} catch (e) {
|
||||
if (e.errno === constants.ENOENT) return false
|
||||
throw e
|
||||
}
|
||||
}
|
||||
|
||||
, grep: function(regex, f, callback) {
|
||||
if (!callback) throw new Error('grep requires a callback');
|
||||
var results = [];
|
||||
FileExt.eachLine(f,
|
||||
{ line: function(line) { if (line.match(regex)) results.push(line); }
|
||||
, end: callback(results)
|
||||
});
|
||||
}
|
||||
|
||||
, home: function(user, callback) {
|
||||
// user is optional so the first param may be a callback
|
||||
if (typeof user === 'function') {
|
||||
callback = user;
|
||||
user = null;
|
||||
}
|
||||
if (user && callback && user !== process.env['USER']) {
|
||||
FileExt.grep(new RegExp('^' + user + ':'), '/etc/passwd', function(line) {
|
||||
callback(line && line.split(':')[4]);
|
||||
});
|
||||
}
|
||||
else if (user)
|
||||
throw new Error('home requires a callback with user');
|
||||
else if (callback)
|
||||
callback(process.env['HOME']);
|
||||
else
|
||||
return process.env['HOME'];
|
||||
}
|
||||
|
||||
, readLines: function(f, cb) {
|
||||
var lines = [];
|
||||
FileExt.eachLine(f, { line: function(line) { lines.push(line); }
|
||||
, end: function() { cb(lines); }
|
||||
});
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
// isDirectory, isFile, isSymbolicLink, etc.
|
||||
var s = fs.statSync(__dirname);
|
||||
Object.keys(Object.getPrototypeOf(s)).forEach(function(k) {
|
||||
if (k.match(/^is/) && typeof s[k] === 'function') {
|
||||
FileExt[k] = function(f, cb) {
|
||||
if (cb) {
|
||||
fs.stat(f, function(err, stat) {
|
||||
cb(err, err ? null : stat[k]());
|
||||
});
|
||||
} else {
|
||||
return fs.statSync(f)[k]();
|
||||
}
|
||||
}
|
||||
if (k.match(/^is/) && typeof s[k] === 'function') {
|
||||
FileExt[k] = function(f, cb) {
|
||||
if (cb) {
|
||||
fs.stat(f, function(err, stat) {
|
||||
cb(err, err ? null : stat[k]());
|
||||
});
|
||||
} else {
|
||||
return fs.statSync(f)[k]();
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
exports.extendNative = function() {
|
||||
ObjectExt.extend(fs, FileExt);
|
||||
};
|
||||
|
||||
ObjectExt.extend(exports, FileExt);
|
||||
|
||||
function eachLine(f, optionsOrLineFn, endFn) {
|
||||
var lineFn, hasLineFn, hasEndFn;
|
||||
if (typeof optionsOrLineFn === 'object') {
|
||||
lineFn = optionsOrLineFn.line;
|
||||
endFn = optionsOrLineFn.end;
|
||||
}
|
||||
else if (typeof optionsOrLineFn === 'function') {
|
||||
lineFn = optionsOrLineFn;
|
||||
}
|
||||
hasLineFn = typeof lineFn == 'function';
|
||||
hasEndFn = typeof endFn == 'function';
|
||||
if (!hasLineFn && !hasEndFn) throw new Error('bad arguments');
|
||||
var le = new LineEmitter(f);
|
||||
if (hasLineFn) le.on('line', lineFn);
|
||||
if (hasEndFn) le.on('end', endFn);
|
||||
}
|
||||
|
||||
function exists(f) {
|
||||
try {
|
||||
fs.statSync(f)
|
||||
return true
|
||||
} catch (e) {
|
||||
if (e.errno === constants.ENOENT) return false
|
||||
throw e
|
||||
}
|
||||
}
|
||||
|
||||
function grep(regex, f, callback) {
|
||||
if (!callback) throw new Error('grep requires a callback');
|
||||
var results = [];
|
||||
eachLine(f,
|
||||
{ line: function(line) { if (line.match(regex)) results.push(line); }
|
||||
, end: callback(results)
|
||||
});
|
||||
}
|
||||
|
||||
function home(user, callback) {
|
||||
// user is optional so the first param may be a callback
|
||||
if (typeof user === 'function') {
|
||||
callback = user;
|
||||
user = null;
|
||||
}
|
||||
if (user && callback && user !== process.env['USER']) {
|
||||
grep(new RegExp('^' + user + ':'), '/etc/passwd', function(line) {
|
||||
callback(line && line.split(':')[4]);
|
||||
});
|
||||
}
|
||||
else if (user)
|
||||
throw new Error('home requires a callback with user');
|
||||
else if (callback)
|
||||
callback(process.env['HOME']);
|
||||
else
|
||||
return process.env['HOME'];
|
||||
}
|
||||
|
||||
function readLines(f, cb) {
|
||||
var lines = [];
|
||||
eachLine(f, { line: function(line) { lines.push(line); }
|
||||
, end: function() { cb(lines); }
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -28,7 +28,7 @@ module.exports =
|
|||
MathExt.extendNative();
|
||||
|
||||
// Extend Node
|
||||
FileExt.extend(require('fs'));
|
||||
FileExt.extendNative(require('fs'));
|
||||
|
||||
global['Range'] = Range;
|
||||
global['repr'] = repr;
|
||||
|
|
|
|||
Loading…
Reference in a new issue