-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: harupy <17039389+harupy@users.noreply.github.com>
- Loading branch information
Showing
3 changed files
with
58 additions
and
0 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
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,55 @@ | ||
import fs from "fs"; | ||
import path from "path"; | ||
import yaml from "js-yaml"; | ||
|
||
function fileExistsWithCaseSync(filepath: string): boolean { | ||
const dir = path.dirname(filepath); | ||
if (dir === "/" || dir === ".") { | ||
return true; | ||
} | ||
const filenames = fs.readdirSync(dir); | ||
if (filenames.indexOf(path.basename(filepath)) === -1) { | ||
return false; | ||
} | ||
return fileExistsWithCaseSync(dir); | ||
} | ||
|
||
type Author = { | ||
name: string; | ||
title: string; | ||
url: string; | ||
image_url?: string; | ||
}; | ||
|
||
function readAuthors(): Record<string, Author> { | ||
const authorsFile = fs.readFileSync( | ||
path.join(process.cwd(), "blog", "authors.yml"), | ||
"utf-8", | ||
); | ||
return yaml.load(authorsFile) as Record<string, Author>; | ||
} | ||
|
||
function main(): void { | ||
const authors = readAuthors(); | ||
const authorsWithInvalidImageUrl: string[] = []; | ||
Object.entries(authors).forEach(([author, authorData]) => { | ||
if ( | ||
authorData.image_url && | ||
!/^https?:\/\//.test(authorData.image_url) && | ||
!fileExistsWithCaseSync( | ||
path.join(process.cwd(), "static", authorData.image_url), | ||
) | ||
) { | ||
authorsWithInvalidImageUrl.push(author); | ||
} | ||
}); | ||
|
||
if (authorsWithInvalidImageUrl.length > 0) { | ||
console.log("Found authors with invalid image URLs:"); | ||
console.log(authorsWithInvalidImageUrl); | ||
console.log("Please make sure the image exists in the static folder."); | ||
process.exit(1); | ||
} | ||
} | ||
|
||
main(); |