-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.coffee
92 lines (69 loc) · 2.29 KB
/
test.coffee
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
log = require( "logging" ).from __filename
async = require "async"
find = require "find"
util = require "util"
fs = require "fs"
coffee_script = require "coffee-script"
directories = [ "./" ]
async.map directories, ( directory, cb ) ->
find.file /\.coffee/, directory, ( files ) ->
async.map files, ( file, cb ) ->
fs.readFile file, { encoding: "utf8" }, ( err, data ) ->
if err
return cb err
nodes = coffee_script.nodes data
type_counter = { }
depths = [ ]
depth = 0
recursive_scan = ( node ) ->
# We're starting a particular node, increment the
# current depth
depth++
# Note that we're on this depth.
depths.push depth
# Get what type this node is..
_type = node.constructor.name
# Increment or set the type counter for the given
# type..
if not type_counter[_type]?
type_counter[_type] = 1
else
type_counter[_type] += 1
# Recruse down the tree.
node.eachChild recursive_scan
# We hit the end of the node.. decrement the current depth.
depth--
# Scan the nodes!
recursive_scan coffee_script.nodes data
# Calculate the average depth as well as the max.
sum = 0
sum += depth for depth in depths
average_depth = Math.round( sum / depths.length )
max_depth = Math.max.apply( null, depths )
# Grab the number of lines both coffeescript and js.
num_cs_lines = data.split("\n").length
num_js_lines = coffee_script.compile( data ).split( "\n" ).length
# Determine the ratio of cs to js lines.
explode_ratio = num_js_lines / num_cs_lines
# Define the object we'll contiue to abuse until we return it.
_o = { "file": file, \
"ops": type_counter,
"max_depth": max_depth,
"average_depth": average_depth,
"num_cs_lines": num_cs_lines,
"num_js_lines": num_js_lines,
"explode_ratio": explode_ratio }
# At this point also compute each operation per line.
for op, c of type_counter
_o[op + "_pl"] = c / num_cs_lines
# Return the object.
return cb null, _o
, cb
, ( err, res ) ->
if err
log "Fatal error: #{err}"
process.exit 1
# Just collapse the response arrays
_res = [ ]
_res.push file_detail for file_detail in detail_array for detail_array in res
log "_res is #{util.inspect _res}"