Skip to content

Commit

Permalink
Write to stderr
Browse files Browse the repository at this point in the history
  • Loading branch information
timacdonald committed Oct 11, 2023
1 parent 3013909 commit 4a66b25
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions bin/purge-orphaned-assets.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ const assetsDirectory = argument(`assets`, () => `${dirname(manifestPath)}/asset
/*
* Helpers.
*/

const write = quiet ? (() => undefined) : console.log
const info = quiet ? (() => undefined) : console.log
const error = quiet ? (() => undefined) : console.error

/*
* Clean.
*/

const main = () => {
write(`Reading manifest [${manifestPath}].`)
info(`Reading manifest [${manifestPath}].`)

const manifest = JSON.parse(readFileSync(manifestPath).toString())

Expand All @@ -47,39 +47,39 @@ const main = () => {
const isSsr = Array.isArray(manifest[manifestKeys[0]])

if (wantsSsr && ! isSsr) {
write('Did not find an SSR manifest.')
error('Did not expected SSR manifest.')

process.exit(1)
}

isSsr
? write(`SSR manifest found.`)
: write(`Non-SSR manifest found.`)
? info(`SSR manifest found.`)
: info(`Non-SSR manifest found.`)

const manifestAssets = isSsr
? manifestKeys.flatMap(key => manifest[key])
: manifestKeys.map(key => manifest[key].file)

write(`Verify assets in [${assetsDirectory}].`)
info(`Verify assets in [${assetsDirectory}].`)

const allAssets = readdirSync(assetsDirectory, { withFileTypes: true })

const orphanedAssets = allAssets.filter(file => file.isFile())
.filter(file => manifestAssets.findIndex(asset => asset.endsWith(`/${file.name}`)) === -1)

if (orphanedAssets.length === 0) {
write(`No ophaned assets found.`)
info(`No ophaned assets found.`)
} else {
orphanedAssets.length === 1
? write(`[${orphanedAssets.length}] orphaned asset found.`)
: write(`[${orphanedAssets.length}] orphaned assets found.`)
? info(`[${orphanedAssets.length}] orphaned asset found.`)
: info(`[${orphanedAssets.length}] orphaned assets found.`)

orphanedAssets.forEach(asset => {
const path = `${assetsDirectory}/${asset.name}`

dryRun
? write(`Orphaned asset [${path}] would be removed.`)
: write(`Removing orphaned asset [${path}].`)
? info(`Orphaned asset [${path}] would be removed.`)
: info(`Removing orphaned asset [${path}].`)

if (! dryRun) {
unlinkSync(path)
Expand Down

0 comments on commit 4a66b25

Please sign in to comment.