phetsims / chipper

Tools for developing and building PhET interactive simulations.
MIT License
11 stars 14 forks source link

Convert grunt entry points to support Typescript #1464

Closed samreid closed 3 weeks ago

samreid commented 1 month ago

In https://github.com/phetsims/chipper/issues/1459 we established how to use TypeScript in chipper via spawned processes. We would like to expand that usage to perennial/aqua/dot/kite/scenery and within chipper/. Be aware of https://github.com/phetsims/chipper/issues/1463 which should likely happen before this.

repos with a typescript_chipper_1464 branch

samreid commented 1 month ago
```diff Subject: [PATCH] Remove debug output, see https://github.com/phetsims/chipper/issues/1464 --- Index: js/grunt/tasks/default.ts IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== diff --git a/js/grunt/tasks/default.ts b/js/grunt/tasks/default.ts new file mode 100644 --- /dev/null (date 1726665967088) +++ b/js/grunt/tasks/default.ts (date 1726665967088) @@ -0,0 +1,29 @@ +// Copyright 2024, University of Colorado Boulder + +/** + * Erases the build/ directory and all its contents, and recreates the build/ directory + * + * @author Sam Reid (PhET Interactive Simulations) + */ + +import * as grunt from 'grunt'; +import { lintAll } from './lint-all'; + +( async () => { + if ( grunt.option( 'lint' ) === false ) { + // do nothing + } + else { + await lintAll(); + } + + if ( grunt.option( 'report-media' ) === false ) { + // do nothing + } + else { + require( './report-media' ); + } + + require( './clean' ); + require( './build' ); +} )(); \ No newline at end of file Index: js/grunt/tasks/lint-all.ts IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== diff --git a/js/grunt/tasks/lint-all.ts b/js/grunt/tasks/lint-all.ts --- a/js/grunt/tasks/lint-all.ts (revision 9450827abc96e869f16ddf0c16df33c71b0ff795) +++ b/js/grunt/tasks/lint-all.ts (date 1726666208980) @@ -1,12 +1,11 @@ // Copyright 2024, University of Colorado Boulder /** - * lint all js files that are required to build this repository (for the specified brands) + * Lints all JS files required to build this repository (for the specified brands) * * @author Sam Reid (PhET Interactive Simulations) */ -// @ts-expect-error import assert from 'assert'; import * as grunt from 'grunt'; import buildLocal from './util/buildLocal'; @@ -22,21 +21,35 @@ const cache = !getOption( 'disable-eslint-cache' ); const fix = getOption( 'fix' ); const chipAway = getOption( 'chip-away' ); -assert && assert( !getOption( 'patterns' ), 'patterns not support for lint-all' ); +assert( !getOption( 'patterns' ), 'patterns not support for lint-all' ); const getPhetLibs = require( '../getPhetLibs' ); const brands = getBrands( grunt, repo, buildLocal ); -( async () => { +/** + * Executes the linting process. + */ +export async function lintAll(): Promise { const lintReturnValue = await lint( getPhetLibs( repo, brands ), { cache: cache, fix: fix, chipAway: chipAway } ); -// Output results on errors. + // Output results on errors. if ( !lintReturnValue.ok ) { grunt.fail.fatal( 'Lint failed' ); } -} )(); \ No newline at end of file + else { + console.log( 'Linting completed successfully.' ); + } +} + +// Detect if the script is run directly +if ( import.meta.url === `file://${process.argv[ 1 ]}` ) { + lintAll().catch( error => { + console.error( 'Linting failed:', error ); + process.exit( 1 ); + } ); +} \ No newline at end of file
zepumph commented 1 month ago

I made https://github.com/phetsims/chipper/issues/1465 for the less interesting commits that are just about TypeScript conversion.

zepumph commented 1 month ago

Working on this now.

zepumph commented 1 month ago

Made a commit on perennial today. This isn't exactly a "commit point" as about 75% of the tasks still aren't converted, but are in their new files. I'll keep cranking away (good thing we are on the development branch).

