Skip to content
This repository was archived by the owner on Apr 25, 2023. It is now read-only.
This repository was archived by the owner on Apr 25, 2023. It is now read-only.

Handle "rename" function in files #40

@Rycochet

Description

@Rycochet

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/");
                }
            }]
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions