-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCakefile
More file actions
75 lines (63 loc) · 2.41 KB
/
Cakefile
File metadata and controls
75 lines (63 loc) · 2.41 KB
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
fs = require 'fs'
util = require 'util'
require 'colors'
{exec} = require 'child_process'
{basename, join, existsSync} = require 'path'
srcCoffeeDir = 'src'
targetJsDir = 'lib'
outputFileName = 'finance'
coffeeOpts = "--bare --output #{targetJsDir} --compile #{srcCoffeeDir}"
coffeeOptsBare = "--bare"
coffeeOptWatch = "--watch"
appFiles = []
# Compile src/*.coffee to lib/*.js
task 'coffeeFiles', 'How many coffee scripts do we have?', ->
traverseFileSystem = (currentPath) ->
files = fs.readdirSync currentPath
for file in files
do (file) ->
currentFile = currentPath + '/' + file
stats = fs.statSync(currentFile)
if stats.isFile() and currentFile.indexOf('.coffee') > 1 and appFiles.join('=').indexOf("#{currentFile}=") < 0
appFiles.push currentFile
else if stats.isDirectory()
traverseFileSystem currentFile
traverseFileSystem "#{srcCoffeeDir}"
util.log "#{appFiles.length} coffee files found. Combining and compiling...".cyan
return appFiles
task 'build', 'Build single application file from source files', ->
invoke 'coffeeFiles'
appContents = new Array
remaining = appFiles.length
for file, index in appFiles then do (file, index) ->
fs.readFile file, 'utf8', (err, fileContents) ->
throw err if err
appContents[index] = fileContents
process() if --remaining is 0
process = ->
fs.writeFile outputFileName+".coffee", appContents.join('\n\n'), 'utf8', (err) ->
throw err if err
exec "coffee --bare --compile #{outputFileName+'.coffee'}", (err, stdout, stderr) ->
if err
util.log 'Error compiling coffee file.'.bold.red
else
fs.unlink outputFileName+".coffee", (err) ->
if err
util.log "Could't delete the #{outputFileName}.coffee file.".bold.yellow
util.log 'finance.js built successfully!'.green
util.log 'Producing minified version...'.cyan
invoke 'minify'
util.log 'Minified!'.green
task 'watch', 'Watch source files and build changes', ->
invoke 'build'
util.log "Watching for changes in #{srcCoffeeDir}".cyan
for file in appFiles then do (file) ->
fs.watchFile file, (curr, prev) ->
if +curr.mtime isnt +prev.mtime
util.log "Saw change in #{file}".cyan
invoke 'build'
# Not as elaborate as it could be, will come back to later
task 'minify', 'Call uglify-js on finance.js', ->
mainExists = existsSync './finance.js'
if mainExists
exec './node_modules/uglify-js/bin/uglifyjs -o finance_min.js finance.js'