Grunt Copy Only Changed/Watched File Only (Node.js)

Only copy modified file in “grunt watch”, see code bellow.


//code for Gruntfile.js
module.exports = function (grunt) {
	var srcF = 'D:/PROJECTS/2016/Website/';
	var outF = '\\\\Computer\\d$\\2016\\Website\\'; //WINDOWS MAPPED DRIVE ie: Q:\2016\Website
		watch: {
			options: {
				spawn: false,
				livereload: true
			asp: {
				files: [srcF + '**/*.asp', srcF + '/blog/**/*', srcF + '/images/wcl/**/*.*'],
				tasks: ['copy:changedFilesONLY'],
		copy: {
			changedFiles: {
				expand: true,
				cwd: srcF,
				src: ['**/*.asp'],
				dest: outF
	//var path = require('path');
	grunt.event.on('watch', function (action, filepath, target) {
		grunt.log.writeln(target + ': ' + filepath + ' might have ' + action);
		//changes changed file source to that of the changed file
		var option = 'copy.changedFiles.src';
		var result = filepath;
		grunt.log.writeln(option + ' changed to ' + result);
		grunt.config(option, result);
		//customizes output directory so that file goes to correct place
		option = 'copy.changedFiles.dest';
		result = outF;
		grunt.log.writeln(option + ' changed to ' + result);
		grunt.config(option, result);
	grunt.registerTask('changedFilesONLY', ['watch:asp']);

If you haven’t used Grunt before, be sure to check out the Getting Started guide, as it explains how to create a Gruntfile as well as install and use Grunt plugins.

Le Kevin

software engineer, photographer

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *