Skip to content

Commit

Permalink
Merge 362c851 into a3da021
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeff Escalante committed Jun 19, 2014
2 parents a3da021 + 362c851 commit a68646b
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 22 deletions.
54 changes: 50 additions & 4 deletions lib/api/add.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,27 @@ fs = require 'fs'
path = require 'path'
rimraf = require 'rimraf'
url = require 'url'
dns = require 'dns'

class Add extends Base

constructor: -> super

execute: (opts) ->
foo = 'wow'
configure_options.call(@, opts).with(@)
.then(determine_if_local)
.then(ensure_local_template_exists)
.then(check_internet_connection)
.then(set_branch)
.then(remove_existing_template)
.then(link_project)
.then(checkout_branch)
.yield("template '#{@name}' added")

# @api private
###*
* @private
###

configure_options = (opts) ->
if not opts or not opts.name
Expand All @@ -38,14 +43,21 @@ class Add extends Base

W.resolve()

###*
* If the template isn't an http or git url, set `@local` to true
###

determine_if_local = ->
# set @local to true if @template isn't an http or git url
url = url.parse(@template)
remote = url.pathname.split('.')[url.pathname.split('.').length-1] == 'git'
if not remote
@local = true
W.resolve()

###*
* If a local template was passed, we need to make sure it exists
###

ensure_local_template_exists = ->
if not @local then return W.resolve()
if not which.sync('git')
Expand All @@ -54,6 +66,26 @@ class Add extends Base
if not fs.existsSync(path.normalize(@template))
return W.reject("there is no sprout template located at '#{@template}'")

###*
* The most legitimate way to find out if someone is connected to the
* internetz, backed by a 5 year money-back guarantee!
###

check_internet_connection = ->
if @local then return W.resolve()

try
nodefn.call(dns.resolve, 'google.com')
.catch(-> throw 'make that you are connected to the internet!')
catch e
console.log 'caught'
console.log(e)

