Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

file 146 lines (110 sloc) 4.091 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
{ exec } = require "child_process"
fs = require "fs"
path = require "path"

colors = require "colors"
glob = require "glob"
_ = require "underscore"

module.exports = class Bin
  constructor: (options={}) ->

    args = process.argv.splice(2)
    names = []
    branches = []
    skip = false

    _.each args, (arg, index) ->
      if skip
        skip = false
        return

      if arg == '-b'
        branches.push(args[index + 1])
        skip = true
      else
        names.push(arg)


    if names.length
      
      ask = options.ask || @ask

      ask "What is your Github username?", (login) =>
        _.each names, (name) =>

          commands = []
          dir = null

          if name.indexOf('/') > -1
            dir = path.dirname(name)
            name = path.basename(name)

          if dir
            commands = commands.concat [
              "mkdir -p #{dir}"
              "cd #{dir}"
            ]

          commands = commands.concat [
            "git clone git://github.com/winton/node-template.git #{name}"
            "cd #{name}"
            "git remote rename origin node-template"
            "git fetch node-template"
            "git remote add origin git@github.com:#{login}/#{name}.git"
          ]

          if branches.length
            commands = commands.concat _.map branches.sort(), (branch) ->
              "git merge node-template/#{branch}"

          commands = commands.concat [
            "rm npm-shrinkwrap.json"
            "rm bin/node-template.coffee"
            "rm test/node-template.coffee"
          ]

          @executing(commands)
          
          exec commands.join(' && '), @catchError =>

            dir = if dir then "#{dir}/" else ""
            commands = []
            cwd = process.cwd()

            glob "#{dir}#{name}/**/node-template*", (e, paths) =>
              _.each paths, (path) ->
                commands.push "mv #{path} #{Bin.renamePath(path, name)}"

              @executing(commands)

              exec commands.join(' && '), @catchError =>

                commands = [ "cd #{dir}#{name}" ]

                # Overwrite files to remove node-template code

                overwrite = {}

                overwrite["bin/#{name}"] =
                  """
#!/usr/bin/env node

require("../lib/#{name}");
"""

                overwrite["src/#{name}.coffee"] =
                  """
common = require './#{name}/common'
async = common.async
_ = common.underscore
"""
              
                _.each overwrite, (body, path) ->
                  if fs.existsSync("#{dir}#{name}/#{path}")
                    body = body.replace(/\n/g, "\\n").replace(/"/g, "\\\"")
                    commands.push "echo \"#{body}\" > #{path}"

                if fs.existsSync("#{dir}#{name}/src")
                  commands.push("coffee -o lib -c src")

                commands = commands.concat [
                  "cake install"
                  "cd #{cwd}"
                ]

                @executing(commands)
                
                exec commands.join(' && '), ->
                  if options.done then options.done() else process.exit()

  ask: (q, fn) ->
    console.log "\n#{q}".bold.yellow
    process.stdin.resume()
    process.stdin.setEncoding "utf8"
    process.stdin.on "data", (path) ->
      fn path.replace(/\s+$/, "")

  catchError: (successFn) ->
    (error, stdout, stderr) ->
      if error
        console.log "\nError :(".bold.red
        console.log error
        console.log stdout
        console.log stderr
      else
        console.log "\nSuccess :)\n".bold.green
        successFn(error, stdout, stderr) if successFn

  executing: (commands) ->
    console.log "\nExecuting:".bold.yellow
    _.each commands, (command) -> console.log(command)

  @renamePath: (p, new_name) ->
    base = path.basename(p)
    dir = path.dirname(p)
    "#{dir}/#{base.replace('node-template', new_name)}"
Something went wrong with that request. Please try again.