forked from simboyz/psd.js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCakefile
119 lines (93 loc) · 3.18 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
fs = require 'fs'
{exec} = require 'child_process'
util = require 'util'
{jsmin} = require 'jsmin'
targetName = "psd"
###
CoffeeScript Options
###
csSrcDir = "src"
csTargetDir = "lib"
depsDir = "deps"
targetCoffee = "#{csSrcDir}/build"
targetCoreJS = "#{csTargetDir}/#{targetName}.js"
coffeeCoreOpts = "-r coffeescript-growl -j #{targetName}.js -o #{csTargetDir} -c #{targetCoffee}.coffee"
# All source files listed in include order
coffeeFiles = [
"psd"
"psdcolor"
"psdfile"
"psdheader"
"psdimage"
"psdlayer"
"psdlayermask"
"psdlayereffect"
"psdresource"
"psdtypetool"
"util"
"log"
]
###
Event System
###
finishedCallback = {}
finished = (type) ->
finishedCallback[type]() if finishedCallback[type]?
finishListener = (type, cb) ->
finishedCallback[type] = cb
###
Options
###
option '-z', '--with-zip', 'Include ZIP decompression library'
###
Tasks
###
task 'docs', 'Generates documentation for the coffee files', ->
util.log 'Invoking docco on the CoffeeScript source files'
files = coffeeFiles
files[i] = "#{csSrcDir}/#{files[i]}.coffee" for i in [0...files.length]
exec "docco #{files.join(' ')}", (err, stdout, stderr) ->
util.log err if err
util.log "Documentation built into docs/ folder."
task 'watch', 'Automatically recompile the CoffeeScript files when updated', ->
util.log "Watching for changes in #{csSrcDir}"
for jsFile in coffeeFiles then do (jsFile) ->
fs.watchFile "#{csSrcDir}/#{jsFile}.coffee", (curr, prev) ->
if +curr.mtime isnt +prev.mtime
util.log "#{csSrcDir}/#{jsFile}.coffee updated"
invoke 'build'
task 'build', 'Compile and minify all CoffeeScript source files', ->
finishListener 'js', ->
invoke 'compile'
task 'compile', 'Compile all CoffeeScript source files', (opts) ->
util.log "Building #{targetCoreJS}"
contents = []
remaining = coffeeFiles.length
util.log "Appending #{coffeeFiles.length} files to #{targetCoffee}.coffee"
for file, index in coffeeFiles then do (file, index) ->
fs.readFile "#{csSrcDir}/#{file}.coffee", "utf8", (err, fileContents) ->
util.log err if err
contents[index] = fileContents
util.log "[#{index + 1}] #{file}.coffee"
process() if --remaining is 0
process = ->
contents.unshift "###\nEND DEPENDENCIES\n###\n\n"
deps = fs.readdirSync depsDir
for dep in deps
# Special case
continue if dep is "zip.js" and not opts['with-zip']
util.log "Adding dependency #{dep}"
contents.unshift "`" + fs.readFileSync("#{depsDir}/#{dep}", "utf8") + "`\n\n"
core = contents.join("\n\n")
fs.writeFile "#{targetCoffee}.coffee", core, "utf8", (err) ->
util.log err if err
exec "./node_modules/.bin/coffee #{coffeeCoreOpts}", (err, stdout, stderr) ->
util.log err if err
util.log "Compiled #{targetCoreJS}"
fs.unlink "#{targetCoffee}.coffee"
finished('js')
task 'minify', 'Minify the CoffeeScript files', ->
util.log "Minifying #{targetCoreJS}"
fs.readFile targetCoreJS, "utf8", (err, contents) ->
fs.writeFile targetCoreMinJS, jsmin(contents), "utf8", (err) ->
util.log err if err