Dan
Dan

Reputation: 57881

grunt: Watch and synchronize files

I want to run a local sync daemon.

It should watch src directory and copy any changed file to dst directory. There are many subdirectories in src, so the relative paths should be preserved. All necessary subdirectories in dst are already created.

I want to filter for *.js files only.

I've found a grunt-sync tool. The documentation is so short that I even don't know if it fits me. Here's my Gruntfile:

module.exports = function (grunt) {

    grunt.initConfig({
        sync: {
          main: {
            files: [{
                cwd: src,
                src: ['**/*.js'],
                dest: dst
            }]
          }
        }    
    });

    grunt.loadNpmTasks('grunt-sync');
    grunt.registerTask('default', 'sync');

};

When I run grunt, I see no watch task. Grunt freezes for several seconds, then copies all files and exits. And I want a watch-sync daemon.

Upvotes: 2

Views: 2424

Answers (2)

trucheromayor
trucheromayor

Reputation: 21

Probably a bit late but.. I had the same question. What I´ve done is to combine both tools.. grunt-sync and grunt-contrib-watch..

module.exports = function (grunt) {

grunt.initConfig({
    sync: {
      main: {
        files: [{
            cwd: src,
            src: ['**/*.js'],
            dest: dst
        }]
      }
    },
    watch: {
       jsxfiles: {
       files: ['**/*.js'],
       tasks: ['sync:main'],
     } //this means that when any of the pattern files changes, the sync task will be run...
     }  
 });

grunt.loadNpmTasks('grunt-sync');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.registerTask('default', 'watch'); //run the watch task isntead of just the sync once..

};

I hope this is helpful

KR Jon

Upvotes: 0

vilicvane
vilicvane

Reputation: 12413

I created a tool called duplicate which will do what you want (though a year later), it copies all matching files when start, and keep watching if there would be any changes.

It's not written as a Gulp or Grunt plugin because it doesn't need to be, but of course you may easily run it in a Gulp or Grunt task.

Check out https://github.com/vilic/duplicate

Upvotes: 2

Related Questions