Skip to content

Commit

Permalink
Standardize execTask with filename and command line args, see #1459
Browse files Browse the repository at this point in the history
  • Loading branch information
samreid committed Sep 12, 2024
1 parent d477d94 commit 00b31de
Show file tree
Hide file tree
Showing 26 changed files with 104 additions and 116 deletions.
84 changes: 36 additions & 48 deletions js/grunt/Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@
const assert = require( 'assert' );
require( './checkNodeVersion' );
const { spawn } = require( 'child_process' ); // eslint-disable-line require-statement-match
const getPhetLibs = require( './getPhetLibs' );

// TODO: Will ctrl-c correctly kill spawned tasks? https://github.com/phetsims/chipper/issues/1459

// Allow other Gruntfiles to potentially handle exiting and errors differently`
// Allow other Gruntfiles to potentially handle exiting and errors differently
if ( !global.processEventOptOut ) {

// See https://medium.com/@dtinth/making-unhandled-promise-rejections-crash-the-node-js-process-ffc27cfcc9dd for how
Expand All @@ -38,30 +37,23 @@ module.exports = function( grunt ) {
assert( typeof repo === 'string' && /^[a-z]+(-[a-z]+)*$/u.test( repo ), 'repo name should be composed of lower-case characters, optionally with dashes used as separators' );

// TODO: This is working well but should we use execute? Let's chat: https://github.com/phetsims/chipper/issues/1459
function wrapExecSync( line ) {
function execTask( taskFilename ) {

const command = line.split( ' ' )[ 0 ];
const args = line.split( ' ' ).slice( 1 );
// TODO switch to npx tsx, see https://github.com/phetsims/chipper/issues/1459 via npx ../chipper/node_modules/tsx
// TODO: test on windows, see https://github.com/phetsims/chipper/issues/1459
const command = 'node';

return () => {

const done = grunt.task.current.async();
try {
const child = spawn( command, args, { stdio: [ 'inherit', 'pipe', 'pipe' ] } );
const child = spawn( command, [ `../chipper/js/grunt/tasks/${taskFilename}`, ...process.argv.slice( 2 ) ], { stdio: [ 'inherit', 'pipe', 'pipe' ] } );

// Stream stdout
child.stdout.on( 'data', data => {
process.stdout.write( data.toString() );
} );

// Stream stderr
child.stderr.on( 'data', data => {
process.stderr.write( data.toString() );
} );
child.stdout.on( 'data', data => process.stdout.write( data.toString() ) );
child.stderr.on( 'data', data => process.stderr.write( data.toString() ) );

// Listen for the close event when the command finishes
child.on( 'close', code => {

done();
if ( code !== 0 ) {
grunt.fail.fatal( `Command failed with exit code ${code}` );
Expand All @@ -84,22 +76,22 @@ module.exports = function( grunt ) {

// TODO: https://github.com/phetsims/chipper/issues/1459 running grunt requires that we register tasks, otherwise it fails out. Is that ok?
grunt.registerTask( 'clean', 'Erases the build/ directory and all its contents, and recreates the build/ directory',
wrapExecSync( `node ../chipper/js/grunt/tasks/clean.js --repo ${repo}` )
execTask( 'clean.js' )
);

grunt.registerTask( 'build-images', 'Build images only',
wrapExecSync( `node ../chipper/js/grunt/tasks/build-images.js --repo ${repo}` )
execTask( 'build-images.js' )
);

grunt.registerTask( 'output-js', 'Outputs JS just for the specified repo',
wrapExecSync( `node ../chipper/js/grunt/tasks/output-js.js --repo ${repo}` )
execTask( 'output-js.js' )
);
grunt.registerTask( 'output-js-project', 'Outputs JS for the specified repo and its dependencies',
wrapExecSync( `node ../chipper/js/grunt/tasks/output-js-project.js --repos ${getPhetLibs( repo ).join( ',' )}` )
execTask( 'output-js-project.js' )
);

grunt.registerTask( 'output-js-all', 'Outputs JS for all repos',
wrapExecSync( 'node ../chipper/js/grunt/tasks/output-js-all.js' )
execTask( '-js-all.js' )
);

grunt.registerTask( 'build',
Expand All @@ -121,14 +113,13 @@ Minify-specific options:
--minify.mangle=false - During uglification, it will not "mangle" variable names (where they get renamed to short constants to reduce file size.)
--minify.beautify=true - After uglification, the source code will be syntax formatted nicely
--minify.stripAssertions=false - During uglification, it will strip assertions.
--minify.stripLogging=false - During uglification, it will not strip logging statements.
`,
wrapExecSync( `node ../chipper/js/grunt/tasks/build.js --repo ${repo} ${process.argv.slice( 2 ).join( ' ' )}` )
--minify.stripLogging=false - During uglification, it will not strip logging statements.`,
execTask( 'build.js' )
);

grunt.registerTask( 'generate-used-strings-file',
'Writes used strings to phet-io-sim-specific/ so that PhET-iO sims only output relevant strings to the API in unbuilt mode',
wrapExecSync( `node ../chipper/js/grunt/tasks/generate-used-strings-file.js --repo ${repo}` )
execTask( 'generate-used-strings-file.js' )
);

grunt.registerTask( 'build-for-server', 'meant for use by build-server only',
Expand All @@ -141,30 +132,30 @@ Minify-specific options:
--fix: autofixable changes will be written to disk
--chip-away: output a list of responsible devs for each repo with lint problems
--repos: comma separated list of repos to lint in addition to the repo from running`,
wrapExecSync( `node ../chipper/js/grunt/tasks/lint.js --repo ${repo} ${process.argv.slice( 2 ).join( ' ' )}` )
execTask( 'lint.js' )
);

grunt.registerTask( 'lint-all', 'lint all js files that are required to build this repository (for the specified brands)',
wrapExecSync( `node ../chipper/js/grunt/tasks/lint-all.js --repo ${repo} ${process.argv.slice( 2 ).join( ' ' )}` )
execTask( 'lint-all.js' )
);

grunt.registerTask( 'generate-development-html',
'Generates top-level SIM_en.html file based on the preloads in package.json.',
wrapExecSync( `node ../chipper/js/grunt/tasks/generate-development-html.js --repo ${repo}` )
execTask( 'generate-development-html.js' )
);

grunt.registerTask( 'generate-test-html',
'Generates top-level SIM-tests.html file based on the preloads in package.json. See https://github.com/phetsims/aqua/blob/main/doc/adding-unit-tests.md ' +
'for more information on automated testing. Usually you should ' +
'set the "generatedUnitTests":true flag in the sim package.json and run `grunt update` instead of manually generating this.',
wrapExecSync( `node ../chipper/js/grunt/tasks/generate-test-html.js --repo ${repo}` )
execTask( 'generate-test-html.js' )
);

grunt.registerTask( 'generate-a11y-view-html',
'Generates top-level SIM-a11y-view.html file used for visualizing accessible content. Usually you should ' +
'set the "phet.simFeatures.supportsInteractiveDescription":true flag in the sim package.json and run `grunt update` ' +
'instead of manually generating this.',
wrapExecSync( `node ../chipper/js/grunt/tasks/generate-a11y-view.js --repo ${repo}` )
execTask( 'generate-a11y-view.js' )
);

grunt.registerTask( 'update', `
Expand All @@ -175,36 +166,35 @@ Updates the normal automatically-generated files for this repository. Includes:
* simulations: generateREADME()
* phet-io simulations: generate overrides file if needed
* create the conglomerate string files for unbuilt mode, for this repo and its dependencies`,
wrapExecSync( `node ../chipper/js/grunt/tasks/update.js --repo ${repo}` )
execTask( 'update.js' )
);

// This is not run in grunt update because it affects dependencies and outputs files outside of the repo.
grunt.registerTask( 'generate-development-strings',
'To support locales=* in unbuilt mode, generate a conglomerate JSON file for each repo with translations in babel. Run on all repos via:\n' +
'* for-each.sh perennial-alias/data/active-repos npm install\n' +
'* for-each.sh perennial-alias/data/active-repos grunt generate-development-strings',
wrapExecSync( `node ../chipper/js/grunt/tasks/generate-development-strings.js --repo ${repo}` )
execTask( 'generate-development-strings.js' )
);

grunt.registerTask( 'published-README',
'Generates README.md file for a published simulation.',
wrapExecSync( `node ../chipper/js/grunt/tasks/published-README.js --repo ${repo}` )
execTask( 'published-README.js' )
);

grunt.registerTask( 'unpublished-README',
'Generates README.md file for an unpublished simulation.',
wrapExecSync( `node ../chipper/js/grunt/tasks/unpublished-README.js --repo ${repo}` )
execTask( 'unpublished-README.js' )
);

// TODO: https://github.com/phetsims/chipper/issues/1459 probably does not need to be here in grunt
grunt.registerTask( 'sort-imports', 'Sort the import statements for a single file (if --file={{FILE}} is provided), or does so for all JS files if not specified',
wrapExecSync( `node ../chipper/js/grunt/tasks/sort-imports.js --repo ${repo} ${process.argv.slice( 2 ).join( ' ' )}` )
execTask( 'sort-imports.js' )
);

// TODO: https://github.com/phetsims/chipper/issues/1459 probably does not need to be here in grunt
grunt.registerTask( 'commits-since',
'Shows commits since a specified date. Use --date=<date> to specify the date.',
wrapExecSync( `node ../chipper/js/grunt/tasks/commits-since.js --repo ${repo} ${process.argv.slice( 2 ).join( ' ' )}` ) // TODO: Just need date, or take everything? See https://github.com/phetsims/chipper/issues/1459
grunt.registerTask( 'commits-since', 'Shows commits since a specified date. Use --date=<date> to specify the date.',
execTask( 'commits-since.js' )
);

// See reportMedia.js
Expand All @@ -215,7 +205,7 @@ Updates the normal automatically-generated files for this repository. Includes:
'(1) incompatible-license (resource license not approved)\n' +
'(2) not-annotated (license.json missing or entry missing from license.json)\n' +
'(3) missing-file (entry in the license.json but not on the file system)',
wrapExecSync( `node ../chipper/js/grunt/tasks/report-media.js --repo ${repo}` )
execTask( 'report-media.js' )
);

// see reportThirdParty.js
Expand All @@ -224,19 +214,17 @@ Updates the normal automatically-generated files for this repository. Includes:
'Creates a report of third-party resources (code, images, sound, etc) used in the published PhET simulations by ' +
'reading the license information in published HTML files on the PhET website. This task must be run from main. ' +
'After running this task, you must push sherpa/third-party-licenses.md.',
wrapExecSync( `node ../chipper/js/grunt/tasks/report-third-party.js --repo ${repo}` )
execTask( 'report-third-party.js' )
);

grunt.registerTask( 'modulify', 'Creates *.js modules for all images/strings/audio/etc in a repo',
wrapExecSync( `node ../chipper/js/grunt/tasks/modulify.js --repo ${repo}` )
execTask( 'modulify.js' )
);

// Grunt task that determines created and last modified dates from git, and
// updates copyright statements accordingly, see #403
grunt.registerTask(
'update-copyright-dates',
'Update the copyright dates in JS source files based on Github dates',
wrapExecSync( `node ../chipper/js/grunt/tasks/update-copyright-dates.js --repo ${repo}` )
grunt.registerTask( 'update-copyright-dates', 'Update the copyright dates in JS source files based on Github dates',
execTask( 'update-copyright-dates.js' )
);

// TODO: https://github.com/phetsims/chipper/issues/1459 probably does not need to be here in grunt
Expand Down Expand Up @@ -277,7 +265,7 @@ Updates the normal automatically-generated files for this repository. Includes:
'--stable - regenerate for all "stable sims" (see perennial/data/phet-io-api-stable/)\n' +
'--temporary - outputs to the temporary directory\n' +
'--transpile=false - skips the transpilation step. You can skip transpilation if a watch process is handling it.',
wrapExecSync( `node ../chipper/js/grunt/tasks/generate-phet-io-api.js --repo ${repo} ${process.argv.slice( 2 ).join( ' ' )}` )
execTask( 'generate-phet-io-api.js' )
);

grunt.registerTask(
Expand All @@ -290,12 +278,12 @@ Updates the normal automatically-generated files for this repository. Includes:
'--delta, by default a breaking-compatibility comparison is done, but --delta shows all changes\n' +
'--temporary, compares API files in the temporary directory (otherwise compares to freshly generated APIs)\n' +
'--compareBreakingAPIChanges - add this flag to compare breaking changes in addition to designed changes',
wrapExecSync( `node ../chipper/js/grunt/tasks/compare-phet-io-api.js --repo ${repo} ${process.argv.slice( 2 ).join( ' ' )}` )
execTask( 'compare-phet-io-api.js' )
);

// TODO: https://github.com/phetsims/chipper/issues/1459 probably does not need to be here in grunt
grunt.registerTask( 'profile-file-size', 'Profiles the file size of the built JS file for a given repo',
wrapExecSync( `node ../chipper/js/grunt/tasks/profile-file-size.js --repo ${repo}` )
execTask( 'profile-file-size.js' )
);

/**
Expand Down
4 changes: 2 additions & 2 deletions js/grunt/tasks/build-images.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
* @author Sam Reid (PhET Interactive Simulations)
*/
const grunt = require( 'grunt' );
const getRequiredArg = require( './getRequiredArg' );
const getRepo = require( './getRepo' );

const jimp = require( 'jimp' );
const generateThumbnails = require( '../generateThumbnails' );
const generateTwitterCard = require( '../generateTwitterCard' );

const brand = 'phet';

const repo = getRequiredArg( '--repo' );
const repo = getRepo();

( async () => {
const buildDir = `../${repo}/build/${brand}`;
Expand Down
4 changes: 2 additions & 2 deletions js/grunt/tasks/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ const getPhetLibs = require( '../getPhetLibs' );
const phetTimingLog = require( '../../../../perennial-alias/js/common/phetTimingLog' );

const grunt = require( 'grunt' );
const getRequiredArg = require( './getRequiredArg' );
const getRepo = require( './getRepo' );
const assert = require( 'assert' );
const Transpiler = require( '../../common/Transpiler' );
const getBrands = require( './getBrands' );
const parseGruntOptions = require( './parseGruntOptions' );

const repo = getRequiredArg( '--repo' );
const repo = getRepo();

// Initialize Grunt options with parsed arguments
// Call this before getBrands
Expand Down
4 changes: 2 additions & 2 deletions js/grunt/tasks/clean.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// Copyright 2024, University of Colorado Boulder

const getRequiredArg = require( './getRequiredArg' );
const getRepo = require( './getRepo' );

/**
* @author Sam Reid (PhET Interactive Simulations)
*/
const grunt = require( 'grunt' );
const repo = getRequiredArg( '--repo' );
const repo = getRepo();

const buildDirectory = `../${repo}/build`;

Expand Down
4 changes: 2 additions & 2 deletions js/grunt/tasks/commits-since.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// Copyright 2024, University of Colorado Boulder

const getRequiredArg = require( './getRequiredArg' );
const getRepo = require( './getRepo' );

/**
* @author Sam Reid (PhET Interactive Simulations)
*/
const grunt = require( 'grunt' );
const repo = getRequiredArg( '--repo' );
const repo = getRepo();
const parseGruntOptions = require( './parseGruntOptions' );

// Initialize Grunt options with parsed arguments
Expand Down
4 changes: 2 additions & 2 deletions js/grunt/tasks/compare-phet-io-api.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// Copyright 2024, University of Colorado Boulder

const getRequiredArg = require( './getRequiredArg' );
const getRepo = require( './getRepo' );

/**
* @author Sam Reid (PhET Interactive Simulations)
*/
const grunt = require( 'grunt' );
const repo = getRequiredArg( '--repo' );
const repo = getRepo();

const parseGruntOptions = require( './parseGruntOptions' );

Expand Down
4 changes: 2 additions & 2 deletions js/grunt/tasks/generate-a11y-view-html.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// Copyright 2024, University of Colorado Boulder

const getRequiredArg = require( './getRequiredArg' );
const getRepo = require( './getRepo' );

/**
* @author Sam Reid (PhET Interactive Simulations)
*/
const repo = getRequiredArg( '--repo' );
const repo = getRepo();

const generateA11yViewHTML = require( '../generateA11yViewHTML' );
( async () => {
Expand Down
4 changes: 2 additions & 2 deletions js/grunt/tasks/generate-development-html.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// Copyright 2024, University of Colorado Boulder

const getRequiredArg = require( './getRequiredArg' );
const getRepo = require( './getRepo' );

/**
* @author Sam Reid (PhET Interactive Simulations)
*/
const repo = getRequiredArg( '--repo' );
const repo = getRepo();

const generateDevelopmentHTML = require( '../generateDevelopmentHTML' );
( async () => {
Expand Down
4 changes: 2 additions & 2 deletions js/grunt/tasks/generate-development-strings.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// Copyright 2024, University of Colorado Boulder

const getRequiredArg = require( './getRequiredArg' );
const getRepo = require( './getRepo' );

/**
* @author Sam Reid (PhET Interactive Simulations)
*/
const repo = getRequiredArg( '--repo' );
const repo = getRepo();

const generateDevelopmentStrings = require( '../../scripts/generateDevelopmentStrings' );
const fs = require( 'fs' );
Expand Down
4 changes: 2 additions & 2 deletions js/grunt/tasks/generate-phet-io-api.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// Copyright 2024, University of Colorado Boulder

const getRequiredArg = require( './getRequiredArg' );
const getRepo = require( './getRepo' );

/**
* @author Sam Reid (PhET Interactive Simulations)
*/
const grunt = require( 'grunt' );
const repo = getRequiredArg( '--repo' );
const repo = getRepo();

const formatPhetioAPI = require( '../../phet-io/formatPhetioAPI' );
const getSimList = require( '../../common/getSimList' );
Expand Down
4 changes: 2 additions & 2 deletions js/grunt/tasks/generate-test-html.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// Copyright 2024, University of Colorado Boulder

const getRequiredArg = require( './getRequiredArg' );
const getRepo = require( './getRepo' );

/**
* @author Sam Reid (PhET Interactive Simulations)
*/
const repo = getRequiredArg( '--repo' );
const repo = getRepo();

const generateTestHTML = require( '../generateTestHTML' );
( async () => {
Expand Down
4 changes: 2 additions & 2 deletions js/grunt/tasks/generate-used-strings-file.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// Copyright 2024, University of Colorado Boulder

const getRequiredArg = require( './getRequiredArg' );
const getRepo = require( './getRepo' );

/**
* @author Sam Reid (PhET Interactive Simulations)
*/
const repo = getRequiredArg( '--repo' );
const repo = getRepo();

const getPhetLibs = require( '../getPhetLibs' );
const fs = require( 'fs' );
Expand Down
Loading

0 comments on commit 00b31de

Please sign in to comment.