forked from angular/angular-seed
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Cakefile
66 lines (53 loc) · 2.05 KB
/
Cakefile
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
# Cakefile
# Loosely based on the Cakefile at https://github.com/krismolendyke/InstantJasmineCoffee/blob/master/Cakefile
fs = require 'fs'
{exec} = require 'child_process'
util = require 'util'
uglify = require 'uglify-js'
srcDirCoffee = 'app/coffee'
dstDirJS = 'app/js'
dstFileName = 'app'
dstFileCoffee = "#{dstDirJS}/#{dstFileName}.coffee"
dstFileJS = "#{dstDirJS}/#{dstFileName}.js"
dstFileJSMin = "#{dstDirJS}/#{dstFileName}.min.js"
task 'build', 'Build a single minified JavaScript file from the source CoffeeScript files', ->
util.log "Building #{dstFileJS}"
appContents = new Array
coffeeFiles = fs.readdirSync srcDirCoffee
coffeeFiles.sort().reverse()
remaining = coffeeFiles.length
util.log "Concatenating #{coffeeFiles.length} files..."
for file, index in coffeeFiles then do (file, index) ->
fs.readFile "#{srcDirCoffee}/#{file}", 'utf8', (err, fileContents) ->
handleError(err) if err
appContents[index] = fileContents
util.log "[#{index + 1}] #{file}"
process() if (--remaining) is 0
process = ->
# create output dir
try
fs.mkdirSync dstDirJS
catch err
# don't care if it fails (probably exists)
fs.writeFile dstFileCoffee, appContents.join('\n\n'), 'utf8', (err) ->
handleError(err) if err
exec "coffee --output #{dstDirJS}/ --compile #{dstFileCoffee}", (err, stdout, stderr) ->
handleError(err) if err
message = "Compiled #{dstFileJS}"
util.log message
fs.unlink dstFileCoffee, (err) -> handleError(err) if err
invoke 'uglify'
task 'uglify', 'Minify and obfuscate the build JavaScript', ->
fs.readFile dstFileJS, 'utf8', (err, fileContents) ->
minified = uglify.minify fileContents,
fromString: true
mangle : true
fs.writeFile dstFileJSMin, minified.code
#fs.unlink dstFileJS, (err) -> handleError(err) if err
message = "Uglified #{dstFileJSMin}"
util.log message
handleError = (error) ->
util.log error
displayNotification error
displayNotification = (message = '') ->
util.log message