zepumph commented 1 month ago

Ok. We are making good coverage here, but the main point of this issue is to convert Gruntfiles into the sub-task pattern so that we have no constaint on Grunt for modules we want to start converting to typescript. Here is the list of gruntfiles that most likely leak into perennial dependencies and so we want to use the new pattern on:

From here we will be largely unblocked for converting to Typescript.

Quake patch ```diff Subject: [PATCH] convert dot to use grunt registerTasks(), https://github.com/phetsims/chipper/issues/1464 --- Index: tsconfig-browser.json IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== diff --git a/tsconfig-browser.json b/tsconfig-browser.json new file mode 100644 --- /dev/null (date 1727735831154) +++ b/tsconfig-browser.json (date 1727735831154) @@ -0,0 +1,11 @@ +{ + "extends": "../chipper/tsconfig-core.json", + "include": [ + "cordova-plugin-native-vibration/types/**/*", + ], + "references": [ + { + "path": "../chipper/tsconfig/buildjson" + } + ] +} \ No newline at end of file Index: tsconfig-node.json IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== diff --git a/tsconfig-node.json b/tsconfig-node.json new file mode 100644 --- /dev/null (date 1727729960915) +++ b/tsconfig-node.json (date 1727729960915) @@ -0,0 +1,12 @@ +{ + "extends": "../chipper/tsconfig/shared/tsconfig-node.json", + "include": [ + "js/grunt/**/*", + "*" + ], + "references": [ + { + "path": "../chipper/tsconfig/buildtools" + } + ] +} \ No newline at end of file Index: Gruntfile.js IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== diff --git a/Gruntfile.js b/Gruntfile.js --- a/Gruntfile.js (revision c423a4107c526b0ce988fdc77edd5d07a20ae2f0) +++ b/Gruntfile.js (date 1727736256652) @@ -1,4 +1,18 @@ -// Copyright 2020, University of Colorado Boulder +// Copyright 2020-2024, University of Colorado Boulder + +/** + * quake-specific grunt configuration, builds the Haptics Playground app + * + * @author John Blanco (PhET Interactive Simulations) + */ +// modules +const Gruntfile = require( '../chipper/js/grunt/Gruntfile' ); +const registerTasks = require( '../perennial/js/grunt/util/registerTasks' ); -module.exports = require( './js/Gruntfile.js' ); \ No newline at end of file +module.exports = grunt => { + Gruntfile( grunt ); + + // Last to override "default" task + registerTasks( grunt, `${__dirname}/js/grunt/tasks/` ); +}; \ No newline at end of file Index: js/Gruntfile.js IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== diff --git a/js/Gruntfile.js b/js/grunt/tasks/build.ts rename from js/Gruntfile.js rename to js/grunt/tasks/build.ts --- a/js/Gruntfile.js (revision c423a4107c526b0ce988fdc77edd5d07a20ae2f0) +++ b/js/grunt/tasks/build.ts (date 1727736683406) @@ -1,77 +1,15 @@ // Copyright 2022, University of Colorado Boulder /** - * quake-specific grunt configuration, builds the Haptics Playground app + * Build the Haptics Playground app for all supported platforms * * @author John Blanco (PhET Interactive Simulations) */ -// modules -const Gruntfile = require( '../../chipper/js/grunt/Gruntfile' ); -const child_process = require( 'child_process' ); - -// constants -const CUSTOM_VIBRATION_PLUGIN_NAME = 'cordova-plugin-native-vibration'; - -module.exports = grunt => { - Gruntfile( grunt ); - -// function to indent the results of a command-line operation - const logIndentedMessage = ( commandResult, indentation ) => { - let indentationString = ''; - for ( let i = 0; i < indentation; i++ ) { - indentationString += ' '; - } - - // For platform independence, replace any CR-LF occurrences with a simple LF. - const modifiedCommandResult = commandResult.toString().replace( '\r\n', '\n' ); - const lines = modifiedCommandResult.split( '\n' ); - lines.forEach( line => { - grunt.log.writeln( `${indentationString}${line}` ); - } ); - }; - - // Register the task that installs or updates the custom native vibration plugin. - grunt.registerTask( - 'install-plugin', - 'Install or update the custom native vibration plugin', - () => { - - // Note to future maintainers: This task removes a previous installation of the plugin and then adds it back in - // order to make sure that the most recent version is installed. I (jbphet) tried using the `update` command, - // which exists, but didn't seem to work, at least not as of early January 2022. - - grunt.log.writeln( `Checking whether ${CUSTOM_VIBRATION_PLUGIN_NAME} plugin is present...` ); - let commandResult = child_process.execSync( 'cordova plugin list' ); - if ( commandResult.includes( CUSTOM_VIBRATION_PLUGIN_NAME ) ) { - grunt.log.writeln( ' Removing previous version of plugin...' ); - const removePluginCommand = `cordova plugin remove ${CUSTOM_VIBRATION_PLUGIN_NAME}`; - grunt.log.writeln( ` command = ${removePluginCommand}` ); - commandResult = child_process.execSync( removePluginCommand ); - grunt.log.writeln( ' result:' ); - logIndentedMessage( commandResult, 4 ); - } - grunt.log.writeln( ' Adding current version of plugin...' ); - const addPluginCommand = `cordova plugin add ./${CUSTOM_VIBRATION_PLUGIN_NAME}/`; - grunt.log.writeln( ` command = ${addPluginCommand}` ); - commandResult = child_process.execSync( addPluginCommand ); - grunt.log.writeln( ' result:' ); - logIndentedMessage( commandResult, 4 ); - } - ); - - grunt.registerTask( - 'build', - 'Build the Haptics Playground app for all supported platforms', - () => { +import child_process from 'child_process'; +import grunt from '../../../../perennial-alias/js/import-shared/grunt'; - // Build the app for all supported platforms. - grunt.log.writeln( 'Building app for all supported platforms...' ); - const commandResult = child_process.execSync( 'cordova build' ); - grunt.log.writeln( `${commandResult}` ); - } - ); - - // register default task - grunt.registerTask( 'default', [ 'lint', 'install-plugin', 'build' ] ); -}; \ No newline at end of file +// Build the app for all supported platforms. +grunt.log.writeln( 'Building app for all supported platforms...' ); +const commandResult = child_process.execSync( 'cordova build' ); +grunt.log.writeln( `${commandResult}` ); \ No newline at end of file Index: js/grunt/tasks/default.ts IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== diff --git a/js/grunt/tasks/default.ts b/js/grunt/tasks/default.ts new file mode 100644 --- /dev/null (date 1727737043952) +++ b/js/grunt/tasks/default.ts (date 1727737043952) @@ -0,0 +1,22 @@ +// Copyright 2022, University of Colorado Boulder + +/** + * Build the Haptics Playground app for all supported platforms + * + * @author John Blanco (PhET Interactive Simulations) + */ +// Copyright 2024, University of Colorado Boulder + +/** + * Default command which runs lint-all, report-media, clean, and build. + * + * @author Sam Reid (PhET Interactive Simulations) + */ + +( async () => { + await ( await import( '../../../../chipper/js/grunt/tasks/lint.ts' ) ).lintTask; + + await import( './build.ts' ); + + await import( './install-plugin.ts' ); +} )(); \ No newline at end of file Index: eslint.config.mjs IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== diff --git a/eslint.config.mjs b/eslint.config.mjs --- a/eslint.config.mjs (revision c423a4107c526b0ce988fdc77edd5d07a20ae2f0) +++ b/eslint.config.mjs (date 1727736988870) @@ -7,6 +7,7 @@ * @author Michael Kauzmann (PhET Interactive Simulations) */ +import buildtoolsEslintConfig from '../chipper/eslint/buildtools.eslint.config.mjs'; import nodeEslintConfig from '../chipper/eslint/node.eslint.config.mjs'; export default [ @@ -29,5 +30,11 @@ 'platforms/', 'plugins/' ] + }, + { + files: [ + 'js/grunt/**/*' + ], + ...buildtoolsEslintConfig } ]; \ No newline at end of file Index: js/grunt/tasks/install-plugin.ts IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== diff --git a/js/grunt/tasks/install-plugin.ts b/js/grunt/tasks/install-plugin.ts new file mode 100644 --- /dev/null (date 1727736169040) +++ b/js/grunt/tasks/install-plugin.ts (date 1727736169040) @@ -0,0 +1,49 @@ +// Copyright 2022, University of Colorado Boulder + +/** + * Install or update the custom native vibration plugin. + * + * Note to future maintainers: This task removes a previous installation of the plugin and then adds it back in + * order to make sure that the most recent version is installed. I (jbphet) tried using the `update` command, + * which exists, but didn't seem to work, at least not as of early January 2022. + * + * @author John Blanco (PhET Interactive Simulations) + */ + +import child_process from 'child_process'; +import grunt from '../../../../perennial-alias/js/import-shared/grunt'; + +// constants +const CUSTOM_VIBRATION_PLUGIN_NAME = 'cordova-plugin-native-vibration'; + +// function to indent the results of a command-line operation +const logIndentedMessage = ( commandResult: Buffer, indentation: number ) => { + let indentationString = ''; + for ( let i = 0; i < indentation; i++ ) { + indentationString += ' '; + } + + // For platform independence, replace any CR-LF occurrences with a simple LF. + const modifiedCommandResult = commandResult.toString().replace( '\r\n', '\n' ); + const lines = modifiedCommandResult.split( '\n' ); + lines.forEach( line => { + grunt.log.writeln( `${indentationString}${line}` ); + } ); +}; + +grunt.log.writeln( `Checking whether ${CUSTOM_VIBRATION_PLUGIN_NAME} plugin is present...` ); +let commandResult = child_process.execSync( 'cordova plugin list' ); +if ( commandResult.includes( CUSTOM_VIBRATION_PLUGIN_NAME ) ) { + grunt.log.writeln( ' Removing previous version of plugin...' ); + const removePluginCommand = `cordova plugin remove ${CUSTOM_VIBRATION_PLUGIN_NAME}`; + grunt.log.writeln( ` command = ${removePluginCommand}` ); + commandResult = child_process.execSync( removePluginCommand ); + grunt.log.writeln( ' result:' ); + logIndentedMessage( commandResult, 4 ); +} +grunt.log.writeln( ' Adding current version of plugin...' ); +const addPluginCommand = `cordova plugin add ./${CUSTOM_VIBRATION_PLUGIN_NAME}/`; +grunt.log.writeln( ` command = ${addPluginCommand}` ); +commandResult = child_process.execSync( addPluginCommand ); +grunt.log.writeln( ' result:' ); +logIndentedMessage( commandResult, 4 ); \ No newline at end of file Index: tsconfig.json IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== diff --git a/tsconfig.json b/tsconfig.json --- a/tsconfig.json (revision c423a4107c526b0ce988fdc77edd5d07a20ae2f0) +++ b/tsconfig.json (date 1727735929310) @@ -1,11 +1,11 @@ { - "extends": "../chipper/tsconfig-core.json", - "include": [ - "cordova-plugin-native-vibration/types/**/*", - ], + "files": [], "references": [ { - "path": "../chipper/tsconfig/buildjson" + "path": "./tsconfig-browser.json" + }, + { + "path": "./tsconfig-node.json" } ] } \ No newline at end of file Index: js/tsconfig.json IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== diff --git a/js/tsconfig.json b/js/tsconfig.json new file mode 100644 --- /dev/null (date 1727735949680) +++ b/js/tsconfig.json (date 1727735949680) @@ -0,0 +1,3 @@ +{ + "extends": "../tsconfig-node.json" +} \ No newline at end of file ```
zepumph commented 3 weeks ago

All items have been handled or are now in side issues. Closing