Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[1.x] Introduce clean-orphaned-assets binary #251

Merged
merged 3 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 91 additions & 0 deletions bin/clear-orphaned-assets.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#!/usr/bin/env node

import { readFileSync, readdirSync, unlinkSync } from 'fs'
import { dirname } from 'path'

/*
* Argv helpers.
*/

const argument = (name, fallback) => {
const index = process.argv.findIndex(argument => argument.startsWith(`--${name}=`))

return index === -1
? fallback()
: process.argv[index].substring(`--${name}=`.length)
}

const option = (name) => process.argv.includes(`--${name}`)

/*
* Configuration.
*/

const dryRun = option(`dry-run`)
const quiet = option(`quiet`)
const wantsSsr = option('ssr')
const manifestPath = argument(`manifest`, () => wantsSsr ? `./bootstrap/ssr/ssr-manifest.json` : `./public/build/manifest.json`)
const assetsDirectory = argument(`assets`, () => `${dirname(manifestPath)}/assets`)

/*
* Helpers.
*/
const info = quiet ? (() => undefined) : console.log
const error = quiet ? (() => undefined) : console.error

/*
* Clean.
*/

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

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

const manifestKeys = Object.keys(manifest)

const isSsr = Array.isArray(manifest[manifestKeys[0]])

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

process.exit(1)
}

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

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

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) {
info(`No ophaned assets found.`)
} else {
orphanedAssets.length === 1
? info(`[${orphanedAssets.length}] orphaned asset found.`)
: info(`[${orphanedAssets.length}] orphaned assets found.`)

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

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

if (! dryRun) {
unlinkSync(path)
}
})
}
}

main()
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
"/dist",
"/inertia-helpers"
],
"bin": {
"clean-orphaned-assets": "bin/clean.js"
},
"scripts": {
"build": "npm run build-plugin && npm run build-inertia-helpers",
"build-plugin": "rm -rf dist && npm run build-plugin-types && npm run build-plugin-esm && cp src/dev-server-index.html dist/",
Expand Down