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

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

Gruntfile.js

//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
 
	grunt.initConfig({
 
		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
			}
		},
 
	});
 
	grunt.loadNpmTasks('grunt-contrib-watch');
	grunt.loadNpmTasks('grunt-contrib-copy');
	grunt.loadNpmTasks('grunt-newer');
 
	//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.
via: http://stackoverflow.com/questions/26664458/grunt-watch-event-with-grunt-copy-for-only-changed-files

https://docs.nodejitsu.com/articles/file-system/how-to-use-the-path-module/

Le Kevin

software engineer, photographer

You may also like...

Leave a Reply

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