11import pathlib from 'path' ;
2- import { DefaultArtifactClient } from '@actions/artifact' ;
2+ import { ArtifactNotFoundError , DefaultArtifactClient } from '@actions/artifact' ;
33import * as core from '@actions/core' ;
44import { exec } from '@actions/exec' ;
55import { bundlesDir , outDir , tabsDir } from '@sourceacademy/modules-repotools/getGitRoot' ;
@@ -19,40 +19,49 @@ export async function main() {
1919 return ;
2020 }
2121
22- const tabPromises = Object . keys ( tabsResult . tabs ) . map ( async tabName => {
22+ const tabPromises = Object . keys ( tabsResult . tabs ) . map ( async ( tabName ) : Promise < string | null > => {
2323 try {
2424 const { artifact : { id } } = await artifact . getArtifact ( `${ tabName } -tab` ) ;
2525 await artifact . downloadArtifact ( id , { path : pathlib . join ( outDir , 'tabs' ) } ) ;
2626 core . info ( `Downloaded artifact for ${ tabName } ` ) ;
27- return ;
27+ return null ;
2828 } catch ( error ) {
29- core . error ( `Error retrieving artifact for ${ tabName } , will try building` ) ;
30- core . error ( error as Error ) ;
29+ if ( ! ( error instanceof ArtifactNotFoundError ) ) {
30+ throw error ;
31+ }
32+ core . error ( `Error retrieving artifact for ${ tabName } , need to try building` ) ;
33+ return tabName ;
3134 }
35+ } ) ;
3236
33- // Artifact could not be found, we probably need to build it
34- const focusExitCode = await exec ( 'yarn' , [ 'workspaces' , 'focus' , `@sourceacademy/tab-${ tabName } ` ] ) ;
35- if ( focusExitCode !== 0 ) {
36- core . setFailed ( `yarn workspace focus failed for ${ tabName } ` ) ;
37- return ;
38- }
37+ // Artifacts could not be found, we probably need to build it
38+ const tabsToBuild = ( await Promise . all ( tabPromises ) ) . filter ( x => x !== null ) ;
39+ if ( tabsToBuild . length === 0 ) return ;
3940
40- const buildExitCode = await exec ( 'yarn' , [ 'workspaces' , 'foreach' , '-A' , '--include' , `@sourceacademy/tab- ${ tabName } ` , 'run' , 'build' ] ) ;
41- if ( buildExitCode !== 0 ) {
42- core . setFailed ( `Building ${ tabName } failed` ) ;
43- return ;
44- }
45- core . info ( `Built ${ tabName } ` ) ;
46- } ) ;
41+ // focus all at once
42+ const workspaces = tabsToBuild . map ( each => `@sourceacademy/tab- ${ each } ` ) ;
43+ const focusExitCode = await exec ( 'yarn workspaces focus' , workspaces , { silent : false } ) ;
44+ if ( focusExitCode !== 0 ) {
45+ core . setFailed ( 'yarn workspace focus failed' ) ;
46+ return ;
47+ }
4748
48- await Promise . all ( tabPromises ) ;
49+ const workspaceBuildArgs = workspaces . map ( each => `--include ${ each } ` ) ;
50+ const buildExitCode = await exec (
51+ 'yarn workspaces foreach -pA' ,
52+ [ ...workspaceBuildArgs , 'run' , 'build' ] ,
53+ { silent : false }
54+ ) ;
55+ if ( buildExitCode !== 0 ) {
56+ core . setFailed ( 'Building tabs failed' ) ;
57+ }
4958}
5059
5160if ( process . env . GITHUB_ACTIONS ) {
5261 // Only automatically execute when running in github actions
5362 try {
5463 await main ( ) ;
5564 } catch ( error : any ) {
56- core . setFailed ( error . message ) ;
65+ core . setFailed ( error ) ;
5766 }
5867}
0 commit comments