Skip to content

Commit

Permalink
Support glob on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
dpilafian committed Nov 10, 2023
1 parent 351dec2 commit a762a97
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 12 deletions.
5 changes: 3 additions & 2 deletions bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { cliArgvUtil } from 'cli-argv-util';
import { globSync } from 'glob';
import { w3cHtmlValidator } from '../dist/w3c-html-validator.js';
import fs from 'fs';
import slash from 'slash';

// Parameters and flags
const validFlags = ['continue', 'delay', 'exclude', 'ignore', 'ignore-config', 'note', 'quiet', 'trim'];
Expand All @@ -39,9 +40,9 @@ const trim = Number(cli.flagMap.trim) || null;
// Validator
const globOptions = { ignore: '**/node_modules/**/*' };
const keep = (filename) => !filename.includes('node_modules/');
const readFolder = (folder) => globSync(folder + '**/*.html', globOptions);
const readFolder = (folder) => globSync(slash(folder + '**/*.html'), globOptions);
const getAllPaths = () => files.map(file => globSync(slash(file), globOptions)).flat();
const expandFolder = (file) => fs.lstatSync(file).isDirectory() ? readFolder(file + '/') : file;
const getAllPaths = () => files.map(file => globSync(file, globOptions)).flat();
const getFilenames = () => getAllPaths().map(expandFolder).flat().filter(keep).sort();
const list = files.length ? getFilenames() : readFolder('');
const excludes = cli.flagMap.exclude?.split(',') ?? [];
Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,16 @@
"cli-argv-util": "~1.2",
"fancy-log": "~2.0",
"glob": "~10.3",
"slash": "~5.1",
"superagent": "~8.1"
},
"devDependencies": {
"@types/fancy-log": "~2.0",
"@types/glob": "~8.1",
"@types/node": "~20.8",
"@types/node": "~20.9",
"@types/superagent": "~4.1",
"@typescript-eslint/eslint-plugin": "~6.9",
"@typescript-eslint/parser": "~6.9",
"@typescript-eslint/eslint-plugin": "~6.10",
"@typescript-eslint/parser": "~6.10",
"add-dist-header": "~1.3",
"assert-deep-strict-equal": "~1.1",
"copy-file-util": "~1.1",
Expand Down
5 changes: 2 additions & 3 deletions spec/mocha.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -412,9 +412,8 @@ describe('Executing the CLI', () => {
});

it('with a glob selects the correct files to validate', () => {
const globbable = process.platform !== 'win32';
const actual = globbable ? run('html-validator "spec/**/valid.html" --note=glob') : null;
const expected = null;
const actual = run('html-validator "spec/**/valid.html" --note=glob');
const expected = null;
assertDeepStrictEqual(actual, expected);
});

Expand Down
10 changes: 6 additions & 4 deletions w3c-html-validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import chalk from 'chalk';
import fs from 'fs';
import log from 'fancy-log';
import request from 'superagent';
import slash from 'slash';

// Type Declarations
export type ValidatorSettings = {
Expand Down Expand Up @@ -74,9 +75,10 @@ const w3cHtmlValidator = {
throw Error('[w3c-html-validator] Invalid ignoreLevel option: ' + settings.ignoreLevel);
if (settings.output !== 'json' && settings.output !== 'html')
throw Error('[w3c-html-validator] Option "output" must be "json" or "html".');
const mode = settings.html ? 'html' : settings.filename ? 'filename' : 'website';
const filename = settings.filename ? slash(settings.filename) : null;
const mode = settings.html ? 'html' : filename ? 'filename' : 'website';
const readFile = (filename: string) => fs.readFileSync(filename, 'utf-8').replace(/\r/g, '');
const inputHtml = settings.html ?? (settings.filename ? readFile(settings.filename) : null);
const inputHtml = settings.html ?? (filename ? readFile(filename) : null);
const makePostRequest = () => request.post(settings.checkUrl)
.set('Content-Type', 'text/html; encoding=utf-8')
.send(<string>inputHtml);
Expand All @@ -89,7 +91,7 @@ const w3cHtmlValidator = {
const success = '<p class="success">';
const titleLookup = {
html: 'HTML String (characters: ' + inputHtml?.length + ')',
filename: settings.filename,
filename: filename,
website: settings.website,
};
const filterMessages = (response: request.Response): request.Response => {
Expand All @@ -111,7 +113,7 @@ const w3cHtmlValidator = {
mode: mode,
title: <string>titleLookup[mode],
html: inputHtml,
filename: settings.filename || null,
filename: filename,
website: settings.website || null,
output: <ValidatorResultsOutput>settings.output,
status: response.statusCode || -1,
Expand Down

0 comments on commit a762a97

Please sign in to comment.