-
Notifications
You must be signed in to change notification settings - Fork 280
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
826 additions
and
208 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
node_modules | ||
src-cov | ||
/lib | ||
cov.html | ||
.vagrant | ||
*.DS_Store | ||
/.idea | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,87 +1,13 @@ | ||
#!/usr/bin/env node | ||
var optimist = require('optimist'); | ||
var parsePackageJson = require('../lib/parse-package-json'); | ||
var path = require('path'); | ||
var Dredd = require('../lib/dredd'); | ||
|
||
var version = parsePackageJson(path.join(__dirname, '../package.json')); | ||
|
||
var argv = optimist | ||
.usage("Usage: \n dredd <path or URL to blueprint> <api_endpoint> [OPTIONS]" + | ||
"\n\nExample: \n " + "dredd ./apiary.md http://localhost:3000 --dry-run") | ||
.options(Dredd.options) | ||
.wrap(80) | ||
.argv; | ||
|
||
var argError = false; | ||
|
||
if (argv.help === true) { | ||
optimist.showHelp(fn=console.error); | ||
process.exit(0); | ||
} | ||
|
||
if (argv.version === true) { | ||
console.log(version); | ||
process.exit(0); | ||
} | ||
|
||
if (argv._[0] === undefined) { | ||
console.error("Error: Must specify path to blueprint file."); | ||
argError = true; | ||
} | ||
if (argv._[1] === undefined) { | ||
console.error("Error: Must specify api endpoint."); | ||
argError = true; | ||
} | ||
|
||
if (argError) { | ||
console.error("\n"); | ||
optimist.showHelp(fn=console.error); | ||
process.exit(1); | ||
} | ||
|
||
// transform path and p argument to array if it's not | ||
if(!Array.isArray(argv['path'])){ | ||
argv['path'] = argv['p'] = [argv['path']]; | ||
} | ||
|
||
// some shells are automatically expanding globs and concating result as arguments | ||
// so I'm taking last argument as API endpoint server URL and removing it forom optimist's args | ||
var server = argv._[argv._.length - 1]; | ||
argv._.splice(argv._.length -1, 1); | ||
|
||
// and rest of arguments concating to 'path' and 'p' opts, duplicates are filtered out later | ||
argv['p'] = argv['path'] = argv['path'].concat(argv._) | ||
|
||
configuration = { | ||
'server': server, | ||
'options': argv | ||
}; | ||
|
||
configuration.options.path.push(argv._[0]); | ||
|
||
dredd = new Dredd(configuration); | ||
|
||
process.on( 'SIGINT', function() { | ||
console.log( "\nShutting down from SIGINT (Ctrl-C)" ); | ||
dredd.transactionsComplete(function() { | ||
process.exit(0); | ||
}); | ||
var DreddCommand = require('../lib/command'); | ||
|
||
var dreddCli = new DreddCommand({ | ||
custom: { | ||
env: process.env, | ||
cwd: process.cwd(), | ||
argv: process.argv.slice(2) | ||
}, | ||
exit: process.exit | ||
}); | ||
|
||
dredd.run(function(error, stats){ | ||
if (error) { | ||
if (error.message) { | ||
console.error(error.message); | ||
} | ||
if (error.stack) { | ||
console.error(error.stack); | ||
} | ||
process.exit(1); | ||
} | ||
if (stats.failures + stats.errors > 0) { | ||
process.exit(1); | ||
} else { | ||
process.exit(0); | ||
} | ||
}); | ||
dreddCli.warmUp().takeoff(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
path = require 'path' | ||
optimist = require 'optimist' | ||
console = require 'console' | ||
|
||
parsePackageJson = require './parse-package-json' | ||
Dredd = require './dredd' | ||
|
||
version = parsePackageJson path.join(__dirname, '../package.json') | ||
|
||
class DreddCommand | ||
constructor: (options = {}, @cb) -> | ||
{@exit, @custom} = options | ||
# support to call both 'new DreddCommand()' and/or 'DreddCommand()' | ||
# without the "new" keyword | ||
@custom ?= {} | ||
|
||
if not @custom.cwd or typeof @custom.cwd isnt 'string' | ||
@custom.cwd = process.cwd() | ||
|
||
if not @custom.argv or not Array.isArray @custom.argv | ||
@custom.argv = [] | ||
|
||
if not (@ instanceof DreddCommand) | ||
return new DreddCommand(options, @cb) | ||
|
||
@finished = false | ||
|
||
warmUp: -> | ||
@finished = false | ||
|
||
@optimist = optimist(@custom['argv'], @custom['cwd']) | ||
|
||
@optimist.usage( | ||
""" | ||
Usage: | ||
dredd <path or URL to blueprint> <api_endpoint> [OPTIONS] | ||
Example: | ||
dredd ./apiary.md http://localhost:3000 --dry-run | ||
""" | ||
) | ||
.options(Dredd.options) | ||
.wrap(80) | ||
|
||
@argv = @optimist.argv | ||
|
||
argError = false | ||
|
||
if not @cb | ||
@exit and (@exit is process.exit) and @sigIntEventAdd = true | ||
@_processExit = @exit or process.exit | ||
else | ||
@_processExit = ((code) -> | ||
if @finished then return | ||
@finished = true | ||
if @sigIntEventAdded | ||
process.removeEventListener 'SIGINT', @commandSigInt | ||
@cb code | ||
return @ | ||
).bind @ | ||
|
||
if @argv.help is true | ||
@optimist.showHelp(console.error) | ||
return @_processExit(0) | ||
|
||
if @argv.version is true | ||
console.log version | ||
return @_processExit(0) | ||
|
||
if not @argv._[0]? | ||
console.error("\nError: Must specify path to blueprint file.") | ||
argError = true | ||
|
||
if not @argv._[1]? | ||
console.error("\nError: Must specify api endpoint.") | ||
argError = true | ||
|
||
if argError | ||
console.error("\n") | ||
@optimist.showHelp(console.error) | ||
return @_processExit(1) | ||
|
||
# transform path and p argument to array if it's not | ||
if !Array.isArray(@argv['path']) | ||
@argv['path'] = @argv['p'] = [@argv['path']] | ||
|
||
configurationForDredd = @initConfig() | ||
@dreddInstance = @initDredd configurationForDredd | ||
return @ | ||
|
||
lastArgvIsApiEndpoint: -> | ||
# some shells are automatically expanding globs and concating result as arguments | ||
# so I'm taking last argument as API endpoint server URL and removing it forom optimist's args | ||
@server = @argv._[@argv._.length - 1] | ||
@argv._.splice(@argv._.length - 1, 1) | ||
return @ | ||
|
||
takeRestOfParamsAsPath: -> | ||
# and rest of arguments concating to 'path' and 'p' opts, duplicates are filtered out later | ||
@argv['p'] = @argv['path'] = @argv['path'].concat(@argv._) | ||
return @ | ||
|
||
initConfig: -> | ||
@ | ||
.lastArgvIsApiEndpoint() | ||
.takeRestOfParamsAsPath() | ||
|
||
configuration = | ||
'server': @server | ||
'options': @argv | ||
|
||
# push first argument (without some known configuration --key) into paths | ||
configuration.options.path ?= [] | ||
configuration.options.path.push @argv._[0] | ||
|
||
if @custom['env'] or @custom['stdout'] or @custom['cwd'] | ||
configuration.custom ?= {} | ||
configuration.custom['env'] = @custom['env'] or process.env | ||
configuration.custom['stdout'] = @custom['stdout'] or process.stdout | ||
configuration.custom['cwd'] = @custom['cwd'] or process.cwd() | ||
|
||
return configuration | ||
|
||
initDredd: (configuration) -> | ||
dredd = new Dredd configuration | ||
return dredd | ||
|
||
commandSigInt: -> | ||
console.log "\nShutting down from SIGINT (Ctrl-C)" | ||
@dreddInstance.transactionsComplete => @_processExit(0) | ||
|
||
takeoff: -> | ||
if @finished then return | ||
|
||
@runDredd @dreddInstance | ||
|
||
runDredd: (dreddInstance) -> | ||
if @sigIntEventAdd | ||
# handle SIGINT from user | ||
@sigIntEventAdded = !@sigIntEventAdd = false | ||
process.on 'SIGINT', @commandSigInt | ||
|
||
dreddInstance.run (error, stats) => | ||
if error | ||
if error.message | ||
console.error error.message | ||
if error.stack | ||
console.error error.stack | ||
return @_processExit(1) | ||
if (stats.failures + stats.errors) > 0 | ||
@_processExit(1) | ||
else | ||
@_processExit(0) | ||
return | ||
|
||
return @ | ||
|
||
exports = module.exports = DreddCommand |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.