Skip to content

Commit

Permalink
function for dest, closes #15
Browse files Browse the repository at this point in the history
  • Loading branch information
Contra committed Jun 10, 2014
1 parent d0ba8bb commit 583d30e
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -65,6 +65,7 @@ This is just [glob-watcher]
### dest(folder[, opt])

- Takes a folder path as the first argument.
- First argument can also be a function that takes in a file and returns a folder path.
- Possible options for the second argument:
- cwd - Specify the working directory the folder is relative to. Default is `process.cwd()`
- mode - Specify the mode the files should be created with. Default is the mode of the input file (file.stat.mode)
Expand Down
10 changes: 8 additions & 2 deletions lib/dest/index.js
Expand Up @@ -12,7 +12,7 @@ var writeContents = require('./writeContents');
var processMode = 511 & (~process.umask());

function dest(outFolder, opt) {
if (typeof outFolder !== 'string') {
if (typeof outFolder !== 'string' && typeof outFolder !== 'function') {
throw new Error('Invalid output folder');
}

Expand All @@ -25,10 +25,16 @@ function dest(outFolder, opt) {
}

var cwd = path.resolve(options.cwd);
var basePath = path.resolve(cwd, outFolder);
var defaultMode = (options.mode || processMode);

function saveFile (file, cb) {
var basePath;
if (typeof outFolder === 'string') {
basePath = path.resolve(cwd, outFolder);
}
if (typeof outFolder === 'function') {
basePath = path.resolve(cwd, outFolder(file));
}
var writePath = path.resolve(basePath, file.relative);
var writeFolder = path.dirname(writePath);

Expand Down
39 changes: 39 additions & 0 deletions test/dest.js
Expand Up @@ -159,6 +159,45 @@ describe('dest stream', function() {
stream.end();
});

it('should write buffer files to the right folder with function and 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 expectedCwd = __dirname;
var expectedBase = path.join(__dirname, './out-fixtures');
var expectedContents = fs.readFileSync(inputPath);

var expectedFile = new File({
base: inputBase,
cwd: __dirname,
path: inputPath,
contents: expectedContents
});

var onEnd = function(){
buffered.length.should.equal(1);
buffered[0].should.equal(expectedFile);
buffered[0].cwd.should.equal(expectedCwd, 'cwd should have changed');
buffered[0].base.should.equal(expectedBase, 'base should have changed');
buffered[0].path.should.equal(expectedPath, 'path should have changed');
fs.existsSync(expectedPath).should.equal(true);
bufEqual(fs.readFileSync(expectedPath), expectedContents).should.equal(true);
done();
};

var stream = vfs.dest(function(file){
should.exist(file);
file.should.equal(expectedFile);
return './out-fixtures';
}, {cwd: path.relative(process.cwd(), __dirname)});

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

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/');
Expand Down

0 comments on commit 583d30e

Please sign in to comment.