Skip to content

Commit

Permalink
Add pre-commit hook for typechecking via tsc, see #1047
Browse files Browse the repository at this point in the history
  • Loading branch information
samreid committed Jun 16, 2021
1 parent 7abf6b7 commit 38458f1
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 17 deletions.
14 changes: 8 additions & 6 deletions js/grunt/Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ module.exports = function( grunt ) {
...( grunt.option( 'lint' ) === false ? [] : [ 'lint-all' ] ),
...( grunt.option( 'report-media' ) === false ? [] : [ 'report-media' ] ),
'clean',
'tsc',
'tsc-build',
'build'
] );

Expand Down Expand Up @@ -143,7 +143,6 @@ module.exports = function( grunt ) {
grunt.file.write( `${buildDir}/${repo}-${size.width}.png`, await generateThumbnails( repo, size.width, size.height, 100, jimp.MIME_PNG ) );
}


const altScreenshots = grunt.file.expand( { filter: 'isFile', cwd: `../${repo}/assets` }, [ `./${repo}-screenshot-alt[0123456789].png` ] );
for ( const altScreenshot of altScreenshots ) {
const imageNumber = parseInt( altScreenshot.substr( `./${repo}-screenshot-alt`.length, 1 ), 10 );
Expand All @@ -157,10 +156,13 @@ module.exports = function( grunt ) {
}
} ) );

grunt.registerTask( 'tsc', 'Runs tsc --build to transpile JS/TS before the webpack step. Requires the chipper branch "typescript"',
wrapTask( async () => {
await tsc( repo );
} ) );
grunt.registerTask( 'tsc', 'Runs tsc with any command line options. Requires the chipper branch "typescript"',
wrapTask( async () => await tsc( repo, process.argv.slice( 3 ) ) )
);

grunt.registerTask( 'tsc-build', 'Runs tsc --build to transpile JS/TS before the webpack step. Requires the chipper branch "typescript"',
wrapTask( async () => await tsc( repo, [ '--build' ] ) )
);

grunt.registerTask( 'build',
`Builds the repository. Depending on the repository type (runnable/wrapper/standalone), the result may vary.
Expand Down
15 changes: 11 additions & 4 deletions js/grunt/execute.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,18 @@ module.exports = function( cmd, args, options ) {
* @param {Array.<string>} args
* @param {string} cwd
* @param {string} stdout
* @param {string} stderr
* @param {number} code - exit code
*/
constructor( cmd, args, cwd, stdout, code ) {
constructor( cmd, args, cwd, stdout, stderr, code ) {
super( `${cmd} ${args.join( ' ' )} in ${cwd} failed with exit code ${code} and stdout:\n${stdout}` );

// @public
this.cmd = cmd;
this.args = args;
this.cwd = cwd;
this.stdout = stdout;
this.stderr = stderr;
this.code = code;
}
}
Expand All @@ -64,17 +66,22 @@ module.exports = function( cmd, args, options ) {
} );
grunt.log.debug( `running ${cmd} ${args.join( ' ' )} from ${cwd}` );

let stdout = ''; // to be appended to
// to be appended to
let stdout = '';
let stderr = '';

process.stderr.on( 'data', data => grunt.log.debug( `stderr: ${data}` ) );
process.stderr.on( 'data', data => {
stderr += data;
grunt.log.debug( `stderr: ${data}` );
} );
process.stdout.on( 'data', data => {
stdout += data;
grunt.log.debug( `stdout: ${data}` );
} );

process.on( 'close', code => {
if ( code !== 0 ) {
reject( new ExecuteError( cmd, args, cwd, stdout, code ) );
reject( new ExecuteError( cmd, args, cwd, stdout, stderr, code ) );
}
else {
resolve( stdout );
Expand Down
32 changes: 25 additions & 7 deletions js/grunt/tsc.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,7 @@
const execute = require( './execute' );
const assert = require( 'assert' );

/**
* @public
*
* @param {string} repo
*/
module.exports = async function( repo ) {
const tsc = async function( repo, commandLineArgs ) {
const startTime = Date.now();

// make sure we are using the right version of the tsc compiler, so we all get the same output
Expand All @@ -27,14 +22,28 @@ module.exports = async function( repo ) {
try {
const version = ( await execute( 'node', [ '../chipper/node_modules/typescript/bin/tsc', '--version' ], `../${repo}` ) ).trim();
assert && assert( version === 'Version 4.3.2', `Incompatible tsc version: ${version}, expected Version 4.3.2` );
const stdout = ( await execute( 'node', [ '../chipper/node_modules/typescript/bin/tsc', '--build' ], `../${repo}` ) ).trim();
let stdout;
try {
stdout = ( await execute( 'node', [ '../chipper/node_modules/typescript/bin/tsc', ...commandLineArgs ], `../${repo}` ) ).trim();
}
catch( e ) {
console.log( `tsc completed with stdout:\n${e.stderr}` );
console.log( `tsc completed with stderr:\n${e.stdout}` );
return e;
}
const endTime = Date.now();
const elapsedTime = endTime - startTime;
if ( stdout.length === 0 ) {
console.log( `tsc completed in ${elapsedTime} ms` );
return {
stderr: '',
stdout: ''
};
}
else {
console.log( `tsc completed with stdout: ${stdout}` );

return { stderr: '', stdout: stdout };
}
}
catch( e ) {
Expand All @@ -44,3 +53,12 @@ module.exports = async function( repo ) {
throw e;
}
};

tsc.apiVersion = '1.0';
/**
* @public
*
* @param {string} repo
* @param {string[]} commandLineArgs
*/
module.exports = tsc;

0 comments on commit 38458f1

Please sign in to comment.