@@ -16,22 +16,23 @@ const prefixMatcher = /^_?(\d+-)?/;
16
16
* to get more details about the behavior of the folder structure
17
17
* https://github.com/pattern-lab/patternlab-node/pull/992
18
18
* https://github.com/pattern-lab/patternlab-node/pull/1016
19
+ * https://github.com/pattern-lab/patternlab-node/pull/1143
19
20
*
20
- * @constructor
21
+ * @param {String } relPath relative directory
22
+ * @param {Object } jsonFileData The JSON used to render values in the pattern.
23
+ * @param {Patternlab } patternlab The actual patternlab instance
24
+ * @param {Boolean } isUnsubRun specifies if the pattern needs to be unsubbed from its folder
21
25
*/
22
- const Pattern = function ( relPath , data , patternlab ) {
26
+ const Pattern = function ( relPath , jsonFileData , patternlab , isUnsubRun ) {
23
27
this . relPath = path . normalize ( relPath ) ; // '00-atoms/00-global/00-colors.mustache'
24
28
25
29
/**
26
30
* We expect relPath to be the path of the pattern template, relative to the
27
31
* root of the pattern tree. Parse out the path parts and save the useful ones.
28
- * @param {relPath } relative directory
29
- * @param {data } The JSON used to render values in the pattern.
30
- * @param {patternlab } rendered html files for the pattern
31
32
*/
32
33
const pathObj = path . parse ( this . relPath ) ;
33
34
34
- const info = this . getPatternInfo ( pathObj , patternlab ) ;
35
+ const info = this . getPatternInfo ( pathObj , patternlab , isUnsubRun ) ;
35
36
36
37
this . fileName = pathObj . name ; // '00-colors'
37
38
this . subdir = pathObj . dir ; // '00-atoms/00-global'
@@ -41,8 +42,8 @@ const Pattern = function(relPath, data, patternlab) {
41
42
if ( info . patternHasOwnDir ) {
42
43
// Since there is still the requirement of having the numbers provided for sorting
43
44
// this will be required to keep the folder prefix and the variant name
44
- // /00-atoms/00-global/00-colors/colors~varian .hbs
45
- // -> 00-atoms-00-global-00-colors
45
+ // /00-atoms/00-global/00-colors/colors~variant .hbs
46
+ // -> 00-atoms-00-global-00-colors-variant
46
47
this . name = `${ info . shortNotation } -${ path . parse ( pathObj . dir ) . base } ${
47
48
this . fileName . indexOf ( '~' ) !== - 1 ? '-' + this . fileName . split ( '~' ) [ 1 ] : ''
48
49
} `;
@@ -52,7 +53,7 @@ const Pattern = function(relPath, data, patternlab) {
52
53
}
53
54
54
55
// the JSON used to render values in the pattern
55
- this . jsonFileData = data || { } ;
56
+ this . jsonFileData = jsonFileData || { } ;
56
57
57
58
// strip leading "00-" from the file name and flip tildes to dashes
58
59
this . patternBaseName = this . fileName
@@ -188,8 +189,10 @@ Pattern.prototype = {
188
189
return this . name + path . sep + this . name + suffix + '.html' ;
189
190
} ,
190
191
191
- // the finders all delegate to the PatternEngine, which also encapsulates all
192
- // appropriate regexes
192
+ /**
193
+ * The finders all delegate to the PatternEngine, which also
194
+ * encapsulates all appropriate regexes
195
+ */
193
196
findPartials : function ( ) {
194
197
return this . engine . findPartials ( this ) ;
195
198
} ,
@@ -210,9 +213,15 @@ Pattern.prototype = {
210
213
return this . engine . findPartial ( partialString ) ;
211
214
} ,
212
215
213
- getDirLevel : function ( level , pathInfo ) {
216
+ /**
217
+ * Get a directory on a specific level of the pattern path
218
+ *
219
+ * @param {Number } level Level of folder to get
220
+ * @param {Object } pInfo general information about the pattern
221
+ */
222
+ getDirLevel : function ( level , pInfo ) {
214
223
const items = this . subdir . split ( path . sep ) ;
215
- pathInfo . patternHasOwnDir && items . pop ( ) ;
224
+ pInfo && pInfo . patternHasOwnDir && items . pop ( ) ;
216
225
217
226
if ( items [ level ] ) {
218
227
return items [ level ] ;
@@ -221,32 +230,54 @@ Pattern.prototype = {
221
230
} else {
222
231
// Im Not quite shure about that but its better than empty node
223
232
// TODO: verify
224
- return pathInfo . patternlab &&
225
- pathInfo . patternlab . config . patternTranslations &&
226
- pathInfo . patternlab . config . patternTranslations [ 'root-name' ]
227
- ? _ . kebabCase (
228
- pathInfo . patternlab . config . patternTranslations [ 'root-name' ]
229
- )
233
+ return pInfo . patternlab &&
234
+ pInfo . patternlab . config . patternTranslations &&
235
+ pInfo . patternlab . config . patternTranslations [ 'root-name' ]
236
+ ? _ . kebabCase ( pInfo . patternlab . config . patternTranslations [ 'root-name' ] )
230
237
: 'root' ;
231
238
}
232
239
} ,
233
240
241
+ /**
242
+ * Reset the information that the pattern has it's own directory,
243
+ * so that this pattern will not be handled as flat pattern if it
244
+ * is located on a top level folder.
245
+ *
246
+ * @param {Patternlab } patternlab Current patternlab instance
247
+ */
248
+ resetSubbing : function ( patternlab ) {
249
+ const p = new Pattern ( this . relPath , this . jsonFileData , patternlab , true ) ;
250
+ // Only reset the specific fields, not everything
251
+ Object . assign ( this , {
252
+ patternLink : p . patternLink ,
253
+ patternGroup : p . patternGroup ,
254
+ patternType : p . patternType ,
255
+ patternSubGroup : p . patternSubGroup ,
256
+ patternSubType : p . patternSubType ,
257
+ isFlatPattern : p . isFlatPattern ,
258
+ flatPatternPath : p . flatPatternPath ,
259
+ patternPartial : p . patternPartial ,
260
+ verbosePartial : p . verbosePartial ,
261
+ } ) ;
262
+ } ,
263
+
234
264
/**
235
265
* Info contains information about pattern structure if it is a nested pattern
236
266
* or if it just a subbed folder structure. Its just used for internal purposes.
237
267
* Remember every pattern infomarion based on "this.*" will be used by other functions
238
268
*
239
269
* @param pathObj path.parse() object containing usefull path information
240
270
*/
241
- getPatternInfo : ( pathObj , patternlab ) => {
271
+ getPatternInfo : ( pathObj , patternlab , isUnsubRun ) => {
242
272
const info = {
243
273
// 00-colors(.mustache) is subbed in 00-atoms-/00-global/00-colors
244
274
patternlab : patternlab ,
245
- patternHasOwnDir :
246
- path . basename ( pathObj . dir ) . replace ( prefixMatcher , '' ) ===
247
- pathObj . name . replace ( prefixMatcher , '' ) ||
248
- path . basename ( pathObj . dir ) . replace ( prefixMatcher , '' ) ===
249
- pathObj . name . split ( '~' ) [ 0 ] . replace ( prefixMatcher , '' ) ,
275
+ patternHasOwnDir : ! isUnsubRun
276
+ ? path . basename ( pathObj . dir ) . replace ( prefixMatcher , '' ) ===
277
+ pathObj . name . replace ( prefixMatcher , '' ) ||
278
+ path . basename ( pathObj . dir ) . replace ( prefixMatcher , '' ) ===
279
+ pathObj . name . split ( '~' ) [ 0 ] . replace ( prefixMatcher , '' )
280
+ : false ,
250
281
} ;
251
282
252
283
info . dir = info . patternHasOwnDir ? pathObj . dir . split ( path . sep ) . pop ( ) : '' ;
0 commit comments