Skip to content

Commit

Permalink
file.copy will now also copy directories recursively. Close gruntjsgh…
Browse files Browse the repository at this point in the history
  • Loading branch information
cowboy committed Jun 18, 2014
1 parent c5ae240 commit b50541b
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 1 deletion.
20 changes: 19 additions & 1 deletion lib/grunt/file.js
Expand Up @@ -304,7 +304,25 @@ file.write = function(filepath, contents, options) {
};

// Read a file, optionally processing its content, then write the output.
file.copy = function(srcpath, destpath, options) {
// Or read a directory, recursively creating directories, reading files,
// processing content, writing output.
file.copy = function copy(srcpath, destpath, options) {
if (file.isDir(srcpath)) {
// Copy a directory, recursively.
// Explicitly create new dest directory.
file.mkdir(destpath);
// Iterate over all sub-files/dirs, recursing.
fs.readdirSync(srcpath).forEach(function(filepath) {
copy(path.join(srcpath, filepath), path.join(destpath, filepath), options);
});
} else {
// Copy a single file.
file._copy(srcpath, destpath, options);
}
};

// Read a file, optionally processing its content, then write the output.
file._copy = function(srcpath, destpath, options) {
if (!options) { options = {}; }
// If a process function was specified, and noProcess isn't true or doesn't
// match the srcpath, process the file's source.
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/expand/css/baz.css
@@ -0,0 +1 @@
baz
1 change: 1 addition & 0 deletions test/fixtures/expand/css/qux.css
@@ -0,0 +1 @@
qux
1 change: 1 addition & 0 deletions test/fixtures/expand/deep/deep.txt
@@ -0,0 +1 @@
deep
1 change: 1 addition & 0 deletions test/fixtures/expand/deep/deeper/deeper.txt
@@ -0,0 +1 @@
deeper
1 change: 1 addition & 0 deletions test/fixtures/expand/deep/deeper/deepest/deepest.txt
@@ -0,0 +1 @@
deepest
1 change: 1 addition & 0 deletions test/fixtures/expand/js/bar.js
@@ -0,0 +1 @@
bar
1 change: 1 addition & 0 deletions test/fixtures/expand/js/foo.js
@@ -0,0 +1 @@
foo
26 changes: 26 additions & 0 deletions test/grunt/file_test.js
Expand Up @@ -604,6 +604,32 @@ exports['file'] = {

test.done();
},
'copy directory recursively': function(test) {
test.expect(34);
var copyroot1 = path.join(tmpdir.path, 'copy-dir-1');
var copyroot2 = path.join(tmpdir.path, 'copy-dir-2');
grunt.file.copy('test/fixtures/expand/', copyroot1);
grunt.file.recurse('test/fixtures/expand/', function(srcpath, rootdir, subdir, filename) {
var destpath = path.join(copyroot1, subdir || '', filename);
test.ok(grunt.file.isFile(srcpath), 'file should have been copied.');
test.equal(grunt.file.read(srcpath), grunt.file.read(destpath), 'file contents should be the same.');
});
grunt.file.mkdir(path.join(copyroot1, 'empty'));
grunt.file.mkdir(path.join(copyroot1, 'deep/deeper/empty'));
grunt.file.copy(copyroot1, copyroot2, {
process: function(contents) {
return '<' + contents + '>';
},
});
test.ok(grunt.file.isDir(path.join(copyroot2, 'empty')), 'empty directory should have been created.');
test.ok(grunt.file.isDir(path.join(copyroot2, 'deep/deeper/empty')), 'empty directory should have been created.');
grunt.file.recurse('test/fixtures/expand/', function(srcpath, rootdir, subdir, filename) {
var destpath = path.join(copyroot2, subdir || '', filename);
test.ok(grunt.file.isFile(srcpath), 'file should have been copied.');
test.equal('<' + grunt.file.read(srcpath) + '>', grunt.file.read(destpath), 'file contents should be processed correctly.');
});
test.done();
},
'delete': function(test) {
test.expect(2);
var oldBase = process.cwd();
Expand Down

0 comments on commit b50541b

Please sign in to comment.