1
0
mirror of synced 2025-12-23 11:54:18 -05:00

Check for orphaned assets in CI (#24074)

This commit is contained in:
Peter Bengtsson
2022-01-05 11:26:45 -05:00
committed by GitHub
parent 3a8c8c4ba3
commit 26d2c5438a
2 changed files with 34 additions and 4 deletions

View File

@@ -0,0 +1,30 @@
name: 'Orphaned assets check'
# **What it does**: Checks that there are no files in ./assets/ that aren't mentioned in any source file.
# **Why we have it**: To avoid orphans into the repo.
# **Who does it impact**: Docs content.
on:
pull_request:
permissions:
contents: read
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@ec3a7ce113134d7a93b817d10a8272cb61118579
- name: Setup node
uses: actions/setup-node@04c56d2f954f1e4c69436aa54cfef261a018f458
with:
node-version: 16.13.x
cache: npm
- name: Install
run: npm ci
- name: Check for orphaned assets
run: ./script/find-orphaned-assets.mjs --verbose --exit

View File

@@ -20,7 +20,7 @@ const EXCEPTIONS = new Set([
])
program
.description('Print all images that are in ./assets/ but not found in any markdown')
.description('Print all images that are in ./assets/ but not found in any source files')
.option('-e, --exit', 'Exit script by count of orphans (useful for CI)')
.option('-v, --verbose', 'Verbose outputs')
.option('--json', 'Output in JSON format')
@@ -83,8 +83,8 @@ async function main(opts) {
verbose && console.log(`${allImages.size.toLocaleString()} images found in total.`)
for (const markdownFile of sourceFiles) {
const content = fs.readFileSync(markdownFile, 'utf-8')
for (const sourceFile of sourceFiles) {
const content = fs.readFileSync(sourceFile, 'utf-8')
for (const imagePath of allImages) {
const needle = imagePath.split(path.sep).slice(-2).join('/')
if (content.includes(needle) || EXCEPTIONS.has(imagePath)) {
@@ -94,7 +94,7 @@ async function main(opts) {
}
if (verbose && allImages.size) {
console.log('The following files are not mentioned anywhere in any Markdown file')
console.log('The following files are not mentioned anywhere in any source file')
}
if (json) {
console.log(JSON.stringify([...allImages], undefined, 2))