-
-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Breaking: Refactor
src()
& make it a through stream
- Loading branch information
Showing
9 changed files
with
105 additions
and
56 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters