Skip to content

Commit fd39dba

Browse files
author
James Wright
committed
CLI wrapper and entry point restructure to allow for programmatic usage
1 parent 51bd5da commit fd39dba

File tree

3 files changed

+39
-22
lines changed

3 files changed

+39
-22
lines changed

lib/index.js

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,18 @@
1-
#! /usr/bin/env node
2-
31
'use strict';
42

5-
const config = require('./config');
63
const intro = require('./intro');
74
const runner = require('./runner');
85
const localAppServer = require('./localAppServer');
96
const resultsPrinter = require('./resultsPrinter');
107

11-
intro();
12-
13-
const prerun = config.localAppServer
14-
? localAppServer.start(config.localAppServer)
15-
: Promise.resolve();
16-
17-
prerun.then(runner)
18-
.then(results => resultsPrinter.printResults(results))
19-
.then(onValidated).catch(onError);
20-
218
function onValidated(results) {
22-
const failed = hasErrors(results);
9+
const hasFailed = hasErrors(results);
2310

24-
if (!failed) {
11+
if (!hasFailed) {
2512
console.info('Congratulations! Your HTML is valid!');
26-
exit(0);
27-
}
13+
}
2814

29-
exit(config.failHard ? 1 : 0);
15+
return hasFailed;
3016
}
3117

3218
function hasErrors(results) {
@@ -35,14 +21,30 @@ function hasErrors(results) {
3521

3622
function onError(error) {
3723
console.error(error);
38-
exit(1);
24+
exit(true);
3925
}
4026

41-
function exit(exitCode) {
27+
function exit(hasFailed, config) {
28+
const exitCode = hasFailed && config.failHard ? 1 : 0;
29+
4230
if (config.localAppServer) {
4331
localAppServer.stop();
4432
}
4533

4634
resultsPrinter.resetStdout();
4735
process.exit(exitCode);
48-
}
36+
}
37+
38+
module.exports = function valimate(config) {
39+
intro();
40+
41+
const prerun = config.localAppServer
42+
? localAppServer.start(config.localAppServer)
43+
: Promise.resolve();
44+
45+
return prerun.then(runner)
46+
.then(results => resultsPrinter.printResults(results))
47+
.then(onValidated)
48+
.then(hasFailed => exit(hasFailed, config))
49+
.catch(() => onError(true, config));
50+
};

package.json

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,37 @@
33
"version": "2.1.2",
44
"description": "Automated HTML validation for Node.js applications",
55
"repository": "https://github.com/jamesseanwright/valimate",
6+
67
"bin": {
78
"valimate": "lib/index.js"
89
},
10+
11+
"main": "lib/index.js",
12+
913
"scripts": {
10-
"test": "eslint {lib,tests} && mocha && node lib"
14+
"test": "eslint {lib,tests} && mocha && node valimate"
1115
},
16+
1217
"keywords": [
1318
"html",
19+
"html5",
1420
"validate",
1521
"validation",
1622
"w3",
1723
"w3c"
1824
],
25+
1926
"author": "James Wright <james@jamesswright.co.uk>",
2027
"license": "ISC",
28+
2129
"devDependencies": {
2230
"chai": "3.4.1",
2331
"eslint": "3.10.2",
2432
"mocha": "2.3.4",
2533
"sinon": "1.17.2",
2634
"valimate-notifier": "1.0.2"
2735
},
36+
2837
"dependencies": {
2938
"request": "2.74.0"
3039
}

valimate.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/usr/bin/env node
2+
3+
const config = require('./lib/config');
4+
const valimate = require('./lib');
5+
6+
valimate(config);

0 commit comments

Comments
 (0)