@@ -7,63 +7,62 @@ import { dirname } from 'path'
77 * Argv helpers.
88 */
99
10- const argument = ( name , fallback ) => {
10+ const argument = ( name ) => {
1111 const index = process . argv . findIndex ( argument => argument . startsWith ( `--${ name } =` ) )
1212
1313 return index === - 1
14- ? fallback ( )
14+ ? undefined
1515 : process . argv [ index ] . substring ( `--${ name } =` . length )
1616}
1717
1818const option = ( name ) => process . argv . includes ( `--${ name } ` )
1919
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` , ( ) => {
28- if ( ! wantsSsr ) {
29- return `./public/build/manifest.json`
30- }
31-
32- return existsSync ( `./bootstrap/ssr/ssr-manifest.json` ) ? `./bootstrap/ssr/ssr-manifest.json` : `./public/build/manifest.json`
33- } )
34- const assetsDirectory = argument ( `assets` , ( ) => `${ dirname ( manifestPath ) } /assets` )
35-
3620/*
3721 * Helpers.
3822 */
39- const info = quiet ? ( ( ) => undefined ) : console . log
23+ const info = option ( `quiet` ) ? ( ( ) => undefined ) : console . log
24+ const error = option ( `quiet` ) ? ( ( ) => undefined ) : console . error
4025
4126/*
4227 * Clean.
4328 */
4429
4530const main = ( ) => {
46- info ( `Reading manifest [${ manifestPath } ].` )
31+ const manifestPaths = argument ( `manifest` ) ? [ argument ( `manifest` ) ] : ( option ( `ssr` )
32+ ? [ `./bootstrap/ssr/ssr-manifest.json` , `./bootstrap/ssr/manifest.json` ]
33+ : [ `./public/build/manifest.json` ] )
4734
48- const manifest = JSON . parse ( readFileSync ( manifestPath ) . toString ( ) )
35+ const foundManifestPath = manifestPaths . find ( existsSync )
4936
50- const manifestKeys = Object . keys ( manifest )
37+ if ( ! foundManifestPath ) {
38+ error ( `Unable to find manifest file.` )
5139
52- const isSsr = Array . isArray ( manifest [ manifestKeys [ 0 ] ] )
40+ process . exit ( 1 )
41+ }
42+
43+ info ( `Reading manifest [${ foundManifestPath } ].` )
44+
45+ const manifest = JSON . parse ( readFileSync ( foundManifestPath ) . toString ( ) )
46+
47+ const manifestFiles = Object . keys ( manifest )
48+
49+ const isSsr = Array . isArray ( manifest [ manifestFiles [ 0 ] ] )
5350
5451 isSsr
5552 ? info ( `SSR manifest found.` )
5653 : info ( `Non-SSR manifest found.` )
5754
5855 const manifestAssets = isSsr
59- ? manifestKeys . flatMap ( key => manifest [ key ] )
60- : manifestKeys . map ( key => manifest [ key ] . file )
56+ ? manifestFiles . flatMap ( key => manifest [ key ] )
57+ : manifestFiles . map ( key => manifest [ key ] . file )
58+
59+ const assetsPath = argument ( 'assets' ) ?? dirname ( foundManifestPath ) + '/assets'
6160
62- info ( `Verify assets in [${ assetsDirectory } ].` )
61+ info ( `Verify assets in [${ assetsPath } ].` )
6362
64- const allAssets = readdirSync ( assetsDirectory , { withFileTypes : true } )
63+ const existingAssets = readdirSync ( assetsPath , { withFileTypes : true } )
6564
66- const orphanedAssets = allAssets . filter ( file => file . isFile ( ) )
65+ const orphanedAssets = existingAssets . filter ( file => file . isFile ( ) )
6766 . filter ( file => manifestAssets . findIndex ( asset => asset . endsWith ( `/${ file . name } ` ) ) === - 1 )
6867
6968 if ( orphanedAssets . length === 0 ) {
@@ -74,13 +73,13 @@ const main = () => {
7473 : info ( `[${ orphanedAssets . length } ] orphaned assets found.` )
7574
7675 orphanedAssets . forEach ( asset => {
77- const path = `${ assetsDirectory } /${ asset . name } `
76+ const path = `${ assetsPath } /${ asset . name } `
7877
79- dryRun
78+ option ( `dry-run` )
8079 ? info ( `Orphaned asset [${ path } ] would be removed.` )
8180 : info ( `Removing orphaned asset [${ path } ].` )
8281
83- if ( ! dryRun ) {
82+ if ( ! option ( `dry-run` ) ) {
8483 unlinkSync ( path )
8584 }
8685 } )
0 commit comments