<commit_before>var assert = require( 'assert' );
var fs = require( 'fs' );
var child_process = require( 'child_process' );
var info = require( '../../sherpa/info' );
var _ = require( '../../sherpa/lodash-2.4.1.min' );
var checkoutShas = require( '../../chipper/grunt/checkout-shas' );
var pullAll = require( '../../chipper/grunt/pull-all' );

//Register fs as a global so it can be accessed through the requirejs build system.  Text.js plugin may have a superior way to handle this but I (SR) couldn't get it working after a small amount of effort
global.fs = fs;

/**
 * Grunt configuration file for simulations.
 * Requires a package.json file containing project settings.
 *
 * @author <NAME> (PixelZoom, Inc.)
 * @author <NAME>
 * @author <NAME>
 * @author <NAME>
 */
module.exports = function( grunt ) {
  function trimWhitespace( str ) {
    return str.replace( /^\s\s*/, '' ).replace( /\s\s*$/, '' );
  }

  function padString( str, n ) {
    while ( str.length < n ) {
      str += ' ';
    }
    return str;
  }

  //TODO: This is copied in requirejs-plugins/loadFileAsDataURI and should be deleted from here if we can figure out how to load that one
  function loadFileAsDataURI( filename ) {
    var mimeType = {
      'png': 'image/png',
      'svg': 'image/svg+xml',
      'jpg': 'image/jpeg',
      'cur': 'image/x-icon', // cursor files (used in build-a-molecule). x-win-bitmap gives off warnings in Chrome
      'mp3': 'audio/mpeg',
      'm4a': 'audio/mp4',
      'ogg': 'audio/ogg',
      'oga': 'audio/ogg',
      'bma': 'audio/webm', // webma is the full extension
      'wav': 'audio/wav'
    }[filename.slice( -3 )];
    assert( mimeType, 'Unknown mime type for filename: ' + filename );

    return 'data:' + mimeType + ';base64,' + Buffer( fs.readFileSync( filename ) ).toString( 'base64' );
  }

  function stringReplace( str, substring, replacement ) {
    var idx = str.indexOf( substring );
    if ( str.indexOf( substring ) !== -1 ) {
      return str.slice( 0, idx ) + replacement + str.slice( idx + substring.length );
    }
    else {
      return str;
    }
  }

  assert( fs.existsSync( 'package.json' ), 'We need to be in a sim directory with package.json' );

  var pkg = grunt.file.readJSON( 'package.json' );

  // TODO: eek, this is scary! we are importing from the sim dir. ideally we should just have uglify-js installed once in chipper?
  var uglify = require( '../../' + pkg.name + '/node_modules/uglify-js' );
  var requirejs = require( '../../' + pkg.name + '/node_modules/requirejs' ); // TODO: not currently used, figure out how to include almond correctly?

  var preloadMapFilename = 'preload.js.map';

  // Project configuration.
  grunt.initConfig(
    {
      // Read in the project settings from the package.json file into the pkg property.
      // This allows us to refer to project settings from within this config file.
      pkg: pkg,

      // configure the RequireJS plugin
      requirejs: {

        // builds the minified script
        build: {
          options: {
            almond: true,
            mainConfigFile: 'js/<%= pkg.name %>-config.js',
            out: 'build/<%= pkg.name %>.min.js',
            name: '<%= pkg.name %>-config',

            //Minification strategy.  Put this to none if you want to debug a non-minified but compiled version
            optimize: 'uglify2',
            wrap: true,
//            generateSourceMaps: true, //#42 commented out this line until source maps are fixed
            preserveLicenseComments: false,
            uglify2: {
              output: {
                inline_script: true // escape </script
              },
              compress: {
                global_defs: {
                  // global assertions
                  assert: false,
                  assertSlow: false,
                  // scenery logging
                  sceneryLayerLog: false,
                  sceneryEventLog: false,
                  sceneryAccessibilityLog: false,
                  phetAllocation: false
                },
                dead_code: true
              }
            },

            //stub out the plugins so their source code won't be included in the minified file
            stubModules: ['string', 'audio', 'image']
          }
        }
      },

      // configure the JSHint plugin
      jshint: {
        // source files that are specific to this simulation
        simFiles: [ 'js/**/*.js' ],
        // source files for common-code dependencies.
        // phetLibs is a string of repo names, space separated.
        // split converts the string to an array of repo names.
        // map then converts each repo name to the form '../repo/js/**/*.js'.
        commonFiles: [ _.map( pkg.phetLibs.split( ' ' ), function( repo ) { return '../' + repo + '/js/**/*.js'; } ) ],
        // reference external JSHint options in jshint-options.js
        options: require( './jshint-options' )
      }
    } );

  var clean = function() {
    if ( fs.existsSync( 'build' ) ) {
      grunt.log.writeln( 'Cleaning build directory' );
      grunt.file.delete( 'build' );
    }
    grunt.file.mkdir( 'build' );
  };

  // Default task ('grunt')<commit_msg>Including linting exclusion for Kite's svg parser auto-generated code<commit_after>var assert = require( 'assert' );
var fs = require( 'fs' );
var child_process = require( 'child_process' );
var info = require( '../../sherpa/info' );
var _ = require( '../../sherpa/lodash-2.4.1.min' );
var checkoutShas = require( '../../chipper/grunt/checkout-shas' );
var pullAll = require( '../../chipper/grunt/pull-all' );

//Register fs as a global so it can be accessed through the requirejs build system.  Text.js plugin may have a superior way to handle this but I (SR) couldn't get it working after a small amount of effort
global.fs = fs;

/**
 * Grunt configuration file for simulations.
 * Requires a package.json file containing project settings.
 *
 * @author <NAME> (PixelZoom, Inc.)
 * @author <NAME>
 * @author <NAME>
 * @author <NAME>
 */
module.exports = function( grunt ) {
  function trimWhitespace( str ) {
    return str.replace( /^\s\s*/, '' ).replace( /\s\s*$/, '' );
  }

  function padString( str, n ) {
    while ( str.length < n ) {
      str += ' ';
    }
    return str;
  }

  //TODO: This is copied in requirejs-plugins/loadFileAsDataURI and should be deleted from here if we can figure out how to load that one
  function loadFileAsDataURI( filename ) {
    var mimeType = {
      'png': 'image/png',
      'svg': 'image/svg+xml',
      'jpg': 'image/jpeg',
      'cur': 'image/x-icon', // cursor files (used in build-a-molecule). x-win-bitmap gives off warnings in Chrome
      'mp3': 'audio/mpeg',
      'm4a': 'audio/mp4',
      'ogg': 'audio/ogg',
      'oga': 'audio/ogg',
      'bma': 'audio/webm', // webma is the full extension
      'wav': 'audio/wav'
    }[filename.slice( -3 )];
    assert( mimeType, 'Unknown mime type for filename: ' + filename );

    return 'data:' + mimeType + ';base64,' + Buffer( fs.readFileSync( filename ) ).toString( 'base64' );
  }

  function stringReplace( str, substring, replacement ) {
    var idx = str.indexOf( substring );
    if ( str.indexOf( substring ) !== -1 ) {
      return str.slice( 0, idx ) + replacement + str.slice( idx + substring.length );
    }
    else {
      return str;
    }
  }

  assert( fs.existsSync( 'package.json' ), 'We need to be in a sim directory with package.json' );

  var pkg = grunt.file.readJSON( 'package.json' );

  // TODO: eek, this is scary! we are importing from the sim dir. ideally we should just have uglify-js installed once in chipper?
  var uglify = require( '../../' + pkg.name + '/node_modules/uglify-js' );
  var requirejs = require( '../../' + pkg.name + '/node_modules/requirejs' ); // TODO: not currently used, figure out how to include almond correctly?

  var preloadMapFilename = 'preload.js.map';

  // Project configuration.
  grunt.initConfig(
    {
      // Read in the project settings from the package.json file into the pkg property.
      // This allows us to refer to project settings from within this config file.
      pkg: pkg,

      // configure the RequireJS plugin
      requirejs: {

        // builds the minified script
        build: {
          options: {
            almond: true,
            mainConfigFile: 'js/<%= pkg.name %>-config.js',
            out: 'build/<%= pkg.name %>.min.js',
            name: '<%= pkg.name %>-config',

            //Minification strategy.  Put this to none if you want to debug a non-minified but compiled version
            optimize: 'uglify2',
            wrap: true,
//            generateSourceMaps: true, //#42 commented out this line until source maps are fixed
            preserveLicenseComments: false,
            uglify2: {
              output: {
                inline_script: true // escape </script
              },
              compress: {
                global_defs: {
                  // global assertions
                  assert: false,
                  assertSlow: false,
                  // scenery logging
                  sceneryLayerLog: false,
                  sceneryEventLog: false,
                  sceneryAccessibilityLog: false,
                  phetAllocation: false
                },
                dead_code: true
              }
            },

            //stub out the plugins so their source code won't be included in the minified file
            stubModules: ['string', 'audio', 'image']
          }
        }
      },

      // configure the JSHint plugin
      jshint: {
        // source files that are specific to this simulation
        simFiles: [ 'js/**/*.js' ],
        // source files for common-code dependencies.
        // phetLibs is a string of repo names, space separated.
        // split converts the string to an array of repo names.
        // map then converts each repo name to the form '../repo/js/**/*.js'.
        // includes an exclusion for kite/js/parser/svgPath.js, which is auto-generated
        commonFiles: [ _.map( pkg.phetLibs.split( ' ' ), function( repo ) { return '../' + repo + '/js/**/*.js'; } ), '!../kite/js/parser/svgPath.js' ],
        // reference external JSHint options in jshint-options.js
        options: require( './jshint-options' )
      }
    } );

  var clean = function() {
    if ( fs.existsSync( 'build' ) ) {
      grunt.log.writeln( 'Cleaning build directory' );
      grunt.file.delete( 'build' );
    }
    grunt.file.mkdir( 'build' );
  };

  // Default task ('grunt')
{'fraction_non_alphanumeric': 0.13366853239730575, 'fraction_numerical': 0.0032255004269044684, 'mean_word_length': 2.073469387755102, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2803047', 'n_tokens_mistral': 3190, 'n_tokens_neox': 3069, 'n_words': 1008}