-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a task that sorts require statements alphabetically, see #595
- Loading branch information
Showing
2 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | ||
} | ||
} ); | ||
}; |