Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions eleventy.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import fs from 'node:fs/promises'
import path from 'node:path'

import { nhsukEleventyPlugin } from '@x-govuk/nhsuk-eleventy-plugin'

import { findFileWithoutExtension } from './lib/find-file-without-extension.js'

const serviceName = 'Digital prevention services design history'

export default function (eleventyConfig) {
Expand Down Expand Up @@ -174,6 +177,17 @@ export default function (eleventyConfig) {

// Reset contents of output directory before each build
eleventyConfig.on('eleventy.before', async ({ directories, runMode }) => {
// Check for files without extensions in the app directory
const fileWithoutExtension = await findFileWithoutExtension(
directories.input
)

if (fileWithoutExtension) {
throw new Error(
`Found file called '${path.basename(fileWithoutExtension)}' without an extension. Did you forget to add .md to it?\nFile path: ${fileWithoutExtension}`
)
}

if (runMode === 'build') {
await fs.rm(directories.output, {
force: true,
Expand Down
38 changes: 38 additions & 0 deletions lib/find-file-without-extension.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import fs from 'node:fs/promises'
import path from 'node:path'

/**
* Recursively find the first file without an extension in a directory
*
* @param {string} dir - Directory to search
* @returns {Promise<string|null>} - Path of first file without extension, or null
*/
export async function findFileWithoutExtension(dir) {
async function scan(currentDir) {
const entries = await fs.readdir(currentDir, { withFileTypes: true })

for (const entry of entries) {
const fullPath = path.join(currentDir, entry.name)

if (entry.isDirectory()) {
// Skip directories starting with underscore (e.g. _layouts, _components)
if (!entry.name.startsWith('_')) {
const found = await scan(fullPath)
if (found) return found
}
} else if (entry.isFile()) {
// Skip hidden files (e.g. .gitkeep)
if (!entry.name.startsWith('.')) {
// Check if the file has no extension
const ext = path.extname(entry.name)
if (ext === '') {
return fullPath
}
}
}
}
return null
}

return scan(dir)
}
Loading