Skip to content

Commit cbb9597

Browse files
committed
Validate that checkPaths exist
1 parent bdbd214 commit cbb9597

File tree

3 files changed

+12
-5
lines changed

3 files changed

+12
-5
lines changed

plugin/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ module.exports = {
1515
}) {
1616
const htmlFilePaths = await pluginCore.generateFilePaths({
1717
fileAndDirPaths: checkPaths,
18-
PUBLISH_DIR
18+
PUBLISH_DIR,
19+
build
1920
});
2021
if (debugMode) {
2122
console.log({ htmlFilePaths });

plugin/pluginCore.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const { extname } = require('path')
22

33
const pa11y = require('pa11y');
44
const readdirp = require('readdirp')
5-
const { isDirectory } = require('path-type')
5+
const { isDirectory, isFile } = require('path-type')
66

77
exports.runPa11y = async function({ htmlFilePaths, testMode, debugMode }) {
88
let results = await Promise.all(htmlFilePaths.map(pa11y));
@@ -28,21 +28,26 @@ exports.runPa11y = async function({ htmlFilePaths, testMode, debugMode }) {
2828
exports.generateFilePaths = async function({
2929
fileAndDirPaths, // array, mix of html and directories
3030
PUBLISH_DIR,
31+
build,
3132
testMode,
3233
debugMode
3334
}) {
3435
const htmlFilePaths = await Promise.all(
35-
fileAndDirPaths.map(fileAndDirPath => findHtmlFiles(`${PUBLISH_DIR}/${fileAndDirPath}`))
36+
fileAndDirPaths.map(fileAndDirPath => findHtmlFiles(`${PUBLISH_DIR}/${fileAndDirPath}`, build))
3637
)
3738
return [].concat(...htmlFilePaths)
3839
};
3940

40-
const findHtmlFiles = async function(fileAndDirPath) {
41+
const findHtmlFiles = async function(fileAndDirPath, build) {
4142
if (await isDirectory(fileAndDirPath)) {
4243
const fileInfos = await readdirp.promise(fileAndDirPath, { fileFilter: '*.html' })
4344
return fileInfos.map(({ fullPath }) => fullPath)
4445
}
4546

47+
if (!(await isFile(fileAndDirPath))) {
48+
build.failBuild(`Input "checkPaths" does not exist: ${fileAndDirPath}`)
49+
}
50+
4651
if (extname(fileAndDirPath) !== '.html') {
4752
return []
4853
}

tests/generateFilePaths/this.test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ const pluginCore = require('../../plugin/pluginCore.js');
77
test('generateFilePaths works', async () => {
88
const results = await pluginCore.generateFilePaths({
99
fileAndDirPaths: ['/blog', '/about.html'],
10-
PUBLISH_DIR
10+
PUBLISH_DIR,
11+
build: { failBuild() {} }
1112
});
1213
expect(results).toMatchSnapshot();
1314
});

0 commit comments

Comments
 (0)