Permalink
Browse files

Extract cli.js from bin/literapi

  • Loading branch information...
1 parent ea0e051 commit 1544810ce70448553540a0322a3d640b806967e7 @agnoster committed Oct 5, 2012
Showing with 157 additions and 153 deletions.
  1. +1 −153 bin/literapi
  2. +156 −0 lib/cli.js
View
@@ -1,155 +1,3 @@
#!/usr/bin/env node
-var LiterAPI = require('literapi')
- , vows = require('vows')
- , util = require('util')
- , fs = require('fs')
-
-var params = process.argv.slice(2)
-var options = { serial: true }
-
-var usageMessage =
- "\n literapi [-vah] http://api.server:port/root file1 [file2 [...]]" +
- "\n -v Show version number" +
- "\n -a Show all results, not just errors" +
- "\n -d Delay 100ms between calls (additional -d will double the delay)" +
- "\n -w Overwrite the document with annotations about successes and failures" +
- "\n -h Show this help"
-
-while (params[0] && params[0][0] && params[0][0] == '-') { // option flag
- for (var opt = params.shift(); opt = opt.slice(1); ) {
- switch (opt[0]) {
- case 'w':
- options.write = true
- break
- case 'v':
- console.log(LiterAPI.getVersion())
- break
- case 'p':
- options.serial = false
- break
- case 's':
- options.serial = true
- break
- case 'd':
- options.delay = options.delay ? (options.delay * 2) : 100
- break
- case 'h':
- usage()
- break
- case 'a':
- options.reporter = require('vows/lib/vows/reporters/spec')
- break
- default:
- console.log("Unrecognized option: -" + opt[0])
- }
- }
-}
-
-options.delay = options.delay || 25
-
-if (!options.serial) {
- console.log("Unfortunately, due to the ways vows work, tests *must* be run serially for now.")
- options.serial = true
-}
-
-function usage() {
- console.log(usageMessage)
- usageMessage = "\n\nPlease specify some options"
-}
-
-if (params.length < 2) {
- usage()
- process.exit()
-}
-
-options.root = params.shift()
-vowsoptions = {}
-if (options.reporter) vowsoptions.reporter = options.reporter
-var running = 0
-var results = {
- honored: 0,
- broken: 0,
- errored: 0,
- pending: 0,
- total: 0,
- time: 0
-};
-
-function markup(suite) {
-
- var batch, token, tags
-
- for (var i = 0; i < suite.batches.length; i++) {
-
- batch = suite.batches[i]
- token = batch.tests._token
- if (!token) continue
-
- if (batch.broken || batch.errored) token.tags = 'fail'
- else token.tags = 'ok'
-
- token.refresh()
- }
- return suite.tokens.join('')
-}
-
-function dumpMarkup(suite, outfile) {
-
- var file = fs.writeFileSync(outfile, markup(suite))
-}
-
-function runNext() {
- var api = new LiterAPI(options)
- if (params.length < 1) {
- checkIfDone()
- return
- }
- running++
-
- vows.reporter.reset();
-
- (function(infile) {
-
- api.compileFile(infile, function(err, suite) {
-
- if (err) throw(err)
-
- if (options.serial) {
- suite.run(vowsoptions, function(result) {
- running--
-
- Object.keys(result).forEach(function (k) {
- results[k] += result[k]
- })
-
- if (options.write) dumpMarkup(suite, infile)
-
- runNext()
- checkIfDone()
- })
- } else {
- suite.run(vowsoptions, function(result) {
- running--
- Object.keys(result).forEach(function (k) {
- results[k] += result[k]
- })
- checkIfDone()
- })
- runNext()
- }
- })
- })(params.shift())
-}
-
-function checkIfDone() {
- if (running <= 0) {
- var status = 0
- if (results.broken) status += 1
- if (results.errored) status += 2
- process.exit(status)
- }
-}
-
-runNext()
-
+require('literapi/lib/cli')()
View
@@ -0,0 +1,156 @@
+var LiterAPI = require('literapi')
+ , vows = require('vows')
+ , util = require('util')
+ , fs = require('fs')
+
+module.exports = function() {
+
+var params = process.argv.slice(2)
+var options = { serial: true }
+
+var usageMessage =
+ "\n literapi [-vah] http://api.server:port/root file1 [file2 [...]]" +
+ "\n -v Show version number" +
+ "\n -a Show all results, not just errors" +
+ "\n -d Delay 100ms between calls (additional -d will double the delay)" +
+ "\n -w Overwrite the document with annotations about successes and failures" +
+ "\n -h Show this help"
+
+while (params[0] && params[0][0] && params[0][0] == '-') { // option flag
+ for (var opt = params.shift(); opt = opt.slice(1); ) {
+ switch (opt[0]) {
+ case 'w':
+ options.write = true
+ break
+ case 'v':
+ console.log(LiterAPI.getVersion())
+ break
+ case 'p':
+ options.serial = false
+ break
+ case 's':
+ options.serial = true
+ break
+ case 'd':
+ options.delay = options.delay ? (options.delay * 2) : 100
+ break
+ case 'h':
+ usage()
+ break
+ case 'a':
+ options.reporter = require('vows/lib/vows/reporters/spec')
+ break
+ default:
+ console.log("Unrecognized option: -" + opt[0])
+ }
+ }
+}
+
+options.delay = options.delay || 25
+
+if (!options.serial) {
+ console.log("Unfortunately, due to the ways vows work, tests *must* be run serially for now.")
+ options.serial = true
+}
+
+function usage() {
+ console.log(usageMessage)
+ usageMessage = "\n\nPlease specify some options"
+}
+
+if (params.length < 2) {
+ usage()
+ process.exit()
+}
+
+options.root = params.shift()
+vowsoptions = {}
+if (options.reporter) vowsoptions.reporter = options.reporter
+var running = 0
+var results = {
+ honored: 0,
+ broken: 0,
+ errored: 0,
+ pending: 0,
+ total: 0,
+ time: 0
+};
+
+function markup(suite) {
+
+ var batch, token, tags
+
+ for (var i = 0; i < suite.batches.length; i++) {
+
+ batch = suite.batches[i]
+ token = batch.tests._token
+ if (!token) continue
+
+ if (batch.broken || batch.errored) token.tags = 'fail'
+ else token.tags = 'ok'
+
+ token.refresh()
+ }
+ return suite.tokens.join('')
+}
+
+function dumpMarkup(suite, outfile) {
+
+ var file = fs.writeFileSync(outfile, markup(suite))
+}
+
+function runNext() {
+ var api = new LiterAPI(options)
+ if (params.length < 1) {
+ checkIfDone()
+ return
+ }
+ running++
+
+ vows.reporter.reset();
+
+ (function(infile) {
+
+ api.compileFile(infile, function(err, suite) {
+
+ if (err) throw(err)
+
+ if (options.serial) {
+ suite.run(vowsoptions, function(result) {
+ running--
+
+ Object.keys(result).forEach(function (k) {
+ results[k] += result[k]
+ })
+
+ if (options.write) dumpMarkup(suite, infile)
+
+ runNext()
+ checkIfDone()
+ })
+ } else {
+ suite.run(vowsoptions, function(result) {
+ running--
+ Object.keys(result).forEach(function (k) {
+ results[k] += result[k]
+ })
+ checkIfDone()
+ })
+ runNext()
+ }
+ })
+ })(params.shift())
+}
+
+function checkIfDone() {
+ if (running <= 0) {
+ var status = 0
+ if (results.broken) status += 1
+ if (results.errored) status += 2
+ process.exit(status)
+ }
+}
+
+runNext()
+
+}

0 comments on commit 1544810

Please sign in to comment.