1
- /*
2
- * patternlab-node - v2.6.0-alpha - 2016
3
- *
1
+ /*
2
+ * patternlab-node - v2.6.1 - 2016
3
+ *
4
4
* Brian Muenzenmeyer, Geoff Pursell, and the web community.
5
- * Licensed under the MIT license.
6
- *
7
- * Many thanks to Brad Frost and Dave Olsen for inspiration, encouragement, and advice.
5
+ * Licensed under the MIT license.
6
+ *
7
+ * Many thanks to Brad Frost and Dave Olsen for inspiration, encouragement, and advice.
8
8
*
9
9
*/
10
10
@@ -17,15 +17,16 @@ var diveSync = require('diveSync'),
17
17
cleanHtml = require ( 'js-beautify' ) . html ,
18
18
inherits = require ( 'util' ) . inherits ,
19
19
pm = require ( './plugin_manager' ) ,
20
+ fs = require ( 'fs-extra' ) ,
20
21
plutils = require ( './utilities' ) ;
21
22
22
23
var EventEmitter = require ( 'events' ) . EventEmitter ;
23
24
24
- function buildPatternData ( dataFilesPath , fs ) {
25
+ function buildPatternData ( dataFilesPath , fsDep ) {
25
26
var dataFiles = glob . sync ( dataFilesPath + '*.json' , { "ignore" : [ dataFilesPath + 'listitems.json' ] } ) ;
26
27
var mergeObject = { } ;
27
28
dataFiles . forEach ( function ( filePath ) {
28
- var jsonData = fs . readJSONSync ( path . resolve ( filePath ) , 'utf8' ) ;
29
+ var jsonData = fsDep . readJSONSync ( path . resolve ( filePath ) , 'utf8' ) ;
29
30
mergeObject = _ . merge ( mergeObject , jsonData ) ;
30
31
} ) ;
31
32
return mergeObject ;
@@ -83,18 +84,42 @@ function checkConfiguration(patternlab) {
83
84
* @param patternlab - global data store
84
85
*/
85
86
function initializePlugins ( patternlab ) {
87
+
88
+ if ( ! patternlab . config . plugins ) { return ; }
89
+
86
90
var plugin_manager = new pm ( patternlab . config , path . resolve ( __dirname , '../../patternlab-config.json' ) ) ;
87
91
var foundPlugins = plugin_manager . detect_plugins ( ) ;
88
92
89
93
if ( foundPlugins && foundPlugins . length > 0 ) {
90
94
91
95
for ( var i = 0 ; i < foundPlugins . length ; i ++ ) {
92
- var plugin = plugin_manager . load_plugin ( foundPlugins [ i ] ) ;
96
+
97
+ let pluginKey = foundPlugins [ i ] ;
98
+
99
+ if ( patternlab . config . debug ) {
100
+ console . log ( 'Found plugin: ' , pluginKey ) ;
101
+ console . log ( 'Attempting to load and initialize plugin.' ) ;
102
+ }
103
+
104
+ var plugin = plugin_manager . load_plugin ( pluginKey ) ;
93
105
plugin ( patternlab ) ;
94
106
}
95
107
}
96
108
}
97
109
110
+ /**
111
+ * Installs a given plugin. Assumes it has already been pulled down via npm
112
+ * @param pluginName - the name of the plugin
113
+ */
114
+ function installPlugin ( pluginName ) {
115
+ //get the config
116
+ var configPath = path . resolve ( process . cwd ( ) , 'patternlab-config.json' ) ;
117
+ var config = fs . readJSONSync ( path . resolve ( configPath ) , 'utf8' ) ;
118
+ var plugin_manager = new pm ( config , configPath ) ;
119
+
120
+ plugin_manager . install_plugin ( pluginName ) ;
121
+ }
122
+
98
123
function PatternLabEventEmitter ( ) {
99
124
EventEmitter . call ( this ) ;
100
125
}
@@ -104,7 +129,6 @@ var patternlab_engine = function (config) {
104
129
'use strict' ;
105
130
106
131
var JSON5 = require ( 'json5' ) ,
107
- fs = require ( 'fs-extra' ) ,
108
132
pa = require ( './pattern_assembler' ) ,
109
133
pe = require ( './pattern_exporter' ) ,
110
134
lh = require ( './lineage_hunter' ) ,
@@ -123,7 +147,6 @@ var patternlab_engine = function (config) {
123
147
124
148
checkConfiguration ( patternlab ) ;
125
149
126
- //todo: determine if this is the best place to wire up plugins
127
150
initializePlugins ( patternlab ) ;
128
151
129
152
var paths = patternlab . config . paths ;
@@ -266,6 +289,37 @@ var patternlab_engine = function (config) {
266
289
}
267
290
}
268
291
292
+ function writePatternFiles ( headHTML , pattern , footerHTML ) {
293
+ const nullFormatter = str => str ;
294
+ const defaultFormatter = codeString => cleanHtml ( codeString , { indent_size : 2 } ) ;
295
+ const makePath = type => path . join ( paths . public . patterns , pattern . getPatternLink ( patternlab , type ) ) ;
296
+ const patternPage = headHTML + pattern . patternPartialCode + footerHTML ;
297
+ const eng = pattern . engine ;
298
+
299
+ //beautify the output if configured to do so
300
+ const formatters = config . cleanOutputHtml ? {
301
+ rendered : eng . renderedCodeFormatter || defaultFormatter ,
302
+ rawTemplate : eng . rawTemplateCodeFormatter || defaultFormatter ,
303
+ markupOnly : eng . markupOnlyCodeFormatter || defaultFormatter
304
+ } : {
305
+ rendered : nullFormatter ,
306
+ rawTemplate : nullFormatter ,
307
+ markupOnly : nullFormatter
308
+ } ;
309
+
310
+ //prepare the path and contents of each output file
311
+ const outputFiles = [
312
+ { path : makePath ( 'rendered' ) , content : formatters . rendered ( patternPage , pattern ) } ,
313
+ { path : makePath ( 'rawTemplate' ) , content : formatters . rawTemplate ( pattern . template , pattern ) } ,
314
+ { path : makePath ( 'markupOnly' ) , content : formatters . markupOnly ( pattern . patternPartialCode , pattern ) }
315
+ ] . concat (
316
+ eng . addOutputFiles ? eng . addOutputFiles ( paths , patternlab ) : [ ]
317
+ ) ;
318
+
319
+ //write the compiled template to the public patterns directory
320
+ outputFiles . forEach ( outFile => fs . outputFileSync ( outFile . path , outFile . content ) ) ;
321
+ }
322
+
269
323
function buildPatterns ( deletePatternDir ) {
270
324
271
325
patternlab . events . emit ( 'patternlab-build-pattern-start' , patternlab ) ;
@@ -357,6 +411,8 @@ var patternlab_engine = function (config) {
357
411
pattern . patternLineageRExists = pattern . lineageR . length > 0 ;
358
412
pattern . patternLineageEExists = pattern . patternLineageExists || pattern . patternLineageRExists ;
359
413
414
+ patternlab . events . emit ( 'patternlab-pattern-before-data-merge' , patternlab , pattern ) ;
415
+
360
416
//render the pattern, but first consolidate any data we may have
361
417
var allData ;
362
418
try {
@@ -425,21 +481,7 @@ var patternlab_engine = function (config) {
425
481
patternlab . events . emit ( 'patternlab-pattern-write-begin' , patternlab , pattern ) ;
426
482
427
483
//write the compiled template to the public patterns directory
428
- var patternPage = headHTML + pattern . patternPartialCode + footerHTML ;
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 ) ;
437
-
438
- //write the mustache file too
439
- fs . outputFileSync ( paths . public . patterns + pattern . getPatternLink ( patternlab , 'rawTemplate' ) , cleanedPatternTemplateCode ) ;
440
-
441
- //write the encoded version too
442
- fs . outputFileSync ( paths . public . patterns + pattern . getPatternLink ( patternlab , 'markupOnly' ) , cleanedPatternPartialCode ) ;
484
+ writePatternFiles ( headHTML , pattern , footerHTML ) ;
443
485
444
486
patternlab . events . emit ( 'patternlab-pattern-write-end' , patternlab , pattern ) ;
445
487
@@ -485,6 +527,9 @@ var patternlab_engine = function (config) {
485
527
} ,
486
528
loadstarterkit : function ( starterkitName , clean ) {
487
529
loadStarterKit ( starterkitName , clean ) ;
530
+ } ,
531
+ installplugin : function ( pluginName ) {
532
+ installPlugin ( pluginName ) ;
488
533
}
489
534
} ;
490
535
} ;
0 commit comments