#!/usr/bin/env node
'use strict'
require('coffee-script').register()
var fs = require('fs')
var Bumped = require('./../lib/Bumped')
var updateNotifier = require('update-notifier')
var partial = require('lodash.partial')
var cli = require('meow')({
  pkg: require('../package.json'),
  help: fs.readFileSync(__dirname + '/help.txt', 'utf8')
})

updateNotifier({pkg: cli.pkg}).notify()
if (cli.input.length === 0) cli.showHelp()

var bumped = new Bumped()
var command = cli.input.shift()

var exit = function (err) {
  if (!err) return process.exit()
  if (!Array.isArray(err)) err = [err]
  var code = err[err.length - 1].code || 1
  return process.exit(code || 1)
}

var commands = {
  init: partial(bumped.init, exit),
  version: partial(bumped.semver.version, exit),

  release: partial(bumped.semver.release, {
    version: cli.input[0],
    prefix: cli.input[1]
  }, exit),

  add: partial(bumped.config.add, {
    detect: true,
    save: true,
    file: cli.input[0]
  }, exit),

  remove: partial(bumped.config.remove, {
    save: true,
    file: cli.input[0]
  }, exit),

  set: (function () {
    var property = cli.input[0]
    cli.input.shift()
    var value = cli.input.join(' ')
    return partial(bumped.config.set, {
      property: property,
      value: value
    }, exit)
  })()
}

var existCommand = Object.keys(commands).indexOf(command) > -1

if (!existCommand) return cli.showHelp()

process.stdout.write('\n')

if (command === 'init') return commands[command]()

return bumped.load(function (err) {
  if (err) throw err
  return commands[command]()
})

@@ -41,17 +41,16 @@
 ('coffee
--
 script')
@@ -352,16 +352,17 @@
 tifier(%7B
+ 
 pkg: cli
@@ -365,16 +365,17 @@
  cli.pkg
+ 
 %7D).notif
@@ -775,32 +775,37 @@
 elease: partial(
+%0A    
 bumped.semver.re
@@ -810,19 +810,25 @@
 release,
+%0A   
  %7B%0A
+  
     vers
@@ -846,16 +846,18 @@
 put%5B0%5D,%0A
+  
     pref
@@ -871,31 +871,40 @@
 .input%5B1%5D%0A  
-%7D,
+  %7D,%0A   
  exit
+%0A  
 ),%0A%0A  add: p
@@ -902,32 +902,37 @@
 %0A  add: partial(
+%0A    
 bumped.config.ad
@@ -933,23 +933,29 @@
 fig.add,
+%0A   
  %7B%0A    
+  
 detect: 
@@ -960,24 +960,26 @@
 : true,%0A    
+  
 save: true,%0A
@@ -970,32 +970,34 @@
     save: true,%0A
+  
     file: cli.in
@@ -1001,31 +1001,40 @@
 .input%5B0%5D%0A  
-%7D,
+  %7D,%0A   
  exit
+%0A  
 ),%0A%0A  remove
@@ -1035,32 +1035,37 @@
 remove: partial(
+%0A    
 bumped.config.re
@@ -1069,19 +1069,25 @@
 .remove,
+%0A   
  %7B%0A
+  
     save
@@ -1090,24 +1090,26 @@
 save: true,%0A
+  
     file: cl
@@ -1121,23 +1121,32 @@
 ut%5B0%5D%0A  
-%7D,
+  %7D,%0A   
  exit
+%0A  
 ),%0A%0A  se
@@ -1268,24 +1268,31 @@
 urn partial(
+%0A      
 bumped.confi
@@ -1297,19 +1297,27 @@
 fig.set,
+%0A     
  %7B%0A
+  
       pr
@@ -1340,16 +1340,18 @@
 ,%0A      
+  
 value: v
@@ -1363,15 +1363,28 @@
     
+ 
+ 
 %7D,
- exit
+%0A      exit%0A    
 )%0A  

Migrate coffeescript