Skip to content

Commit

Permalink
Breaking: Refactor src() & make it a through stream
Browse files Browse the repository at this point in the history
  • Loading branch information
yocontra authored and phated committed Nov 28, 2017
1 parent c010fe5 commit 93aceb6
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 56 deletions.
11 changes: 0 additions & 11 deletions lib/src/bufferFile.js

This file was deleted.

16 changes: 16 additions & 0 deletions lib/src/getContents/bufferFile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict';

var fs = require('graceful-fs');
var stripBom = require('strip-bom');

function bufferFile(file, cb) {
fs.readFile(file.path, function (err, data) {
if (err) {
return cb(err);
}
file.contents = stripBom(data);
cb(null, file);
});
}

module.exports = bufferFile;
12 changes: 8 additions & 4 deletions lib/src/getContents.js → lib/src/getContents/index.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
'use strict';

var map = require('map-stream');

var readDir = require('./readDir');
var bufferFile = require('./bufferFile');
var streamFile = require('./streamFile');

module.exports = function(opt) {
function getContents(opt) {
return map(function (file, cb) {
// don't fail to read a directory
if (file.stat && file.stat.isDirectory()) {
if (file.isDirectory()) {
return readDir(file, cb);
}

// read and pass full contents
if (opt.buffer) {
if (opt.buffer !== false) {
return bufferFile(file, cb);
}

// dont buffer anything - just pass streams
return streamFile(file, cb);
});
};
}

module.exports = getContents;
8 changes: 8 additions & 0 deletions lib/src/getContents/readDir.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict';

function readDir(file, cb) {
// do nothing for now
cb(null, file);
}

module.exports = readDir;
9 changes: 7 additions & 2 deletions lib/src/streamFile.js → lib/src/getContents/streamFile.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
'use strict';

var fs = require('graceful-fs');
var stripBom = require('strip-bom');

module.exports = function (file, cb) {
function streamFile(file, cb) {
file.contents = fs.createReadStream(file.path)
.pipe(stripBom.stream());

cb(null, file);
};
}

module.exports = streamFile;
26 changes: 17 additions & 9 deletions lib/src/getStats.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
'use strict';

var map = require('map-stream');
var fs = require('graceful-fs');

module.exports = function(opt) {
return map(function (file, cb) {
fs.stat(file.path, function (err, stat) {
if (stat) {
file.stat = stat;
}
cb(err, file);
});
function getStats() {
return map(fetchStats);
}

function fetchStats(file, cb) {
fs.stat(file.path, function (err, stat) {
if (err) {
return cb(err);
}

file.stat = stat;
cb(null, file);
});
};
}

module.exports = getStats;
70 changes: 45 additions & 25 deletions lib/src/index.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,54 @@
var map = require('map-stream');
var gs = require('glob-stream');
'use strict';

var defaults = require('lodash.defaults');
var through = require('through2');
var map = require('through2-map');
var gs = require('glob-stream');
var File = require('vinyl');

var getContents = require('./getContents');
var getStats = require('./getStats');

var validateGlob = function(glob) {
var isArr = Array.isArray(glob);
if (typeof glob !== 'string' && !isArr) return false;
if (isArr && isArr.length === 0) return false;
return true;
};
var formatStream = map.ctor({
objectMode: true
}, function (globFile) {
return new File(globFile);
});

module.exports = function(glob, opt) {
if (!validateGlob(glob)) throw new Error('Invalid glob pattern');
function src(glob, opt) {
if (!isValidGlob(glob)) {
throw new Error('Invalid glob argument');
}

if (!opt) opt = {};
if (typeof opt.read !== 'boolean') opt.read = true;
if (typeof opt.buffer !== 'boolean') opt.buffer = true;

var globStream = gs.create(glob, opt);
var formatStream = map(function(file, cb){
cb(null, new File(file));
var options = defaults({}, opt, {
read: true,
buffer: true
});

var stream = globStream
.pipe(formatStream)
.pipe(getStats(opt));

if (!opt.read) return stream; // no reading required

return stream.pipe(getContents(opt));
};
var globStream = gs.create(glob, options);

// when people write to use just pass it through
var outputStream = globStream
.pipe(formatStream())
.pipe(getStats(options));

if (options.read !== false) {
outputStream = outputStream
.pipe(getContents(options));
}

return outputStream
.pipe(through.obj());
}

function isValidGlob(glob) {
if (typeof glob === 'string') {
return true;
}
if (Array.isArray(glob) && glob.length !== 0) {
return true;
}
return false;
}

module.exports = src;
4 changes: 0 additions & 4 deletions lib/src/readDir.js

This file was deleted.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@
"glob-stream": "^3.1.5",
"glob-watcher": "^0.0.6",
"graceful-fs": "^3.0.0",
"lodash.defaults": "^2.4.1",
"map-stream": "^0.1.0",
"mkdirp": "^0.5.0",
"strip-bom": "^0.3.0",
"through2": "^0.5.1",
"through2-map": "^1.3.0",
"vinyl": "^0.2.0"
},
"devDependencies": {
Expand All @@ -24,7 +27,7 @@
"mocha-lcov-reporter": "^0.0.1",
"rimraf": "^2.2.5",
"should": "^4.0.0",
"through2": "^0.4.0"
"through2": "^0.5.1"
},
"scripts": {
"test": "mocha --reporter spec && jshint",
Expand Down

0 comments on commit 93aceb6

Please sign in to comment.