1- const core = require ( '@actions/core' ) ;
2- const exec = require ( '@actions/exec' ) ;
3- const glob = require ( '@actions/glob' ) ;
4- const github = require ( '@actions/github' ) ;
5- const path = require ( 'path' ) ;
1+ import * as core from '@actions/core' ;
2+ import * as exec from '@actions/exec' ;
3+ import * as glob from '@actions/glob' ;
4+ import * as github from '@actions/github' ;
5+ import * as path from 'path' ;
66
7- function getInput ( inputAlternativeNames , { required = false } = { } ) {
8- if ( ! ( inputAlternativeNames && inputAlternativeNames . length ) ) throw new Error ( "inputAlternativeNames is empty" ) ;
7+ function getInput ( inputAlternativeNames : string [ ] , { required = false } = { } ) {
8+ if ( inputAlternativeNames . length === 0 ) throw new Error ( "inputAlternativeNames is empty" ) ;
99 let val = "" ;
1010 for ( const [ i , inputName ] of inputAlternativeNames . entries ( ) ) {
1111 val = core . getInput ( inputName , {
@@ -21,40 +21,38 @@ const owner = 'google';
2121const repo = 'google-java-format' ;
2222const githubToken = getInput ( [ 'githubToken' , 'github-token' ] , { required : false } ) ;
2323const commitMessage = getInput ( [ 'commitMessage' , 'commit-message' ] , { required : false } ) ;
24- const executable = path . join ( process . env . HOME || process . env . USERPROFILE , 'google-java-format.jar' ) ;
24+ const executable = path . join ( ( process . env . HOME || process . env . USERPROFILE ) ! , 'google-java-format.jar' ) ;
2525const apiReleases = `https://api.github.com/repos/${ owner } /${ repo } /releases` ;
2626
27- class ExecResult {
28- constructor ( exitCode , stdOut , stdErr ) {
29- this . exitCode = exitCode ;
30- this . stdOut = stdOut ;
31- this . stdErr = stdErr ;
32- }
27+ interface ExecResult {
28+ exitCode : number ;
29+ stdOut : string ;
30+ stdErr : string ;
3331}
3432
35- async function executeGJF ( args = [ ] ) {
36- const arguments = [ ] ;
33+ async function executeGJF ( args : string [ ] = [ ] ) {
34+ const allArgs = new Array < string > ( ) ;
3735 const javaVersion = await getJavaVersion ( ) ;
3836 // see https://github.com/google/google-java-format#jdk-16
3937 if ( javaVersion !== undefined && javaVersion >= 11 )
40- arguments . push ( ...[ 'api' , 'file' , 'parser' , 'tree' , 'util' ]
38+ allArgs . push ( ...[ 'api' , 'file' , 'parser' , 'tree' , 'util' ]
4139 . flatMap ( l => [ '--add-exports' , `jdk.compiler/com.sun.tools.javac.${ l } =ALL-UNNAMED` ] ) ) ;
42- arguments . push ( '-jar' , executable ) ;
43- arguments . push ( ...args ) ;
40+ allArgs . push ( '-jar' , executable ) ;
41+ allArgs . push ( ...args ) ;
4442 const options = {
4543 cwd : process . env . GITHUB_WORKSPACE ,
4644 ignoreReturnCode : true
4745 }
48- const exitCode = await exec . exec ( 'java' , arguments , options ) ;
46+ const exitCode = await exec . exec ( 'java' , allArgs , options ) ;
4947 if ( exitCode !== 0 ) {
5048 throw `Google Java Format failed with exit code ${ exitCode } ` ;
5149 }
5250}
5351
54- async function execute ( command , { silent = false , ignoreReturnCode = false } = { } ) {
52+ async function execute ( command : string , { silent = false , ignoreReturnCode = false } = { } ) : Promise < ExecResult > {
5553 let stdErr = '' ;
5654 let stdOut = '' ;
57- const options = {
55+ const options : exec . ExecOptions = {
5856 silent : silent ,
5957 ignoreReturnCode : true ,
6058 listeners : {
@@ -63,18 +61,18 @@ async function execute(command, { silent = false, ignoreReturnCode = false } = {
6361 }
6462 } ;
6563 core . debug ( `Executing: ${ command } ` ) ;
66- const exitCode = await exec . exec ( command , null , options ) ;
64+ const exitCode = await exec . exec ( command , undefined , options ) ;
6765 core . debug ( `Exit code: ${ exitCode } ` ) ;
6866 if ( ! ignoreReturnCode && exitCode !== 0 ) {
6967 command = command . split ( ' ' ) [ 0 ] ;
7068 throw `The command '${ command } ' failed with exit code ${ exitCode } ` ;
7169 }
72- return new ExecResult ( exitCode , stdOut , stdErr ) ;
70+ return { exitCode, stdOut, stdErr } ;
7371}
7472
75- async function curl ( url , arguments ) {
73+ async function curl ( url : string , args ?: string ) {
7674 let command = `curl -sL "${ url } "` ;
77- if ( arguments ) command += ` ${ arguments } ` ;
75+ if ( args ) command += ` ${ args } ` ;
7876 return await execute ( command , { silent : ! core . isDebug ( ) } ) ;
7977}
8078
@@ -91,7 +89,7 @@ async function listGJFReleases() {
9189 return releases . data ;
9290}
9391
94- async function getRelease ( releaseId ) {
92+ async function getRelease ( releaseId : number ) {
9593 if ( ! githubToken ) {
9694 const url = `${ apiReleases } /${ releaseId } ` ;
9795 core . debug ( `URL: ${ url } ` ) ;
@@ -108,21 +106,23 @@ async function getRelease(releaseId) {
108106}
109107
110108async function getJavaVersion ( ) {
111- let javaVersion = await execute ( 'java -version' , { silent : ! core . isDebug ( ) } ) ;
112- javaVersion = javaVersion . stdErr
109+ const javaVersion = await execute ( 'java -version' , { silent : ! core . isDebug ( ) } ) ;
110+ let versionNumber = javaVersion . stdErr
113111 . split ( '\n' ) [ 0 ]
114- . match ( RegExp ( / [ 0 - 9 \. ] + / ) ) [ 0 ] ;
115- core . debug ( `Extracted version number: ${ javaVersion } ` ) ;
116- if ( javaVersion . startsWith ( '1.' ) ) javaVersion = javaVersion . replace ( RegExp ( / ^ 1 \. / ) , '' ) ;
117- javaVersion = javaVersion . split ( '\.' ) [ 0 ] ;
118- return parseInt ( javaVersion ) ;
112+ . match ( RegExp ( / [ 0 - 9 \. ] + / ) ) ?. [ 0 ] ;
113+ if ( ! versionNumber ) throw new Error ( "Cannot find Java version number" ) ;
114+ core . debug ( `Extracted version number: ${ versionNumber } ` ) ;
115+ if ( versionNumber . startsWith ( '1.' ) ) versionNumber = versionNumber . replace ( RegExp ( / ^ 1 \. / ) , '' ) ;
116+ versionNumber = versionNumber . split ( '\.' ) [ 0 ] ;
117+ return parseInt ( versionNumber ) ;
119118}
120119
121120async function getReleaseId ( ) {
122121 let releaseId = 'latest' ;
123122 const releases = await listGJFReleases ( ) ;
124123 core . debug ( `releases is ${ typeof releases } ` ) ;
125- const findRelease = function ( name ) { return releases . find ( r => r [ 'name' ] === name ) ; } ;
124+ // TODO model type
125+ const findRelease = function ( name : string ) { return releases . find ( ( r : any ) => r [ 'name' ] === name ) ; } ;
126126 // Check if a specific version is requested
127127 const input = core . getInput ( 'version' ) ;
128128 if ( input ) {
@@ -162,7 +162,8 @@ async function run() {
162162 core . debug ( `release is ${ typeof release } ` ) ;
163163 const assets = release [ 'assets' ] ;
164164 core . debug ( `assets is ${ typeof assets } ` ) ;
165- const downloadUrl = assets . find ( asset => asset [ 'name' ] . endsWith ( 'all-deps.jar' ) ) [ 'browser_download_url' ] ;
165+ // TODO model type of asset
166+ const downloadUrl = assets . find ( ( asset : any ) => asset [ 'name' ] . endsWith ( 'all-deps.jar' ) ) [ 'browser_download_url' ] ;
166167 core . info ( `Downloading executable to ${ executable } ` ) ;
167168 await curl ( downloadUrl , `-o ${ executable } ` )
168169 await executeGJF ( [ '--version' ] ) ;
0 commit comments