<commit_before>module.exports = function( grunt ) {

var path = require( "path" ),
	crypto = require( "crypto" ),
	async = grunt.utils.async;

function createChecksum( str ) {
	var md5 = crypto.createHash( "md5" );
	md5.update( str, "utf8" );
	return md5.digest( "hex" );
}

grunt.registerHelper( "wordpress-get-resources", function( fn ) {
	var client = grunt.helper( "wordpress-client" );
	grunt.verbose.write( "Getting resources from WordPress..." );
	client.call( "gw.getResources", function( error, resources ) {
		if ( error ) {
			grunt.verbose.error();
			grunt.verbose.or.error( "Error getting resources from WordPress." );
			return fn( error );
		}

		grunt.verbose.ok();
		grunt.verbose.writeln();
		fn( null, resources );
	});
});

grunt.registerHelper( "wordpress-publish-resource", function( filepath, content, fn ) {
	var client = grunt.helper( "wordpress-client" );

	grunt.verbose.write( "Publishing " + filepath + "..." );
	client.authenticatedCall( "gw.addResource", filepath, content, function( error, checksum ) {
		if ( error ) {
			grunt.verbose.error();
			grunt.verbose.or.error( "Error publishing " + filepath + "." );
			return fn( error );
		}

		grunt.verbose.ok();
		grunt.verbose.or.writeln( "Published " + filepath + "." );
		fn( null, checksum );
	});
});

grunt.registerHelper( "wordpress-delete-resource", function( filepath, fn ) {
	var client = grunt.helper( "wordpress-client" );

	grunt.verbose.write( "Deleting " + filepath + "..." );
	client.authenticatedCall( "gw.deleteResource", filepath, function( error, checksum ) {
		if ( error ) {
			grunt.verbose.error();
			grunt.verbose.or.error( "Error deleting " + filepath + "." );
			return fn( error );
		}

		grunt.verbose.ok();
		fn( null, checksum );
	});
});

grunt.registerHelper( "wordpress-sync-resources", function( dir, fn ) {
	grunt.verbose.writeln( "Synchronizing resources.".bold );

	// Check if there are any resources to process
	if ( !path.existsSync( dir ) ) {
		grunt.verbose.writeln( "No resources to process." );
		grunt.verbose.writeln();
		return fn( null );
	}

	async.waterfall([
		function getResources( fn ) {
			grunt.helper( "wordpress-get-resources", fn );
		},

		function publishResources( resources, fn ) {
			grunt.verbose.writeln( "Processing resources.".bold );
			grunt.helper( "wordpress-recurse", dir, function( file, fn ) {
				var resource = file.substr( dir.length, file.length - dir.length ),
					content = grunt.file.read( file, "base64" ).toString( "base64" ),
					checksum = createChecksum( content );

				// Already exists, no need to update
				if ( resource in resources && checksum === resources[ resource ] ) {
					grunt.verbose.writeln( "Skiping " + resource + "; already up-to-date." );<commit_msg>Log when deleting resources. Fixes #7.<commit_after>module.exports = function( grunt ) {

var path = require( "path" ),
	crypto = require( "crypto" ),
	async = grunt.utils.async;

function createChecksum( str ) {
	var md5 = crypto.createHash( "md5" );
	md5.update( str, "utf8" );
	return md5.digest( "hex" );
}

grunt.registerHelper( "wordpress-get-resources", function( fn ) {
	var client = grunt.helper( "wordpress-client" );
	grunt.verbose.write( "Getting resources from WordPress..." );
	client.call( "gw.getResources", function( error, resources ) {
		if ( error ) {
			grunt.verbose.error();
			grunt.verbose.or.error( "Error getting resources from WordPress." );
			return fn( error );
		}

		grunt.verbose.ok();
		grunt.verbose.writeln();
		fn( null, resources );
	});
});

grunt.registerHelper( "wordpress-publish-resource", function( filepath, content, fn ) {
	var client = grunt.helper( "wordpress-client" );

	grunt.verbose.write( "Publishing " + filepath + "..." );
	client.authenticatedCall( "gw.addResource", filepath, content, function( error, checksum ) {
		if ( error ) {
			grunt.verbose.error();
			grunt.verbose.or.error( "Error publishing " + filepath + "." );
			return fn( error );
		}

		grunt.verbose.ok();
		grunt.verbose.or.writeln( "Published " + filepath + "." );
		fn( null, checksum );
	});
});

grunt.registerHelper( "wordpress-delete-resource", function( filepath, fn ) {
	var client = grunt.helper( "wordpress-client" );

	grunt.verbose.write( "Deleting " + filepath + "..." );
	client.authenticatedCall( "gw.deleteResource", filepath, function( error, checksum ) {
		if ( error ) {
			grunt.verbose.error();
			grunt.verbose.or.error( "Error deleting " + filepath + "." );
			return fn( error );
		}

		grunt.verbose.ok();
		grunt.verbose.or.writeln( "Deleted " + filepath + "." );
		fn( null, checksum );
	});
});

grunt.registerHelper( "wordpress-sync-resources", function( dir, fn ) {
	grunt.verbose.writeln( "Synchronizing resources.".bold );

	// Check if there are any resources to process
	if ( !path.existsSync( dir ) ) {
		grunt.verbose.writeln( "No resources to process." );
		grunt.verbose.writeln();
		return fn( null );
	}

	async.waterfall([
		function getResources( fn ) {
			grunt.helper( "wordpress-get-resources", fn );
		},

		function publishResources( resources, fn ) {
			grunt.verbose.writeln( "Processing resources.".bold );
			grunt.helper( "wordpress-recurse", dir, function( file, fn ) {
				var resource = file.substr( dir.length, file.length - dir.length ),
					content = grunt.file.read( file, "base64" ).toString( "base64" ),
					checksum = createChecksum( content );

				// Already exists, no need to update
				if ( resource in resources && checksum === resources[ resource ] ) {
					grunt.verbose.writeln( "Skiping " + resource + "; already up-to-date." );
{'fraction_non_alphanumeric': 0.16535150645624103, 'fraction_numerical': 0.0034074605451936872, 'mean_word_length': 3.6865546218487393, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4712162', 'n_tokens_mistral': 2001, 'n_tokens_neox': 1810, 'n_words': 428}