Skip to content

Commit

Permalink
New: Support cwd option in dest()
Browse files Browse the repository at this point in the history
  • Loading branch information
yocontra authored and phated committed Nov 27, 2017
1 parent eabbfd5 commit 5c78d8b
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 18 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Expand Up @@ -3,4 +3,5 @@
node_modules
build
*.node
components
components
coverage
3 changes: 2 additions & 1 deletion .npmignore
Expand Up @@ -3,4 +3,5 @@
node_modules
build
*.node
components
components
coverage
12 changes: 4 additions & 8 deletions lib/dest/index.js
@@ -1,23 +1,19 @@
var map = require('map-stream');
var path = require('path');
var mkdirp = require('mkdirp');
var writeFile = require('./writeContents');
var writeDir = require('./writeDir');
var writeContents = require('./writeContents');

module.exports = function(folder, opt) {
if (!opt) opt = {};
// TODO: support opt.cwd
if (!opt.cwd) opt.cwd = process.cwd();

// TODO: clean this crap up
// createOutputStream should be a mirror of createInputStream file-wise
function saveFile (file, cb) {
var writePath = path.join(folder, file.relative);
var writePath = path.resolve(opt.cwd, folder, file.relative);
var writeFolder = path.dirname(writePath);

console.log(writePath, writeFolder);
mkdirp(writeFolder, function(err){
if (err) return cb(err);
writeFile(writePath, file, cb);
writeContents(writePath, file, cb);
});
}
var stream = map(saveFile);
Expand Down
7 changes: 4 additions & 3 deletions lib/src/bufferFile.js
Expand Up @@ -2,8 +2,9 @@ var fs = require('graceful-fs');

module.exports = function (file, cb) {
fs.readFile(file.path, function (err, data) {
if (err) return cb(err);
file.contents = data;
cb(null, file);
if (data) {
file.contents = data;
}
cb(err, file);
});
};
7 changes: 4 additions & 3 deletions lib/src/getStats.js
Expand Up @@ -4,9 +4,10 @@ var fs = require('graceful-fs');
module.exports = function(opt) {
return map(function (file, cb) {
fs.stat(file.path, function (err, stat) {
if (err) return cb(err);
file.stat = stat;
cb(null, file);
if (stat) {
file.stat = stat;
}
cb(err, file);
});
});
};
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -27,7 +27,7 @@
},
"scripts": {
"test": "mocha --reporter spec && jshint",
"coveralls": "istanbul cover _mocha --report lcovonly -- -R spec && cat ./coverage/lcov.info | coveralls && rm -rf ./coverage"
"coveralls": "istanbul cover _mocha -- -R spec && cat ./coverage/lcov.info | coveralls && rm -rf ./coverage"
},
"engines": {
"node": ">= 0.9"
Expand Down
47 changes: 47 additions & 0 deletions test/dest.js
@@ -0,0 +1,47 @@
var vfs = require('../');

var path = require('path');
var fs = require('graceful-fs');
var rimraf = require('rimraf');

var bufEqual = require('buffer-equal');
var es = require('event-stream');
var File = require('vinyl');

var should = require('should');
require('mocha');

var wipeOut = function(cb) {
rimraf(path.join(__dirname, "./out-fixtures/"), cb);
};

describe('dest stream', function() {
beforeEach(wipeOut);
afterEach(wipeOut);

it('should pass through writes', function(done) {
var expectedPath = path.join(__dirname, "./out-fixtures/test.coffee");

var expectedFile = new File({
base: __dirname,
cwd: __dirname,
path: expectedPath,
contents: null
});

var onEnd = function(){
buffered.length.should.equal(1);
buffered[0].should.equal(expectedFile);
done();
};

var stream = vfs.dest("./out-fixtures/", {cwd: __dirname});

var buffered = [];
bufferStream = es.through(buffered.push.bind(buffered), onEnd);
stream.pipe(bufferStream);
stream.write(expectedFile);
stream.end();
});

});
3 changes: 2 additions & 1 deletion test/src.js
Expand Up @@ -30,12 +30,13 @@ describe('source stream', function() {
done();
};

var stream = vfs.src("./fixtures/nothing.coffee", {cwd: __dirname});
var stream = vfs.src("./fixtures/nothing.coffee");

var buffered = [];
bufferStream = es.through(buffered.push.bind(buffered), onEnd);
stream.pipe(bufferStream);
stream.write(expectedFile);
stream.end();
});

it('should glob a file with default settings', function(done) {
Expand Down

0 comments on commit 5c78d8b

Please sign in to comment.