1 | # ** Cakefile Template ** is a Template for a common Cakefile that you may use in a coffeescript nodejs project.
|
2 | #
|
3 | # It comes baked in with 4 tasks:
|
4 | #
|
5 | # * build - compiles your src directory to your lib directory
|
6 | # * watch - watches any changes in your src directory and automatically compiles to the lib directory
|
7 | # * test - runs mocha test framework, you can edit this task to use your favorite test framework
|
8 | # * docs - generates annotated documentation using docco
|
9 | fs = require 'fs'
|
10 | {print} = require 'util'
|
11 | {spawn, exec} = require 'child_process'
|
12 |
|
13 | # ANSI Terminal Colors
|
14 | bold = ''
|
15 | green = ''
|
16 | reset = ''
|
17 | red = ''
|
18 |
|
19 | # Internal Functions
|
20 | #
|
21 | # ## *walk*
|
22 | #
|
23 | # **given** string as dir which represents a directory in relation to local directory
|
24 | # **and** callback as done in the form of (err, results)
|
25 | # **then** recurse through directory returning an array of files
|
26 | walk = (dir, done) ->
|
27 | results = []
|
28 | fs.readdir dir, (err, list) ->
|
29 | return done(err, []) if err
|
30 | pending = list.length
|
31 | return done(null, results) unless pending
|
32 | for name in list
|
33 | file = "#{dir}/#{name}"
|
34 | try
|
35 | stat = fs.statSync file
|
36 | catch err
|
37 | stat = null
|
38 | if stat?.isDirectory()
|
39 | walk file, (err, res) ->
|
40 | results.push name for name in res
|
41 | done(null, results) unless --pending
|
42 | else
|
43 | results.push file
|
44 | done(null, results) unless --pending
|
45 |
|
46 | # ## *log*
|
47 | #
|
48 | # **given** string as a message
|
49 | # **and** string as a color
|
50 | # **and** optional string as an explaination
|
51 | # **then** builds a statement and logs to console.
|
52 | log = (message, color, explanation) -> console.log color + message + reset + ' ' + (explanation or '')
|
53 |
|
54 | # ## *launch*
|
55 | #
|
56 | # **given** string as a cmd
|
57 | # **and** optional array and option flags
|
58 | # **and** optional callback
|
59 | # **then** spawn cmd with options
|
60 | # **and** pipe to process stdout and stderr respectively
|
61 | # **and** on child process exit emit callback if set and status is 0
|
62 | launch = (cmd, options=[], callback) ->
|
63 | app = spawn cmd, options
|
64 | app.stdout.pipe(process.stdout)
|
65 | app.stderr.pipe(process.stderr)
|
66 | app.on 'exit', (status) -> callback?() if status is 0
|
67 |
|
68 | # ## *build*
|
69 | #
|
70 | # **given** optional boolean as watch
|
71 | # **and** optional function as callback
|
72 | # **then** invoke launch passing coffee command
|
73 | # **and** defaulted options to compile src to lib
|
74 | build = (watch, callback) ->
|
75 | if typeof watch is 'function'
|
76 | callback = watch
|
77 | watch = false
|
78 |
|
79 | options = ['-c', '-b', '-o', 'lib', 'src']
|
80 | options.unshift '-w' if watch
|
81 | launch 'coffee', options, callback
|
82 |
|
83 | # ## *mocha*
|
84 | #
|
85 | # **given** optional array of option flags
|
86 | # **and** optional function as callback
|
87 | # **then** invoke launch passing mocha command
|
88 | mocha = (options, callback) ->
|
89 | if typeof options is 'function'
|
90 | callback = options
|
91 | options = []
|
92 |
|
93 | launch 'mocha', options, callback
|
94 |
|
95 | # ## *docco*
|
96 | #
|
97 | # **given** optional function as callback
|
98 | # **then** invoke launch passing docco command
|
99 | docco = (callback) ->
|
100 | walk 'src', (err, files) -> launch 'docco', files, callback
|
101 |
|
102 | # Cakefile Tasks
|
103 | #
|
104 | # ## *docs*
|
105 | #
|
106 | # Generate Annotated Documentation
|
107 | #
|
108 | # <small>Usage</small>
|
109 | #
|
110 | # ```
|
111 | # cake docs
|
112 | # ```
|
113 | task 'docs', 'generate documentation', -> docco()
|
114 |
|
115 | # ## *build*
|
116 | #
|
117 | # Builds Source
|
118 | #
|
119 | # <small>Usage</small>
|
120 | #
|
121 | # ```
|
122 | # cake build
|
123 | # ```
|
124 | task 'build', 'compile source', -> build -> log ":)", green
|
125 |
|
126 | # ## *watch*
|
127 | #
|
128 | # Builds your source whenever it changes
|
129 | #
|
130 | # <small>Usage</small>
|
131 | #
|
132 | # ```
|
133 | # cake watch
|
134 | # ```
|
135 | task 'watch', 'compile and watch', -> build true, -> log ":-)", green
|
136 |
|
137 | # ## *test*
|
138 | #
|
139 | # Runs your test suite.
|
140 | #
|
141 | # <small>Usage</small>
|
142 | #
|
143 | # ```
|
144 | # cake test
|
145 | # ```
|
146 | task 'test', 'run tests', -> build -> mocha -> log ":)", green
|