grunt-contrib-copy
grunt-contrib-copy copied to clipboard
Do not overwrite an existing file
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
Why? What's your use-case for needing that?
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
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.
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?
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.
see "cp --update"
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 :)
An alternative is to use the grunt-newer plugin, which works alongside with this one.