@@ -184,6 +184,40 @@ async function buildAndCopyCoreJS() {
184184 console . timeEnd ( `Core JS bundle built` ) ;
185185}
186186
187+ async function getExcludedBuilds ( ) {
188+ const branchBuilds = await fs . readdir ( './branches/' ) ;
189+ const excluded = new Set ( ) ;
190+ for ( const branch of branchBuilds ) {
191+ const link = await fs . readlink ( `./branches/${ branch } ` ) ;
192+ if ( ! link ) continue ;
193+ const parts = link . split ( '/' ) ;
194+ const buildId = parts [ parts . length - 2 ] ;
195+ excluded . add ( buildId ) ;
196+ }
197+ const latestBuildLink = await fs . readlink ( `./latest` ) ;
198+ if ( latestBuildLink ) {
199+ const parts = latestBuildLink . split ( '/' ) ;
200+ const buildId = parts [ parts . length - 2 ] ;
201+ excluded . add ( buildId ) ;
202+ }
203+
204+ return Array . from ( excluded ) ;
205+ }
206+
207+ async function clearOldBuilds ( ) {
208+ console . log ( `Clearing old builds...` ) ;
209+ console . time ( `Cleared old builds` ) ;
210+ const excluded = await getExcludedBuilds ( ) ;
211+ const builds = await fs . readdir ( buildsRootDir ) ;
212+ for ( const build of builds ) {
213+ if ( ! excluded . includes ( build ) ) {
214+ await exec ( `rm -rf ${ path . join ( './' , buildsRootDir , '/' , build ) } ` ) ;
215+ console . log ( `Build removed: "${ path . join ( './' , buildsRootDir , '/' , build ) } "` , path ) ;
216+ }
217+ }
218+ console . timeEnd ( `Cleared old builds` ) ;
219+ }
220+
187221async function run ( ) {
188222 console . time ( 'Finished in' ) ;
189223 await createBuildDir ( ) ;
@@ -216,6 +250,7 @@ async function run() {
216250 await switchBranchToLatestBuild ( targetBranch ) ;
217251 }
218252 await clearBuildDir ( ) ;
253+ await clearOldBuilds ( ) ;
219254 console . timeEnd ( 'Finished in' ) ;
220255}
221256
0 commit comments