Skip to content

Commit b77e7ad

Browse files
authored
[1.x] Introduce clean-orphaned-assets binary (#251)
* Introduce script to purge orphaned assets * Write to stderr * Adopt "clean" naming
1 parent 6450b9f commit b77e7ad

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed

bin/clear-orphaned-assets.js

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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()

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
"/dist",
3131
"/inertia-helpers"
3232
],
33+
"bin": {
34+
"clean-orphaned-assets": "bin/clean.js"
35+
},
3336
"scripts": {
3437
"build": "npm run build-plugin && npm run build-inertia-helpers",
3538
"build-plugin": "rm -rf dist && npm run build-plugin-types && npm run build-plugin-esm && cp src/dev-server-index.html dist/",

0 commit comments

Comments
 (0)