Skip to content

Commit

Permalink
Added a script to insert a require statement, see #586
Browse files Browse the repository at this point in the history
  • Loading branch information
samreid committed Jul 27, 2017
1 parent 96a9b32 commit a295d0e
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 4 deletions.
11 changes: 9 additions & 2 deletions js/grunt/Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' );

Expand Down Expand Up @@ -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 );
} );
};
95 changes: 95 additions & 0 deletions js/grunt/insertRequireStatement.js
Original file line number Diff line number Diff line change
@@ -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 );
}
};
4 changes: 2 additions & 2 deletions js/grunt/sortRequireStatements.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -68,7 +69,6 @@ module.exports = function( grunt ) {
};

// option to sort a single file
var file = grunt.option( 'file' );
if ( file ) {
sortRequireStatementsForFile( file );
}
Expand Down

0 comments on commit a295d0e

Please sign in to comment.