Skip to content

Commit

Permalink
Add check.ts, see #1487
Browse files Browse the repository at this point in the history
  • Loading branch information
samreid committed Oct 14, 2024
1 parent 425b1d0 commit fd06ded
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 6 deletions.
93 changes: 93 additions & 0 deletions js/grunt/tasks/check.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// Copyright 2013-2024, University of Colorado Boulder

/**
* Type checks *.ts files. Named after deno check. Automatically uses -b as appropriate.
*
* Usage:
* grunt check
*
* Options (can be combined):
* --everything: check all repos
* --clean: clean before checking
*
* @author Sam Reid (PhET Interactive Simulations)
*/
import fs from 'fs';
import getOption from '../../../../perennial-alias/js/grunt/tasks/util/getOption.ts';
import getRepo from '../../../../perennial-alias/js/grunt/tasks/util/getRepo';
import { spawn } from 'child_process';

const repo = getRepo();

// Utility function to spawn a child process with inherited stdio
const runCommand = ( command: string, args: string[], cwd: string ): Promise<void> => {
return new Promise( ( resolve, reject ) => {
const child = spawn( command, args, {
cwd: cwd,
stdio: 'inherit', // Inherit stdio to preserve colors and interactive output
shell: process.platform.startsWith( 'win' )
} );

child.on( 'error', error => reject( error ) );
child.on( 'close', code => {
if ( code !== 0 ) { reject( new Error( `Command "${command} ${args.join( ' ' )}" exited with code ${code}` ) ); }
else { resolve(); }
} );
} );
};

export const checkTask = ( async () => {
try {
const everything = getOption( 'everything' );

if ( everything ) {
writeEverythingTSConfigFile();
}

const tsconfigDir = everything ? '../chipper/tsconfig/all' : `../${repo}`;
const pathToTSC = everything ? '../../../chipper/node_modules/typescript/bin/tsc'
: '../chipper/node_modules/typescript/bin/tsc';

const clean = getOption( 'clean' );
if ( clean ) {
await runCommand( 'node', [ pathToTSC, '-b', '--clean' ], tsconfigDir );
}

await runCommand( 'node', [ pathToTSC, '-b', '--pretty' ], tsconfigDir );
}
catch( error ) {

// error printed to console above via inherit
process.exit( 1 ); // Exit with a failure code
}
} )();

/**
* Write an aggregate tsconfig file that checks all entry points.
*/
function writeEverythingTSConfigFile(): void {
const activeRepos = fs.readFileSync( '../perennial-alias/data/active-repos', 'utf-8' ).trim().split( /\r?\n/ ).map( s => s.trim() );

const filteredRepos = activeRepos.filter( repo => {
return fs.existsSync( `../${repo}/tsconfig.json` ) &&
// TODO: include these repos, see https://github.com/phetsims/chipper/issues/1487
repo !== 'phet-lib' && repo !== 'phet-vite-demo';
} );

const json = {
references: filteredRepos.map( repo => ( { path: `../../../${repo}` } ) )
};

const fileOutput = `/**
* File auto-generated by check.ts
*
* Explicitly list all entry points that we want to type check.
* Imported images/mipmaps/sounds are still type checked.
* This structure was determined in https://github.com/phetsims/chipper/issues/1245
* @author Michael Kauzmann (PhET Interactive Simulations)
* @author Sam Reid (PhET Interactive Simulations)
*/
${JSON.stringify( json, null, 2 )}`;

fs.writeFileSync( '../chipper/tsconfig/all/tsconfig.json', fileOutput );
}
8 changes: 2 additions & 6 deletions tsconfig/all/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
/**
* File auto-generated by check.ts
*
* Explicitly list all entry points that we want to type check.
* Imported images/mipmaps/sounds are still type checked.
* This structure was determined in https://github.com/phetsims/chipper/issues/1245
Expand Down Expand Up @@ -421,12 +423,6 @@
{
"path": "../../../phet-io-wrappers"
},
// {
// "path": "../../../phet-lib/tsconfig.json"
// },
// {
// "path": "../../../phet-vite-demo/tsconfig.json"
// },
{
"path": "../../../phetcommon"
},
Expand Down

0 comments on commit fd06ded

Please sign in to comment.