Skip to content

Commit

Permalink
Use a single js file to run all pre-commit hooks, see phetsims/aqua#90
Browse files Browse the repository at this point in the history
  • Loading branch information
samreid committed May 16, 2020
0 parents commit da70f9d
Showing 1 changed file with 76 additions and 0 deletions.
76 changes: 76 additions & 0 deletions js/scripts/hook-pre-commit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Copyright 2020, University of Colorado Boulder

/**
* Runs tasks for pre-commit, including lint and qunit testing. Avoids the overhead of grunt and Gruntfile.js for speed
*
* USAGE:
* cd ${repo}
* node ../perennial/js/scripts/hook-pre-commit.js
*
* OPTIONS:
* --console: outputs information to the console for debugging
*
* See also phet-info/git-template-dir/hooks/pre-commit for how this is used in precommit hooks.
*
* @author Sam Reid (PhET Interactive Simulations)
*/

// Require statements which should be generally available via node or perennial
const path = require( 'path' );
const fs = require( 'fs' );
const puppeteer = require( 'puppeteer' );
const buildLocal = require( '../common/buildLocal' );

// Identify the current repo
const repo = process.cwd().split( path.sep ).pop();

// Console logging via --console
const commandLineArguments = process.argv.slice( 2 );
const outputToConsole = commandLineArguments.indexOf( '--console' ) >= 0;

// Run lint tests if they exist in the checked-out SHAs.
try {
const lint = require( '../../../chipper/js/grunt/lint' );

// lint() automatically filters out non-lintable repos
const report = lint( [ repo ], true, false, true );

if ( report.errorCount > 0 || report.warningCount > 0 ) {
process.exit( 1 );
}

outputToConsole && console.log( 'Linting passed with results.length: ' + report.results.length );
}
catch( e ) {
console.log( 'chipper/js/grunt/lint not found' );
}

// Run qunit tests if puppeteerQUnit exists in the checked-out SHAs and a test HTML exists.
try {
const puppeteerQUnit = require( '../../../aqua/js/local/puppeteerQUnit' );
if ( repo !== 'scenery' ) {
( async () => {
const testFilePath = `${repo}/${repo}-tests.html`;
const exists = fs.existsSync( `../${testFilePath}` );
if ( exists ) {
const browser = await puppeteer.launch();

// phet-io repo unit tests must be run with brand=phet-io
const queryString = repo === 'phet-io' ? '&brand=phet-io' : '';

const result = await puppeteerQUnit( browser, `${buildLocal.localTestingURL}/${testFilePath}?ea${queryString}` );
await browser.close();
outputToConsole && console.log( `${repo}: ${JSON.stringify( result, null, 2 )}` );
if ( !result.ok ) {
process.exit( 1 ); // fail as soon as there is one problem
}
}

outputToConsole && console.log( 'no problems detected' );
process.exit( 0 );
} )();
}
}
catch( e ) {
console.log( 'puppeteerQUnit not found.' );
}

0 comments on commit da70f9d

Please sign in to comment.