#!/usr/bin/env node

var program = require('commander')
  , Graph = require('..')
  , path = require('path')
  , fs = require('fs')
  , all = require('when-all')
  , unique = require('unique')
  , debug = require('debug')('sourcegraph:cli')

require('colors')

program.version(require('../package').version)
	.usage('[options] <entry files...> usually there is only one')
	.option('-p, --plugins <plugins...>', 'A comma separated list of plugins', list)
	.option('-b, --beautify', 'present the output for humans')
	.option('-l, --list-files', 'list all files in the graph')
	.option('-m, --menu', 'list available plugins')
	.option('-d, debug', 'sourcegraph takes any of node\'s debug options')

function list (args) {
	return args.split(',')
}

program.on('--help', function () {
	write('  Examples: \n')
	write('\n')
	write('    # basic (outputs a list of file objects)\n')
	write('    $ '.blue + 'sourcegraph examples/husband.js -p javascript\n')
	write('\n')
})

program.parse(process.argv)

// display available plugins
if (program.menu) (function(){
	var list = fs.readdirSync(path.resolve(__dirname, '../src/plugins'))
	console.log('')
	console.log('  Available plugins: \n%s', render(list))
	console.log('')

	function removeExt (file) {
		return file.replace(/\.js$/, '')
	}
	function render (list) {
		return renderJSON(list.map(removeExt).sort()).replace(/^/gm, '    ')
	}
	process.exit(0)
})()

var graph = new Graph

if (!program.plugins) {
	console.warn('You should probably specify at least one plugin')
} else {
	program.plugins.forEach(function (plugin) {
		console.warn('Install plugin: %s'.blue, plugin)
		graph.use(plugin)
	})
}

if (!program.args) {
	console.warn('You need to provide at least on entry file')
	process.exit(0)
}

// Convert to absolute paths
var files = program.args.map(function(file){
	return path.resolve(file)
})

var pending = files.map(function(file){
	console.warn(('Tracing from file: %s').green, file)
	return graph.add(file)
})

all(pending).then(function(files){
	// there all the same object so pick any one
	files = files[0]
	var paths = Object.keys(files)

	// print
	if (program.listFiles) {
		if (program.beautify) write(JSON.stringify(paths, null, 2))
		else write(JSON.stringify(paths, null, 2))
	} else {
		// to array
		files = paths.map(function(path){ return files[path] })
		write(JSON.stringify(unique(files), null, 2))
	}

	write('\n')
}).throw()

function write(s){
	process.stdout.write(s)
}

@@ -336,38 +336,9 @@
 iles
-...%3E usually there is only one
+%3E
 ')%0A%09
@@ -372,11 +372,8 @@
 gins
-...
 %3E', 
@@ -413,16 +413,20 @@
 s', list
+, %5B%5D
 )%0A%09.opti
@@ -543,57 +543,8 @@
 h')%0A
-%09.option('-m, --menu', 'list available plugins')%0A
 %09.op
@@ -610,16 +610,61 @@
 ptions')
+%0A%09.option('--menu', 'list available plugins')
 %0A%0Afuncti
@@ -932,78 +932,27 @@
 %7D)%0A%0A
-program.parse(process.argv)%0A%0A// display available plugins%0Aif (
+// --menu%0A%0A
 program.
 menu
@@ -951,15 +951,19 @@
 ram.
+on('
 menu
-) (
+', 
 func
@@ -1062,110 +1062,44 @@
 ')%0A%09
-console.log('  Available plugins: %5Cn%25s', render(list))%0A%09console.log('')%0A%0A%09function removeExt 
+list.sort()%0A%09%09.map(function
 (file)
- 
 %7B%0A
+%09
 %09%09re
@@ -1134,255 +1134,174 @@
 ')%0A%09
-%7D%0A%09function render (list) %7B%0A%09%09return renderJSON(list.map(removeExt).sort()).replace(/%5E/gm, '    ')%0A%09%7D%0A%09process.exit(0)%0A%7D)()%0A%0Avar graph = new Graph%0A%0Aif (!program.plugins) %7B%0A%09console.warn('You should probably specify at least one plugin')%0A%7D else %7B%0A%09
+%09%7D)%0A%09%09.forEach(function(item)%7B%0A%09%09%09console.log('  '+item)%0A%09%09%7D)%0A%09console.log('')%0A%09process.exit(0)%0A%7D)%0A%0Aprogram.parse(process.argv)%0A%0Avar graph = new Graph%0A%0A// --plugins%0A%0A
 prog
@@ -1340,17 +1340,16 @@
 ugin) %7B%0A
-%09
 %09console
@@ -1389,17 +1389,16 @@
 plugin)%0A
-%09
 %09graph.u
@@ -1412,13 +1412,10 @@
 in)%0A
-%09
 %7D)
-%0A%7D
 %0A%0Aif
@@ -1781,16 +1781,19 @@
 %0A%09// the
+y a
 re all t
@@ -1810,24 +1810,8 @@
 ject
- so pick any one
 %0A%09fi

tidy cli