From d0ba8bba4519df2b6bb41f0f07cfaf1d7a5fbf7d Mon Sep 17 00:00:00 2001 From: Contra Date: Tue, 10 Jun 2014 00:32:11 -0700 Subject: [PATCH] make src a passthrough, clean up mode logic in dest, jshint, stylize --- .editorconfig | 10 +++ .jshintrc | 19 +++++ README.md | 16 +++-- index.js | 4 +- lib/dest/index.js | 47 ++++++++----- lib/dest/writeBuffer.js | 11 --- .../index.js} | 15 ++-- lib/dest/writeContents/writeBuffer.js | 13 ++++ lib/dest/writeContents/writeDir.js | 9 +++ lib/dest/writeContents/writeStream.js | 22 ++++++ lib/dest/writeDir.js | 12 ---- lib/dest/writeStream.js | 20 ------ lib/src/bufferFile.js | 11 --- lib/src/getContents/bufferFile.js | 16 +++++ .../{getContents.js => getContents/index.js} | 12 ++-- lib/src/getContents/readDir.js | 8 +++ lib/src/{ => getContents}/streamFile.js | 9 ++- lib/src/getStats.js | 26 ++++--- lib/src/index.js | 70 ++++++++++++------- lib/src/readDir.js | 4 -- package.json | 9 ++- test/dest.js | 68 +++++++++--------- test/src.js | 51 ++++++-------- 23 files changed, 291 insertions(+), 191 deletions(-) create mode 100644 .editorconfig create mode 100644 .jshintrc delete mode 100644 lib/dest/writeBuffer.js rename lib/dest/{writeContents.js => writeContents/index.js} (74%) create mode 100644 lib/dest/writeContents/writeBuffer.js create mode 100644 lib/dest/writeContents/writeDir.js create mode 100644 lib/dest/writeContents/writeStream.js delete mode 100644 lib/dest/writeDir.js delete mode 100644 lib/dest/writeStream.js delete mode 100644 lib/src/bufferFile.js create mode 100644 lib/src/getContents/bufferFile.js rename lib/src/{getContents.js => getContents/index.js} (75%) create mode 100644 lib/src/getContents/readDir.js rename lib/src/{ => getContents}/streamFile.js (67%) delete mode 100644 lib/src/readDir.js diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 00000000..6c5424ed --- /dev/null +++ b/.editorconfig @@ -0,0 +1,10 @@ +# editorconfig.org +root = true + +[*] +indent_style = space +indent_size = 2 +end_of_line = lf +charset = utf-8 +trim_trailing_whitespace = true +insert_final_newline = true \ No newline at end of file diff --git a/.jshintrc b/.jshintrc new file mode 100644 index 00000000..af7124ee --- /dev/null +++ b/.jshintrc @@ -0,0 +1,19 @@ +{ + "camelcase": true, + "curly": true, + "eqeqeq": true, + "freeze": true, + "indent": 2, + "newcap": false, + "quotmark": "single", + "maxdepth": 3, + "maxstatements": 50, + "maxlen": 80, + "eqnull": true, + "funcscope": true, + "strict": true, + "undef": true, + "unused": true, + "node": true, + "mocha": true +} \ No newline at end of file diff --git a/README.md b/README.md index b34b9f2a..1a664e1c 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ ## Usage ```javascript -var es = require('event-stream'); +var map = require('map-stream'); var fs = require('vinyl-fs'); var log = function(file, cb) { @@ -27,9 +27,9 @@ var log = function(file, cb) { cb(null, file); }; -fs.src(["./js/**/*.js", "!./js/vendor/*.js"]) - .pipe(es.map(log)) - .pipe(fs.dest("./output")); +fs.src(['./js/**/*.js', '!./js/vendor/*.js']) + .pipe(map(log)) + .pipe(fs.dest('./output')); ``` ## API @@ -38,6 +38,8 @@ fs.src(["./js/**/*.js", "!./js/vendor/*.js"]) - Takes a glob string or an array of glob strings as the first argument. - Possible options for the second argument: + - cwd - Specify the working directory the folder is relative to. Default is `process.cwd()` + - base - Specify the folder relative to the cwd. Default is where the glob begins. This is used to determine the file names when saving in `.dest()` - buffer - `true` or `false` if you want to buffer the file. - Default value is `true` - `false` will make file.contents a paused Stream @@ -68,7 +70,11 @@ This is just [glob-watcher] - mode - Specify the mode the files should be created with. Default is the mode of the input file (file.stat.mode) - Returns a Readable/Writable stream. - On write the stream will save the [vinyl] File to disk at the folder/cwd specified. -- After writing the file to disk it will be emitted from the stream so you can keep piping these around +- After writing the file to disk, it will be emitted from the stream so you can keep piping these around +- The file will be modified after being written to this stream + - `cwd`, `base`, and `path` will be overwritten to match the folder + - `stat.mode` will be overwritten if you used a mode parameter + - `contents` will have it's position reset to the beginning if it is a stream [glob-stream]: https://github.com/wearefractal/glob-stream [node-glob]: https://github.com/isaacs/node-glob diff --git a/index.js b/index.js index 85d2b629..7306aa78 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,7 @@ +'use strict'; + module.exports = { src: require('./lib/src'), dest: require('./lib/dest'), watch: require('glob-watcher') -}; \ No newline at end of file +}; diff --git a/lib/dest/index.js b/lib/dest/index.js index b2776191..f32e5d7b 100644 --- a/lib/dest/index.js +++ b/lib/dest/index.js @@ -1,42 +1,55 @@ +'use strict'; + +var defaults = require('lodash.defaults'); var map = require('map-stream'); var path = require('path'); var mkdirp = require('mkdirp'); +var fs = require('graceful-fs'); var writeContents = require('./writeContents'); -var defaultMode = 0777 & (~process.umask()); +// 511 = 0777 +var processMode = 511 & (~process.umask()); + +function dest(outFolder, opt) { + if (typeof outFolder !== 'string') { + throw new Error('Invalid output folder'); + } -module.exports = function(outFolder, opt) { - if (typeof outFolder !== 'string') throw new Error('Invalid output folder'); + var options = defaults({}, opt, { + cwd: process.cwd() + }); - if (!opt) opt = {}; - if (!opt.cwd) opt.cwd = process.cwd(); - if (typeof opt.mode === 'string') opt.mode = parseInt(opt.mode, 8); + if (typeof options.mode === 'string') { + options.mode = parseInt(options.mode, 8); + } - var cwd = path.resolve(opt.cwd); + var cwd = path.resolve(options.cwd); var basePath = path.resolve(cwd, outFolder); - var folderMode = (opt.mode || defaultMode); + var defaultMode = (options.mode || processMode); function saveFile (file, cb) { var writePath = path.resolve(basePath, file.relative); var writeFolder = path.dirname(writePath); - if (typeof opt.mode !== 'undefined') { - if (!file.stat) file.stat = {}; - file.stat.mode = opt.mode; - } - + // wire up new properties + file.stat = file.stat ? file.stat : new fs.Stats(); + file.stat.mode = (options.mode || file.stat.mode || processMode); file.cwd = cwd; file.base = basePath; file.path = writePath; // mkdirp the folder the file is going in - // then write to it - mkdirp(writeFolder, folderMode, function(err){ - if (err) return cb(err); + mkdirp(writeFolder, defaultMode, function(err){ + if (err) { + return cb(err); + } writeContents(writePath, file, cb); }); } + var stream = map(saveFile); return stream; -}; +} + +module.exports = dest; diff --git a/lib/dest/writeBuffer.js b/lib/dest/writeBuffer.js deleted file mode 100644 index 6a2c4283..00000000 --- a/lib/dest/writeBuffer.js +++ /dev/null @@ -1,11 +0,0 @@ -var fs = require('graceful-fs'); - -module.exports = function(writePath, file, cb) { - var opt = {}; - - if (file.stat && typeof file.stat.mode !== 'undefined') { - opt.mode = file.stat.mode; - } - - fs.writeFile(writePath, file.contents, opt, cb); -}; diff --git a/lib/dest/writeContents.js b/lib/dest/writeContents/index.js similarity index 74% rename from lib/dest/writeContents.js rename to lib/dest/writeContents/index.js index 9b6a00f8..1cb62139 100644 --- a/lib/dest/writeContents.js +++ b/lib/dest/writeContents/index.js @@ -1,9 +1,11 @@ +'use strict'; + var writeDir = require('./writeDir'); var writeStream = require('./writeStream'); var writeBuffer = require('./writeBuffer'); -module.exports = function (writePath, file, cb) { - var done = function(err){ +function writeContents(writePath, file, cb) { + var done = function(err){ cb(err, file); }; @@ -26,5 +28,10 @@ module.exports = function (writePath, file, cb) { } // if no contents then do nothing - if (file.isNull()) return done(); -}; \ No newline at end of file + if (file.isNull()) { + done(); + return; + } +} + +module.exports = writeContents; diff --git a/lib/dest/writeContents/writeBuffer.js b/lib/dest/writeContents/writeBuffer.js new file mode 100644 index 00000000..fe4be8f2 --- /dev/null +++ b/lib/dest/writeContents/writeBuffer.js @@ -0,0 +1,13 @@ +'use strict'; + +var fs = require('graceful-fs'); + +function writeBuffer(writePath, file, cb) { + var opt = { + mode: file.stat.mode + }; + + fs.writeFile(writePath, file.contents, opt, cb); +} + +module.exports = writeBuffer; diff --git a/lib/dest/writeContents/writeDir.js b/lib/dest/writeContents/writeDir.js new file mode 100644 index 00000000..9614b54d --- /dev/null +++ b/lib/dest/writeContents/writeDir.js @@ -0,0 +1,9 @@ +'use strict'; + +var mkdirp = require('mkdirp'); + +function writeDir (writePath, file, cb) { + mkdirp(writePath, file.stat.mode, cb); +} + +module.exports = writeDir; diff --git a/lib/dest/writeContents/writeStream.js b/lib/dest/writeContents/writeStream.js new file mode 100644 index 00000000..c49017aa --- /dev/null +++ b/lib/dest/writeContents/writeStream.js @@ -0,0 +1,22 @@ +'use strict'; + +var streamFile = require('../../src/getContents/streamFile'); +var fs = require('graceful-fs'); + +function writeStream (writePath, file, cb) { + var opt = { + mode: file.stat.mode + }; + + var outStream = fs.createWriteStream(writePath, opt); + + file.contents.once('error', cb); + outStream.once('error', cb); + outStream.once('finish', function() { + streamFile(file, cb); + }); + + file.contents.pipe(outStream); +} + +module.exports = writeStream; diff --git a/lib/dest/writeDir.js b/lib/dest/writeDir.js deleted file mode 100644 index 32fa7f07..00000000 --- a/lib/dest/writeDir.js +++ /dev/null @@ -1,12 +0,0 @@ -var mkdirp = require('mkdirp'); - -module.exports = function (writePath, file, cb) { - // mode needs to be done - if (file.stat && typeof file.stat.mode !== 'undefined') { - mkdirp(writePath, file.stat.mode, cb); - return; - } - - // no mode - mkdirp(writePath, cb); -}; diff --git a/lib/dest/writeStream.js b/lib/dest/writeStream.js deleted file mode 100644 index d0cc9514..00000000 --- a/lib/dest/writeStream.js +++ /dev/null @@ -1,20 +0,0 @@ -var streamFile = require('../src/streamFile'); -var fs = require('graceful-fs'); - -module.exports = function(writePath, file, cb) { - var opt = {}; - - if (file.stat && typeof file.stat.mode !== 'undefined') { - opt.mode = file.stat.mode; - } - - var outStream = fs.createWriteStream(writePath, opt); - - // TODO: can we pass the file along before the stream is unloaded? - file.contents.once('error', cb); - file.contents.pipe(outStream).once('finish', function() { - streamFile(file, cb); - }); - - return outStream; -}; diff --git a/lib/src/bufferFile.js b/lib/src/bufferFile.js deleted file mode 100644 index f8f980b6..00000000 --- a/lib/src/bufferFile.js +++ /dev/null @@ -1,11 +0,0 @@ -var fs = require('graceful-fs'); -var stripBom = require('strip-bom'); - -module.exports = function (file, cb) { - fs.readFile(file.path, function (err, data) { - if (data) { - file.contents = stripBom(data); - } - cb(err, file); - }); -}; diff --git a/lib/src/getContents/bufferFile.js b/lib/src/getContents/bufferFile.js new file mode 100644 index 00000000..4448eb7e --- /dev/null +++ b/lib/src/getContents/bufferFile.js @@ -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; diff --git a/lib/src/getContents.js b/lib/src/getContents/index.js similarity index 75% rename from lib/src/getContents.js rename to lib/src/getContents/index.js index de8538ba..6cd54a59 100644 --- a/lib/src/getContents.js +++ b/lib/src/getContents/index.js @@ -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); }); -}; \ No newline at end of file +} + +module.exports = getContents; diff --git a/lib/src/getContents/readDir.js b/lib/src/getContents/readDir.js new file mode 100644 index 00000000..783fac2c --- /dev/null +++ b/lib/src/getContents/readDir.js @@ -0,0 +1,8 @@ +'use strict'; + +function readDir(file, cb) { + // do nothing for now + cb(null, file); +} + +module.exports = readDir; diff --git a/lib/src/streamFile.js b/lib/src/getContents/streamFile.js similarity index 67% rename from lib/src/streamFile.js rename to lib/src/getContents/streamFile.js index a4f95860..1743edd7 100644 --- a/lib/src/streamFile.js +++ b/lib/src/getContents/streamFile.js @@ -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); -}; \ No newline at end of file +} + +module.exports = streamFile; diff --git a/lib/src/getStats.js b/lib/src/getStats.js index 305ed2b1..2040247e 100644 --- a/lib/src/getStats.js +++ b/lib/src/getStats.js @@ -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); }); -}; \ No newline at end of file +} + +module.exports = getStats; diff --git a/lib/src/index.js b/lib/src/index.js index cad61053..794509ae 100644 --- a/lib/src/index.js +++ b/lib/src/index.js @@ -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; diff --git a/lib/src/readDir.js b/lib/src/readDir.js deleted file mode 100644 index 7d3c2d10..00000000 --- a/lib/src/readDir.js +++ /dev/null @@ -1,4 +0,0 @@ -module.exports = function (file, cb) { - // do nothing for now - cb(null, file); -}; \ No newline at end of file diff --git a/package.json b/package.json index bdbee77e..d7d1cc95 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "vinyl-fs", "description": "Vinyl adapter for the file system", - "version": "0.2.1", + "version": "0.3.0", "homepage": "http://github.com/wearefractal/vinyl-fs", "repository": "git://github.com/wearefractal/vinyl-fs.git", "author": "Fractal (http://wearefractal.com/)", @@ -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": { @@ -24,10 +27,10 @@ "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", + "test": "mocha --reporter spec && jshint lib", "coveralls": "istanbul cover _mocha -- -R spec && cat ./coverage/lcov.info | coveralls && rm -rf ./coverage" }, "engines": { diff --git a/test/dest.js b/test/dest.js index c7facd44..cc1bb2ee 100644 --- a/test/dest.js +++ b/test/dest.js @@ -12,7 +12,7 @@ var should = require('should'); require('mocha'); var wipeOut = function(cb) { - rimraf(path.join(__dirname, "./out-fixtures/"), cb); + rimraf(path.join(__dirname, './out-fixtures/'), cb); }; var dataWrap = function(fn) { @@ -42,7 +42,7 @@ describe('dest stream', function() { }); it('should pass through writes with cwd', function(done) { - var inputPath = path.join(__dirname, "./fixtures/test.coffee"); + var inputPath = path.join(__dirname, './fixtures/test.coffee'); var expectedFile = new File({ base: __dirname, @@ -57,7 +57,7 @@ describe('dest stream', function() { done(); }; - var stream = vfs.dest("./out-fixtures/", {cwd: __dirname}); + var stream = vfs.dest('./out-fixtures/', {cwd: __dirname}); var buffered = []; bufferStream = through.obj(dataWrap(buffered.push.bind(buffered)), onEnd); @@ -67,7 +67,7 @@ describe('dest stream', function() { }); it('should pass through writes with default cwd', function(done) { - var inputPath = path.join(__dirname, "./fixtures/test.coffee"); + var inputPath = path.join(__dirname, './fixtures/test.coffee'); var expectedFile = new File({ base: __dirname, @@ -82,7 +82,7 @@ describe('dest stream', function() { done(); }; - var stream = vfs.dest(path.join(__dirname, "./out-fixtures/")); + var stream = vfs.dest(path.join(__dirname, './out-fixtures/')); var buffered = []; bufferStream = through.obj(dataWrap(buffered.push.bind(buffered)), onEnd); @@ -92,11 +92,11 @@ describe('dest stream', function() { }); it('should not write null files', function(done) { - var inputPath = path.join(__dirname, "./fixtures/test.coffee"); - var inputBase = path.join(__dirname, "./fixtures/"); - var expectedPath = path.join(__dirname, "./out-fixtures/test.coffee"); + var inputPath = path.join(__dirname, './fixtures/test.coffee'); + var inputBase = path.join(__dirname, './fixtures/'); + var expectedPath = path.join(__dirname, './out-fixtures/test.coffee'); var expectedCwd = __dirname; - var expectedBase = path.join(__dirname, "./out-fixtures"); + var expectedBase = path.join(__dirname, './out-fixtures'); var expectedFile = new File({ base: inputBase, @@ -115,7 +115,7 @@ describe('dest stream', function() { done(); }; - var stream = vfs.dest("./out-fixtures/", {cwd: __dirname}); + var stream = vfs.dest('./out-fixtures/', {cwd: __dirname}); var buffered = []; bufferStream = through.obj(dataWrap(buffered.push.bind(buffered)), onEnd); @@ -125,11 +125,11 @@ describe('dest stream', function() { }); it('should write buffer files to the right folder with relative cwd', function(done) { - var inputPath = path.join(__dirname, "./fixtures/test.coffee"); - var inputBase = path.join(__dirname, "./fixtures/"); - var expectedPath = path.join(__dirname, "./out-fixtures/test.coffee"); + var inputPath = path.join(__dirname, './fixtures/test.coffee'); + var inputBase = path.join(__dirname, './fixtures/'); + var expectedPath = path.join(__dirname, './out-fixtures/test.coffee'); var expectedCwd = __dirname; - var expectedBase = path.join(__dirname, "./out-fixtures"); + var expectedBase = path.join(__dirname, './out-fixtures'); var expectedContents = fs.readFileSync(inputPath); var expectedFile = new File({ @@ -150,7 +150,7 @@ describe('dest stream', function() { done(); }; - var stream = vfs.dest("./out-fixtures/", {cwd: path.relative(process.cwd(), __dirname)}); + var stream = vfs.dest('./out-fixtures/', {cwd: path.relative(process.cwd(), __dirname)}); var buffered = []; bufferStream = through.obj(dataWrap(buffered.push.bind(buffered)), onEnd); @@ -160,12 +160,12 @@ describe('dest stream', function() { }); it('should write buffer files to the right folder', function(done) { - var inputPath = path.join(__dirname, "./fixtures/test.coffee"); - var inputBase = path.join(__dirname, "./fixtures/"); - var expectedPath = path.join(__dirname, "./out-fixtures/test.coffee"); + var inputPath = path.join(__dirname, './fixtures/test.coffee'); + var inputBase = path.join(__dirname, './fixtures/'); + var expectedPath = path.join(__dirname, './out-fixtures/test.coffee'); var expectedContents = fs.readFileSync(inputPath); var expectedCwd = __dirname; - var expectedBase = path.join(__dirname, "./out-fixtures"); + var expectedBase = path.join(__dirname, './out-fixtures'); var expectedMode = 0655; var expectedFile = new File({ @@ -190,7 +190,7 @@ describe('dest stream', function() { done(); }; - var stream = vfs.dest("./out-fixtures/", {cwd: __dirname}); + var stream = vfs.dest('./out-fixtures/', {cwd: __dirname}); var buffered = []; bufferStream = through.obj(dataWrap(buffered.push.bind(buffered)), onEnd); @@ -200,12 +200,12 @@ describe('dest stream', function() { }); it('should write streaming files to the right folder', function(done) { - var inputPath = path.join(__dirname, "./fixtures/test.coffee"); - var inputBase = path.join(__dirname, "./fixtures/"); - var expectedPath = path.join(__dirname, "./out-fixtures/test.coffee"); + var inputPath = path.join(__dirname, './fixtures/test.coffee'); + var inputBase = path.join(__dirname, './fixtures/'); + var expectedPath = path.join(__dirname, './out-fixtures/test.coffee'); var expectedContents = fs.readFileSync(inputPath); var expectedCwd = __dirname; - var expectedBase = path.join(__dirname, "./out-fixtures"); + var expectedBase = path.join(__dirname, './out-fixtures'); var expectedMode = 0655; var contentStream = through.obj(); @@ -231,7 +231,7 @@ describe('dest stream', function() { done(); }; - var stream = vfs.dest("./out-fixtures/", {cwd: __dirname}); + var stream = vfs.dest('./out-fixtures/', {cwd: __dirname}); var buffered = []; bufferStream = through.obj(dataWrap(buffered.push.bind(buffered)), onEnd); @@ -245,11 +245,11 @@ describe('dest stream', function() { }); it('should write directories to the right folder', function(done) { - var inputPath = path.join(__dirname, "./fixtures/test"); - var inputBase = path.join(__dirname, "./fixtures/"); - var expectedPath = path.join(__dirname, "./out-fixtures/test"); + var inputPath = path.join(__dirname, './fixtures/test'); + var inputBase = path.join(__dirname, './fixtures/'); + var expectedPath = path.join(__dirname, './out-fixtures/test'); var expectedCwd = __dirname; - var expectedBase = path.join(__dirname, "./out-fixtures"); + var expectedBase = path.join(__dirname, './out-fixtures'); var expectedMode = 0655; var expectedFile = new File({ @@ -277,7 +277,7 @@ describe('dest stream', function() { done(); }; - var stream = vfs.dest("./out-fixtures/", {cwd: __dirname}); + var stream = vfs.dest('./out-fixtures/', {cwd: __dirname}); var buffered = []; bufferStream = through.obj(dataWrap(buffered.push.bind(buffered)), onEnd); @@ -287,10 +287,10 @@ describe('dest stream', function() { }); it('should allow piping multiple dests in streaming mode', function(done) { - var inputPath1 = path.join(__dirname, "./out-fixtures/multiple-first"); - var inputPath2 = path.join(__dirname, "./out-fixtures/multiple-second"); - var inputBase = path.join(__dirname, "./out-fixtures/"); - var srcPath = path.join(__dirname, "./fixtures/test.coffee"); + var inputPath1 = path.join(__dirname, './out-fixtures/multiple-first'); + var inputPath2 = path.join(__dirname, './out-fixtures/multiple-second'); + var inputBase = path.join(__dirname, './out-fixtures/'); + var srcPath = path.join(__dirname, './fixtures/test.coffee'); var stream1 = vfs.dest('./out-fixtures/', {cwd: __dirname}); var stream2 = vfs.dest('./out-fixtures/', {cwd: __dirname}); var content = fs.readFileSync(srcPath); diff --git a/test/src.js b/test/src.js index c9ee2e45..99608e0a 100644 --- a/test/src.js +++ b/test/src.js @@ -53,7 +53,7 @@ describe('source stream', function() { }); it('should pass through writes', function(done) { - var expectedPath = path.join(__dirname, "./fixtures/test.coffee"); + var expectedPath = path.join(__dirname, './fixtures/test.coffee'); var expectedContent = fs.readFileSync(expectedPath); var expectedFile = new File({ @@ -63,24 +63,17 @@ describe('source stream', function() { contents: expectedContent }); - var onEnd = function(){ - buffered.length.should.equal(1); - buffered[0].should.equal(expectedFile); - bufEqual(buffered[0].contents, expectedContent).should.equal(true); + var stream = vfs.src('./fixtures/noexist.coffee'); + stream.on('data', function(file){ + file.should.equal(expectedFile); + bufEqual(file.contents, expectedContent).should.equal(true); done(); - }; - - var stream = vfs.src("./fixtures/nothing.coffee"); - - var buffered = []; - bufferStream = through.obj(dataWrap(buffered.push.bind(buffered)), onEnd); - stream.pipe(bufferStream); + }); stream.write(expectedFile); - stream.end(); }); it('should strip BOM from UTF-8-encoded files', function(done) { - var expectedPath = path.join(__dirname, "./fixtures/bom-utf8.txt"); + var expectedPath = path.join(__dirname, './fixtures/bom-utf8.txt'); var expectedContent = fs.readFileSync(expectedPath) // U+FEFF takes up 3 bytes in UTF-8: http://mothereff.in/utf-8#%EF%BB%BF .slice(3); @@ -94,7 +87,7 @@ describe('source stream', function() { done(); }; - var stream = vfs.src("./fixtures/bom-utf8.txt", {cwd: __dirname}); + var stream = vfs.src('./fixtures/bom-utf8.txt', {cwd: __dirname}); var buffered = []; bufferStream = through.obj(dataWrap(buffered.push.bind(buffered)), onEnd); @@ -104,7 +97,7 @@ describe('source stream', function() { it('should not strip anything that looks like a UTF-8-encoded BOM from UTF-16-BE-encoded files', function(done) { // Note: this goes for any non-UTF-8 encoding, but testing for UTF-16-BE // and UTF-16-LE is enough to demonstrate this is done properly. - var expectedPath = path.join(__dirname, "./fixtures/bom-utf16be.txt"); + var expectedPath = path.join(__dirname, './fixtures/bom-utf16be.txt'); var expectedContent = fs.readFileSync(expectedPath); var onEnd = function(){ @@ -116,7 +109,7 @@ describe('source stream', function() { done(); }; - var stream = vfs.src("./fixtures/bom-utf16be.txt", {cwd: __dirname}); + var stream = vfs.src('./fixtures/bom-utf16be.txt', {cwd: __dirname}); var buffered = []; bufferStream = through.obj(dataWrap(buffered.push.bind(buffered)), onEnd); @@ -126,7 +119,7 @@ describe('source stream', function() { it('should not strip anything that looks like a UTF-8-encoded BOM from UTF-16-LE-encoded files', function(done) { // Note: this goes for any non-UTF-8 encoding, but testing for UTF-16-BE // and UTF-16-LE is enough to demonstrate this is done properly. - var expectedPath = path.join(__dirname, "./fixtures/bom-utf16le.txt"); + var expectedPath = path.join(__dirname, './fixtures/bom-utf16le.txt'); var expectedContent = fs.readFileSync(expectedPath); var onEnd = function(){ @@ -138,7 +131,7 @@ describe('source stream', function() { done(); }; - var stream = vfs.src("./fixtures/bom-utf16le.txt", {cwd: __dirname}); + var stream = vfs.src('./fixtures/bom-utf16le.txt', {cwd: __dirname}); var buffered = []; bufferStream = through.obj(dataWrap(buffered.push.bind(buffered)), onEnd); @@ -146,7 +139,7 @@ describe('source stream', function() { }); it('should glob a file with default settings', function(done) { - var expectedPath = path.join(__dirname, "./fixtures/test.coffee"); + var expectedPath = path.join(__dirname, './fixtures/test.coffee'); var expectedContent = fs.readFileSync(expectedPath); var onEnd = function(){ @@ -158,7 +151,7 @@ describe('source stream', function() { done(); }; - var stream = vfs.src("./fixtures/*.coffee", {cwd: __dirname}); + var stream = vfs.src('./fixtures/*.coffee', {cwd: __dirname}); var buffered = []; bufferStream = through.obj(dataWrap(buffered.push.bind(buffered)), onEnd); @@ -166,7 +159,7 @@ describe('source stream', function() { }); it('should glob a file with default settings and relative cwd', function(done) { - var expectedPath = path.join(__dirname, "./fixtures/test.coffee"); + var expectedPath = path.join(__dirname, './fixtures/test.coffee'); var expectedContent = fs.readFileSync(expectedPath); var onEnd = function(){ @@ -178,7 +171,7 @@ describe('source stream', function() { done(); }; - var stream = vfs.src("./fixtures/*.coffee", {cwd: path.relative(process.cwd(), __dirname)}); + var stream = vfs.src('./fixtures/*.coffee', {cwd: path.relative(process.cwd(), __dirname)}); var buffered = []; bufferStream = through.obj(dataWrap(buffered.push.bind(buffered)), onEnd); @@ -186,7 +179,7 @@ describe('source stream', function() { }); it('should glob a directory with default settings', function(done) { - var expectedPath = path.join(__dirname, "./fixtures/wow"); + var expectedPath = path.join(__dirname, './fixtures/wow'); var onEnd = function(){ buffered.length.should.equal(1); @@ -196,7 +189,7 @@ describe('source stream', function() { done(); }; - var stream = vfs.src("./fixtures/wow/", {cwd: __dirname}); + var stream = vfs.src('./fixtures/wow/', {cwd: __dirname}); var buffered = []; bufferStream = through.obj(dataWrap(buffered.push.bind(buffered)), onEnd); @@ -204,7 +197,7 @@ describe('source stream', function() { }); it('should glob a file with with no contents', function(done) { - var expectedPath = path.join(__dirname, "./fixtures/test.coffee"); + var expectedPath = path.join(__dirname, './fixtures/test.coffee'); var expectedContent = fs.readFileSync(expectedPath); var onEnd = function(){ @@ -215,7 +208,7 @@ describe('source stream', function() { done(); }; - var stream = vfs.src("./fixtures/*.coffee", {cwd: __dirname, read: false}); + var stream = vfs.src('./fixtures/*.coffee', {cwd: __dirname, read: false}); var buffered = []; bufferStream = through.obj(dataWrap(buffered.push.bind(buffered)), onEnd); @@ -223,7 +216,7 @@ describe('source stream', function() { }); it('should glob a file with streaming contents', function(done) { - var expectedPath = path.join(__dirname, "./fixtures/test.coffee"); + var expectedPath = path.join(__dirname, './fixtures/test.coffee'); var expectedContent = fs.readFileSync(expectedPath); var onEnd = function(){ @@ -243,7 +236,7 @@ describe('source stream', function() { }); }; - var stream = vfs.src("./fixtures/*.coffee", {cwd: __dirname, buffer: false}); + var stream = vfs.src('./fixtures/*.coffee', {cwd: __dirname, buffer: false}); var buffered = []; bufferStream = through.obj(dataWrap(buffered.push.bind(buffered)), onEnd);