forked from altdesktop/i3-style
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cakefile
117 lines (94 loc) · 2.85 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
files = [
'lib'
'src'
]
fs = require 'fs'
{print} = require 'util'
{spawn, exec} = require 'child_process'
try
which = require('which').sync
catch err
if process.platform.match(/^win/)?
console.log 'WARNING: the which module is required for windows\ntry: npm install which'
which = null
bold = '\x1b[0;1m'
green = '\x1b[0;32m'
reset = '\x1b[0m'
red = '\x1b[0;31m'
task 'docs', 'generate documentation', -> docco()
task 'build', 'compile source', -> build -> log ":)", green
task 'watch', 'compile and watch', -> build true, -> log ":-)", green
task 'test', 'run tests', -> build -> mocha -> log ":)", green
task 'clean', 'clean generated files', -> clean -> log ";)", green
walk = (dir, done) ->
results = []
fs.readdir dir, (err, list) ->
return done(err, []) if err
pending = list.length
return done(null, results) unless pending
for name in list
file = "#{dir}/#{name}"
try
stat = fs.statSync file
catch err
stat = null
if stat?.isDirectory()
walk file, (err, res) ->
results.push name for name in res
done(null, results) unless --pending
else
results.push file
done(null, results) unless --pending
log = (message, color, explanation) -> console.log color + message + reset + ' ' + (explanation or '')
launch = (cmd, options=[], callback) ->
cmd = which(cmd) if which
app = spawn cmd, options
app.stdout.pipe(process.stdout)
app.stderr.pipe(process.stderr)
app.on 'exit', (status) -> callback?() if status is 0
build = (watch, callback) ->
if typeof watch is 'function'
callback = watch
watch = false
options = ['-c', '-b', '-o' ]
options = options.concat files
options.unshift '-w' if watch
launch 'coffee', options, ->
cliText = fs.readFileSync('./lib/cli.js').toString()
cliText = "#!/usr/bin/env node\n#{cliText}"
fs.writeFile './lib/cli.js', cliText, (err) ->
throw err if err?
fs.chmod './lib/cli.js', '0755', callback
unlinkIfCoffeeFile = (file) ->
if file.match /\.coffee$/
fs.unlink file.replace('src','lib').replace(/\.coffee$/, '.js'), ->
true
else false
clean = (callback) ->
try
for file in files
unless unlinkIfCoffeeFile file
walk file, (err, results) ->
for f in results
unlinkIfCoffeeFile f
callback?()
catch err
moduleExists = (name) ->
try
require name
catch err
log "#{name} required: npm install #{name}", red
false
mocha = (options, callback) ->
#if moduleExists('mocha')
if typeof options is 'function'
callback = options
options = []
# add coffee directive
options.push '--compilers'
options.push 'coffee:coffee-script/register'
options.push '-C'
launch 'mocha', options, callback
docco = (callback) ->
#if moduleExists('docco')
walk 'src', (err, files) -> launch 'docco', files, callback