forked from docpad/docpad-plugin-babel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cakefile
272 lines (224 loc) · 8.22 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# February 2, 2015
# https://github.com/bevry/base
# =====================================
# Imports
fsUtil = require('fs')
pathUtil = require('path')
# =====================================
# Variables
WINDOWS = process.platform.indexOf('win') is 0
NODE = process.execPath
NPM = (if WINDOWS then process.execPath.replace('node.exe', 'npm.cmd') else 'npm')
EXT = (if WINDOWS then '.cmd' else '')
GIT = "git"
APP_PATH = process.cwd()
PACKAGE_PATH = pathUtil.join(APP_PATH, "package.json")
PACKAGE_DATA = require(PACKAGE_PATH)
MODULES_PATH = pathUtil.join(APP_PATH, "node_modules")
DOCPAD_PATH = pathUtil.join(MODULES_PATH, "docpad")
CAKE = pathUtil.join(MODULES_PATH, "coffee-script/bin/cake")
COFFEE = pathUtil.join(MODULES_PATH, "coffee-script/bin/coffee")
PROJECTZ = pathUtil.join(MODULES_PATH, "projectz/bin/projectz")
DOCCO = pathUtil.join(MODULES_PATH, "docco/bin/docco")
DOCPAD = pathUtil.join(MODULES_PATH, "docpad/bin/docpad")
BISCOTTO = pathUtil.join(MODULES_PATH, "biscotto/bin/biscotto")
config = {}
config.TEST_PATH = "test"
config.DOCCO_SRC_PATH = null
config.DOCCO_OUT_PATH = "docs"
config.BISCOTTO_SRC_PATH = null
config.BISCOTTO_OUT_PATH = "docs"
config.COFFEE_SRC_PATH = null
config.COFFEE_OUT_PATH = "out"
config.DOCPAD_SRC_PATH = null
config.DOCPAD_OUT_PATH = "out"
for own key,value of (PACKAGE_DATA.cakeConfiguration or {})
config[key] = value
#for own key,value of config
# config[key] = pathUtil.resolve(APP_PATH, value) if value
# ^ causes issues with biscotto, as it just wants relative paths
# =====================================
# Generic
child_process = require('child_process')
spawn = (command, args, opts) ->
if opts.output is true
console.log(command, args.join(' '))
opts.stdio = 'inherit'
return child_process.spawn(command, args, opts)
exec = (command, opts, next) ->
if opts.output is true
console.log(command)
return child_process.exec command, opts, (err, stdout, stderr) ->
console.log(stdout)
console.log(stderr)
next()
else
return child_process.exec(command, opts, next)
safe = (next,fn) ->
next ?= (err) -> console.log(err.stack ? err)
fn ?= next # support only one argument
return (err) ->
# success status code
if err is 0
err = null
# error status code
else if err is 1
err = new Error('Process exited with error status code')
# Error
return next(err) if err
# Continue
return fn()
finish = (err) ->
throw err if err
console.log('OK')
# =====================================
# Actions
actions =
clean: (opts,next) ->
# Prepare
(next = opts; opts = {}) unless next?
# Add compilation paths
args = ['-Rf', config.COFFEE_OUT_PATH, config.DOCPAD_OUT_PATH, config.DOCCO_OUT_PATH]
# Add common ignore paths
for path in [APP_PATH, config.TEST_PATH]
args.push(
pathUtil.join(path, 'build')
pathUtil.join(path, 'components')
pathUtil.join(path, 'bower_components')
pathUtil.join(path, 'node_modules')
pathUtil.join(path, '*out')
pathUtil.join(path, '*log')
)
# rm
console.log('\nclean:')
spawn('rm', args, {output:true, cwd:APP_PATH}).on('close', safe next)
install: (opts,next) ->
# Prepare
(next = opts; opts = {}) unless next?
# Steps
step1 = ->
console.log('\nnpm install (for app):')
spawn(NPM, ['install'], {output:true, cwd:APP_PATH}).on('close', safe next, step2)
step2 = ->
return step3() if !config.TEST_PATH or !fsUtil.existsSync(config.TEST_PATH)
console.log('\nnpm install (for test):')
spawn(NPM, ['install'], {output:true, cwd:config.TEST_PATH}).on('close', safe next, step3)
step3 = ->
return step4() if !fsUtil.existsSync(DOCPAD_PATH)
console.log('\nnpm install (for docpad tests):')
spawn(NPM, ['install'], {output:true, cwd:DOCPAD_PATH}).on('close', safe next, step4)
step4 = next
# Start
step1()
compile: (opts,next) ->
# Prepare
(next = opts; opts = {}) unless next?
# Steps
step1 = ->
console.log('\ncake install')
actions.install(opts, safe next, step2)
step2 = ->
return step3() if !config.COFFEE_SRC_PATH or !fsUtil.existsSync(COFFEE)
console.log('\ncoffee compile:')
spawn(NODE, [COFFEE, '-co', config.COFFEE_OUT_PATH, config.COFFEE_SRC_PATH], {output:true, cwd:APP_PATH}).on('close', safe next, step3)
step3 = ->
return step4() if !config.DOCPAD_SRC_PATH or !fsUtil.existsSync(DOCPAD)
console.log('\ndocpad generate:')
spawn(NODE, [DOCPAD, 'generate'], {output:true, cwd:APP_PATH}).on('close', safe next, step4)
step4 = next
# Start
step1()
watch: (opts,next) ->
# Prepare
(next = opts; opts = {}) unless next?
# Steps
step1 = ->
console.log('\ncake install')
actions.install(opts, safe next, step2)
step2 = ->
return step3() if !config.COFFEE_SRC_PATH or !fsUtil.existsSync(COFFEE)
console.log('\ncoffee watch:')
spawn(NODE, [COFFEE, '-wco', config.COFFEE_OUT_PATH, config.COFFEE_SRC_PATH], {output:true, cwd:APP_PATH}).on('close', safe) # background
step3() # continue while coffee runs in background
step3 = ->
return step4() if !config.DOCPAD_SRC_PATH or !fsUtil.existsSync(DOCPAD)
console.log('\ndocpad run:')
spawn(NODE, [DOCPAD, 'run'], {output:true, cwd:APP_PATH}).on('close', safe) # background
step4() # continue while docpad runs in background
step4 = next
# Start
step1()
test: (opts,next) ->
# Prepare
(next = opts; opts = {}) unless next?
# Steps
step1 = ->
console.log('\ncake compile')
actions.compile(opts, safe next, step2)
step2 = ->
console.log('\nnpm test:')
spawn(NPM, ['test'], {output:true, cwd:APP_PATH}).on('close', safe next, step3)
step3 = next
# Start
step1()
prepublish: (opts,next) ->
# Prepare
(next = opts; opts = {}) unless next?
# Steps
step1 = ->
console.log('\ncake compile')
actions.compile(opts, safe next, step2)
step2 = ->
return step3() if !fsUtil.existsSync(PROJECTZ)
console.log('\nprojectz compile')
spawn(NODE, [PROJECTZ, 'compile'], {output:true, cwd:APP_PATH}).on('close', safe next, step3)
step3 = ->
return step4() if !config.DOCCO_SRC_PATH or !fsUtil.existsSync(DOCCO)
console.log('\ndocco compile:')
exec("#{NODE} #{DOCCO} -o #{config.DOCCO_OUT_PATH} #{config.DOCCO_SRC_PATH}", {output:true, cwd:APP_PATH}, safe next, step4)
step4 = ->
return step5() if !config.BISCOTTO_SRC_PATH or !fsUtil.existsSync(BISCOTTO)
console.log('\nbiscotto compile:')
exec("""#{BISCOTTO} -n #{PACKAGE_DATA.title or PACKAGE_DATA.name} --title "#{PACKAGE_DATA.title or PACKAGE_DATA.name} API Documentation" -r README.md -o #{config.BISCOTTO_OUT_PATH} #{config.BISCOTTO_SRC_PATH} - LICENSE.md HISTORY.md""", {output:true, cwd:APP_PATH}, safe next, step5)
step5 = ->
console.log('\ncake test')
actions.test(opts, safe next, step6)
step6 = next
# Start
step1()
publish: (opts,next) ->
# Prepare
(next = opts; opts = {}) unless next?
# Steps
step1 = ->
console.log('\ncake prepublish')
actions.prepublish(opts, safe next, step2)
step2 = ->
console.log('\nnpm publish:')
spawn(NPM, ['publish'], {output:true, cwd:APP_PATH}).on('close', safe next, step3)
step3 = ->
console.log('\ngit tag:')
spawn(GIT, ['tag', 'v'+PACKAGE_DATA.version, '-a'], {output:true, cwd:APP_PATH}).on('close', safe next, step4)
step4 = ->
console.log('\ngit push origin master:')
spawn(GIT, ['push', 'origin', 'master'], {output:true, cwd:APP_PATH}).on('close', safe next, step5)
step5 = ->
console.log('\ngit push tags:')
spawn(GIT, ['push', 'origin', '--tags'], {output:true, cwd:APP_PATH}).on('close', safe next, step6)
step6 = next
# Start
step1()
# =====================================
# Commands
commands =
clean: 'clean up instance'
install: 'install dependencies'
compile: 'compile our files (runs install)'
watch: 'compile our files initially, and again for each change (runs install)'
test: 'run our tests (runs compile)'
prepublish: 'prepare our package for publishing'
publish: 'publish our package (runs prepublish)'
Object.keys(commands).forEach (key) ->
description = commands[key]
fn = actions[key]
task key, description, (opts) -> fn(opts, finish)