Skip to content

Commit 747e564

Browse files
committed
fix(build): prune stale generated api endpoints
Clean list-specific JSON endpoint directories before rewriting them so local and deploy-style API builds do not fail on leftover files from older runs. Keep the count check, but count only generated JSON outputs so the verification reflects the actual endpoint set.
1 parent 7dfb03b commit 747e564

1 file changed

Lines changed: 15 additions & 3 deletions

File tree

build-lists.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { dirname, basename } from 'path'
1+
import { dirname, basename, extname, join } from 'path'
22

33
import os from 'os'
44
import fs from 'fs-extra'
@@ -448,6 +448,14 @@ class BuildLists {
448448

449449
const apiListDirectory = `${ apiDirectory }/${ listOptions.endpointPrefix }`
450450

451+
await fs.ensureDir( apiListDirectory )
452+
453+
for ( const existingFile of await fs.readdir( apiListDirectory ) ) {
454+
if ( extname( existingFile ) !== '.json' ) continue
455+
456+
await fs.remove( join( apiListDirectory, existingFile ) )
457+
}
458+
451459
// const poolSize = 1000
452460

453461
// Store app bundles to memory
@@ -516,14 +524,18 @@ class BuildLists {
516524
}
517525

518526
// Count saved files
519-
const fileCount = fs.readdirSync( apiListDirectory ).length
527+
const fileCount = fs.readdirSync( apiListDirectory )
528+
.filter( fileName => extname( fileName ) === '.json' )
529+
.length
520530

521531
console.log( fileCount, 'Files saved in', apiListDirectory )
522532
console.log( this.lists[listOptions.name].size, 'Entries' )
523533

524534
if ( fileCount !== this.lists[listOptions.name].size ) {
525535
const listSlugs = Array.from( this.lists[listOptions.name] ).map( listEntry => listEntry.slug )
526-
const fileNames = fs.readdirSync( apiListDirectory ).map( fileName => basename(fileName).split('.')[0] )
536+
const fileNames = fs.readdirSync( apiListDirectory )
537+
.filter( fileName => extname( fileName ) === '.json' )
538+
.map( fileName => basename(fileName).split('.')[0] )
527539

528540
logArraysDifference( listSlugs, fileNames )
529541

0 commit comments

Comments
 (0)