diff --git a/README.md b/README.md index e1ed145..e443e91 100644 --- a/README.md +++ b/README.md @@ -53,6 +53,7 @@ Command-line flags: | ----------------- | ------------------------------------------------------------------- | ---------- | | `--continue` | Report messages but do not throw an error if validation failed. | N/A | | `--delay` | Debounce pause in milliseconds between each file validation. | **number** | +| `--dry-run` | Bypass validation (for usage while building your CI). | N/A | | `--exclude` | Comma separated list of strings to match in paths to skip. | **string** | | `--ignore` | Skip validation messages containing a string or matching a regex. | **string** | | `--ignore-config` | File containing strings and regexes of validation messages to skip. | **string** | @@ -136,12 +137,12 @@ $ node examples.js | Name (key) | Type | Default | Description | | :--------------- | :---------------------- | :------------------------------- | :------------------------------------------------------ | | `checkUrl` | **string** | `'https://validator.w3.org/nu/'` | W3C validation API endpoint. | +| `dryRun` | **boolean** | `false` | Bypass validation (for usage while building your CI). | | `filename` | **string** | `null` | HTML file to validate. | | `html` | **string** | `null` | HTML string to validate. | | `ignoreLevel` | `'info'` or `'warning'` | `null` | Skip unwanted messages.* | | `ignoreMessages` | **array** | `[]` | Skip messages containing a string or matching a regex.* | | `output` | `'json'` or `'html'` | `'json'` | Get results as an array or as a web page. | -| `skip` | **boolean** | `false` | bypass validation (for usage while building your CI). | | `website` | **string** | `null` | URL of website to validate. | *The `ignoreMessages` and `ignoreLevel` options only work for `'json'` output.  @@ -172,7 +173,7 @@ type ValidatorResults = { status: number, messages: ValidatorResultsMessage[] | null, //for 'json' output display: string | null, //for 'html' output - skip: boolean, + dryRun: boolean, }; ``` diff --git a/bin/cli.js b/bin/cli.js index 1a7b898..c0e9042 100755 --- a/bin/cli.js +++ b/bin/cli.js @@ -29,14 +29,14 @@ import fs from 'fs'; import slash from 'slash'; // Parameters and flags -const validFlags = ['continue', 'delay', 'exclude', 'ignore', 'ignore-config', 'note', 'quiet', 'trim']; +const validFlags = ['continue', 'delay', 'dry-run', 'exclude', 'ignore', 'ignore-config', 'note', 'quiet', 'trim']; const cli = cliArgvUtil.parse(validFlags); const files = cli.params; const ignore = cli.flagMap.ignore ?? null; const ignoreConfig = cli.flagMap.ignoreConfig ?? null; const delay = Number(cli.flagMap.delay) || 500; //default half second debounce pause const trim = Number(cli.flagMap.trim) || null; -const skip = process.env.w3cHtmlValidator === 'skip'; //bash: export w3cHtmlValidator=skip +const dryRunMode = cli.flagOn.dryRun || process.env.w3cHtmlValidator === 'dry-run'; //bash: export w3cHtmlValidator=dry-run // Validator const globOptions = { ignore: '**/node_modules/**/*' }; @@ -55,8 +55,8 @@ const error = null; if (error) throw Error('[w3c-html-validator] ' + error); -if (skip) - w3cHtmlValidator.skipNotice(); +if (dryRunMode) + w3cHtmlValidator.dryRunNotice(); if (filenames.length > 1 && !cli.flagOn.quiet) w3cHtmlValidator.summary(filenames.length); const reporterOptions = { @@ -74,7 +74,7 @@ const getIgnoreMessages = () => { const isRegex = /^\/.*\/$/; //starts and ends with a slash indicating it's a regex return rawLines.map(line => isRegex.test(line) ? new RegExp(line.slice(1, -1)) : line); }; -const baseOptions = { ignoreMessages: getIgnoreMessages(), skip: skip }; +const baseOptions = { ignoreMessages: getIgnoreMessages(), dryRun: dryRunMode }; const options = (filename) => ({ filename: filename, ...baseOptions }); const handleResults = (results) => w3cHtmlValidator.reporter(results, reporterOptions); const getReport = (filename) => w3cHtmlValidator.validate(options(filename)).then(handleResults); diff --git a/spec/mocha.spec.js b/spec/mocha.spec.js index 8cd388f..5295f5f 100644 --- a/spec/mocha.spec.js +++ b/spec/mocha.spec.js @@ -49,15 +49,15 @@ describe('Library module', () => { assertDeepStrictEqual(actual, expected); }); - it('has functions named validate(), skipNotice() summary(), and reporter()', () => { + it('has functions named validate(), dryRunNotice() summary(), and reporter()', () => { const module = w3cHtmlValidator; const actual = Object.keys(module).sort().map(key => [key, typeof module[key]]); const expected = [ - ['reporter', 'function'], - ['skipNotice', 'function'], - ['summary', 'function'], - ['validate', 'function'], - ['version', 'string'], + ['dryRunNotice', 'function'], + ['reporter', 'function'], + ['summary', 'function'], + ['validate', 'function'], + ['version', 'string'], ]; assertDeepStrictEqual(actual, expected); }); @@ -81,7 +81,7 @@ describe('Pretty-Print JSON website', () => { status: 200, messages: [], display: null, - skip: false, + dryRun: false, }; assertDeepStrictEqual(actual, expected, done); }; @@ -107,7 +107,7 @@ describe('Valid HTML string', () => { status: 200, messages: [], display: null, - skip: false, + dryRun: false, }; assertDeepStrictEqual(actual, expected, done); }; @@ -128,7 +128,7 @@ describe('Valid HTML string', () => { output: 'html', status: 200, messages: null, - skip: false, + dryRun: false, }; assertDeepStrictEqual(actual, expected, done); }; @@ -180,7 +180,7 @@ describe('Invalid HTML string', () => { }, ], display: null, - skip: false, + dryRun: false, }; assertDeepStrictEqual(actual, expected, done); }; @@ -201,7 +201,7 @@ describe('Invalid HTML string', () => { output: 'html', status: 200, messages: null, - skip: false, + dryRun: false, }; assertDeepStrictEqual(actual, expected, done); }; @@ -227,7 +227,7 @@ describe('HTML file', () => { status: 200, messages: [], display: null, - skip: false, + dryRun: false, }; assertDeepStrictEqual(actual, expected, done); }; @@ -358,7 +358,7 @@ describe('Network request failure', () => { message: '503 Service Unavailable https://centerkey.com/rest/status/503/?out=json', }], display: null, - skip: false, + dryRun: false, }; assertDeepStrictEqual(actual, expected, done); }; @@ -389,7 +389,7 @@ describe('The reporter() function', () => { status: 200, messages: [], display: null, - skip: false, + dryRun: false, }; assertDeepStrictEqual(actual, expected, done); }; diff --git a/w3c-html-validator.ts b/w3c-html-validator.ts index 3ca7b68..64aec0e 100644 --- a/w3c-html-validator.ts +++ b/w3c-html-validator.ts @@ -16,7 +16,7 @@ export type ValidatorSettings = { ignoreLevel: 'info' | 'warning', //skip unwanted validation messages ('warning' also skips 'info') ignoreMessages: (string | RegExp)[], //patterns to skip unwanted validation messages output: ValidatorResultsOutput, //'json' or 'html' - skip: boolean, //bypass validation (for usage while building your CI) + dryRun: boolean, //bypass validation (for usage while building your CI) }; export type ValidatorResultsMessage = { // type subType @@ -48,7 +48,7 @@ export type ValidatorResults = { status: number, messages: ValidatorResultsMessage[] | null, display: string | null, - skip: boolean, + dryRun: boolean, }; export type ValidatorResultsOutput = ValidatorResults['output']; export type ReporterSettings = { @@ -66,10 +66,10 @@ const w3cHtmlValidator = { validate(options: Partial): Promise { const defaults = { checkUrl: 'https://validator.w3.org/nu/', + dryRun: false, ignoreLevel: null, ignoreMessages: [], output: 'json', - skip: false, }; const settings = { ...defaults, ...options }; if (!settings.html && !settings.filename && !settings.website) @@ -122,7 +122,7 @@ const w3cHtmlValidator = { status: response.statusCode || -1, messages: json ? response.body.messages : null, display: json ? null : response.text, - skip: settings.skip, + dryRun: settings.dryRun, }); type ReasonResponse = { request: { url: string }, res: { statusMessage: string }}; type ReasonError = Error & { errno: number, response: request.Response & ReasonResponse }; @@ -136,17 +136,17 @@ const w3cHtmlValidator = { const pseudoResponse = { statusCode: 200, body: { messages: [] }, - text: 'Validation skipped.', + text: 'Validation bypassed.', }; const pseudoRequest = (): Promise => new Promise(resolve => resolve(pseudoResponse)); - const validation = settings.skip ? pseudoRequest() : w3cRequest; + const validation = settings.dryRun ? pseudoRequest() : w3cRequest; return validation.then(filterMessages).then(toValidatorResults).catch(handleError); }, - skipNotice() { + dryRunNotice() { log(chalk.gray('w3c-html-validator'), - chalk.yellowBright('skip mode:'), chalk.whiteBright('validation being bypassed')); + chalk.yellowBright('dry run mode:'), chalk.whiteBright('validation being bypassed')); }, summary(numFiles: number) {