Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integrate Browsersync #681

Merged
merged 17 commits into from
Feb 9, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
language: node_js
sudo: false
node_js:
- "0.10"
- "4"
- "5"
after_script:
- npm run coveralls
4 changes: 2 additions & 2 deletions lib/api/new.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ class New
W.promise (resolve, reject) ->
qs = []
for question in questions
qs.push(question) unless _.contains(skip, question.name)
qs.push(question) unless _.includes(skip, question.name)
inquirer.prompt qs, (answers) -> resolve(answers)

W.resolve(_.contains(_.keys(sprout.templates), base_tpl_name))
W.resolve(_.includes(_.keys(sprout.templates), base_tpl_name))
.then (res) ->
if not res
sprout.add(base_tpl_name, base_tpl_url)
Expand Down
2 changes: 1 addition & 1 deletion lib/api/template.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ exports.default = (args = {}) ->
if not args.name
return W.reject(new Error('please provide a template name'))

if not _.contains(_.keys(sprout.templates), args.name)
if not _.includes(_.keys(sprout.templates), args.name)
return W.reject(new Error "you do not have this template installed")

config = global_config()
Expand Down
8 changes: 4 additions & 4 deletions lib/api/watch.coffee
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
chokidar = require 'chokidar'
minimatch = require 'minimatch'
_ = require 'lodash'
chokidar = require 'chokidar'
mm = require 'micromatch'
_ = require 'lodash'

###*
* @class Watcher
Expand Down Expand Up @@ -42,7 +42,7 @@ class Watcher
ignore = (p) ->
f = p.replace(@roots.root, '').slice(1)
@roots.config.watcher_ignores
.map (i) -> minimatch(f, i, { dot: true })
.map (i) -> mm.isMatch(f, i, { dot: true })
.filter (i) -> i
.length

Expand Down
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
6 changes: 3 additions & 3 deletions lib/compiler.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class CompileFile

constructor: (@roots, @extensions, @compile_options, @category, @file) ->
@adapters = get_adapters.call(@)
@is_compiled = !!_(@adapters).pluck('name').compact().value().length
@is_compiled = !!_(@adapters).map('name').compact().value().length
@out_ext = _.last(@adapters).output
@file_options = {filename: @file.path, _path: url_path.call(@)}

Expand Down Expand Up @@ -157,7 +157,7 @@ class CompileFile

process_write_hook_results = (results) ->
if results.length < 1 then return [write_task.call(@)]
if _.contains(results, false) then return []
if _.includes(results, false) then return []

write_tasks = []
normal_write_pushed = false
Expand Down Expand Up @@ -258,7 +258,7 @@ class CompileFile

for ext in _.clone(extensions).reverse()
compiler = _.find @roots.config.compilers, (c) ->
_.contains(c.extensions, ext)
_.includes(c.extensions, ext)

adapters.push(if compiler then compiler else { output: ext })

Expand Down
4 changes: 2 additions & 2 deletions 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 Expand Up @@ -144,7 +144,7 @@ class Config

out: (f, ext) ->
res = f.relative.split(path.sep)
if _.contains(@dump_dirs, res[0]) then res.shift()
if _.includes(@dump_dirs, res[0]) then res.shift()
res.unshift(@output_path())
res = res.join(path.sep)
if ext
Expand Down
2 changes: 1 addition & 1 deletion lib/extensions/compiled.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ module.exports = ->

detect_fn = (f) ->
exts = _(@roots.config.compilers).map((i)->i.extensions).flatten().value()
_.contains(exts, path.extname(f.relative).slice(1))
_.includes(exts, path.extname(f.relative).slice(1))
22 changes: 11 additions & 11 deletions lib/fs_parser.coffee
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
fs = require 'graceful-fs'
path = require 'path'
W = require 'when'
readdirp = require 'readdirp'
_ = require 'lodash'
minimatch = require 'minimatch'
pipeline = require 'when/pipeline'
File = require 'vinyl'
fs = require 'graceful-fs'
path = require 'path'
W = require 'when'
readdirp = require 'readdirp'
_ = require 'lodash'
mm = require 'micromatch'
pipeline = require 'when/pipeline'
File = require 'vinyl'

###*
* @class FS Parser
Expand Down Expand Up @@ -150,7 +150,7 @@ class FSParser
if not detected then return false
cat = extfs.category ? ext.category
@ast[cat] ?= []
@ast[cat].push(file) unless _.contains(@ast[cat], file)
@ast[cat].push(file) unless _.includes(@ast[cat], file)
return extfs.extract

###*
Expand All @@ -175,8 +175,8 @@ class FSParser

ignored = (f) ->
@config.ignores
.map (i) -> minimatch(f, i, dot: true)
.filter (i) -> i
.map((i) -> mm.isMatch(f, i, dot: true))
.filter((i) -> i)
.length

module.exports = FSParser
83 changes: 52 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,70 @@ 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)
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('compiling...', 999999)

###*
* 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
"compile error!"
@bs.notify(cleanError, 999999)

module.exports = Server
24 changes: 12 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,30 @@
"Sam Saccone <sam@samx.it>"
],
"dependencies": {
"accord": "0.20.x",
"accord": "0.22.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.11.x",
"keen.io": "0.1.x",
"lodash": "3.x",
"minimatch": "2.x",
"lodash": "4.x",
"micromatch": "2.x",
"mkdirp": "0.5.x",
"npm": "2.x",
"npm": "3.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",
"update-notifier": "0.6.x",
"vinyl": "1.x",
"when": "3.x"
},
"devDependencies": {
Expand All @@ -46,7 +47,6 @@
"mocha": "2.x",
"mocha-lcov-reporter": "0.0.2",
"mockery": "1.x",
"phantomjs": "1.x",
"roots-util": "0.1.x",
"selenium-webdriver": "2.x",
"sinon": "1.x",
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
2 changes: 1 addition & 1 deletion test/new.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ new_path = path.join(base_path, 'new/testing')

before ->
@starting_templates = Roots.template.list()
if _.contains(@starting_templates, 'roots-base')
if _.includes(@starting_templates, 'roots-base')
# remove roots-base to verify 'base template added'
# functionality in lib/api/new.coffee
Roots.template.remove(name: 'roots-base')
Expand Down