Skip to content

Commit

Permalink
Convert tsconfig to use project references, see phetsims/chipper#1356
Browse files Browse the repository at this point in the history
  • Loading branch information
samreid committed Sep 20, 2024
1 parent 754702e commit 3f08d30
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 12 deletions.
133 changes: 123 additions & 10 deletions js/grunt/Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -541,10 +541,17 @@ module.exports = function( grunt ) {

// --disable-eslint-cache disables the cache, useful for developing rules
const cache = !grunt.option( 'disable-eslint-cache' );
const activeRepos = getDataFile( 'active-repos' ).filter( repo => repo !== 'perennial-alias' ); // remove duplicate perennial copy
const activeRepos = getDataFile( 'active-repos' ).filter( repo =>

// remove duplicate perennial copy
repo !== 'perennial-alias' &&

// TODO: Has no grunt file, maybe has a different way of linting? see https://github.com/phetsims/chipper/issues/1356
repo !== 'phet-vite-demo' );
const fix = grunt.option( 'fix' );
const chipAway = grunt.option( 'chip-away' );
const showProgressBar = !grunt.option( 'hide-progress-bar' );

console.log( `linting ${activeRepos.length} repos:` );

let lint;
try {
Expand All @@ -557,17 +564,123 @@ module.exports = function( grunt ) {

// The APIs are the same for these two versions of lint support
if ( lint.chipperAPIVersion === 'promisesPerRepo1' || lint.chipperAPIVersion === 'npx' ) {
const lintReturnValue = await lint( activeRepos, {

/**
* Lints repositories using a worker pool approach.
*
* @param {Array} activeRepos - Array of repositories to lint.
* @param {object} options - Configuration options for the lint function.
*/
// eslint-disable-next-line no-inner-declarations
async function lintRepositoriesWithWorkers( activeRepos, options ) {
// Clone the activeRepos array to avoid mutating the original array
const reposQueue = [ ...activeRepos ];

// Shared counter to keep track of processed repositories
let processedCount = 0;

// Total number of repositories
const totalRepos = reposQueue.length;

// Array to collect failed repositories
const failedRepos = [];

/**
* Worker function that continuously processes repositories from the queue.
*/
const worker = async workerId => {
// eslint-disable-next-line no-constant-condition
while ( true ) {

// Synchronize access to the queue
// Since JavaScript is single-threaded, this is safe
if ( reposQueue.length === 0 ) {
break; // No more repositories to process
}

const repo = reposQueue.shift(); // Get the next repository

try {
// Lint the single repository.
const lintReturnValue = await lint( [ repo ], options );

// Check linting result
if ( !lintReturnValue.ok ) {

// Collect the failed repository and its errors
failedRepos.push( {
repo: repo,
errors: lintReturnValue.errors || lintReturnValue.message || 'Unknown lint error'
} );
console.log( `✖ Lint failed: ${repo}` );
}
else {
console.log( `✔ Lint passed: ${repo}` );
}

// Update progress
processedCount++;
if ( options.showProgressBar ) {
// Example: Update a progress bar here
// progressBar.update(processedCount / totalRepos);
console.log( `Progress: ${processedCount}/${totalRepos} repositories linted.` );
}
}
catch( error ) {

// Handle unexpected errors during linting
failedRepos.push( {
repo: repo,
errors: error.message || 'Unexpected error during linting'
} );
console.error( `✖ An error occurred while trying to lint repo: ${repo}`, error );
}
}

// TODO: https://github.com/phetsims/chipper/issues/1356 Clean up debugging output
// console.log( `Worker ${workerId} has completed all assigned repositories.` );
};

// TODO: https://github.com/phetsims/chipper/issues/1356 fine tune the number of workers
const numWorkers = 4;
const workers = _.times( numWorkers, i => worker( i + 1 ) );

// Wait for all workers to complete
await Promise.all( workers );

// After all workers have finished, check for any failed repositories
if ( failedRepos.length > 0 ) {
console.log( `\nLinting completed with ${failedRepos.length} error(s):` );

// Iterate through failedRepos and log each error
failedRepos.forEach( ( fail, index ) => {
console.log( `${index + 1}. Repository: ${fail.repo}` );
console.log( ` Error: ${fail.errors}` );
} );

// Fail the Grunt task with a summary of failures
grunt.fail.fatal( `Linting failed for ${failedRepos.length} out of ${totalRepos} repositories.` );
}
else {
// If all repositories passed linting
console.log( 'All repositories linted successfully.' );
}
}

const options = {
cache: cache,
fix: fix,
chipAway: chipAway,
showProgressBar: showProgressBar
} );

// Output results on errors.
if ( !lintReturnValue.ok ) {
grunt.fail.fatal( 'Lint failed' );
}
showProgressBar: false
};

// Call the function with activeRepos and options
await lintRepositoriesWithWorkers( activeRepos, options )
.catch( error => {
// Handle any unexpected errors that occurred outside the worker processing
console.error( 'An unexpected error occurred during linting:', error );
grunt.fail.fatal( 'Linting process encountered an unexpected error.' );
} );
}
} ) );

Expand Down
2 changes: 1 addition & 1 deletion js/scripts/lint-tsc.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const reset = '\u001b[0m';
};

const runTsc = async () => {
tscResults = await execute( 'node', [ '--max-old-space-size=8192', '../../node_modules/typescript/bin/tsc' ], '../chipper/tsconfig/all', {
tscResults = await execute( 'node', [ '--max-old-space-size=8192', '../../node_modules/typescript/bin/tsc', '-b' ], '../chipper/tsconfig/all', {
errors: 'resolve'
} );
outputResult( 'tsc', tscResults );
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
],
"parserOptions": {
"project": [
"../perennial-alias/tsconfig.json"
"../perennial/tsconfig.json"
]
}
}
Expand Down

0 comments on commit 3f08d30

Please sign in to comment.