#!/usr/bin/env node
'use strict'
var commandLineArgs = require('command-line-args')
var ansi = require('ansi-escape-sequences')
var dmd = require('../')
var fs = require('fs')

var cli = commandLineArgs(dmd.cliOptions.concat([
  { name: 'help', alias: 'h', type: Boolean },
  { name: 'verbose', alias: 'v', type: Boolean }
]))

var usage = cli.getUsage({
  title: 'dmd',
  description: 'Generate markdown API documentation',
  footer: 'Project home: [underline]{https://github.com/jsdoc2md/dmd}',
  synopsis: [
    '$ cat jsdoc-parse-output.json | dmd <options>',
    '$ dmd --help'
  ]
})

try {
  var config = cli.parse()
} catch (err) {
  halt(err)
}

if (config.help) {
  console.log(usage)
  process.exit(0)
}

if (config.template) {
  config.template = fs.readFileSync(config.template, 'utf8')
}

var dmdStream = dmd(config)
dmdStream.on('error', halt)

process.stdin.pipe(dmdStream).pipe(process.stdout)

function halt (err) {
  if (err.code === 'EPIPE') process.exit(0) /* no big deal */

  if (config) {
    if (config.verbose) {
      logError('Error: ' + err.message)
      logError(err.stack || err)
    } else {
      logError('Error: ' + err.message)
      logError('(run dmd with --verbose for a stack trace)')
    }
  } else {
    logError(err.stack)
  }
  console.error(usage)
  process.exit(1)
}

function logError (msg) {
  console.error(ansi.format(msg, 'red'))
}

@@ -645,16 +645,25 @@
 lt(err)%0A
+  return%0A
 %7D%0A%0Aif (c
@@ -699,31 +699,22 @@
 sage)%0A  
-process.exit(0)
+return
 %0A%7D%0A%0Aif (
@@ -959,23 +959,14 @@
 E') 
-process.exit(0)
+return
  /* 
@@ -1296,11 +1296,16 @@
 exit
-(1)
+Code = 1
 %0A%7D%0A%0A

fix issue with half-written usage output on node v6