Skip to content

Commit

Permalink
Added a task that sorts require statements alphabetically, see #595
Browse files Browse the repository at this point in the history
  • Loading branch information
samreid committed Jul 26, 2017
1 parent f4ce86d commit 930f70f
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
8 changes: 8 additions & 0 deletions js/grunt/Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' );

Expand Down Expand Up @@ -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 );
} );
};
63 changes: 63 additions & 0 deletions js/grunt/sortRequireStatements.js
Original file line number Diff line number Diff line change
@@ -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 );
}
} );
};

0 comments on commit 930f70f

Please sign in to comment.