11'use strict' ;
22
33const fs = require ( 'fs-extra' ) ;
4+ const path = require ( 'path' ) ;
5+
6+ function exportSinglePattern ( patternlab , pattern ) {
7+ const preserveDirStructure =
8+ patternlab . config . patternExportPreserveDirectoryStructure ;
9+ let patternName = pattern . patternPartial ;
10+ let patternDir = patternlab . config . patternExportDirectory ;
11+ let patternCode = pattern . patternPartialCode ;
12+ let patternFileExtension = '.html' ;
13+ if ( preserveDirStructure ) {
14+ // Extract the first part of the pattern partial as the directory in which
15+ // it should go.
16+ patternDir = path . join ( patternDir , pattern . patternPartial . split ( '-' ) [ 0 ] ) ;
17+ patternName = pattern . patternPartial
18+ . split ( '-' )
19+ . slice ( 1 )
20+ . join ( '-' ) ;
21+ }
22+
23+ if ( patternlab . config . patternExportRaw ) {
24+ patternCode = pattern . extendedTemplate ;
25+ patternFileExtension = `.${ patternlab . config . patternExtension } ` ;
26+ }
27+
28+ fs . outputFileSync (
29+ path . join ( patternDir , patternName ) + patternFileExtension ,
30+ patternCode
31+ ) ;
32+ }
433
534const pattern_exporter = function ( ) {
635 /**
@@ -13,18 +42,24 @@ const pattern_exporter = function() {
1342 function exportPatterns ( patternlab ) {
1443 //read the config export options
1544 const exportPartials = patternlab . config . patternExportPatternPartials ;
45+ const exportAll = patternlab . config . patternExportAll ;
46+
47+ if ( exportAll ) {
48+ for ( let i = 0 ; i < patternlab . patterns . length ; i ++ ) {
49+ if ( ! patternlab . patterns [ i ] . patternPartial . startsWith ( '-' ) ) {
50+ exportSinglePattern ( patternlab , patternlab . patterns [ i ] ) ;
51+ }
52+ }
53+
54+ return ;
55+ }
1656
1757 //find the chosen patterns to export
1858 for ( let i = 0 ; i < exportPartials . length ; i ++ ) {
1959 for ( let j = 0 ; j < patternlab . patterns . length ; j ++ ) {
2060 if ( exportPartials [ i ] === patternlab . patterns [ j ] . patternPartial ) {
2161 //write matches to the desired location
22- fs . outputFileSync (
23- patternlab . config . patternExportDirectory +
24- patternlab . patterns [ j ] . patternPartial +
25- '.html' ,
26- patternlab . patterns [ j ] . patternPartialCode
27- ) ;
62+ exportSinglePattern ( patternlab , patternlab . patterns [ j ] ) ;
2863 }
2964 }
3065 }
0 commit comments