-
Notifications
You must be signed in to change notification settings - Fork 30
/
Cakefile
138 lines (116 loc) · 4.02 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
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
{exec} = require 'child_process'
# exec = require "exec-sync"
fs = require 'fs'
DIST_BASE = "dist"
SRC_BASE = "core"
COFFEE_SRC = "#{SRC_BASE}/scripts"
LESS_SRC = "#{SRC_BASE}/styles"
JS_DIST = "#{DIST_BASE}/scripts"
CSS_DIST = "#{DIST_BASE}/styles"
MEDIA_LIST = ["icons", "images", "dialogs/*", "partials/*", "manifest.json", "index.html", "sandbox.html"]
lastChange = {}
copyFile = (src, dist) ->
console.log "copy #{src} to #{dist}"
exec("cp -a #{src} #{dist}")
handleMedia = (fn) ->
for item in MEDIA_LIST
if item[item.length - 1] == '*' # files in the dir
item = item.substring(0, item.length - 1)
files = fs.readdirSync("#{SRC_BASE}/#{item}")
for file in files
fullpath = "#{SRC_BASE}/#{item}/#{file}"
# st = fs.statSync(fullpath)
# if st.isDirectory() # ignore directory
# continue
fn(fullpath, "#{DIST_BASE}/#{item}/#{file}")
else
fn("#{SRC_BASE}/#{item}", "#{DIST_BASE}/")
handleCoffeeFiles = (fn)->
if not fs.existsSync(JS_DIST)
fs.mkdirSync(JS_DIST)
fs.readdir(COFFEE_SRC, (err, files) ->
for file in files
file = "#{COFFEE_SRC}/#{file}"
fn file
)
handleLessFiles = (fn)->
if not fs.existsSync(CSS_DIST)
fs.mkdirSync(CSS_DIST)
fs.readdir(LESS_SRC, (err, files) ->
for file in files
file = "#{LESS_SRC}/#{file}"
fn file
)
compileCoffee = (file) =>
if file.lastIndexOf('.coffee') == file.length - 1 - 'coffee'.length
console.log "Compile coffee", file
exec "coffee -c -o #{JS_DIST} #{file}", (err, stdout, stderr) ->
return console.error err if err
console.log "Compiled #{file}"
else
_file = file.split('/')
_file = _file[_file.length - 1]
console.log "copy only", file
copyFile(file, "#{JS_DIST}/#{_file}")
compileLess = (file) =>
_file = file.split('/')
_file = _file[_file.length - 1]
if file.lastIndexOf('.less') == file.length - 1 - 'less'.length
console.log "Compile less", file
exec "lessc #{file} #{CSS_DIST}/#{_file.replace('.less', '.css')}", (err, stdout, stderr) ->
return console.error err if err
console.log "Compiled #{file}"
else
console.log "copy only", file
copyFile(file, "#{CSS_DIST}/#{_file}")
watchFile = (file, fn) ->
lastChange[file] = 0
try
fs.watch file, (event, filename) ->
return if event isnt 'change'
# ignore repeated event misfires
fn file if Date.now() - lastChange[file] > 1000
lastChange[file] = Date.now()
catch e
console.log "Error watching #{file}"
watchCoffee = (file) ->
watchFile file, compileCoffee
watchLess = (file) ->
watchFile file, compileLess
watchMedia = (src, dist) ->
lastChange[src] = 0
try
fs.watch src, (event, filename) =>
return if event isnt 'change'
# ignore repeated event misfires
copyFile src, dist if Date.now() - lastChange[src] > 1000
lastChange[src] = Date.now()
catch e
console.log "Error watching #{src}", e
build = ->
if not fs.existsSync(DIST_BASE)
fs.mkdirSync(DIST_BASE)
handleMedia(copyFile)
handleCoffeeFiles(compileCoffee)
handleLessFiles(compileLess)
compressScripts = ->
fs.readdir(JS_DIST, (err, files) ->
for file in files
file = "#{JS_DIST}/#{file}"
exec("uglifyjs -o #{file} #{file}")
)
compressStyles = ->
fs.readdir(CSS_DIST, (err, files) ->
for file in files
file = "#{CSS_DIST}/#{file}"
exec("cleancss -o #{file} #{file}")
)
task 'sbuild', '', ->
build()
task 'watch', 'Compile + watch *.coffee and *.less', ->
handleCoffeeFiles watchCoffee
handleLessFiles watchLess
handleMedia watchMedia
task 'compress', 'Compress scripts and css files', ->
compressScripts()
compressStyles()