This repository has been archived by the owner on Aug 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Cakefile
184 lines (142 loc) · 4.52 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
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
fs = require 'fs'
path = require 'path'
child_process = require 'child_process'
assert = require 'assert'
Browserify = require 'browserify'
UglifyJS = require 'uglify-js'
IS_WIN = /^win/.test process.platform
# coffee
COFFEE_PATH = if IS_WIN then 'coffee.cmd' else 'coffee'
SRC_DIR = path.join __dirname, 'src'
BUILD_DIR = path.join __dirname, 'build'
# browserify
ENTRYPOINT = path.join BUILD_DIR, 'typewriterbuilder.js'
BUNDLE = path.join BUILD_DIR, 'typewriter-bundle.js'
BUNDLE_STANDALONE = path.join BUILD_DIR, 'typewriter-bundle-sa.js'
# uglify
BUNDLE_MIN = path.join BUILD_DIR, 'typewriter-bundle.min.js'
BUNDLE_STANDALONE_MIN = path.join BUILD_DIR, 'typewriter-bundle-sa.min.js'
# mocha
MOCHA_PATH = if IS_WIN then 'mocha.cmd' else 'mocha'
# release
ZIP = path.join BUILD_DIR, 'release.zip'
README = path.join __dirname, 'README.md'
EXAMPLES_DIR = path.join __dirname, 'examples'
walkSync = (dir, fileAction, dirAction) ->
items = fs.readdirSync dir
items.forEach (item) ->
item = path.join dir, item
stat = fs.statSync item
if stat.isDirectory()
walkSync item, fileAction, dirAction
dirAction? item
else
fileAction? item
clean = (cb) ->
walkSync(BUILD_DIR, ((file) => fs.unlink(file)), ((dir) => fs.rmdir(dir)))
cb?()
compile = (cb) ->
p = child_process.spawn COFFEE_PATH,
['-c', '-o', BUILD_DIR, SRC_DIR], { stdio: 'inherit' }
p.on 'close', (code) ->
cb? code
browserify = (standalone, cb) ->
# compile if ENTRYPOINT doesn't exist
if !fs.existsSync(ENTRYPOINT)
compile browserify.bind null, standalone, cb
return;
# standalone arg is optional
if !cb?
cb = standalone
standalone = false
if (standalone)
b = Browserify { standalone: 'typewriter-standalone' }
b.add ENTRYPOINT
rstream = b.bundle()
wstream = fs.createWriteStream BUNDLE_STANDALONE
else
b = Browserify()
b.require ENTRYPOINT, { expose: 'typewriter' }
rstream = b.bundle()
wstream = fs.createWriteStream BUNDLE
rstream.pipe wstream
wstream.on 'finish', cb
browserifyMin = (standalone, cb) ->
# standalone arg is optional
if !cb?
cb = standalone
standalone = false
bundleName = (if standalone then BUNDLE_STANDALONE else BUNDLE)
# browserify if neither BUNDLE_STANDALONE nor BUNDLE exist
if !fs.existsSync bundleName
browserify standalone, browserifyMin.bind null, standalone, cb
return;
result = UglifyJS.minify bundleName
minBundleName = (if standalone then BUNDLE_STANDALONE_MIN else BUNDLE_MIN)
fs.writeFile minBundleName, result.code, cb
test = (cb) ->
p = child_process.spawn MOCHA_PATH,
['--require', 'coffeescript/register', '-R', 'spec', 'test/**/*.{js,coffee}'],
{ stdio: 'inherit' }
p.on 'close', (code) ->
cb? code
all = (cb) ->
compile () ->
browserify () ->
browserify true, () ->
browserifyMin () ->
browserifyMin true, () ->
cb?()
zip = (cb) ->
output = fs.createWriteStream ZIP
output.on 'close', cb
archive = require('archiver')('zip')
archive.on 'error', (err) ->
throw err
archive.pipe output
toRelativePath = (p) ->
return p.substring __dirname.length + 1
addFile = (fileName) ->
archive.append fs.createReadStream(fileName),
name: toRelativePath fileName
addDirectory = (dirName) ->
walkSync dirName, addFile
addFile README
addFile BUNDLE
addFile BUNDLE_MIN
addFile BUNDLE_STANDALONE
addFile BUNDLE_STANDALONE_MIN
addDirectory EXAMPLES_DIR
archive.finalize (err, bytes) ->
throw err if err?
task 'clean', \
'Recursively Removes all files and directories in build directory', () ->
clean () ->
console.log 'Done.'
task 'compile', 'Compiles CoffeeScript source files', () ->
compile () ->
console.log 'Done.'
task 'browserify', 'Makes a Browserify bundle', () ->
browserify () ->
console.log 'Done.'
task 'browserify-sa', 'Makes a standalone Browserify bundle', () ->
browserify true, () ->
console.log 'Done.'
task 'browserify-min', 'Makes a minified Browserify bundle', () ->
browserifyMin () ->
console.log 'Done.'
task 'browserify-sa-min', 'Makes a minified standalone Browserify' +
'bundle', () ->
browserifyMin true, () ->
console.log 'Done.'
task 'all', 'Compiles CoffeeScript source files and makes all targets', () ->
all () ->
console.log 'Done.'
task 'release', 'Creates a release zip', () ->
all () ->
zip () ->
console.log 'Done.'
task 'test', 'Runs tests', () ->
test()
task 'doc', 'Generates documentation', () ->
throw new Error("Not Implemented");