Skip to content
Draft
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
39 changes: 0 additions & 39 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@
"env-cmd": "11.0.0",
"fdir": "^6.5.0",
"file-type": "^22.0.1",
"fs-extra": "^11.3.6",
"gray-matter": "^4.0.3",
"imagemin": "^9.0.1",
"imagemin-gifsicle": "^7.0.0",
Expand Down
34 changes: 19 additions & 15 deletions scripts/filecheck/checker.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import os from "node:os";
import { eachLimit } from "async";
import cliProgress from "cli-progress";
import { fdir } from "fdir";
import fse from "fs-extra";
import { temporaryDirectory } from "tempy";
import * as cheerio from "cheerio";
import { fileTypeFromFile } from "file-type";
Expand Down Expand Up @@ -83,6 +82,19 @@ function getRelativePath(filePath) {
return path.relative(process.cwd(), filePath);
}

/**
* @param {string} filePath
* @returns {Promise<boolean>}
*/
async function pathExists(filePath) {
try {
await fs.access(filePath);
return true;
} catch {
return false;
}
}

/**
* Check a single file for naming, type, reference, and compression rules.
* @param {string} filePath
Expand Down Expand Up @@ -185,16 +197,10 @@ export async function checkFile(filePath, options = {}) {
}
}

// The image has to be mentioned in the adjacent index.html document
// The image has to be mentioned in the adjacent index.md document
const parentPath = path.dirname(filePath);
const htmlFilePath = path.join(parentPath, "index.html");
const mdFilePath = path.join(parentPath, "index.md");
const docFilePath = (await fse.exists(htmlFilePath))
? htmlFilePath
: (await fse.exists(mdFilePath))
? mdFilePath
: null;
if (!docFilePath) {
const docFilePath = path.join(parentPath, "index.md");
if (!(await pathExists(docFilePath))) {
throw new FixableError(
`${getRelativePath(
filePath,
Expand All @@ -204,9 +210,7 @@ export async function checkFile(filePath, options = {}) {
}

// The image must be mentioned (as a string) in the content
const rawContent = docFilePath
? await fs.readFile(docFilePath, "utf-8")
: null;
const rawContent = await fs.readFile(docFilePath, "utf-8");
if (!rawContent.includes(path.basename(filePath))) {
throw new FixableError(
`${getRelativePath(
Expand Down Expand Up @@ -297,7 +301,7 @@ async function checkCompression(filePath, options) {
0,
)}% smaller.`,
);
fse.copyFileSync(compressed.destinationPath, filePath);
await fs.copyFile(compressed.destinationPath, filePath);
} else {
throw new FixableError(
`${filePath} is ${formatSize(
Expand All @@ -310,7 +314,7 @@ async function checkCompression(filePath, options) {
}
}
} finally {
fse.removeSync(tempdir);
await fs.rm(tempdir, { recursive: true, force: true });
}
}

Expand Down
15 changes: 4 additions & 11 deletions scripts/filecheck/checker.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ import { describe, it } from "node:test";

import { checkFile } from "./checker.js";

const HTML_FIXTURES = new URL("fixtures/html/", import.meta.url);
const MD_FIXTURES = new URL("fixtures/md/", import.meta.url);

describe("checking files", () => {
it("should spot SVGs with scripts inside them", async () => {
const filePath = fileURLToPath(new URL("./script.svg", HTML_FIXTURES));
const filePath = fileURLToPath(new URL("./script.svg", MD_FIXTURES));
console.assert(fs.existsSync(filePath), `${filePath} does not exist`);
await assert.rejects(
() => checkFile(filePath),
Expand All @@ -19,7 +18,7 @@ describe("checking files", () => {
});

it("should spot SVGs with onLoad inside an element", async () => {
const filePath = fileURLToPath(new URL("./onhandler.svg", HTML_FIXTURES));
const filePath = fileURLToPath(new URL("./onhandler.svg", MD_FIXTURES));
console.assert(fs.existsSync(filePath), `${filePath} does not exist`);
await assert.rejects(
() => checkFile(filePath),
Expand All @@ -28,25 +27,19 @@ describe("checking files", () => {
});

it("should spot files that are not mentioned in source", async () => {
const filePath = fileURLToPath(new URL("./orphan.png", HTML_FIXTURES));
console.assert(fs.existsSync(filePath), `${filePath} does not exist`);
await assert.rejects(() => checkFile(filePath), /is not mentioned in/);
});

it("should spot files that are not mentioned in md source", async () => {
const filePath = fileURLToPath(new URL("./orphan.png", MD_FIXTURES));
console.assert(fs.existsSync(filePath), `${filePath} does not exist`);
await assert.rejects(() => checkFile(filePath), /is not mentioned in/);
});

it("should spot files that are completely empty", async () => {
const filePath = fileURLToPath(new URL("./zero.gif", HTML_FIXTURES));
const filePath = fileURLToPath(new URL("./zero.gif", MD_FIXTURES));
console.assert(fs.existsSync(filePath), `${filePath} does not exist`);
await assert.rejects(() => checkFile(filePath), /is 0 bytes/);
});

it("should spot mismatch between file-type and file extension", async () => {
const filePath = fileURLToPath(new URL("./png.jpeg", HTML_FIXTURES));
const filePath = fileURLToPath(new URL("./png.jpeg", MD_FIXTURES));
console.assert(fs.existsSync(filePath), `${filePath} does not exist`);
await assert.rejects(
() => checkFile(filePath),
Expand Down
4 changes: 0 additions & 4 deletions scripts/filecheck/fixtures/html/index.html

This file was deleted.

Binary file removed scripts/filecheck/fixtures/html/orphan.png
Binary file not shown.