Skip to content

Commit

Permalink
chore: add filename checker to enforce use kebab-case (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
SigureMo committed Jul 10, 2024
1 parent c7a291d commit 25ccb33
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
4 changes: 4 additions & 0 deletions .github/workflows/lint-and-fmt.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,7 @@ jobs:
- name: Format check
run: |
pnpm fmt:check
- name: Check filename
run: |
pnpm lint:filename
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"serve": "vitepress serve src",
"fmt": "lint-md src/**/*.md --fix && prettier --write .",
"fmt:check": "lint-md src/**/*.md && prettier --check .",
"img:compress": "tsx scripts/compress-image.mts"
"img:compress": "tsx scripts/compress-image.mts",
"lint:filename": "tsx scripts/check-filename.mts"
},
"dependencies": {
"@tailwindcss/typography": "^0.5.11",
Expand Down
38 changes: 38 additions & 0 deletions scripts/check-filename.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { resolve } from 'node:path'
import { readdirSync } from 'node:fs'

const POST_DIR = resolve('src/posts')
const IN_GITHUB_ACTIONS = process.env.GITHUB_ACTIONS === 'true'

function isKebabCase(filename: string): boolean {
return /^[a-z0-9-]+$/.test(filename)
}

function suggestKebabCase(filename: string): string {
return filename.toLowerCase().replace('_', '-').toLowerCase()
}

function getAllPostFilenames(): string[] {
return readdirSync(POST_DIR).map((filename) => filename.replace(/\.md$/, ''))
}

function main() {
const postFilenames = getAllPostFilenames()
let hasError = false
for (const filename of postFilenames) {
const filepath = resolve(POST_DIR, `${filename}.md`)
if (!isKebabCase(filename)) {
const errorMesssage = `Filename "${filename}" is not in kebab-case, suggest: "${suggestKebabCase(filename)}"`
if (IN_GITHUB_ACTIONS) {
console.log(`::error file=${filepath}::${errorMesssage}`)
}
console.error(errorMesssage)
hasError = true
}
}
if (hasError) {
process.exit(1)
}
}

main()

0 comments on commit 25ccb33

Please sign in to comment.