This repository has been archived by the owner on Jun 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 232
/
Gruntfile.coffee
330 lines (292 loc) · 9.14 KB
/
Gruntfile.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
'use strict'
# Package
# =======
pkg = require './package.json'
banner = """
/*!
* Chaplin #{pkg.version}
*
* Chaplin may be freely distributed under the MIT license.
* For all details and documentation:
* http://chaplinjs.org
*/
"""
umdHead = '''
(function(root, factory) {
if (typeof define === 'function' && define.amd) {
define(['backbone', 'underscore'], factory);
} else if (typeof module === 'object' && module && module.exports) {
module.exports = factory(require('backbone'), require('underscore'));
} else if (typeof require === 'function') {
factory(window.Backbone, window._ || window.Backbone.utils);
} else {
throw new Error('Chaplin requires Common.js or AMD modules');
}
}(this, function(Backbone, _) {
function require(name) {
return {backbone: Backbone, underscore: _}[name];
}
require =
'''
umdTail = '''
return require(1);
}))
'''
setupJSDOM = ->
require('jsdom-global')(undefined,
url: 'https://github.com'
)
setupChai = ->
chai = require 'chai'
chai.use require 'sinon-chai'
require 'chai/register-expect'
module.exports = (grunt) ->
# Configuration
# =============
grunt.initConfig {
# Package
# -------
pkg
coffeelint:
src: 'src/**/*.coffee'
test: 'test/*.coffee'
grunt: 'Gruntfile.coffee'
options:
configFile: 'coffeelint.json'
mochaTest:
native:
options:
timeout: 1e5
reporter: 'spec'
require: [
'coffeescript/register'
'coffee-coverage/register-istanbul'
setupJSDOM
-> require.cache[require.resolve 'jquery'] = {}
'backbone.nativeview'
setupChai
]
src: 'test/*.coffee'
jquery:
options:
timeout: 1e5
reporter: 'spec'
require: [
'coffeescript/register'
setupJSDOM
setupChai
]
src: 'test/*.coffee'
makeReport:
src: 'coverage/coverage-coffee.json',
options:
type: 'html'
dir: 'coverage'
browserify:
dist:
files:
'build/chaplin.js': ['./src/chaplin.coffee']
options: {
banner
external: ['backbone', 'underscore']
transform: ['coffeeify']
browserifyOptions:
debug: true
bare: true
extensions: ['.coffee']
postBundleCB: (err, src, next) ->
if err
next err
else
src = umdHead + src + umdTail
next null, new Buffer src
}
# Minify
# ======
uglify:
options:
mangle: true
universal:
files:
'build/chaplin.min.js': 'build/chaplin.js'
# Compression
# ===========
compress:
files:
src: 'build/chaplin.min.js'
dest: 'build/chaplin.min.js.gz'
transbrute:
docs:
remote: '[email protected]:chaplinjs/chaplin.git'
branch: 'gh-pages'
files: [
{ expand: true, cwd: 'docs/', src: '**/*' }
]
downloads:
message: "Release #{pkg.version}."
tag: pkg.version
tagMessage: "Version #{pkg.version}."
remote: '[email protected]:chaplinjs/downloads.git'
branch: 'gh-pages'
files: [
{
expand: true,
cwd: 'build/',
src: 'chaplin.{js,min.js}'
},
{
dest: 'bower.json',
body: {
name: 'chaplin',
repo: 'chaplinjs/downloads',
version: pkg.version,
main: 'chaplin.js',
scripts: ['chaplin.js'],
dependencies: { backbone: '1.x' }
}
},
{
dest: 'component.json',
body: {
name: 'chaplin',
repo: 'chaplinjs/downloads',
version: pkg.version,
main: 'chaplin.js',
scripts: ['chaplin.js'],
dependencies: { 'bower_components/backbone': '1.x' }
}
},
{
dest: 'package.json',
body: {
name: 'chaplin',
version: pkg.version,
description: 'Chaplin.js',
main: 'chaplin.js',
scripts: { test: 'echo "Error: no test specified" && exit 1' },
repository: {
type: 'git', url: 'git://github.com/chaplinjs/downloads.git'
},
author: 'Chaplin team',
license: 'MIT',
bugs: { url: 'https://github.com/chaplinjs/downloads/issues' },
dependencies: pkg.dependencies
}
}
]
# Watching for changes
# ====================
watch:
coffee:
files: ['src/**/*.coffee', 'test/*.coffee']
tasks: ['test']
}
# Dependencies
# ============
for name of pkg.devDependencies when name.startsWith 'grunt-'
grunt.loadNpmTasks name
# Releasing
# =========
grunt.registerTask 'check:versions:component', 'Check that package.json and bower.json versions match', ->
componentVersion = grunt.file.readJSON('bower.json').version
unless componentVersion is pkg.version
grunt.fail.warn "bower.json is version #{componentVersion}, package.json is #{pkg.version}."
else
grunt.log.ok()
grunt.registerTask 'check:versions:changelog', 'Check that CHANGELOG.md is up to date', ->
# Require CHANGELOG.md to contain "Chaplin VERSION (DIGIT"
changelogMd = grunt.file.read('CHANGELOG.md')
unless RegExp("Chaplin #{pkg.version} \\(\\d").test changelogMd
grunt.fail.warn "CHANGELOG.md does not seem to be updated for #{pkg.version}."
else
grunt.log.ok()
grunt.registerTask 'check:versions:docs', 'Check that package.json and docs versions match', ->
template = grunt.file.read path.join('docs', '_layouts', 'default.html')
match = template.match /^version: ((\d+)\.(\d+)\.(\d+)(?:-[\dA-Za-z\-]*)?)$/m
unless match
grunt.fail.warn "Version missing in docs layout."
docsVersion = match[1]
unless docsVersion is pkg.version
grunt.fail.warn "Docs layout is version #{docsVersion}, package.json is #{pkg.version}."
else
grunt.log.ok()
grunt.registerTask 'check:versions', [
'check:versions:component',
'check:versions:changelog',
'check:versions:docs'
]
grunt.registerTask 'release:git', 'Check context, commit and tag for release.', ->
prompt = require 'prompt'
prompt.start()
prompt.message = prompt.delimiter = ''
prompt.colors = false
# Command/query wrapper, turns description object for `spawn` into runner
command = (desc, message) ->
(next) ->
grunt.log.writeln message if message
grunt.util.spawn desc, (err, result, code) -> next(err)
query = (desc) ->
(next) -> grunt.util.spawn desc, (err, result, code) -> next(err, result)
# Help checking input from prompt. Returns a callback that calls the
# original callback `next` only if the input was as expected
checkInput = (expectation, next) ->
(err, input) ->
unless input and input.question is expectation
grunt.fail.warn "Aborted: Expected #{expectation}, got #{input}"
next()
steps = []
continuation = this.async()
# Check for master branch
steps.push query(cmd: 'git', args: ['rev-parse', '--abbrev-ref', 'HEAD'])
steps.push (result, next) ->
result = result.toString().trim()
if result is 'master'
next()
else
prompt.get([
description: "Current branch is #{result}, not master. 'ok' to continue, Ctrl-C to quit."
pattern: /^ok$/, required: true
],
checkInput('ok', next)
)
# List dirty files, ask for confirmation
steps.push query(cmd: 'git', args: ['status', '--porcelain'])
steps.push (result, next) ->
grunt.fail.warn "Nothing to commit." unless result.toString().length
grunt.log.writeln "The following dirty files will be committed:"
grunt.log.writeln result
prompt.get([
description: "Commit these files? 'ok' to continue, Ctrl-C to quit.",
pattern: /^ok$/, required: true
],
checkInput('ok', next)
)
# Commit
steps.push command(cmd: 'git', args: ['commit', '-a', '-m', "Release #{pkg.version}"])
# Tag
steps.push command(cmd: 'git', args: ['tag', '-a', pkg.version, '-m', "Version #{pkg.version}"])
grunt.util.async.waterfall steps, continuation
grunt.registerTask 'release', [
'check:versions'
'release:git'
'build'
'transbrute:docs'
'transbrute:downloads'
]
# Publish Documentation
# =====================
grunt.registerTask 'docs:publish', ['check:versions:docs', 'transbrute:docs']
# Tests
# =====
grunt.registerTask 'lint', 'coffeelint'
grunt.registerTask 'test', 'mochaTest:native'
grunt.registerTask 'test:jquery', 'mochaTest:jquery'
# Coverage
# ========
grunt.registerTask 'coverage', ['mochaTest:native', 'makeReport']
# Building
# ========
grunt.registerTask 'build', ['browserify', 'uglify', 'compress']
# Default
# =======
grunt.registerTask 'default', ['lint', 'test']