Skip to content

Commit

Permalink
implement promise support
Browse files Browse the repository at this point in the history
  • Loading branch information
joshrowley committed Jan 7, 2015
1 parent baedbdf commit 7337edf
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 8 deletions.
17 changes: 9 additions & 8 deletions lib/index.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,29 @@ module.exports = (opts) ->

class RootsNetlify
constructor: (@roots) ->
opts.redirects ?= {}
opts.rewrites ?= {}
opts.headers ?= {}
@util = new RootsUtil(@roots)

setup: ->
W.all([write_headers.call(@), write_redirects.call(@)])
W(opts).with(@)
.then (opts) ->
@opts = _.defaults(opts, {redirects: {}, rewrites: {}, headers: {}})
.then ->
W.all([write_headers.call(@), write_redirects.call(@)])

write_headers = ->
res = _.reduce opts.headers, (str, conf, path) ->
res = _.reduce @opts.headers, (str, conf, path) ->
str += "#{path}\n"
str += " #{k}: #{v}\n" for k, v of conf
return str
, ''
@util.write '_headers', res

write_redirects = ->
redirects = _.pick(opts.redirects, codes)
redirects = _.pick(@opts.redirects, codes)
redirects['200'] ?= {}
redirects['301'] ?= {}
_.merge(redirects['200'], opts.rewrites)
_.merge(redirects['301'], _.omit(opts.redirects, codes))
_.merge(redirects['200'], @opts.rewrites)
_.merge(redirects['301'], _.omit(@opts.redirects, codes))

res = _.reduce redirects, (str, conf, code) ->
for k, v of conf
Expand Down
27 changes: 27 additions & 0 deletions test/fixtures/promises/app.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
netlify = require '../../..'
W = require 'when'

config =
redirects:
'/news': '/blog'
'/news/:year/:month:/:date/:slug': '/blog/:year/:month/:date/:story_id'
'/news/*': '/blog/:splat'
'302':
'/temp_redirect': '/'
'404':
'/ecommerce': '/closed'
rewrites:
'/*': '/index.html'
headers:
'/protected/path':
'Cache-Control': 'max-age: 3000'
'Basic-Auth': 'username:password'
'/*':
'X-Frame-Options': 'DENY'
'X-XSS-Protection': '1; mode=block'

module.exports =
ignores: ["**/.DS_Store"]
extensions: [
netlify(W.resolve(config))
]
6 changes: 6 additions & 0 deletions test/fixtures/promises/expected/_headers
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/protected/path
Cache-Control: max-age: 3000
Basic-Auth: username:password
/*
X-Frame-Options: DENY
X-XSS-Protection: 1; mode=block
6 changes: 6 additions & 0 deletions test/fixtures/promises/expected/_redirects
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/* /index.html 200
/news /blog 301
/news/:year/:month:/:date/:slug /blog/:year/:month/:date/:story_id 301
/news/* /blog/:splat 301
/temp_redirect / 302
/ecommerce /closed 404
1 change: 1 addition & 0 deletions test/fixtures/promises/index.jade
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
p hello world
7 changes: 7 additions & 0 deletions test/fixtures/promises/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "test",
"dependencies": {
"jade": "*",
"when": "^3.6.4"
}
}
13 changes: 13 additions & 0 deletions test/test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,16 @@ describe 'basic setup', ->
compiled = path.join(@public, '_redirects')
expected = path.join(@public, 'expected', '_redirects')
h.file.matches_file(compiled, expected).should.be.true

describe 'promises support', ->
before (done) -> compile_fixture.call(@, 'promises', -> done())

it 'compiles the headers config file using promises grep', ->
compiled = path.join(@public, '_headers')
expected = path.join(@public, 'expected', '_headers')
h.file.matches_file(compiled, expected).should.be.true

it 'compiles the redirects config file using promises', ->
compiled = path.join(@public, '_redirects')
expected = path.join(@public, 'expected', '_redirects')
h.file.matches_file(compiled, expected).should.be.true

0 comments on commit 7337edf

Please sign in to comment.