###*
* If a branch was passed via hash (github.com/foo/bar#some-branch), extract
* it and save to a local variable, then remove it from the template uri
###

set_branch = ->
if @local then return W.resolve()
@branch = null
Expand All @@ -63,13 +95,27 @@ class Add extends Base
@template = @template.replace(branch_matcher, '')
W.resolve()

###*
* If there was a template already there, get rid of it because we're about
* to update it with a new version.
###

remove_existing_template = ->
nodefn.call(rimraf, @path(@name))
if not @no_internet then nodefn.call(rimraf, @path(@name))

###*
* Link up the template to the right spot, whether this is locally or through
* a git clone.
###

link_project = ->
cmd = "git clone #{@template} #{@path(@name)}"
if @local then cmd = "rm -rf #{@path(@name)} && ln -s #{@template} #{@path(@name)}"
nodefn.call(exec, cmd)
if not @no_internet then nodefn.call(exec, cmd)

###*
* If there was a branch provided, check it out.
###

checkout_branch = ->
if not @branch then return W.resolve()
Expand Down
18 changes: 12 additions & 6 deletions lib/api/init.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ inquirer = require 'inquirer'
Base = require '../base'
S = require 'string'
_ = require 'lodash'
dns = require 'dns'

class Init extends Base

Expand All @@ -24,6 +25,7 @@ class Init extends Base
.then(add_defaults_to_questions)
.then(prompt_user_for_answers)
.then(merge_config_values_with_overrides)
.then(check_internet_connection)
.then(ensure_template_is_updated)
.then(copy_template)
.then(replace_ejs)
Expand All @@ -36,9 +38,9 @@ class Init extends Base
remove: (f) ->
fs.unlinkSync(path.resolve(@target, f))

#
# @api private
#
###*
* @private
###

configure_options = (opts) ->
if not opts or not opts.name
Expand Down Expand Up @@ -93,9 +95,13 @@ class Init extends Base
merge_config_values_with_overrides = ->
@config_values = _.assign(@answers, @overrides)

# TODO: fix this
ensure_template_is_updated = ->
nodefn.call(exec, "cd #{@sprout_path} && git pull")
check_internet_connection = ->
nodefn.call(dns.resolve, 'google.com')
.then(-> true).catch(-> false)

ensure_template_is_updated = (internet) ->
if not internet then return W.resolve()
nodefn.call(exec, "git pull", { cwd: @sprout_path })
.catch(-> return W.resolve())

copy_template = ->
Expand Down
12 changes: 7 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,15 @@
"argparse": "0.1.x"
},
"devDependencies": {
"mocha": "*",
"should": "*",
"shelljs": "0.2.x",
"rimraf": "2.x",
"coveralls": "2.x",
"errno": "^0.1.1",
"istanbul": "0.2.x",
"mocha": "*",
"mocha-lcov-reporter": "0.0.1",
"istanbul": "0.2.x"
"mockery": "^1.4.0",
"rimraf": "2.x",
"shelljs": "0.2.x",
"should": "*"
},
"scripts": {
"test": "mocha",
Expand Down
56 changes: 49 additions & 7 deletions test/test.coffee
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
path = require 'path'
fs = require 'fs'
should = require 'should'
rimraf = require 'rimraf'
_path = path.join(__dirname, 'fixtures')
sprout = require '..'
cli = new (require '../lib/cli')(debug: true)
path = require 'path'
fs = require 'fs'
should = require 'should'
rimraf = require 'rimraf'
_path = path.join(__dirname, 'fixtures')
sprout = require '..'
cli = new (require '../lib/cli')(debug: true)
mockery = require 'mockery'
errno = require 'errno'
_ = require 'lodash'

test_template_url = 'https://github.com/carrot/sprout-test-template.git'
test_template_path = path.join(_path, 'basic')
Expand Down Expand Up @@ -59,6 +62,21 @@ describe 'js api', ->
err.should.match /there is no sprout template located at/
).done((-> done()), done)

it 'errors when trying to add a remote template with no internet', (done) ->
mockery.enable(useCleanCache: true, warnOnUnregistered: false)
mockery.registerMock 'dns',
resolve: (name, cb) -> cb(errno.code.ECONNREFUSED)

sprout = require '..'

sprout.add(name: 'foobar', uri: test_template_url)
.catch (e) ->
e.should.eql('make that you are connected to the internet!')
done()

mockery.deregisterMock('dns')
mockery.disable()

describe 'list', ->

it 'lists available templates', (done) ->
Expand All @@ -70,6 +88,8 @@ describe 'js api', ->

describe 'init', ->

before -> sprout = require '..'

it 'errors when no args provided', (done) ->
sprout.init()
.catch((err) -> should.exist(err); done())
Expand Down Expand Up @@ -177,6 +197,28 @@ describe 'js api', ->
.then(-> sprout.remove('foobar-6'))
.done((-> done()), done)

it 'works even when not connected to the internet', (done) ->
mockery.enable(useCleanCache: true, warnOnUnregistered: false)
mockery.registerMock 'dns',
resolve: (name, cb) -> cb(errno.code.ECONNREFUSED)

sprout = require '..'
test_template = path.join(_path, 'basic')

sprout.add(name: 'foobar', uri: test_template)
.then(-> sprout.init(name: 'foobar', path: test_path, overrides: { foo: 'bar'}))
.tap(->
fs.existsSync(path.join(test_path, 'index.html')).should.be.ok
contents = fs.readFileSync(path.join(test_path, 'index.html'), 'utf8')
contents.should.match /bar/
rimraf.sync(test_path)
)
.then(-> sprout.remove('foobar'))
.done((-> done()), done)

mockery.deregisterMock('dns')
mockery.disable()

describe 'cli', ->

describe 'add', ->
Expand Down

0 comments on commit a68646b

Please sign in to comment.