Skip to content

Commit

Permalink
fix cli prompt for info
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeff Escalante committed Dec 13, 2013
1 parent 26197be commit 9b0750e
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 36 deletions.
3 changes: 2 additions & 1 deletion bin/sprout
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ if (cmd.sync) {

function cb(err, res){
if (err){
console.error("ERROR: ".red + err)
console.error("ERROR: ".red)
console.error(new Error(err).stack)
process.exit(64) // command line usage error
}
console.log(res)
Expand Down
8 changes: 7 additions & 1 deletion lib/commands/add.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,17 @@ class Add extends Base
@url = @name
@name = @url.split('/')[@url.split('/').length-1]

@branch = ''
branch_matcher = /#(.*)$/
if @url.match(branch_matcher)
@branch = "-b #{@url.match(branch_matcher)[1]}"
@url = @url.replace(branch_matcher, '')

@done = @cb

execute: ->
error = null
exec "git clone #{@url} #{@path(@name)}", (err, stdout, stderr) =>
exec "git clone #{@branch} #{@url} #{@path(@name)}", (err) =>
if err and err.code > 0
str = err.toString()
if str.match /already exists and is not an empty directory/
Expand Down
60 changes: 27 additions & 33 deletions lib/commands/init.coffee
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
path = require 'path'
es = require 'event-stream'
fs = require 'fs'
W = require 'when'
nodefn = require 'when/node/function'
readdirp = require 'readdirp'
ncp = require('ncp').ncp
exec = require('child_process').exec
ejs = require 'ejs'
prompt = require 'prompt'
Base = require '../base'
Expand All @@ -16,6 +16,7 @@ class Init extends Base
super
accord.call(@, { name: name, target: target, options: opts, cb: cb })
if not @name then return @cb('please provide a template name')
if not @target then @target = path.join(process.cwd(), @name)
@done = @cb

execute: ->
Expand All @@ -26,13 +27,13 @@ class Init extends Base
get_user_config.call(@)

user_before_fn.call(@)
.then(prompt_for_info.bind(@))
.then(user_after_fn.bind(@))
.then(copy_template.bind(@))
.then(replace_ejs.bind(@))
.otherwise(@cb)
.then =>
@cb(null, "project #{@name} created!")
.then(prompt_for_info.bind(@))
.then(user_after_fn.bind(@))
.then(update_template.bind(@))
.then(copy_template.bind(@))
.then(replace_ejs.bind(@))
.catch(@cb.bind(@))
.done => @cb(null, "project #{@name} created!")

#
# @api private
Expand All @@ -43,49 +44,42 @@ class Init extends Base
if not fs.existsSync(init_file) then return @config = {}
@config = require(init_file)

# TODO: the return value from the before call must be passed back into the class
user_before_fn = ->
if not @config.before then return W.promise((r)-> r())
if not @config.before then return W.resolve()
nodefn.call(@config.before, @)

prompt_for_info = ->
deferred = W.defer()

if not @config.configure
@config_values = @options
return W.promise((r)-> r())
return W.resolve()

console.log '\nplease enter the following information:'.yellow
prompt.override = @options
prompt.message = ''
prompt.delimiter = ''
prompt.start()
prompt.get @config, (err, result) ->
if err then return deferred.reject(err)
@config_values = result
deferred.resolve()

return deferred.promise
nodefn.call(prompt.get, @config.configure).tap (res) =>
@config_values = res
console.log('')

user_after_fn = ->
if not @config.before then return W.promise((r)-> r())
if not @config.before then return W.resolve()
nodefn.call(@config.before, @)

# pull from git to ensure most recent version
update_template = ->
nodefn.call(exec, "cd #{@sprout_path}; git pull")

copy_template = ->
nodefn.call(ncp, path.join(@sprout_path, 'root'), @target)

replace_ejs = ->
deferred = W.defer()

# grab all files in the template
readdirp root: @target, (err, res) =>
if err then return deferred.reject(err)

# replace the ejs in all files
res.files.map (f) =>
processed = ejs.render(fs.readFileSync(f.fullPath, 'utf8'), @config_values)
fs.writeFileSync(f.fullPath, processed)

deferred.resolve()

return deferred.promise
nodefn.call(readdirp, { root: @target })
.tap (res) =>
res.files.map (f) =>
processed = ejs.render(fs.readFileSync(f.fullPath, 'utf8'), @config_values)
fs.writeFileSync(f.fullPath, processed)

module.exports = (name, p, opts, cb) ->
cmd = new Init(name, p, opts, cb)
Expand Down
2 changes: 1 addition & 1 deletion test/test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ describe 'js api', ->
should.not.exist(err)
testpath = path.join(__dirname, 'testproj')
@cmd.init 'foobar', testpath, { foo: 'bar' }, (err, res) =>
if err then console.error(err.toString())
if err then done(err)
should.not.exist(err)
fs.existsSync(path.join(testpath, 'index.html')).should.be.ok
contents = fs.readFileSync(path.join(testpath, 'index.html'), 'utf8')
Expand Down

0 comments on commit 9b0750e

Please sign in to comment.