-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.coffee
111 lines (90 loc) · 3.01 KB
/
gulpfile.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
gulp = require 'gulp'
gutil = require 'gulp-util'
settings = require './settings'
gulp.task 'clean', ->
del = require 'del'
del.sync ['build', 'release']
gulp.task 'metalsmith', require './metalsmith'
gulp.task 'build', ['metalsmith']
servers =
dev: null
gulp.task 'serve:dev', (done) ->
connect = require 'connect'
serveStatic = require 'serve-static'
portFallback = require 'port-fallback'
http = require 'http'
app = connect()
.use serveStatic('build')
server = http.createServer app
portFallback.listen server,
defaultPort: settings.port, (err, port) ->
throw err if err
console.log 'Listing on '+port
settings.port = server.address().port
server.unref()
done()
servers.dev = server
gulp.task 'crawl', ['build', 'serve:dev'], (done) ->
Crawler = require 'simplecrawler'
referrers = {}
crawler = Crawler.crawl settings.devServerUrl()
.on 'discoverycomplete', (item, urls) ->
referrers[url] = item.url for url in urls
.on 'fetchheaders', (item, res) ->
unless res.statusCode is 200
message = "Bad link #{res.statusCode} #{item.url} from #{referrers[item.url]}"
gutil.log gutil.colors.red message
throw new Error message
.on 'complete', done
crawler.timeout = 2000
gulp.task 'mocha', ['build', 'serve:dev'], (done) ->
{spawn} = require 'child_process'
logProcess = require 'process-logger'
extend = require 'extend'
mocha = spawn 'mocha', [
'--compilers', 'coffee:coffee-script/register'
'--reporter', 'spec'
'--timeout', 10000
'test/*.test.coffee'
], env: extend({}, process.env, PORT: settings.port)
.on 'exit', (code) -> done code or null
logProcess mocha, prefix: settings.verbose and '[mocha]' or ''
return null # don't return a stream
gulp.task 'test', ['crawl', 'mocha']
gulp.task 'watch', ->
watch = require 'este-watch'
watch ['src/documents', 'src/files'], (e) ->
gutil.log 'Changed', gutil.colors.cyan e.filepath
gulp.start 'metalsmith'
.start()
gulp.task 'dev', ['build', 'serve:dev', 'watch'], ->
servers.dev.ref()
gulp.task 'open', ['dev'], ->
open = require 'open'
open settings.devServerUrl()
# Fails if there are uncommitted changes
gulp.task 'nochanges', (done) ->
git = require 'gift'
git('.').status (err, status) ->
unless status?.clean
for filename, status of status.files
gutil.log gutil.colors.red "#{status.type} #{filename}"
done new Error 'There are uncommitted changes'
else
done()
# Commits built site to gh-pages branch
release = ({push}={}) ->
(done) ->
git = require 'gift'
end = require 'stream-end'
ghPages = require 'gulp-gh-pages'
git('.').current_commit (err, commit) ->
sourceId = commit.id[...12]
gulp.src 'build/**/*'
.pipe ghPages
cacheDir: './release'
message: "Released from #{sourceId}"
push: push
.pipe end done
gulp.task 'stage', ['build'], release push: false
gulp.task 'publish', ['clean', 'nochanges', 'build', 'test'], release push: true