Skip to content

Commit

Permalink
apply review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
OnkarRuikar committed Jun 17, 2024
1 parent 007f749 commit a483511
Showing 1 changed file with 27 additions and 15 deletions.
42 changes: 27 additions & 15 deletions filecheck/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import imageminMozjpeg from "imagemin-mozjpeg";
import imageminGifsicle from "imagemin-gifsicle";
import imageminSvgo from "imagemin-svgo";
import isSvg from "is-svg";
import sanitizeFilename from "sanitize-filename";

import { MAX_FILE_SIZE } from "../libs/env/index.js";
import {
Expand Down Expand Up @@ -283,7 +284,7 @@ function isRelevantFile(filePath: string) {
}

function isBinaryFile(filePath: string) {
return !/\.(html|json|md|txt|yml)$/i.test(filePath);
return !/\.(html|json|jsonc|md|txt|yml|drawio)$/i.test(filePath);
}

async function resolveDirectory(file: string): Promise<string[]> {
Expand All @@ -310,41 +311,52 @@ function validatePath(filePath: string): string[] {
path.basename(filePath)
);

// Must have supported extension
const supportedFilesRegExp = /\.(md|json|txt|yml|jpg|jpeg|png|gif|svg)$/i;
if (!supportedFilesRegExp.test(filePath)) {
// TODO: Read allowed extensions from `mdn/content` repo and check here.

// All characters must be lower case.
if (shortPath !== shortPath.toLowerCase() && !shortPath.endsWith(".json")) {
errors.push(
`Error: Invalid file: ${shortPath}. The file extension is not supported.`
`Error: Invalid path: ${filePath}. All characters must be lowercase.`
);
}

// All characters must be lower case.
if (shortPath !== shortPath.toLowerCase() && !filePath.endsWith(".json")) {
// Whitespaces are not allowed.
if (shortPath.includes(" ")) {
errors.push(
`Error: Invalid path: ${shortPath}. All characters must be lowercase.`
`Error: Invalid path: ${filePath}. File path must not include whitespaces.`
);
}

// Whitespaces are not allowed.
if (filePath.includes(" ")) {
if (shortPath.includes("\u200b")) {
errors.push(
`Error: Invalid path: ${shortPath}. File path must not include whitespaces.`
`Error: Invalid path: ${filePath}. File path must not include zero width whitespaces.`
);
}
if (filePath.includes("\u200b")) {

// Use only ASCII characters
const normalized = shortPath.normalize("NFD");
if (shortPath !== normalized) {
errors.push(
`Error: Invalid path: ${shortPath}. File path must not include zero width whitespaces.`
`Error: Invalid path: ${filePath}. Use only plain ASCII characters.`
);
}

// File path should't contain banned characters: `(`, `)`
const bannedCharsRegExp = /[()]/;
if (bannedCharsRegExp.test(filePath)) {
if (bannedCharsRegExp.test(shortPath)) {
errors.push(
`Error: Invalid path: ${shortPath}. File path must not include characters: '(', ')'`
`Error: Invalid path: ${filePath}. File path must not include characters: '(', ')'`
);
}

const sanitizedPath = shortPath
.split(/\\|\//g)
.map((p) => sanitizeFilename(p))
.join(path.sep);
if (shortPath !== sanitizedPath) {
errors.push(`Error: Invalid path ${shortPath}. Change to ${sanitizedPath}`);
}

return errors;
}

Expand Down

0 comments on commit a483511

Please sign in to comment.