-
Notifications
You must be signed in to change notification settings - Fork 152
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8e1a71a
commit 31a4573
Showing
2 changed files
with
61 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import fs from 'fs' | ||
|
||
/* | ||
* 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 root = argument('root', () => process.cwd()) | ||
const manifestPath = argument('manifest-path', () => `${root}/public/build/manifest.json`) | ||
const assetsDirectory = argument('asset-directory', () => `${root}/public/build/assets`) | ||
|
||
/* | ||
* Clean. | ||
*/ | ||
|
||
console.log('Removing orphaned assets...') | ||
|
||
const manifest = JSON.parse(fs.readFileSync(manifestPath).toString()) | ||
|
||
const manifestAssets = Object.keys(manifest).map(key => manifest[key].file) | ||
|
||
const allAssets = fs.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) { | ||
console.log('There are no ophaned assets.') | ||
} else { | ||
orphanedAssets.forEach(asset => { | ||
const path = `${assetsDirectory}/${asset.name}` | ||
|
||
console.log(`Removing ${path}`) | ||
|
||
if (! dryRun) { | ||
fs.unlinkSync(path) | ||
} | ||
}) | ||
|
||
console.log('Done cleaning.') | ||
} | ||
|