forked from jmrocela/munchjs
-
Notifications
You must be signed in to change notification settings - Fork 2
/
munch.js
942 lines (786 loc) · 24.4 KB
/
munch.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
/**
* MUNCH.js
* http://jmrocela.github.com/munchjs
*
* munch.js is a utility that rewrites classes and ids in CSS, HTML, and JavaScript files
* in order to save precious bytes and obfuscate your code.
*
* Copyright (c) 2013 John Rocela
* Licensed under the MIT license.
* https://github.com/gruntjs/grunt/blob/master/LICENSE-MIT
*/
'use strict';
var path = require('path'),
glob = require('glob'),
fs = require('fs'),
jsdom = require('jsdom').jsdom,
$ = require('jquery'),
clc = require('cli-color'),
parse = require('css-parse'),
Hashids = require('hashids'),
hashids = new Hashids("use the force harry", 8),
serializeDocument = require("jsdom").serializeDocument;;
/**
* MUNCHER!!!!
*
* @param args Object an optimist.args object.
*/
var Muncher = function (args) {
// tokens from files within views, css and js together
this.map = {
"id": {},
"class": {}
};
// files, keep track of sizes
this.files = {}
// token counter
this.mapCounter = 0;
// ignore classes
this.ignoreClasses = [];
this.ignoreIds = [];
// custom parser collection
this.parsers = {
"js": []
}
this.writers = {
"js": []
}
this.postfix = '.munched';
this.prefix = '';
// pass to the init function
if (args) {
this.init(args);
}
}
Muncher.prototype.init = function (args) {
// a reference to `this`
var that = this;
// set the ignore maps for Ids and Classes
this.ignore = args['ignore'] || '';
this.ignore.split(',').forEach(function (ign) {
ign = ign.replace(/\s/, '');
if (ign.indexOf('.') === 0) that.ignoreClasses.push(ign.replace('.', ''));
if (ign.indexOf('#') === 0) that.ignoreIds.push(ign.replace('#', ''));
});
// set the default view extension
this.extensions = {
"view": args['view-ext'] || '.html',
"css": args['css-ext'] || '.css',
"js": args['js-ext'] || '.js'
}
// you may want to use another way for compressing CSS and JS
this.compress = {
"view": (args['compress-view'] === true),
"css": (args['compress-css'] === true),
"js": (args['compress-js'] === true)
}
// if we want to nag the CLI
this.silent = (args['silent'] === true);
this.showSavings = (args['show-savings'] === true);
// flag if we only want to map the Ids and Classes for later use
this.mapFile = args['map'];
this.readFile = args['read'];
// paths given from the configuration
this.paths = {
"view": args['view'],
"css": args['css'],
"js": args['js']
}
this.postfix = typeof args['postfix'] != undefined ? args['postfix'] : this.postfix;
this.prefix = typeof args['prefix'] != undefined ? args['prefix'] : this.prefix;
// chainable
return this;
}
/**
* run
*
* sets various options to run the Muncher
*/
Muncher.prototype.run = function () {
if (!this.readFile) {
// run through the HTML files
if (this.paths["view"]) this.parseDir(this.paths["view"], "view");
if (this.paths['css']) this.parseDir(this.paths["css"], "css");
if (this.paths['js']) this.parseDir(this.paths["js"], "js");
} else {
this.read(this.readFile);
}
// map feature
if (typeof this.mapFile === 'string' && !this.readFile) {
if (this.mapFile) {
var map = {id: [], class: []};
for (var key in this.map["id"]) {
map["id"].push(key);
}
for (var key in this.map["class"]) {
map["class"].push(key);
}
fs.writeFileSync(this.mapFile, JSON.stringify(map, null, '\t'));
}
this.echo('-------------------------------');
this.echo(clc.bold('Wrote ' + this.mapCounter + ' ids and classes in ' + this.mapFile));
this.echo('-------------------------------\n');
return;
} else {
this.echo('-------------------------------');
this.echo(clc.bold('Mapped ' + this.mapCounter + ' IDs and Classes'));
this.echo('-------------------------------\n');
}
// we do it again so this we are sure we have everything we need
if (this.paths["view"]) this.buildDir(this.paths["view"], "view");
if (this.paths['css']) this.buildDir(this.paths["css"], "css");
if (this.paths['js']) this.buildDir(this.paths["js"], "js");
}
/**
* read
*
* use a map file to serve as the dictionary
*
* @param read String the path to the mapfile
*/
Muncher.prototype.read = function (read) {
var manifest = JSON.parse(fs.readFileSync(read, 'utf-8').toString()),
that = this;
$.each(manifest["id"], function (i, id) {
that.addId(id);
});
$.each(manifest["class"], function (i, cls) {
that.addClass(cls);
});
}
/**
* parseDir
*
* parse directories according to context
*
* @param path String the path of the directory
* @param context String either view, js or css
*/
Muncher.prototype.parseDir = function (path, context) {
var that = this;
that.echo(clc.bold('Processing ' + context));
that.paths[context].split(',').forEach(function (path) {
if (fs.statSync(path).isDirectory()) {
var files = glob.sync(path.replace(/\/$/, '') + '/**/*' + that.extensions[context]);
files.forEach(function (file) {
that.parse(file, context);
});
} else if (fs.statSync(path).isFile()) {
that.parse(path, context);
}
});
that.echo(clc.green.bold('Finished!\n'));
}
/**
* buildDir
*
* build directories according to context
*
* @param path String the path of the directory
* @param context String either view, js or css
*/
Muncher.prototype.buildDir = function (path, context) {
var that = this;
that.echo(clc.bold('Rewriting ' + context));
that.paths[context].split(',').forEach(function (path) {
if (fs.lstatSync(path).isDirectory()) {
var files = glob.sync(path.replace(/\/$/, '') + '/**/*' + that.extensions[context]);
files.forEach(function (file) {
that.build(file, context);
});
} else if (fs.lstatSync(path).isFile()) {
that.build(path, context);
}
});
that.echo(clc.green.bold('Finished!\n'));
}
/**
* echo
*
* wrap the console.log method
*
* @param message String
*/
Muncher.prototype.echo = function (message) {
if (!this.silent) console.log(message);
}
/**
* parse
*
* parse files according to context
*
* @param file String the file name of the document
* @param context String either view, js or css
*/
Muncher.prototype.parse = function (file, context) {
if (fs.existsSync(file)) {
this.echo(file);
var content = fs.readFileSync(file, 'utf8').toString();
switch (context) {
case "view":
this.parseHtml(content);
break;
case "css":
this.parseCss(content);
break;
case "js":
this.parseJs(content);
break;
}
} else {
this.echo(clc.red(file + ' doesn\'t exist'));
}
}
/**
* build
*
* build files according to context
*
* @param file String the file name of the document
* @param context String either view, js or css
*/
Muncher.prototype.build = function (file, context) {
var content = fs.readFileSync(file, 'utf8').toString();
switch (context) {
case "view":
this.rewriteHtml(content, file);
break;
case "css":
this.rewriteCss(content, file);
break;
case "js":
this.rewriteJs(content, file);
break;
}
}
/**
* addCss
*
* adds Classes to the CLASS map
*
* @param cl String
*/
Muncher.prototype.addClass = function (cl) {
var that = this;
var addClass = function (cls) {
if (that.ignoreClasses.indexOf(cls) > -1) return true; // shoul be a list of no-nos
if (!that.map["class"][cls]) {
that.map["class"][cls] = that.prefix + hashids.encrypt(that.mapCounter);
that.mapCounter++;
}
}
if (typeof cl == 'object') {
if (cl) {
cl.forEach(function (pass) {
addClass(pass);
});
}
} else {
addClass(cl);
}
}
/**
* addId
*
* adds Ids to the ID map
*
* @param id String
*/
Muncher.prototype.addId = function (id) {
if (!this.map["id"][id]) {
if (!this.ignoreIds.indexOf(id)) return true; // shoul be a list of no-nos
this.map["id"][id] = this.prefix + hashids.encrypt(this.mapCounter);
this.mapCounter++;
}
}
/**
* parseCssSelector
*
* parse CSS strings to get their classes and ids
*
* @param css String the css string
*/
Muncher.prototype.parseCssSelector = function (selector) {
var that = this,
match = null,
tid = selector.match(/#[\w\-]+/gi),
tcl = selector.match(/\.[\w\-]+/gi);
if (tid) {
tid.forEach(function (match) {
var id = match.replace('#', '');
that.addId(id);
});
}
if (tcl) {
tcl.forEach(function (match) {
var cl = match.replace('.', '');
that.addClass(cl);
});
}
}
/**
* parseHtml
*
* parse HTML documents to get their classes and ids
*
* @param html String the html document
*/
Muncher.prototype.parseHtml = function (html) {
var document = jsdom(html);
var that = this,
html = $(document.defaultView)(document);
html.find('*').each(function (i, elem) {
var target = html.find(elem),
id = target.attr('id'),
classes = target.attr('class');
if (id) {
that.addId(id);
}
if (classes) {
var newClass = [];
classes.split(' ').forEach(function (cl) {
that.addClass(cl);
});
}
if (target.is('style')) {
var style = target.text();
that.parseCss(style);
}
});
// parse JS
var script = '';
html.filter('script').each(function (i, tag) {
script += this.text || this.textContent || this.innerHTML || '';
});
if (script != '') {
that.parseJs(script);
}
}
/**
* parseCss
*
* parse CSS documents to get their classes and ids
*
* @param css String the css document
*/
Muncher.prototype.parseCss = function (css) {
var document = jsdom('');
var that = this,
css = parse(css),
styles = [];
$(document.defaultView)(css.stylesheet.rules).each(function (i, style) {
if (style.media) {
styles = styles.concat(style.rules);
}
if (!style.selectors) return true;
styles.push(css.stylesheet.rules[i]);
});
$(document.defaultView)(styles).each(function (o, style) {
style.selectors.forEach(function (selector) {
that.parseCssSelector(selector);
});
});
}
/**
* parseJs
*
* parse JS documents to get their classes and ids
*
* @param js String the js document
*/
Muncher.prototype.parseJs = function (js) {
var that = this,
match;
// custom parsers
if (this.parsers.js.length > 0) {
this.parsers.js.forEach(function (cb) {
cb.call(that, js);
});
}
// id and class
var pass4 = /getElementsByClassName\([\'"](.*?)[\'"]/gi;
while ((match = pass4.exec(js)) !== null) {
this.addClass(match[1].split(' '));
}
var pass5 = /getElementById\([\'"](.*?)[\'"]/gi;
while ((match = pass5.exec(js)) !== null) {
this.addId(match[1]);
}
// attr
var pass7 = /setAttribute\([\'"](id|class)[\'"],\s[\'"](.+?)[\'"]/gi;
while ((match = pass7.exec(js)) !== null) {
if (match[1] == 'class') this.addClass(match[2].split(' '));
if (match[1] == 'id') this.addId(match[2]);
}
}
/**
* rewriteHtml
*
* replaces the ids and classes in the files specified
*
* @param html String the html document
* @param to String the file name
*/
Muncher.prototype.rewriteHtml = function (html, to) {
var document = jsdom(html);
var that = this,
html = $(document.defaultView)(document);
that.files[to] = fs.statSync(to).size;
html.find('*').each(function (i, elem) {
var target = html.find(elem),
id = target.attr('id'),
classes = target.attr('class');
if (id) {
if (!that.ignoreIds.indexOf(id)) return true;
target.attr('id', that.map["id"][id]);
}
if (classes) {
var newClass = [];
classes.split(' ').forEach(function (cl) {
if (!that.ignoreClasses.indexOf(cl)) return true;
if (that.map["class"][cl]) {
target.removeClass(cl).addClass(that.map["class"][cl]);
}
});
}
});
// write
html = serializeDocument(html.find('body > *'));
//html = this.rewriteJsBlock(html);
//html = this.rewriteCssBlock(html, this.compress['view']);
fs.writeFileSync(to + this.postfix, (this.compress['view']) ? this.compressHtml(html) : html);
var percent = 100 - ((fs.statSync(to + this.postfix).size / this.files[to]) * 100);
var savings = (that.showSavings) ? clc.blue.bold(percent.toFixed(2) + '%') + ' Saved for ' : '';
that.echo(savings + to + this.postfix);
}
/**
* rewriteCssString
*
* rewrite a CSS String
*
* @param css String the css document
*/
Muncher.prototype.rewriteCssString = function (css) {
var document = jsdom('');
var that = this,
text = css,
css = parse(text),
styles = [];
$(document.defaultView)(css.stylesheet.rules).each(function (i, style) {
if (style.media) {
styles = styles.concat(style.rules);
}
if (!style.selectors) return true;
styles.push(css.stylesheet.rules[i]);
});
$(document.defaultView)(styles).each(function (u, style) {
style.selectors.forEach(function (selector) {
var original = selector,
tid = selector.match(/#[\w\-]+/gi),
tcl = selector.match(/\.[\w\-]+/gi);
if (tid) {
$.each(tid, function (i, match) {
match = match.replace('#', '');
if (that.ignoreIds.indexOf(match) > -1) return true;
selector = selector.replace(new RegExp("#" + match.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&"), "gi"), '#' + that.map["id"][match]);
});
}
if (tcl) {
$(document.defaultView)(tcl).each(function (o, match) {
match = match.replace('.', '');
if (that.ignoreClasses.indexOf(match) > -1) return true;
selector = selector.replace(new RegExp("\\." + match.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&"), "gi"), '.' + that.map["class"][match]);
});
}
text = text.replace(original, selector);
});
});
return text;
}
/**
* rewriteCssBlock
*
* rewrite a CSS block in an html document
*
* @param html String the html document
* @param compress Boolean flag whether to compress the CSS block
*/
Muncher.prototype.rewriteCssBlock = function (html, compress) {
var document = jsdom(html);
var that = this,
html = $(document.defaultView)(document);
html.find('*').each(function (i, elem) {
var target = html.find(elem);
if (target.is('style')) {
var text = that.rewriteCssString(target.text());
target.text(compress ? that.compressCss(text) : text);
}
});
return serializeDocument(html.find('body *'));
}
/**
* rewriteCss
*
* rewrite a CSS file
*
* @param css String the css document
* @param to String the file name
*/
Muncher.prototype.rewriteCss = function (css, to) {
var that = this,
text = that.rewriteCssString(css);
that.files[to] = fs.statSync(to).size;
fs.writeFileSync(to + this.postfix, (this.compress['css']) ? this.compressCss(text) : text);
var percent = 100 - ((fs.statSync(to + this.postfix).size / this.files[to]) * 100);
var savings = (that.showSavings) ? clc.blue.bold(percent.toFixed(2) + '%') + ' Saved for ' : '';
that.echo(savings + to + this.postfix);
}
/**
* rewriteJsString
*
* rewrite a JS String
*
* @param js String the js document
*/
Muncher.prototype.rewriteJsString = function (js) {
var that = this,
match = null;
// id and class
var pass4 = /getElementsByClassName\([\'"](.*?)[\'"]/gi;
while ((match = pass4.exec(js)) !== null) {
if (that.ignoreClasses.indexOf(match[1]) > -1) continue;
var passed = match[0].replace(new RegExp(match[1], "gi"), that.map["class"][match[1]]);
js = js.replace(match[0], passed);
}
var pass5 = /getElementById\([\'"](.*?)[\'"]/gi;
while ((match = pass5.exec(js)) !== null) {
if (that.ignoreIds.indexOf(match[1]) > -1) continue;
var passed = match[0].replace(new RegExp(match[1], "gi"), that.map["id"][match[1]]);
js = js.replace(match[0], passed);
}
// attr
var pass7 = /setAttribute\([\'"](id|class)[\'"],\s[\'"](.+?)[\'"]/gi;
while ((match = pass7.exec(js)) !== null) {
var key = (match[1] == 'id') ? 'id' : 'class';
if (key == 'class') {
var passed = match[0],
splitd = match[2].split(' ');
$.each(splitd, function (i, cls) {
if (that.ignoreClasses.indexOf(cls) > -1) return true;
passed = passed.replace(new RegExp(cls, "gi"), that.map[key][cls]);
});
} else {
if (that.ignoreIds.indexOf(match[2]) > -1) continue;
var passed = match[0].replace(new RegExp(match[2], "gi"), that.map[key][match[2]]);
}
js = js.replace(match[0], passed);
}
// custom parsers
if (this.writers.js.length > 0) {
this.writers.js.forEach(function (cb) {
js = cb.call(that, js);
});
}
return js;
}
/**
* rewriteJsBlock
*
* rewrite a JS block in an html document
*
* @param html String the html document
* @param compress Boolean flag whether to compress the JS block
*/
Muncher.prototype.rewriteJsBlock = function (html, compress) {
var document = jsdom(html);
var that = this,
html = $(document.defaultView)(document);
var match;
html.find('script').each(function (i, elem) {
var target = html.find(elem),
js = that.rewriteJsString(target.text());
target.text(js);
});
var block = (compress) ? this.compressJs(serializeDocument(html.find('body *'))) : serializeDocument(html.find('body *'));
return block;
}
/**
* rewriteJs
*
* rewrite a JS file
*
* @param js String the js document
* @param to String the file name
*/
Muncher.prototype.rewriteJs = function (js, to) {
var that = this;
that.files[to] = fs.statSync(to).size;
js = that.rewriteJsString(js);
fs.writeFileSync(to + this.postfix, (this.compress['js']) ? this.compressJs(js) : js);
var percent = 100 - ((fs.statSync(to + this.postfix).size / this.files[to]) * 100);
var savings = (that.showSavings) ? clc.blue.bold(percent.toFixed(2) + '%') + ' Saved for ' : '';
that.echo(savings + to + this.postfix);
}
/**
* compressHtml
*
* Compress HTML Files to save a couple of bytes. Someone tell me where I got this. I need to
* credit them. I forgot :(
*
* @param html String The HTML string to be minified
* @param compressHead Boolean Option whether the <head> tag should be compressed as well
*/
Muncher.prototype.compressHtml = function (html, compressHead) {
var allHTML = html,
headHTML = '',
removeThis = '',
headstatus = compressHead || true;
if (headstatus != true) {
//Compress all the things!
allHTML = allHTML.replace(/(\r\n|\n|\r|\t)/gm, '');
allHTML = allHTML.replace(/\s+/g, ' ');
} else {
//Don't compress the head
allHTML = allHTML.replace(new RegExp('</HEAD', 'gi'), '</head');
allHTML = allHTML.replace(new RegExp('</head ', 'gi'), '</head');
var bodySplit = '</head>';
var i = allHTML.indexOf(bodySplit) != -1;
if (i == true) {
var bodySplit = '</head>';
var tempo = allHTML.split(new RegExp(bodySplit, 'i'));
headHTML = tempo[0];
allHTML = tempo[1];
} else {
bodySplit = '';
}
allHTML = allHTML.replace(/(\r\n|\n|\r|\t)/gm, '');
allHTML = allHTML.replace(/\s+/g, ' ');
allHTML = headHTML + bodySplit + '\n' + allHTML.replace(/<!--(.*?)-->/gm, '');
}
return allHTML;
}
/**
* compressCss
*
* A simple CSS minifier. removes all newlines, tabs and spaces. also strips out comments.
*
* @param css String The CSS string to be minified
*/
Muncher.prototype.compressCss = function (css) {
css = css.replace(/(\r\n|\n|\r|\t)/gm, "");
css = css.replace(/\s+/g, " ");
return css.replace(/\/\*(.*?)\*\//gm, "");
}
/**
* compressJs
*
* A placeholder for future use perhaps?
*
* @param js String The JS string to be minified
*/
Muncher.prototype.compressJs = function (js) {
return js;
}
/**
* addJsParser
*
* plug different JS parsers here. Parsers are loaded dynamically from the `parsers` folder
*
* @param cb Function the callback for parsing JS Strings
*/
Muncher.prototype.addJsParser = function (cb) {
if (typeof cb == 'function') {
this.parsers.js.push(cb);
}
}
/**
* addJsWriter
*
* plug different JS writers here. Writers are loaded dynamically from the `parsers` folder
*
* @param cb Function the callback for writing JS Strings
*/
Muncher.prototype.addJsWriter = function (cb) {
if (typeof cb == 'function') {
this.writers.js.push(cb);
}
}
/**
* module_exists
*
* Check if a module exists
*
* @param module_exists String the module name
*/
function module_exists(name) {
try {
return require.resolve(name);
} catch (e) {
return false
}
}
/**
* allow to run with args
*/
exports.runWithArgs = function (args) {
var munch = new Muncher(args);
munch.run();
};
/**
* let's not forget to expose this
*/
exports.run = function () {
// fetch the script options from CLI
var args = require('optimist')
.usage(fs.readFileSync(__dirname + '/usage').toString())
.demand(['view'])
.argv;
// we have a settings file specifically specified or args is empty
if (args['manifest']) {
args['manifest'] = (typeof args['manifest'] == 'string') ? args['manifest'] : '.muncher';
// see if the file exists and get it
if (fs.existsSync(args['manifest'])) {
args = JSON.parse(fs.readFileSync(args['manifest']));
// normalize everything
if (typeof args.view == 'object') args.view = args.view.join(',');
if (typeof args.css == 'object') args.css = args.css.join(',');
if (typeof args.js == 'object') args.js = args.js.join(',');
if (typeof args.ignore == 'object') args.ignore = args.ignore.join(',');
if (typeof args.parsers == 'object') args.parsers = args.parsers.join(',');
}
}
// check if args is usable.
if (!args) {
console.log('There are no options specified. Aborting');
return;
} else if (!args['silent']) {
// echo a pretty name if we are allowed to
console.log(clc.red('\n __ ___ __ _ '));
console.log(clc.red(' / |/ /_ _____ ____/ / (_)__'));
console.log(clc.red(' / /|_/ / // / _ \\/ __/ _ \\ / (_-<'));
console.log(clc.red('/_/ /_/\\_,_/_//_/\\__/_//_/_/ /___/'));
console.log(clc.red(' |___/ \n'));
console.log(clc.white('Copyright (c) 2013 John Rocela <[email protected]>\n\n'));
}
if (args['manifest']) {
console.log('Reading Manifest file to get configuration...\n');
}
// let's start munching
var munch = new Muncher(args);
// add custom JS parsers
if (args['parsers']) {
var parsers = args['parsers'].split(',');
parsers.forEach(function (parser) {
if (module_exists(parser)) {
var lib = require(parser);
if (lib.parser) munch.addJsParser(lib.parser);
if (lib.writer) munch.addJsWriter(lib.writer);
}
});
}
// bon appetit`
munch.run();
}
// expose for testing
exports.Muncher = Muncher;
// have fun <3