1
1
/*
2
- * patternlab-node - v2.5.1 - 2016
2
+ * patternlab-node - v2.6.0-alpha - 2016
3
3
*
4
4
* Brian Muenzenmeyer, Geoff Pursell, and the web community.
5
5
* Licensed under the MIT license.
@@ -14,8 +14,13 @@ var diveSync = require('diveSync'),
14
14
glob = require ( 'glob' ) ,
15
15
_ = require ( 'lodash' ) ,
16
16
path = require ( 'path' ) ,
17
+ cleanHtml = require ( 'js-beautify' ) . html ,
18
+ inherits = require ( 'util' ) . inherits ,
19
+ pm = require ( './plugin_manager' ) ,
17
20
plutils = require ( './utilities' ) ;
18
21
22
+ var EventEmitter = require ( 'events' ) . EventEmitter ;
23
+
19
24
function buildPatternData ( dataFilesPath , fs ) {
20
25
var dataFiles = glob . sync ( dataFilesPath + '*.json' , { "ignore" : [ dataFilesPath + 'listitems.json' ] } ) ;
21
26
var mergeObject = { } ;
@@ -73,6 +78,28 @@ function checkConfiguration(patternlab) {
73
78
patternlab . config . outputFileSuffixes = _ . extend ( outputFileSuffixes , patternlab . config . outputFileSuffixes ) ;
74
79
}
75
80
81
+ /**
82
+ * Finds and calls the main method of any found plugins.
83
+ * @param patternlab - global data store
84
+ */
85
+ function initializePlugins ( patternlab ) {
86
+ var plugin_manager = new pm ( patternlab . config , path . resolve ( __dirname , '../../patternlab-config.json' ) ) ;
87
+ var foundPlugins = plugin_manager . detect_plugins ( ) ;
88
+
89
+ if ( foundPlugins && foundPlugins . length > 0 ) {
90
+
91
+ for ( var i = 0 ; i < foundPlugins . length ; i ++ ) {
92
+ var plugin = plugin_manager . load_plugin ( foundPlugins [ i ] ) ;
93
+ plugin ( patternlab ) ;
94
+ }
95
+ }
96
+ }
97
+
98
+ function PatternLabEventEmitter ( ) {
99
+ EventEmitter . call ( this ) ;
100
+ }
101
+ inherits ( PatternLabEventEmitter , EventEmitter ) ;
102
+
76
103
var patternlab_engine = function ( config ) {
77
104
'use strict' ;
78
105
@@ -92,9 +119,13 @@ var patternlab_engine = function (config) {
92
119
93
120
patternlab . package = fs . readJSONSync ( path . resolve ( __dirname , '../../package.json' ) ) ;
94
121
patternlab . config = config || fs . readJSONSync ( path . resolve ( __dirname , '../../patternlab-config.json' ) ) ;
122
+ patternlab . events = new PatternLabEventEmitter ( ) ;
95
123
96
124
checkConfiguration ( patternlab ) ;
97
125
126
+ //todo: determine if this is the best place to wire up plugins
127
+ initializePlugins ( patternlab ) ;
128
+
98
129
var paths = patternlab . config . paths ;
99
130
100
131
function getVersion ( ) {
@@ -236,6 +267,9 @@ var patternlab_engine = function (config) {
236
267
}
237
268
238
269
function buildPatterns ( deletePatternDir ) {
270
+
271
+ patternlab . events . emit ( 'patternlab-build-pattern-start' , patternlab ) ;
272
+
239
273
try {
240
274
patternlab . data = buildPatternData ( paths . source . data , fs ) ;
241
275
} catch ( ex ) {
@@ -245,7 +279,7 @@ var patternlab_engine = function (config) {
245
279
try {
246
280
patternlab . listitems = fs . readJSONSync ( path . resolve ( paths . source . data , 'listitems.json' ) ) ;
247
281
} catch ( ex ) {
248
- plutils . logRed ( ' missing or malformed' + paths . source . data + 'listitems.json Pattern Lab may not work without this file.' ) ;
282
+ plutils . logOrange ( 'WARNING: missing or malformed ' + paths . source . data + 'listitems.json file. Pattern Lab may not work without this file.' ) ;
249
283
patternlab . listitems = { } ;
250
284
}
251
285
try {
@@ -268,9 +302,13 @@ var patternlab_engine = function (config) {
268
302
269
303
pattern_assembler . combine_listItems ( patternlab ) ;
270
304
305
+ patternlab . events . emit ( 'patternlab-build-global-data-end' , patternlab ) ;
306
+
271
307
// diveSync once to perform iterative populating of patternlab object
272
308
processAllPatternsIterative ( pattern_assembler , paths . source . patterns , patternlab ) ;
273
309
310
+ patternlab . events . emit ( 'patternlab-pattern-iteration-end' , patternlab ) ;
311
+
274
312
//diveSync again to recursively include partials, filling out the
275
313
//extendedTemplate property of the patternlab.patterns elements
276
314
processAllPatternsRecursive ( pattern_assembler , paths . source . patterns , patternlab ) ;
@@ -384,15 +422,26 @@ var patternlab_engine = function (config) {
384
422
385
423
var footerHTML = pattern_assembler . renderPattern ( patternlab . userFoot , allFooterData ) ;
386
424
425
+ patternlab . events . emit ( 'patternlab-pattern-write-begin' , patternlab , pattern ) ;
426
+
387
427
//write the compiled template to the public patterns directory
388
428
var patternPage = headHTML + pattern . patternPartialCode + footerHTML ;
389
- fs . outputFileSync ( paths . public . patterns + pattern . getPatternLink ( patternlab , 'rendered' ) , patternPage ) ;
429
+
430
+ //beautify the output if configured to do so
431
+ var cleanedPatternPage = config . cleanOutputHtml ? cleanHtml ( patternPage , { indent_size : 2 } ) : patternPage ;
432
+ var cleanedPatternPartialCode = config . cleanOutputHtml ? cleanHtml ( pattern . patternPartialCode , { indent_size : 2 } ) : pattern . patternPartialCode ;
433
+ var cleanedPatternTemplateCode = config . cleanOutputHtml ? cleanHtml ( pattern . template , { indent_size : 2 } ) : pattern . template ;
434
+
435
+ //write the compiled template to the public patterns directory
436
+ fs . outputFileSync ( paths . public . patterns + pattern . getPatternLink ( patternlab , 'rendered' ) , cleanedPatternPage ) ;
390
437
391
438
//write the mustache file too
392
- fs . outputFileSync ( paths . public . patterns + pattern . getPatternLink ( patternlab , 'rawTemplate' ) , pattern . template ) ;
439
+ fs . outputFileSync ( paths . public . patterns + pattern . getPatternLink ( patternlab , 'rawTemplate' ) , cleanedPatternTemplateCode ) ;
393
440
394
441
//write the encoded version too
395
- fs . outputFileSync ( paths . public . patterns + pattern . getPatternLink ( patternlab , 'markupOnly' ) , pattern . patternPartialCode ) ;
442
+ fs . outputFileSync ( paths . public . patterns + pattern . getPatternLink ( patternlab , 'markupOnly' ) , cleanedPatternPartialCode ) ;
443
+
444
+ patternlab . events . emit ( 'patternlab-pattern-write-end' , patternlab , pattern ) ;
396
445
397
446
return true ;
398
447
} ) ;
0 commit comments