Skip to content

Commit

Permalink
Merge 7503dac into 59fb83a
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeff Escalante committed Dec 10, 2015
2 parents 59fb83a + 7503dac commit 91391a4
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 47 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
language: node_js
sudo: false
node_js:
- "0.10"
- 4.1
after_script:
- npm run coveralls
10 changes: 4 additions & 6 deletions lib/cli/watch.coffee
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
open = require 'open'
node = require 'when/node'
Roots = require '../../lib'
Server = require '../local_server'

Expand All @@ -24,6 +25,7 @@ module.exports = (cli, args) ->
project = new Roots args.path,
env: args.env
verbose: args.verbose
no_open: args.no_open

app = new Server(project)
port = process.env.port or args.port
Expand All @@ -39,12 +41,8 @@ module.exports = (cli, args) ->
project.watch()
.then (w) ->
res.watcher = w
res.server = app.start(port)
if project.config.open_browser and not args.no_open
if project.config.open_browser == true
open("http://localhost:#{port}/")
else
open(project.config.open_browser)
res.server = app
node.call(app.start.bind(app), port)
.yield(res)

###*
Expand Down
2 changes: 1 addition & 1 deletion lib/config.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class Config
@verbose = opts.verbose ? false
@debug = false
@live_reload = true
@open_browser = true
@open_browser = !opts.no_open ? true

load_config.call(@)

Expand Down
89 changes: 58 additions & 31 deletions lib/local_server.coffee
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
path = require 'path'
serve_static = require 'serve-static'
charge = require 'charge'
browsersync = require 'browser-sync'
_ = require 'lodash'

###*
* @class Server
Expand All @@ -17,6 +19,7 @@ class Server
###

constructor: (@project) ->
@bs = browsersync.create()

###*
* Start the local server on the given port.
Expand All @@ -26,52 +29,76 @@ class Server
###

start: (port, cb) ->
opts = @project.config.server ? {}
opts.log = false

if @project.config.env is 'development'
opts.write = content:
"<!-- roots development configuration -->
<script>var __livereload = #{@project.config.live_reload};</script>
<script src='/__roots__/main.js'></script>"
opts.cache_control = {'**': 'max-age=0, no-cache, no-store'}

app = charge(@project.config.output_path(), opts)

if @project.config.env is 'development'
app.stack.splice app.stack.length - 2, 0,
route: '/__roots__'
handle: serve_static(path.resolve(__dirname, 'browser'))

@server = app.start(port, cb)
# opts = @project.config.server ? {}

bs_options =
port: port
logLevel: 'silent'
open: @project.config.open_browser
server:
baseDir: @project.config.output_path()

if @project.config.browser then _.merge(bs_options, @project.config.browser)

# add charge middleware after merge to prevent errors
opts = @project.config.server or {}
middlewares = []

if opts.clean_urls
middlewares.push(charge.hygienist(@project.config.output_path()))
if opts.exclude
middlewares.push(charge.escapist(opts.exclude))
if opts.auth
middlewares.push(charge.publicist(opts.auth))
if opts.cache_control
middlewares.push(charge.archivist(opts.cache_control))
if opts.gzip
middlewares.push(charge.minimist(opts.gzip))
if opts.log
middlewares.push(charge.journalist(opts.log))
if opts.error_page
middlewares.push(charge.apologist(opts.error_page))

bs_options.server.middleware = middlewares

@bs.init(bs_options, cb)

###*
* Close the server and remove it.
###

stop: (cb) ->
@server.close(cb)
delete @server
@bs.exit()
cb()

###*
* Send a message through websockets to the browser.
*
* @param {String} k - message key
* @param {*} v - message value
* Reload the browser
###

send_msg: (k, v) ->
@server.send(type: k, data: v)
reload: ->
@bs.reload()

###*
* Inject loading spinner while compiling
###
compiling: ->
@bs.notify('<div id="roots-load-container"><div id="roots-compile-loader">
<div id="l1"></div><div id="l2"></div><div id="l3"></div><div id="l4"></div>
<div id="l5"></div><div id="l6"></div><div id="l7"></div><div id="l8"></div>
</div></div>')

###*
* These three methods send 'reload', 'compiling', and 'error' messages
* through to the browser.
* Sanitize error message and inject into page
* @param {Error} err - an error object
###

reload: -> @send_msg('reload')
compiling: -> @send_msg('compiling')
show_error: (err) ->
err = err.toString() if err instanceof Error
@send_msg('error', err)
cleanError = if err.replace
err.replace(/(\r\n|\n|\r)/gm, '<br>')
else
""
@bs.notify("<div id='roots-error'><pre><span>compile
error</span>#{cleanError}</pre></div>")

module.exports = Server
15 changes: 8 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,28 @@
"dependencies": {
"accord": "0.20.x",
"argparse": "1.x",
"charge": "0.0.4",
"chokidar": "1.0.x",
"coffee-script": "1.9.x",
"browser-sync": "2.x",
"charge": "0.1.x",
"chokidar": "1.x",
"coffee-script": "1.10.x",
"colors": "1.x",
"configstore": "0.3.x",
"graceful-fs": "4.x",
"inquirer": "0.9.x",
"inquirer": "0.10.x",
"keen.io": "0.1.x",
"lodash": "3.x",
"minimatch": "2.x",
"minimatch": "3.x",
"mkdirp": "0.5.x",
"npm": "2.x",
"open": "0.0.5",
"osenv": "0.1.x",
"readdirp": "1.x",
"readdirp": "2.x",
"rimraf": "2.x",
"serve-static": "1.x",
"ship": "0.2.x",
"sprout": "0.4.x",
"update-notifier": "0.5.x",
"vinyl": "0.5.x",
"vinyl": "1.x",
"when": "3.x"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion test/cli.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ describe 'cli', ->
cli.removeListener('inline', spy)
cli.removeListener('data', spy)
cli.removeListener('err', spy)
obj.server.close(done)
obj.server.stop(done)

it 'should error when trying to compile invalid code'

Expand Down

0 comments on commit 91391a4

Please sign in to comment.