1 |
|
2 |
|
3 |
|
4 | child_proc = require 'child_process'
|
5 |
|
6 | async = require 'async'
|
7 | colors = require 'colors'
|
8 |
|
9 |
|
10 | colors.setTheme
|
11 | silly: 'rainbow'
|
12 | input: 'grey'
|
13 | verbose: 'cyan'
|
14 | prompt: 'grey'
|
15 | info: 'green'
|
16 | data: 'grey'
|
17 | help: 'cyan'
|
18 | warn: 'yellow'
|
19 | debug: 'blue'
|
20 | error: 'red'
|
21 |
|
22 | class Utils
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 | @print: (data) ->
|
29 | data = (data ? "").toString().replace /[\r\n]+$/, ""
|
30 | console.log data if data
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 | @printOnError: (err) =>
|
37 | @print err ? "Done."
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 | @fail: (msg) ->
|
44 | process.stderr.write "#{msg}\n".error.bold if msg
|
45 | process.exit 1
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 | @spawn: (cmd, args = [], callback = null) =>
|
54 | @print [cmd, args.join " "].join " "
|
55 | ps = child_proc.spawn cmd, args
|
56 | ps.stdout.pipe process.stdout
|
57 | ps.stderr.pipe process.stderr
|
58 | ps.on "exit", callback if callback
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 | @exec: (cmd, callback = @print) =>
|
66 | @print cmd
|
67 | child_proc.exec cmd, (error, stdout, stderr) ->
|
68 | process.stderr.write stderr if stderr
|
69 | callback stdout.toString()
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 | @pids: (pattern, callback = @print) =>
|
77 | @exec "ps ax | egrep \"#{pattern}\" | egrep -v egrep", (matches) ->
|
78 | matches = matches?.split("\n") ? []
|
79 | callback (m.match(/\s*([0-9]+)/)[0] for m in matches when m)
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 | @find: (dirs = ["./"], pattern, callback = @print) =>
|
88 | finder = (dir, cb) =>
|
89 | @exec "find \"#{dir}\" -name \"#{pattern}\"", (files) ->
|
90 | files = files?.split("\n") ? []
|
91 | cb null, (f for f in files when f)
|
92 |
|
93 | async.map dirs, finder, (err, results) =>
|
94 | @fail err if err
|
95 |
|
96 |
|
97 | files = []
|
98 | if not err
|
99 | for collection in results
|
100 | files = files.concat collection
|
101 |
|
102 | callback files
|
103 |
|
104 | class Style
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 | @coffeelint: (paths = [], opts = {}, callback = Utils.printOnError) ->
|
115 | suffix = opts.suffix ? "coffee"
|
116 | filesRe = new RegExp ".*\.#{suffix}$"
|
117 | isCs = (name) -> name is "Cakefile" or filesRe.test name
|
118 |
|
119 | config = if opts.config then ["--file", opts.config] else []
|
120 | files = (f for f in paths when isCs f)
|
121 | dirs = (f for f in paths when not isCs f)
|
122 |
|
123 | cbs =
|
124 | searchDirs: (cb) ->
|
125 | if dirs.length > 0
|
126 | Utils.find dirs, "*.#{suffix}", (dirFiles) ->
|
127 | cb null, dirFiles
|
128 |
|
129 | else
|
130 |
|
131 | cb null, []
|
132 |
|
133 | runLint: ["searchDirs", (cb, results) ->
|
134 | dirFiles = results.searchDirs
|
135 | args = files.concat(dirFiles).concat(config)
|
136 | Utils.spawn "coffeelint", args, (code) =>
|
137 | err = if code is 0 then null else new Error "coffeelint failed"
|
138 | ]
|
139 |
|
140 | async.auto cbs, (err) ->
|
141 | Utils.fail "CoffeeScript style checks failed." if err
|
142 | Utils.print "CoffeeScript style checks passed.\n".info
|
143 | callback()
|
144 |
|
145 | module.exports =
|
146 | utils: Utils
|
147 | style: Style
|