2013-05-14 9 views
20

Quindi, nella pagina di informazioni sui plugin di grunt-contrib-watch, c'è un esempio su come eseguire jshint solo per il file modificato.Gruntjs: come eseguire un'attività di copia per copiare solo i file modificati sull'orologio

grunt.initConfig({ 
    watch: { 
    scripts: { 
     files: ['lib/*.js'], 
     tasks: ['jshint'], 
     options: { 
     nospawn: true, 
     }, 
    }, 
    }, 
    jshint: { 
    all: ['lib/*.js'], 
    }, 
}); 

grunt.event.on('watch', function(action, filepath) { 
    grunt.config(['jshint', 'all'], filepath); 
}); 

Non ho provato l'esempio da solo. Ma ha preso questo e applicato alla mia attività di copia, senza successo. attività grunt-contrib-copy impostata per copiare immagini e modelli per il mio progetto angolare. E sarei felice di sapere se posso fare questo lavoro per il compito di copia e se posso, cosa sto facendo di sbagliato.

Grazie mille.

Ecco il mio estratto Gruntfile.js.

// Build configurations. 
module.exports = function(grunt){ 

    // Project configuration. 
    grunt.initConfig({ 

     pkg: grunt.file.readJSON('package.json'), 

     // Copies directories and files from one location to another. 
     copy: { 
     // DEVELOPMENT 
     devTmpl: { 
      files: [{ 
      cwd  : 'src/tpl/', 
      src  : ['**/*'], 
      dest : 'app/tpl/', 
      flatten : false, 
      expand : true 
      }] 
     }, 
     devImg: { 
      files: [{ 
      cwd  : 'src/img/', 
      src  : ['**/*'], 
      dest : 'app/img/', 
      flatten : false, 
      expand : true 
      }] 
     } 
     }, 

     // Watch files for changes and run tasks 
     watch: { 
     // Templates, copy 
     templates: { 
      files : 'src/tpl/**/*', 
      tasks : ['copy:devTmpl'], 
      options: { 
      nospawn: true, 
      } 
     }, 
     // Images, copy 
     images: { 
      files : 'src/img/**/*', 
      tasks : ['copy:devImg'], 
      options: { 
      nospawn: true, 
      } 
     } 
     } 

    }); 

    // Watch events 
    grunt.event.on('watch', function(action, filepath) { 
     // configure copy:devTmpl to only run on changed file 
     grunt.config(['copy','devTmpl'], filepath); 
     // configure copy:devImg to only run on changed file 
     grunt.config(['copy','devImg'], filepath); 
    }); 

    // PLUGINS: 
    grunt.loadNpmTasks('grunt-contrib-copy'); 


    // TASKS: 

    /* DEV: Compiles the app with non-optimized build settings, places the build artifacts in the dist directory, and watches for file changes. 
    run: grunt dev */ 
    grunt.registerTask('dev', 'Running "DEVELOPMENT", watching files and compiling...', [ 
     'default', 
     'watch' 
    ]); 

    /* DEFAULT: Compiles the app with non-optimized build settings and places the build artifacts in the dist directory. 
    run: grunt */ 
    grunt.registerTask('default', 'Running "DEFAULT", compiling everything.', [ 
     'copy:devTmpl', 
     'copy:devImg' 
    ]); 

} 
+0

C'è un'opzione per utilizzare un'attività più recente, penso. – GnrlBzik

risposta

5

è necessario puntare grunt.config alla proprietà corretta nella configurazione:

grunt.event.on('watch', function(action, filepath) { 
    var cfgkey = ['copy', 'devTmpl', 'files']; 
    grunt.config.set(cfgkey, grunt.config.get(cfgkey).map(function(file) { 
    file.src = filepath; 
    return file; 
    })); 
}); 
+0

In attesa ... Errore irreversibile: l'oggetto 0 non ha il metodo 'replace' – GnrlBzik

+0

btw, grazie @ kyle-robinson-young – GnrlBzik

+0

Oh mio errore, ho pensato che potevi impostare la configurazione in questo modo. Ho modificato la risposta. Prova quello invece. –

17

Usa grugnito-sync (https://npmjs.org/package/grunt-sync) invece di grugnito-contrib-copy, e guardare le directory che si desidera essere sincronizzato.

Update - ecco un esempio:

grunt.initConfig({ 
    sync: { 
    copy_resources_to_www: { 
     files: [ 
     { cwd: 'src', src: 'img/**', dest: 'www' }, 
     { cwd: 'src', src: 'res/**', dest: 'www' } 
     ] 
    } 
    } 
}); 

cwd significa directory di lavoro corrente. copy_resources_to_www è solo un'etichetta.

+0

sarebbe bello se i documenti avessero qualcosa di più utile da leggere. grunt-sync potrebbe essere bello, ma gli esempi sono molto ottusi. – lordB8r

Problemi correlati