@@ -14,8 +14,12 @@ var diveSync = require('diveSync'),
1414 glob = require ( 'glob' ) ,
1515 _ = require ( 'lodash' ) ,
1616 path = require ( 'path' ) ,
17- plutils = require ( './utilities' ) ,
18- cleanHtml = require ( 'js-beautify' ) . html ;
17+ cleanHtml = require ( 'js-beautify' ) . html ,
18+ inherits = require ( 'util' ) . inherits ,
19+ pm = require ( './plugin_manager' ) ,
20+ plutils = require ( './utilities' ) ;
21+
22+ var EventEmitter = require ( 'events' ) . EventEmitter ;
1923
2024function buildPatternData ( dataFilesPath , fs ) {
2125 var dataFiles = glob . sync ( dataFilesPath + '*.json' , { "ignore" : [ dataFilesPath + 'listitems.json' ] } ) ;
@@ -74,6 +78,28 @@ function checkConfiguration(patternlab) {
7478 patternlab . config . outputFileSuffixes = _ . extend ( outputFileSuffixes , patternlab . config . outputFileSuffixes ) ;
7579}
7680
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+
77103var patternlab_engine = function ( config ) {
78104 'use strict' ;
79105
@@ -93,9 +119,13 @@ var patternlab_engine = function (config) {
93119
94120 patternlab . package = fs . readJSONSync ( path . resolve ( __dirname , '../../package.json' ) ) ;
95121 patternlab . config = config || fs . readJSONSync ( path . resolve ( __dirname , '../../patternlab-config.json' ) ) ;
122+ patternlab . events = new PatternLabEventEmitter ( ) ;
96123
97124 checkConfiguration ( patternlab ) ;
98125
126+ //todo: determine if this is the best place to wire up plugins
127+ initializePlugins ( patternlab ) ;
128+
99129 var paths = patternlab . config . paths ;
100130
101131 function getVersion ( ) {
@@ -237,6 +267,9 @@ var patternlab_engine = function (config) {
237267 }
238268
239269 function buildPatterns ( deletePatternDir ) {
270+
271+ patternlab . events . emit ( 'patternlab-build-pattern-start' , patternlab ) ;
272+
240273 try {
241274 patternlab . data = buildPatternData ( paths . source . data , fs ) ;
242275 } catch ( ex ) {
@@ -269,9 +302,13 @@ var patternlab_engine = function (config) {
269302
270303 pattern_assembler . combine_listItems ( patternlab ) ;
271304
305+ patternlab . events . emit ( 'patternlab-build-global-data-end' , patternlab ) ;
306+
272307 // diveSync once to perform iterative populating of patternlab object
273308 processAllPatternsIterative ( pattern_assembler , paths . source . patterns , patternlab ) ;
274309
310+ patternlab . events . emit ( 'patternlab-pattern-iteration-end' , patternlab ) ;
311+
275312 //diveSync again to recursively include partials, filling out the
276313 //extendedTemplate property of the patternlab.patterns elements
277314 processAllPatternsRecursive ( pattern_assembler , paths . source . patterns , patternlab ) ;
@@ -385,6 +422,9 @@ var patternlab_engine = function (config) {
385422
386423 var footerHTML = pattern_assembler . renderPattern ( patternlab . userFoot , allFooterData ) ;
387424
425+ patternlab . events . emit ( 'patternlab-pattern-write-begin' , patternlab , pattern ) ;
426+
427+ //write the compiled template to the public patterns directory
388428 var patternPage = headHTML + pattern . patternPartialCode + footerHTML ;
389429
390430 //beautify the output if configured to do so
@@ -401,6 +441,8 @@ var patternlab_engine = function (config) {
401441 //write the encoded version too
402442 fs . outputFileSync ( paths . public . patterns + pattern . getPatternLink ( patternlab , 'markupOnly' ) , cleanedPatternPartialCode ) ;
403443
444+ patternlab . events . emit ( 'patternlab-pattern-write-end' , patternlab , pattern ) ;
445+
404446 return true ;
405447 } ) ;
406448
0 commit comments