Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
zrrrzzt committed Sep 26, 2015
1 parent a8d306a commit 3a49044
Show file tree
Hide file tree
Showing 11 changed files with 280 additions and 1 deletion.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# IDE
.idea

# OS X
.DS_Store

# Logs
logs
*.log
Expand All @@ -12,6 +18,7 @@ lib-cov

# Coverage directory used by tools like istanbul
coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt
Expand Down
6 changes: 6 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
language: node_js
node_js:
- "0.12"
- "iojs"
after_success:
- npm run coveralls
49 changes: 48 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,49 @@
[![Build Status](https://travis-ci.org/zrrrzzt/html-validator-cli.svg?branch=master)](https://travis-ci.org/zrrrzzt/html-validator-cli)
[![Coverage Status](https://coveralls.io/repos/zrrrzzt/html-validator-cli/badge.svg?branch=master&service=github)](https://coveralls.io/github/zrrrzzt/html-validator-cli?branch=master)
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://github.com/feross/standard)
# html-validator-cli
CLI for validating html using validator.w3.org/nu
CLI for validating html using [validator.w3.org/nu](http://validator.w3.org/nu/)

## Installation

```sh
$ npm install html-validator-cli -g
```

## Usage

Pass in <url> or --file and optional --format or --validator.

### Examples

With url

```sh
$ html-validator <url>
```

With file

```sh
$ html-validator --file=path-to-file
```

Optional pass in format for returned data.

Valid options: json, html, xhtml, xml, gnu and text (default).

```sh
$ html-validator <url> --format=gnu
```

Optional pass in another validator.

It needs to expose the same REST interface.

```sh
$ html-validator <url> --validator='http://html5.validator.nu'
```

## Related

- [html-validator](https://github.com/zrrrzzt/html-validator) API for this module
44 changes: 44 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/usr/bin/env node
'use strict'

var fs = require('fs')
var validator = require('html-validator')
var getHelpText = require('./lib/getHelpText')
var pkg = require('./package.json')
var query = process.argv[2]
var argv = require('minimist')((process.argv.slice(2)))
var opts = {format: 'text'}

if (!query || process.argv.indexOf('-h') !== -1 || process.argv.indexOf('--help') !== -1) {
console.log(getHelpText())
process.exit(0)
}

if (process.argv.indexOf('-v') !== -1 || process.argv.indexOf('--version') !== -1) {
console.log(pkg.version)
process.exit(0)
}

if (query.indexOf('http') !== -1) {
opts.url = argv._[0]
}

if (argv.format) {
opts.format = argv.format
}

if (argv.url) {
opts.url = argv.url
}

if (argv.file) {
opts.data = fs.readFileSync(argv.file)
}

validator(opts, function (err, data) {
if (err) {
throw err
} else {
console.log(opts.format === 'json' ? JSON.stringify(data) : data)
}
})
8 changes: 8 additions & 0 deletions lib/getHelpText.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict'

function getHelptext () {
var help = require('./helptext.json')
return help.join('\n')
}

module.exports = getHelptext
13 changes: 13 additions & 0 deletions lib/helptext.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[
"Usage: ",
"",
"$ html-validator <url>",
"",
"or",
"$ html-validator --file=<file>",
"",
"Optional, specify format of returned data",
"Valid options: json, html, xhtml, xml, gnu and text (default)",
"",
"$ html-validator <url> --format=<format>"
]
45 changes: 45 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"name": "html-validator-cli",
"version": "1.0.0",
"description": "CLI for validating html using validator.w3.org/nu",
"license": "MIT",
"author": {
"name": "Geir Gåsodden",
"email": "geir.gasodden@pythonia.no",
"url": "https://github.com/zrrrzzt"
},
"main": "index.js",
"bin": {
"html-validator": "index.js"
},
"engines": {
"node": ">=0.12.0"
},
"scripts": {
"setup": "npm install && node setup.js",
"test": "standard && tap --reporter=spec test/*.js",
"coverage": "tap test/*.js --coverage",
"coveralls": "tap --cov --coverage-report=lcov test/*.js && cat coverage/lcov.info | coveralls"
},
"keywords": [
"html-validator",
"validation",
"cli"
],
"repository": {
"type": "git",
"url": "http://github.com/zrrrzzt/html-validator-cli.git"
},
"bugs": {
"url": "http://github.com/zrrrzzt/html-validator-cli/issues"
},
"dependencies": {
"minimist": "^1.2.0",
"html-validator": "^0.0.8"
},
"devDependencies": {
"coveralls": "^2.11.4",
"standard": "^5.3.1",
"tap": "^1.4.1"
}
}
3 changes: 3 additions & 0 deletions setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict'

console.log('Everything\'s shiny, Cap\'n. Not to fret.')
89 changes: 89 additions & 0 deletions test/cli-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
'use strict'

var exec = require('child_process').execFile
var tap = require('tap')
var getHelpText = require('../lib/getHelpText')
var pkgVersion = require('../package.json').version

tap.test('It returns helptext with -h flag', function helpTextWithH (test) {
exec('./index.js', ['-h'], function helpTextWithH (error, stdout, stderr) {
if (error) {
console.error(stderr.toString())
throw error
} else {
test.equal(stdout.toString().trim(), getHelpText().toString().trim())
test.end()
}
})
})

tap.test('It returns helptext with --help flag', function helpTextWithH (test) {
exec('./index.js', ['--help'], function helpTextWithH (error, stdout, stderr) {
if (error) {
console.error(stderr.toString())
throw error
} else {
test.equal(stdout.toString().trim(), getHelpText().toString().trim())
test.end()
}
})
})

tap.test('It returns version with -v flag', function versionWithV (test) {
exec('./index.js', ['-v'], function versionWithV (error, stdout, stderr) {
if (error) {
console.error(stderr.toString())
throw error
} else {
test.equal(stdout.toString().trim(), pkgVersion)
test.end()
}
})
})

tap.test('It returns error on error', function testError (test) {
exec('./index.js', ['npmlovesyou-do-you-love-npm'], function versionWithV (error, stdout, stderr) {
test.ok(error, 'Error OK')
test.end()
})
})

tap.test('It returns data if url supplied', function testError (test) {
exec('./index.js', ['https://www.github.com'], function versionWithV (error, stdout, stderr) {
if (error) {
throw error
}
test.ok(stdout.toString().trim(), 'Data OK')
test.end()
})
})

tap.test('It returns data if file supplied', function testError (test) {
exec('./index.js', ['--file=test/data/invalid.html'], function versionWithV (error, stdout, stderr) {
if (error) {
throw error
}
test.ok(stdout.toString().trim(), 'Data OK')
test.end()
})
})

tap.test('It returns data on supplied supplied format', function testError (test) {
exec('./index.js', ['--file=test/data/invalid.html', '--format=json'], function versionWithV (error, stdout, stderr) {
if (error) {
throw error
}
test.equal(JSON.parse(stdout.toString().trim()).messages.length, 3)
test.end()
})
})

tap.test('You can supply url by flag', function testError (test) {
exec('./index.js', ['--url=https://www.github.com'], function versionWithV (error, stdout, stderr) {
if (error) {
throw error
}
test.ok(stdout.toString().trim(), 'Data OK')
test.end()
})
})
10 changes: 10 additions & 0 deletions test/data/invalid.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>Invalid</title>
</head>
<body>
<p>I'm baaaaaaaaaaaad code</div></p>
</body>
</html>
7 changes: 7 additions & 0 deletions test/getHelpText-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict'

var tap = require('tap')
var getHelpText = require('../lib/getHelpText')
var helpText = require('../lib/helptext.json').join('\n')

tap.equal(helpText, getHelpText(), 'It returns correct helptext')

0 comments on commit 3a49044

Please sign in to comment.