|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +import { readFileSync, readdirSync, unlinkSync } from 'fs' |
| 4 | +import { dirname } from 'path' |
| 5 | + |
| 6 | +/* |
| 7 | + * Argv helpers. |
| 8 | + */ |
| 9 | + |
| 10 | +const argument = (name, fallback) => { |
| 11 | + const index = process.argv.findIndex(argument => argument.startsWith(`--${name}=`)) |
| 12 | + |
| 13 | + return index === -1 |
| 14 | + ? fallback() |
| 15 | + : process.argv[index].substring(`--${name}=`.length) |
| 16 | +} |
| 17 | + |
| 18 | +const option = (name) => process.argv.includes(`--${name}`) |
| 19 | + |
| 20 | +/* |
| 21 | + * Configuration. |
| 22 | + */ |
| 23 | + |
| 24 | +const dryRun = option(`dry-run`) |
| 25 | +const quiet = option(`quiet`) |
| 26 | +const wantsSsr = option('ssr') |
| 27 | +const manifestPath = argument(`manifest`, () => wantsSsr ? `./bootstrap/ssr/ssr-manifest.json` : `./public/build/manifest.json`) |
| 28 | +const assetsDirectory = argument(`assets`, () => `${dirname(manifestPath)}/assets`) |
| 29 | + |
| 30 | +/* |
| 31 | + * Helpers. |
| 32 | + */ |
| 33 | +const info = quiet ? (() => undefined) : console.log |
| 34 | +const error = quiet ? (() => undefined) : console.error |
| 35 | + |
| 36 | +/* |
| 37 | + * Clean. |
| 38 | + */ |
| 39 | + |
| 40 | +const main = () => { |
| 41 | + info(`Reading manifest [${manifestPath}].`) |
| 42 | + |
| 43 | + const manifest = JSON.parse(readFileSync(manifestPath).toString()) |
| 44 | + |
| 45 | + const manifestKeys = Object.keys(manifest) |
| 46 | + |
| 47 | + const isSsr = Array.isArray(manifest[manifestKeys[0]]) |
| 48 | + |
| 49 | + if (wantsSsr && ! isSsr) { |
| 50 | + error('Did not expected SSR manifest.') |
| 51 | + |
| 52 | + process.exit(1) |
| 53 | + } |
| 54 | + |
| 55 | + isSsr |
| 56 | + ? info(`SSR manifest found.`) |
| 57 | + : info(`Non-SSR manifest found.`) |
| 58 | + |
| 59 | + const manifestAssets = isSsr |
| 60 | + ? manifestKeys.flatMap(key => manifest[key]) |
| 61 | + : manifestKeys.map(key => manifest[key].file) |
| 62 | + |
| 63 | + info(`Verify assets in [${assetsDirectory}].`) |
| 64 | + |
| 65 | + const allAssets = readdirSync(assetsDirectory, { withFileTypes: true }) |
| 66 | + |
| 67 | + const orphanedAssets = allAssets.filter(file => file.isFile()) |
| 68 | + .filter(file => manifestAssets.findIndex(asset => asset.endsWith(`/${file.name}`)) === -1) |
| 69 | + |
| 70 | + if (orphanedAssets.length === 0) { |
| 71 | + info(`No ophaned assets found.`) |
| 72 | + } else { |
| 73 | + orphanedAssets.length === 1 |
| 74 | + ? info(`[${orphanedAssets.length}] orphaned asset found.`) |
| 75 | + : info(`[${orphanedAssets.length}] orphaned assets found.`) |
| 76 | + |
| 77 | + orphanedAssets.forEach(asset => { |
| 78 | + const path = `${assetsDirectory}/${asset.name}` |
| 79 | + |
| 80 | + dryRun |
| 81 | + ? info(`Orphaned asset [${path}] would be removed.`) |
| 82 | + : info(`Removing orphaned asset [${path}].`) |
| 83 | + |
| 84 | + if (! dryRun) { |
| 85 | + unlinkSync(path) |
| 86 | + } |
| 87 | + }) |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +main() |
0 commit comments