Skip to content

Commit 26d2c54

Browse files
author
Peter Bengtsson
authored
Check for orphaned assets in CI (#24074)
1 parent 3a8c8c4 commit 26d2c54

2 files changed

Lines changed: 34 additions & 4 deletions

File tree

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
name: 'Orphaned assets check'
2+
3+
# **What it does**: Checks that there are no files in ./assets/ that aren't mentioned in any source file.
4+
# **Why we have it**: To avoid orphans into the repo.
5+
# **Who does it impact**: Docs content.
6+
7+
on:
8+
pull_request:
9+
10+
permissions:
11+
contents: read
12+
13+
jobs:
14+
build:
15+
runs-on: ubuntu-latest
16+
steps:
17+
- name: Checkout
18+
uses: actions/checkout@ec3a7ce113134d7a93b817d10a8272cb61118579
19+
20+
- name: Setup node
21+
uses: actions/setup-node@04c56d2f954f1e4c69436aa54cfef261a018f458
22+
with:
23+
node-version: 16.13.x
24+
cache: npm
25+
26+
- name: Install
27+
run: npm ci
28+
29+
- name: Check for orphaned assets
30+
run: ./script/find-orphaned-assets.mjs --verbose --exit

script/find-orphaned-assets.mjs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const EXCEPTIONS = new Set([
2020
])
2121

2222
program
23-
.description('Print all images that are in ./assets/ but not found in any markdown')
23+
.description('Print all images that are in ./assets/ but not found in any source files')
2424
.option('-e, --exit', 'Exit script by count of orphans (useful for CI)')
2525
.option('-v, --verbose', 'Verbose outputs')
2626
.option('--json', 'Output in JSON format')
@@ -83,8 +83,8 @@ async function main(opts) {
8383

8484
verbose && console.log(`${allImages.size.toLocaleString()} images found in total.`)
8585

86-
for (const markdownFile of sourceFiles) {
87-
const content = fs.readFileSync(markdownFile, 'utf-8')
86+
for (const sourceFile of sourceFiles) {
87+
const content = fs.readFileSync(sourceFile, 'utf-8')
8888
for (const imagePath of allImages) {
8989
const needle = imagePath.split(path.sep).slice(-2).join('/')
9090
if (content.includes(needle) || EXCEPTIONS.has(imagePath)) {
@@ -94,7 +94,7 @@ async function main(opts) {
9494
}
9595

9696
if (verbose && allImages.size) {
97-
console.log('The following files are not mentioned anywhere in any Markdown file')
97+
console.log('The following files are not mentioned anywhere in any source file')
9898
}
9999
if (json) {
100100
console.log(JSON.stringify([...allImages], undefined, 2))

0 commit comments

Comments
 (0)