diff --git a/js/grunt/Gruntfile.js b/js/grunt/Gruntfile.js index e867a0a6a..a5e8761ab 100644 --- a/js/grunt/Gruntfile.js +++ b/js/grunt/Gruntfile.js @@ -44,6 +44,7 @@ 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 insertRequireStatement = require( '../../../chipper/js/grunt/insertRequireStatement' ); var wrapperDeploy = require( '../../../chipper/js/grunt/wrapperDeploy' ); var bumpVersion = require( '../../../chipper/js/grunt/bumpVersion' ); @@ -407,7 +408,13 @@ module.exports = function( grunt ) { '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.\n' + - '--file (optional) absolute path of a single file to sort¬', function() { - sortRequireStatements( grunt, buildConfig ); + '--file (optional) absolute path of a single file to sort', function() { + sortRequireStatements( grunt, grunt.option( 'file' ) ); + } ); + + grunt.registerTask( 'insert-require-statement', 'Insert a require statement into the specified file.\n' + + '--file absolute path of a single file to sort\n' + + '--name to be required', function() { + insertRequireStatement( grunt, buildConfig ); } ); }; \ No newline at end of file diff --git a/js/grunt/insertRequireStatement.js b/js/grunt/insertRequireStatement.js new file mode 100644 index 000000000..e614295d3 --- /dev/null +++ b/js/grunt/insertRequireStatement.js @@ -0,0 +1,95 @@ +// Copyright 2015, University of Colorado Boulder + +/** + * Inserts a require statement in the given file + * + * @author Sam Reid (PhET Interactive Simulations) + */ + +var assert = require( 'assert' ); // eslint-disable-line require-statement-match +var fs = require( 'fs' ); +var sortRequireStatements = require( '../../../chipper/js/grunt/sortRequireStatements' ); + +// constants +var KEY = ' = require( '; // the substring that is searched to find require statements + +/** + * @param grunt - the grunt instance + */ +module.exports = function( grunt ) { + 'use strict'; + + // option to sort a single file + var file = grunt.option( 'file' ); + var name = grunt.option( 'name' ); + + assert && assert( file, 'file should be defined' ); + assert && assert( name, 'name should be defined' ); + + var activeSims = fs.readFileSync( '../chipper/data/active-repos' ).toString(); + activeSims = activeSims.split( /\r?\n/ ); + activeSims.length = activeSims.length - 1; + + var simulationRoot = process.cwd(); + + var statement = null; + try { + + // Search over all active sims for a require statement that matches the desired one + for ( var k = 0; k < activeSims.length; k++ ) { + var simPath = simulationRoot + '/../' + activeSims[ k ] + '/js'; + if ( grunt.file.exists( simPath ) ) { + grunt.file.recurse( simPath, function( absolutePath ) { + var t = grunt.file.read( absolutePath, 'utf8' ); + var index = t.indexOf( 'var ' + name + ' = require( \'' ); + if ( index >= 0 ) { + var nextEndLine = t.indexOf( '\n', index ); + var substring = t.substring( index, nextEndLine ); + + // poor man's way out of recursion + throw substring; + } + } ); + } + } + } + catch( x ) { + + // poor man's way out of recursion + console.log( x ); + statement = x; + } + + if ( !statement ) { + grunt.log.warn.log( 'no import found for ' + name ); + } + else { + + // read the file as text + var text = grunt.file.read( file ).toString(); + + // split by line + var lines = text.split( /\r?\n/ ); + + // full text + var result = []; + var inserted = false; + + 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( KEY ) >= 0 && !inserted ) { + result.push( ' ' + statement ); + inserted = true; + } + result.push( line ); + } + + grunt.file.write( file, result.join( '\n' ) ); + console.log( 'inserted a require statements in ' + file ); + + // Make sure it ends up in the right place + sortRequireStatements( grunt, file ); + } +}; \ No newline at end of file diff --git a/js/grunt/sortRequireStatements.js b/js/grunt/sortRequireStatements.js index b77cad12e..5efb633bc 100644 --- a/js/grunt/sortRequireStatements.js +++ b/js/grunt/sortRequireStatements.js @@ -14,8 +14,9 @@ var KEY = ' = require( '; // the substring that is searched to find require stat /** * @param grunt - the grunt instance + * @param {string} [file] - optional absolute path to the file to sort */ -module.exports = function( grunt ) { +module.exports = function( grunt, file ) { 'use strict'; var sourceRoot = process.cwd() + '/js'; @@ -68,7 +69,6 @@ module.exports = function( grunt ) { }; // option to sort a single file - var file = grunt.option( 'file' ); if ( file ) { sortRequireStatementsForFile( file ); }