 path = require 'path'
+url = require 'url'
 
 require 'colors'
 CSON = require 'season'
 optimist = require 'optimist'
-
+request = require 'request'
+
+auth = require './auth'
 config = require './config'
 Command = require './command'
 

        process.stdout.write '\u2717\n'.red
        callback("#{stdout}\n#{stderr}".red)

  run: (options) ->
    {callback} = options
    options = @parseOptions(options.commandArgs)

    if version = options.argv._[0]
      @versionPackage version, (error) =>
        if error?
          callback(error)
        else
          @publishPackage(options, callback)
    else
      @publishPackage(options, callback)


      If a new version or version increment is specified than a new Git tag is
      created and the package.json file is updated with that new version before
      it is published to the apm registry. The HEAD branch and the new tag are
      pushed up to the remote repository automatically using this option.

      Run `apm available` to see all the currently published packages.
    """
    options.alias('h', 'help').describe('help', 'Print this usage message')
    options.alias('t', 'tag').string('tag').describe('tag', 'Specify a tag to publish')

  showHelp: (argv) -> @parseOptions(argv).showHelp()

  # Create a new version and tag use the `npm version` command.
  #
  #  * version: The new version or version increment.
  #  * callback: The callback function to invoke with an error as the first
  #    argument and a the generated tag string as the second argument.
  versionPackage: (version, callback) ->
    process.stdout.write 'Preparing and tagging a new version '
    versionArgs = ['version', version, '-m', 'Prepare %s release']
    @fork @atomNpmPath, versionArgs, (code, stderr='', stdout='') ->
      if code is 0
        process.stdout.write '\u2713\n'.green
        callback(null, stdout.trim())
      else
        process.stdout.write '\u2717\n'.red
        callback("#{stdout}\n#{stderr}".red)

  # Push a tag to the remote repository.
  #
  #  * tag: The tag to push.
  #  * callback: The callback function to invoke with an error as the first
  #    argument.
  pushVersion: (tag, callback) ->
    process.stdout.write "Pushing #{tag} tag "
    pushArgs = ['push', 'origin', 'HEAD', tag]
    @spawn 'git', pushArgs, (code, stderr='', stdout='') ->
      if code is 0
        process.stdout.write '\u2713\n'.green
        callback()
