-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathindex.js
165 lines (134 loc) · 3.73 KB
/
index.js
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
var through = require('through')
var duplex = require('duplexer')
var path = require('path')
var fs = require('fs')
var bl = require('bl')
var versions = require('./lib/versions')
var jsonTree = require('./json-tree')
module.exports = createStream
createStream.bundle = bundle
function createStream(opts) {
opts = opts || {}
var buffer = bl(function(err, content) {
if (err) return stream.emit('error', err)
bundle(content, opts, function(err, html) {
if (err) return stream.emit('error', err)
output.queue(html)
output.queue(null)
})
})
var output = through()
var stream = duplex(buffer, output)
return stream
}
function parseBundle(bundles) {
if (!bundles) {
return null;
}
bundles = bundles && bundles[0]
if (bundles) {
bundles = JSON.parse(bundles.toString())
}
return bundles;
}
function getStats(bundles) {
if (Array.isArray(bundles)) {
return parseBundle(bundles)
} else {
throw new Error("**** Pass the stats.json as per instructions electrify -h ****")
}
}
function bundle(bundles, opts, callback) {
if (typeof opts === 'function') {
callback = opts
opts = {}
}
opts = opts || {}
callback = callback || noop
var header = opts.header || opts.button || ''
var footer = opts.footer || ''
var data = {};
data.mode = opts.mode || 'size'
var statsContainer =
`<div id="statsDropBox" class="statsDropBox">
<div class="innerDropBox">
<input type="file" id="fileInput" class="hiddenFileInput"/>
<img class="uploadImg" src="img/file_upload.png"/>
<div class='uploadMsg'>
<button>Choose a file</button>
<span class="box__dragndrop"> or drag it here.</span>
</div>
</div>
</div>
<div id="visualizations">
<div class="rightColumn">
<h1>Assets</h1>
<div class="assets"></div>
</div>
<div class="leftColumn">
<h1>Modules</h1>
<div class="selectors">
<input
type="text"
id="search"
placeholder="Search File ..."
class="search-box"
style="opacity: 0"
>
<div class="modes">
<ul class="scale-list"></ul>
</div>
<div class="palette-wrap"></div>
</div>
<div class="chart"></div>
</div>
</div>`
if(bundles.length){
data = '<script type="text/javascript">'
+ ';window.electrify = ('
+ JSON.stringify(jsonTree(getStats(bundles)))
+ ');</script>'
}
var scripts = '<script type="text/javascript">'
+ bundled().replace(/\/script/gi, '\\/script')
+ '</script>'
return callback(null, template()({
scripts
, statsContainer
, styles: styles()
, markdown: footer
, header
, data
}))
}
function template() {
if (template.text) return template.text
return template.text = require('./lib/lazy-template')(
fs.readFileSync(__dirname + '/src/base.html', 'utf8')
)
}
function styles() {
if (styles.text) return styles.text
return styles.text = fs.readFileSync(__dirname + '/build/style.css', 'utf8')
}
function bundled() {
if (bundled.text) return bundled.text
return bundled.text = fs.readFileSync(__dirname + '/build/bundle.js', 'utf8')
}
function dirsizes(child) {
return child.size = "size" in child ? child.size : child.children.reduce(function(size, child) {
return size + ("size" in child ? child.size : dirsizes(child))
}, 0)
}
function noop(){}
function values(object) {
return Object.keys(object).map(function(key) {
return object[key]
})
}
function isEmpty(module) {
return (
path.basename(module.identifier) === '_empty.js' &&
(!fs.existsSync(module.identifier) || !fs.statSync(module.identifier).size)
)
}