From 930f70faf2ec0b5a708c169ab193a398a8649ff2 Mon Sep 17 00:00:00 2001 From: samreid Date: Wed, 26 Jul 2017 14:23:07 -0600 Subject: [PATCH] Added a task that sorts require statements alphabetically, see https://github.com/phetsims/chipper/issues/595 --- js/grunt/Gruntfile.js | 8 ++++ js/grunt/sortRequireStatements.js | 63 +++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 js/grunt/sortRequireStatements.js diff --git a/js/grunt/Gruntfile.js b/js/grunt/Gruntfile.js index 8392b6f0d..e96ea582b 100644 --- a/js/grunt/Gruntfile.js +++ b/js/grunt/Gruntfile.js @@ -43,6 +43,7 @@ var updateCopyrightDates = require( '../../../chipper/js/grunt/updateCopyrightDa var updatePhETiOSite = require( '../../../chipper/js/grunt/updatePhETiOSite' ); var findDuplicates = require( '../../../chipper/js/grunt/findDuplicates' ); var wrapperBuild = require( '../../../chipper/js/grunt/wrapperBuild' ); +var sortRequireStatements = require( '../../../chipper/js/grunt/sortRequireStatements' ); var wrapperDeploy = require( '../../../chipper/js/grunt/wrapperDeploy' ); var bumpVersion = require( '../../../chipper/js/grunt/bumpVersion' ); @@ -401,4 +402,11 @@ module.exports = function( grunt ) { } ); grunt.registerTask( 'build-wrapper', 'Build PhET-iO wrapper', optionalTasks.concat( [ 'clean', 'wrapper-basic-build' ] ) ); + + grunt.registerTask( 'sort-require-statements', 'Sort the require statements for all *.js files in the js/ directory. ' + + 'This assumes the code is formatted with IDEA code style and that ' + + 'require statements take one line each (not split across lines). The ' + + 'files are overwritten.', function() { + sortRequireStatements( grunt, buildConfig ); + } ); }; \ No newline at end of file diff --git a/js/grunt/sortRequireStatements.js b/js/grunt/sortRequireStatements.js new file mode 100644 index 000000000..a231d75a4 --- /dev/null +++ b/js/grunt/sortRequireStatements.js @@ -0,0 +1,63 @@ +// Copyright 2015, University of Colorado Boulder + +/** + * Sorts require statements for each file in the js/ directory + * + * @author Sam Reid (PhET Interactive Simulations) + */ + +/** + * @param grunt - the grunt instance + */ +module.exports = function( grunt ) { + 'use strict'; + + var sourceRoot = process.cwd() + '/js'; + + // Count the number of start and end dates we need + grunt.file.recurse( sourceRoot, function( abspath ) { + + // only address js files + if ( abspath.indexOf( '.js' ) ) { + + // read the file as text + var text = grunt.file.read( abspath ).toString(); + + // split by line + var lines = text.split( /\r?\n/ ); + + // full text + var result = []; + + // accumulated require statement lines + var accumulator = []; + + // total number of require statements + var count = 0; + + for ( var i = 0; i < lines.length; i++ ) { + var line = lines[ i ]; + + // If it was a require statement, store it for sorting. + if ( line.indexOf( ' = require( ' ) >= 0 ) { + accumulator.push( line ); + count++; + } + else { + + // Not a require statement, sort and flush any pending require statements then continue + accumulator.sort(); + accumulator.forEach( function( a ) { + result.push( a ); + } ); + accumulator.length = 0; + result.push( line ); + } + } + + // console.log( result.join( '\n' ) ); + grunt.file.write( abspath, result.join( '\n' ) ); + console.log( 'sorted ' + count + ' require statements in ' + abspath ); + } + } ); +}; \ No newline at end of file