<commit_before>/*
 * grunt-cartero
 * https://github.com/rotundasoftware/cartero
 *
 * Copyright (c) 2013 Rotunda Software, LLC
 * Licensed under the MIT license.
 */

var	_ = require( "underscore" ),
	_s = require( "underscore.string" ),
	fs = require( "fs" ),
	findit = require( "findit" ),
	path = require( "path" ),
	detective = require( "detective" ),
	resolve = require( "resolve" ),
	browserify = require( "browserify" ),
	async = require( "async" ),
	Bundle = require( "./../lib/bundle" ),
	Parcel = require( "./../lib/parcel" ),
	File = require( "./../lib/file" );

'use strict';

module.exports = function(grunt) {

	// Prefix for all task targets added by cartero to avoid conflicts with already existing targets.
	var kCarteroTaskPrefix = "cartero_";
	var kCarteroJsonFile = "cartero.json";

	var kLibraryAssetsDirPrefix = "library-assets";
	var kViewAssetsDirPrefix = "view-assets";

	var kRequiredConfigOptions = [ "mode", "projectDir", "publicDir", "library", "views", "tmplExt" ];
	var kRequiredLibraryConfigOptions = [ "path" ];
	var kRequiredViewsConfigOptions = [ "path", "viewFileExt" ];

	// cartero directive: When browserify is enabled, this directive is used in js files in views that should be automatically run upon loading.
	var kBrowserifyExecuteOnLoad = "##cartero_browserify_executeOnLoad";

	// Default values for the views task option.
	var kViewsDirDefaults = {
		filesToIgnore : /^_.*/,
		directoriesToIgnore : /^__.*/,
		directoriesToFlatten : /^_.*/
	};

	// Default values for the library task option.
	var kLibraryDirDefaults = {
		childrenDependOnParents : true,
		directoriesToFlatten : /^_.*/
	};

	// Global default values
	var kOptionsDefaults = {
		browserify : false
	};

	var kValidImageExts = [ ".jpg", ".png", ".gif", ".bmp", ".jpeg" ];

	// Will contain options passed into the cartero task with defaults applied.
	var options = {};

	var bundleRegistry = {};
	var parcelRegistry = {};

	// Files that are browserified and need to be run upon loading.
	var browserifyExecuteOnLoadFiles = [];

	// Convenience function that is used by the watch tasks when cartero metadata changes and the pageMap and bundleMap need to be rebuilt.
	function rebundle() {
		grunt.task.run( kCarteroTaskPrefix + "buildBundleAndParcelRegistries:dev" );
		grunt.task.run( kCarteroTaskPrefix + "separateFilesToServeByType:dev" );
		grunt.task.run( kCarteroTaskPrefix + "saveCarteroJson" );
	}

	// Processes a CSS file looking for url().  Replaces relative paths with absolute ones ( to the staticDir ).
	function makeUrlsAbsoluteInCssFile( fileName, callback ) {
		fs.readFile( fileName, function( err, data ) {

			var fileContents = data.toString().replace( /url\(\s*?[\"\']?\s*?([^)]+)\s*?[\"\']?\s*?\)/g, function( match, url ) {
				url = url.trim();

				// we don't support absolute URLs for now
				if( url[0] === "/" ) return match;

				var pathRelativeToProjectDir = fileName.replace(/\/[^\/]*$/,"/") + path.sep + url;

				// sanity check: make sure url() contains a file path
				if( fs.existsSync( pathRelativeToProjectDir ) ) {
					return "url(" + "/" + path.relative( options.publicDir, pathRelativeToProjectDir ) + ")";
				}
				else {
					return match;
				}

			} );

			fs.writeFile( fileName, fileContents, function( err ) {
				callback( err );
			} );
		} );
	}

	// returns true if the given fileName represents a server-side view
	function isViewFile( fileName ) {
		var viewFile = _.find( options.views, function( dirOptions ) {
			return _s.startsWith( fileName, dirOptions.path ) && _.contains( dirOptions.viewFileExt, File.getFileExtension( fileName ) );
		} );

		return ! _.isUndefined( viewFile );
	}

	// returns true if the given fileName is in a `views` directory
	function fileIsInViewDirectory( fileName ) {
		var result = _.find( options.views, function( dirOptions ) {
			return _s.startsWith( fileName, dirOptions.path );
		} );

		return ! _.isUndefined( result );
	}

	function configureCarteroBrowserifyTask( libraryAndViewDirs, projectDir ) {

		var carteroBrowserify = grunt.config( kCarteroTaskPrefix + "browserify" ) || {};

		var browserifyFiles = _.map( libraryAndViewDirs, function( dirOptions ) {
			return {
				cwd : dirOptions.path,
				src : [ "**/*.js" ],
				dest : dirOptions.destDir,
				expand : true
			};
		} );

		carteroBrowserify[ "default" ] = {
			files : browserifyFiles
		};

		grunt.config( kCarteroTaskPrefix + "browserify", carteroBrowserify );
	}

	function configureUserDefinedTask( libraryAndViewDirs, taskConfig, doWatch, sourceIsDest ) {
		var taskName = taskConfig.name;
		var task = grunt.config( taskName ) || {};
		var watch = grunt.config( "watch" ) || {};

		_.each( libraryAndViewDirs, function( dir ) {

			var taskTarget = kCarteroTaskPrefix + dir.path;

			var files = [ {
				expand: true,
				cwd: sourceIsDest ? dir.destDir : dir.path,
				src: [ "**/*" + taskConfig.inExt ],
				dest: dir.destDir,
				ext: taskConfig.outExt
			} ];

			task[ taskTarget ] = {
				files : files
			};

			if( ! _.isUndefined( taskConfig.options ) ) {
				if( _.isFunction( taskConfig.options ) )
					task[ taskTarget ].options = taskConfig.options( { srcDir : dir.path, destDir : dir.destDir } );
				else
					task[ taskTarget ].options = taskConfig.options;
			}

			if( doWatch && options.watch ) {
				watch[ kCarteroTaskPrefix + taskName + "_" + taskTarget ] = {
					files : [ dir.path + "/**/*" + taskConfig.inExt ],
					tasks : [ taskName + ":" + taskTarget ],
					options : {
						nospawn : true
					}
				};
			}

		} );

		grunt.config( taskName, task );
		grunt.config( "watch", watch );

	}
<commit_msg>small watch related change<commit_after>/*
 * grunt-cartero
 * https://github.com/rotundasoftware/cartero
 *
 * Copyright (c) 2013 Rotunda Software, LLC
 * Licensed under the MIT license.
 */

var	_ = require( "underscore" ),
	_s = require( "underscore.string" ),
	fs = require( "fs" ),
	findit = require( "findit" ),
	path = require( "path" ),
	detective = require( "detective" ),
	resolve = require( "resolve" ),
	browserify = require( "browserify" ),
	async = require( "async" ),
	Bundle = require( "./../lib/bundle" ),
	Parcel = require( "./../lib/parcel" ),
	File = require( "./../lib/file" );

'use strict';

module.exports = function(grunt) {

	// Prefix for all task targets added by cartero to avoid conflicts with already existing targets.
	var kCarteroTaskPrefix = "cartero_";
	var kCarteroJsonFile = "cartero.json";

	var kLibraryAssetsDirPrefix = "library-assets";
	var kViewAssetsDirPrefix = "view-assets";

	var kRequiredConfigOptions = [ "mode", "projectDir", "publicDir", "library", "views", "tmplExt" ];
	var kRequiredLibraryConfigOptions = [ "path" ];
	var kRequiredViewsConfigOptions = [ "path", "viewFileExt" ];

	// cartero directive: When browserify is enabled, this directive is used in js files in views that should be automatically run upon loading.
	var kBrowserifyExecuteOnLoad = "##cartero_browserify_executeOnLoad";

	// Default values for the views task option.
	var kViewsDirDefaults = {
		filesToIgnore : /^_.*/,
		directoriesToIgnore : /^__.*/,
		directoriesToFlatten : /^_.*/
	};

	// Default values for the library task option.
	var kLibraryDirDefaults = {
		childrenDependOnParents : true,
		directoriesToFlatten : /^_.*/
	};

	// Global default values
	var kOptionsDefaults = {
		browserify : false
	};

	var kValidImageExts = [ ".jpg", ".png", ".gif", ".bmp", ".jpeg" ];

	// Will contain options passed into the cartero task with defaults applied.
	var options = {};

	var bundleRegistry = {};
	var parcelRegistry = {};

	// Files that are browserified and need to be run upon loading.
	var browserifyExecuteOnLoadFiles = [];

	// Convenience function that is used by the watch tasks when cartero metadata changes and the pageMap and bundleMap need to be rebuilt.
	function rebundle() {
		grunt.task.run( kCarteroTaskPrefix + "buildBundleAndParcelRegistries:dev" );
		grunt.task.run( kCarteroTaskPrefix + "separateFilesToServeByType:dev" );
		grunt.task.run( kCarteroTaskPrefix + "saveCarteroJson" );
	}

	// Processes a CSS file looking for url().  Replaces relative paths with absolute ones ( to the staticDir ).
	function makeUrlsAbsoluteInCssFile( fileName, callback ) {
		fs.readFile( fileName, function( err, data ) {

			var fileContents = data.toString().replace( /url\(\s*?[\"\']?\s*?([^)]+)\s*?[\"\']?\s*?\)/g, function( match, url ) {
				url = url.trim();

				// we don't support absolute URLs for now
				if( url[0] === "/" ) return match;

				var pathRelativeToProjectDir = fileName.replace(/\/[^\/]*$/,"/") + path.sep + url;

				// sanity check: make sure url() contains a file path
				if( fs.existsSync( pathRelativeToProjectDir ) ) {
					return "url(" + "/" + path.relative( options.publicDir, pathRelativeToProjectDir ) + ")";
				}
				else {
					return match;
				}

			} );

			fs.writeFile( fileName, fileContents, function( err ) {
				callback( err );
			} );
		} );
	}

	// returns true if the given fileName represents a server-side view
	function isViewFile( fileName ) {
		var viewFile = _.find( options.views, function( dirOptions ) {
			return _s.startsWith( fileName, dirOptions.path ) && _.contains( dirOptions.viewFileExt, File.getFileExtension( fileName ) );
		} );

		return ! _.isUndefined( viewFile );
	}

	// returns true if the given fileName is in a `views` directory
	function fileIsInViewDirectory( fileName ) {
		var result = _.find( options.views, function( dirOptions ) {
			return _s.startsWith( fileName, dirOptions.path );
		} );

		return ! _.isUndefined( result );
	}

	function configureCarteroBrowserifyTask( libraryAndViewDirs, projectDir ) {

		var carteroBrowserify = grunt.config( kCarteroTaskPrefix + "browserify" ) || {};

		var browserifyFiles = _.map( libraryAndViewDirs, function( dirOptions ) {
			return {
				cwd : dirOptions.path,
				src : [ "**/*.js" ],
				dest : dirOptions.destDir,
				expand : true
			};
		} );

		carteroBrowserify[ "default" ] = {
			files : browserifyFiles
		};

		grunt.config( kCarteroTaskPrefix + "browserify", carteroBrowserify );
	}

	function configureUserDefinedTask( libraryAndViewDirs, taskConfig, doWatch, sourceIsDest ) {
		var taskName = taskConfig.name;
		var task = grunt.config( taskName ) || {};
		var watch = grunt.config( "watch" ) || {};

		_.each( libraryAndViewDirs, function( dir ) {

			var taskTarget = kCarteroTaskPrefix + dir.path;

			var files = [ {
				expand: true,
				cwd: sourceIsDest ? dir.destDir : dir.path,
				src: [ "**/*" + taskConfig.inExt ],
				dest: dir.destDir,
				ext: taskConfig.outExt
			} ];

			task[ taskTarget ] = {
				files : files
			};

			if( ! _.isUndefined( taskConfig.options ) ) {
				if( _.isFunction( taskConfig.options ) )
					task[ taskTarget ].options = taskConfig.options( { srcDir : dir.path, destDir : dir.destDir } );
				else
					task[ taskTarget ].options = taskConfig.options;
			}

			if( doWatch && options.watch ) {
				watch[ kCarteroTaskPrefix + taskName + "_" + taskTarget ] = {
					files : [ dir.path + "/**/*" + taskConfig.inExt ],
					tasks : [ taskName + ":" + taskTarget ],
					options : {
						nospawn : true
					}
				};
			}

		} );

		grunt.config( taskName, task );

		if( doWatch && options.watch ) {
			grunt.config( "watch", watch );
		}
	}

{'fraction_non_alphanumeric': 0.140582146332832, 'fraction_numerical': 0.0008847208705653366, 'mean_word_length': 3.434680266771283, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, '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': '6912690', 'n_tokens_mistral': 4080, 'n_tokens_neox': 3636, 'n_words': 950}