grunt-contrib-copy icon indicating copy to clipboard operation
grunt-contrib-copy copied to clipboard

Do not overwrite an existing file

Open monolithed opened this issue 10 years ago • 8 comments

copy: {
    main: {
        files: [
            {
                cwd: 'config/',
                expand: true,
                src   : ['*.yml'],
                dest  : 'config/local/',
            }
        ]
    }
}
➜ grunt copy
Running "copy:config" (copy) task
Copied 3 files

My solution:

filter: function (name) {
    try {
        var file = path.join(this.cwd, 'local', path.basename(name)),
            stat = fs.statSync(file);

        return !stat.isFile();
    }
    catch (error) {
        return true;
    }
}

You'd add the override option like -n in the cp command

monolithed avatar Jun 22 '14 21:06 monolithed

Why? What's your use-case for needing that?

sindresorhus avatar Jun 23 '14 07:06 sindresorhus

Hm. I've to create a local copy of the configuration files. If the files already exist, I don't need to overwrite them. And these files I add to .gitignore (very popular use-case)

PS: And very strange that there is no similar functionality with cp

monolithed avatar Jun 23 '14 14:06 monolithed

This would also come in useful to speed up copying dependencies and such. grunt-sync looks more useful in this case though.

Also, cp has the -u option.

timdp avatar Oct 09 '14 14:10 timdp

i work on a very huge project, it contains many separated modules. Every module contains html,js,css and img.

When i run the grunt-task for build the project, it creates for production only one css file, and i want to copy all the images file into 'production/images/'.

I wish to get, at least a Warning message if the files are overwritten. It is possible?

lucianomurr avatar Feb 13 '15 18:02 lucianomurr

We are copying to a store of all versions and want to make sure we do not overwrite a particular version. so I'd love an overwrite option I can set to false that would error the task. Instead I have to make a new task that errors if the directory exists.

lukeapage avatar Apr 28 '15 14:04 lukeapage

see "cp --update"

masi avatar Dec 04 '15 14:12 masi

You can use a filter to detect existing files itself. Credit to Chris Sherman

copy: {
  main: {
    expand: true,
    cwd: '/src',
    src: ['js/*'],
    dest: '/some-dest'
    filter: function (filepath) {
      var path = require('path');
      var dest = path.join(
        grunt.config('copy.main.dest'),
        path.basename(filepath)
      );

      return !(grunt.file.exists(dest));
    },
  },
}

So, issue can be closed in my opinion :)

justusromijn avatar May 27 '16 08:05 justusromijn

An alternative is to use the grunt-newer plugin, which works alongside with this one.

lonix1 avatar Feb 05 '21 06:02 lonix1