1
1
mirror of https://github.com/theoludwig/html-w3c-validator.git synced 2025-05-21 23:21:29 +02:00

refactor: simplify logic

This commit is contained in:
2024-02-02 14:47:31 +01:00
parent 2e0e136355
commit 09731d4749
11 changed files with 140 additions and 719 deletions

View File

@ -1,19 +1,17 @@
import path from "node:path"
import fs from "node:fs"
import path from "node:path"
import { Command, Option } from "clipanion"
import * as typanion from "typanion"
import chalk from "chalk"
import ora from "ora"
import logSymbols from "log-symbols"
import { Command, Option } from "clipanion"
import type {
ValidationMessageLocationObject,
ParsedJsonAsValidationResults,
ValidationMessageLocationObject,
} from "html-validator"
import validateHTML from "html-validator"
import logSymbols from "log-symbols"
import ora from "ora"
import { table } from "table"
import { isExistingPath } from "./utils/isExistingPath.js"
import * as typanion from "typanion"
export const CONFIG_FILE_NAME = ".html-w3c-validatorrc.json"
@ -65,22 +63,20 @@ export class HTMLValidatorCommand extends Command {
public async execute(): Promise<number> {
const configPath = path.join(this.currentWorkingDirectory, CONFIG_FILE_NAME)
try {
if (!(await isExistingPath(configPath))) {
let configData: string
try {
configData = await fs.promises.readFile(configPath, {
encoding: "utf-8",
})
} catch (error) {
throw new Error(
`No config file found at ${configPath}. Please create "${CONFIG_FILE_NAME}".`,
)
}
const configData = await fs.promises.readFile(configPath, {
encoding: "utf-8",
})
let config: Config = { urls: [], files: [] }
let isValidConfig = true
try {
config = JSON.parse(configData)
} catch {
isValidConfig = false
}
if (!isValidConfig) {
throw new Error(
`Invalid config file at "${configPath}". Please check the JSON syntax.`,
)
@ -149,14 +145,16 @@ export class HTMLValidatorCommand extends Command {
})
} else if (type === "file") {
const htmlPath = path.resolve(this.currentWorkingDirectory, data)
if (!(await isExistingPath(htmlPath))) {
let html: string
try {
html = await fs.promises.readFile(htmlPath, {
encoding: "utf-8",
})
} catch (error) {
throw new Error(
`No file found at "${htmlPath}". Please check the path.`,
)
}
const html = await fs.promises.readFile(htmlPath, {
encoding: "utf-8",
})
result = await validateHTML({
data: html,
...options,

View File

@ -1,7 +1,9 @@
import test from "node:test"
import assert from "node:assert/strict"
import path from "node:path"
import fs from "node:fs"
import { PassThrough } from "node:stream"
import { fileURLToPath } from "node:url"
import sinon from "sinon"
import { execa } from "execa"
@ -33,7 +35,10 @@ await test("html-w3c-validator", async (t) => {
async () => {
const exampleURL = new URL("../../example", import.meta.url)
process.chdir(exampleURL.pathname)
await execa("rimraf", ["node_modules"])
await fs.promises.rm(
path.join(fileURLToPath(exampleURL), "node_modules"),
{ recursive: true, force: true },
)
await execa("npm", ["install"])
const { exitCode } = await execa("npm", [
"run",

View File

@ -1,26 +0,0 @@
import test from "node:test"
import assert from "node:assert/strict"
import fsMock from "mock-fs"
import { isExistingPath } from "../isExistingPath.js"
await test("utils/isExistingPath", async (t) => {
t.afterEach(() => {
fsMock.restore()
})
await t.test("should return true if the file exists", async () => {
fsMock({
"/file.txt": "",
})
assert.strictEqual(await isExistingPath("/file.txt"), true)
})
await t.test("should return false if the file doesn't exists", async () => {
fsMock({
"/file.txt": "",
})
assert.strictEqual(await isExistingPath("/randomfile.txt"), false)
})
})

View File

@ -1,10 +0,0 @@
import fs from "node:fs"
export const isExistingPath = async (path: string): Promise<boolean> => {
try {
await fs.promises.access(path, fs.constants.F_OK)
return true
} catch {
return false
}
}