UNPKG

4.37 kBtext/coffeescriptView Raw
1####
2# CakePop!
3####
4child_proc = require 'child_process'
5
6async = require 'async'
7colors = require 'colors'
8
9# Colors configuration
10colors.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
22class Utils
23
24 # Log to console if non-empty string.
25 #
26 # @param [String] data String to print.
27 #
28 @print: (data) ->
29 data = (data ? "").toString().replace /[\r\n]+$/, ""
30 console.log data if data
31
32 # Print "done" or error.
33 #
34 # @param [Object] err Error.
35 #
36 @printOnError: (err) =>
37 @print err ? "Done."
38
39 # Log failure and exit process.
40 #
41 # @param [String] msg Failure message.
42 #
43 @fail: (msg) ->
44 process.stderr.write "#{msg}\n".error.bold if msg
45 process.exit 1
46
47 # Spawn with log pipes to stdout, stderr.
48 #
49 # @param [String] cmd Command / binary path.
50 # @param [Array<String>] args Array of arguments to command.
51 # @param [Function] callback Callback on process end (or null).
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 # Exec with log hooks to stdout, stderr.
61 #
62 # @param [String] cmd Command and arguments.
63 # @param [Function] callback Callback on process end (default: print).
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 # Return list of process id's matching egrep pattern.
72 #
73 # @param [String] pattern Egrep pattern.
74 # @param [Function] callback Callback on process end (default: print).
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 # Return list of files matching egrep pattern.
82 #
83 # @param [Array<String>] dirs Array of directories (default: ["./"]).
84 # @param [String] pattern Egrep pattern.
85 # @param [Function] callback Callback on process end (default: print).
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 # Merge arrays
97 files = []
98 if not err
99 for collection in results
100 files = files.concat collection
101
102 callback files
103
104class Style
105
106 # Run coffeelint on an array of files, directory paths.
107 #
108 # @param [Array<String>] paths Array of file / directory paths.
109 # @param [Object] opts Options.
110 # @option options [String] suffix CoffeeScript file suffix ("coffee").
111 # @option options [String] config Path to coffeelint config file.
112 # @param [Function] callback Callback on process end (or null).
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 # No directories to search.
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
145module.exports =
146 utils: Utils
147 style: Style