Ok, I was stuck on this for 2 weeks, so hopefully someone else ran into this problem. I am trying to use Grunt to copy only files that have been modified. I have seen many examples of how to do this with JSLINT and UGLIFY, but there are no concrete examples of how to do this with grunt-contrib-copy.
When you register a viewing event and pass the file name into the copy subtask, the file name is available (I exit), but the file is never copied properly.
I hope this is a simple thing that I do not notice. Can someone please take a look at my code and see what I am doing wrong?
//Gruntfile.js: module.exports = function(grunt) { grunt.initConfig({ pkg: grunt.file.readJSON('package.json'), options: { base: 'app', dist: 'dist', }, copy: { changedFiles: { expand: true, dot: true, cwd: '<%= options.base %>', src: ['**/*.*'], dest: '<%= options.dist %>/' } }, watch: { options: { nospawn: true, //debounceDelay: 1000, }, css: { files: ['app/css/*.css', 'app/js/*.js' ], tasks: ['copy:changedFiles'], } } }); grunt.event.on('watch', function(action, filepath, target){ grunt.log.writeln('target: ', target + '\n filepath: ' + filepath + '\n action: has ' + action); grunt.config('copy.changedFiles.src', new Array(filepath) ); }); //load our copy task grunt.loadNpmTasks('grunt-contrib-copy'); //load our watch task grunt.loadNpmTasks('grunt-contrib-watch'); grunt.registerTask('copyChangedFiles', [ 'watch:css' ]); };
Basically, setting up my folder is as follows:
-app | - css | - js -dist
I am browsing the application folder and trying to copy files that change in the application directory and copy them to the dist directory. Dynamically changing the src copy does not seem to work.
The task of copying at startup by itself with the clock, and not on the hourly event, works just fine and copies all the files, but I'm only interested in copying files that change.
I also tried a variant of this in my watch event, but to no avail:
var copyDest = filepath.replace(grunt.config('copy.changedFiles.dest'), ''); var copyCwd = filepath.replace(grunt.config('copy.changedFiles.cwd'), ''); grunt.config('copy.changedFiles.cwd' , copyCwd); grunt.config(['copy', 'changedFiles', 'src'] , [filepath]);
Has anyone ever done this before using a grunted copy? Or is there another task that I should use? I tried the same with grunt-sync, and that didn't work either. I am stuck.
Thanks for the help.