From 144ff713e6793d7bb126e2a3a51f00e117c4e5f0 Mon Sep 17 00:00:00 2001 From: Sami Samhuri Date: Sun, 29 May 2011 23:59:41 -0700 Subject: [PATCH] update file-ext to new format --- lib/file-ext.js | 167 +++++++++++++++++++++++++----------------------- lib/index.js | 2 +- 2 files changed, 88 insertions(+), 81 deletions(-) diff --git a/lib/file-ext.js b/lib/file-ext.js index 6e69401..4fe652b 100644 --- a/lib/file-ext.js +++ b/lib/file-ext.js @@ -2,94 +2,101 @@ // Copyright 2010 - 2011 Sami Samhuri 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); } + }); +} diff --git a/lib/index.js b/lib/index.js index 9cb5be4..eece656 100644 --- a/lib/index.js +++ b/lib/index.js @@ -28,7 +28,7 @@ module.exports = MathExt.extendNative(); // Extend Node - FileExt.extend(require('fs')); + FileExt.extendNative(require('fs')); global['Range'] = Range; global['repr'] = repr;