I've had to write my own cut-down sync because it's a strange project, but the biggest change is handling the rename function as some paths get extended in it (ie, "src/from/file.ext" becomes "dest/extra/from/file.ext") - without this rename support checking for files that should be deleted in the dest can't work properly.
http://gruntjs.com/configuring-tasks#building-the-files-object-dynamically
This doesn't support things properly, and could do with handling options like "flatten" properly too (not sure if that option should delete any subfolders, or only concern itself with files - separate issue though) ;-)
My code is as follows, quickly written and not polished, but hopefully shows the issue:
var i, j, paths, destfile, srcfile, exclude,
options = this.options(),
deleted = 0,
copied = 0,
src = {},
dests = [];
// list all files in the dest that should be there - quick access
for (i = 0, paths = this.files; i < paths.length; i++) {
src[paths[i].dest] = true;
}
// list all potential destination paths
for (i = 0, paths = this.data.files; i < paths.length; i++) {
for (j = 0; j < paths[i].src.length; j++) {
srcfile = paths[i].src[j];
exclude = srcfile[0] === "!";
if (exclude) {
srcfile = srcfile.substring(1);
}
destfile = paths[i].dest;
// IMPORTANT PART HERE
dests.push((exclude ? "!" : "") + (paths[i].rename ? paths[i].rename(destfile, srcfile) : destfile + srcfile));
}
}
grunt.verbose.writeln("Checking: " + chalk.cyan(dests.join(", ")));
// list all files in potential destination paths and delete the ones that shouldn't be there
for (i = 0, paths = grunt.file.expand({filter: "isFile"}, dests); i < paths.length; i++) {
if (!src[paths[i]]) {
grunt.verbose.writeln("Deleting " + chalk.cyan(paths[i]));
grunt.file.delete(paths[i]);
deleted++;
}
}
// copy any files that have changed
// --snipped--
...and the relevant options:
sync_files: {
main: {
files: [{
expand: true,
cwd: "src/",
src: ["*/from/**"],
dest: "dest/",
filter: "isFile",
rename: function(dest, src) {
return dest + src.replace("/from/", "/extra/from/");
}
}]
}
}
I've had to write my own cut-down sync because it's a strange project, but the biggest change is handling the rename function as some paths get extended in it (ie, "src/from/file.ext" becomes "dest/extra/from/file.ext") - without this rename support checking for files that should be deleted in the dest can't work properly.
http://gruntjs.com/configuring-tasks#building-the-files-object-dynamically
This doesn't support things properly, and could do with handling options like "flatten" properly too (not sure if that option should delete any subfolders, or only concern itself with files - separate issue though) ;-)
My code is as follows, quickly written and not polished, but hopefully shows the issue:
...and the relevant options: