Skip to content

Commit 410032e

Browse files
committed
Make pattern export optionally preserve directory structure.
Instead of prefixing patterns with "<category>-", we prefix the directory path with "<category>/".
1 parent 19b0cce commit 410032e

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

core/lib/pattern_exporter.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"use strict";
22

33
var fs = require('fs-extra');
4+
var path = require('path');
45

56
var pattern_exporter = function () {
67

@@ -19,7 +20,7 @@ var pattern_exporter = function () {
1920
if (exportAll) {
2021
for (var i = 0; i < patternlab.patterns.length; i++) {
2122
if (!patternlab.patterns[i].patternPartial.startsWith('-')) {
22-
fs.outputFileSync(patternlab.config.patternExportDirectory + patternlab.patterns[i].patternPartial + '.html', patternlab.patterns[i].patternPartialCode);
23+
exportSinglePattern(patternlab, patternlab.patterns[i]);
2324
}
2425
}
2526

@@ -31,12 +32,26 @@ var pattern_exporter = function () {
3132
for (var j = 0; j < patternlab.patterns.length; j++) {
3233
if (exportPartials[i] === patternlab.patterns[j].patternPartial) {
3334
//write matches to the desired location
34-
fs.outputFileSync(patternlab.config.patternExportDirectory + patternlab.patterns[j].patternPartial + '.html', patternlab.patterns[j].patternPartialCode);
35+
exportSinglePattern(patternlab, patternlab.patterns[j]);
3536
}
3637
}
3738
}
3839
}
3940

41+
function exportSinglePattern(patternlab, pattern) {
42+
var preserveDirStructure = patternlab.config.patternExportPreserveDirectoryStructure;
43+
var patternName = pattern.patternPartial;
44+
var patternDir = patternlab.config.patternExportDirectory;
45+
if (preserveDirStructure) {
46+
// Extract the first part of the pattern partial as the directory in which
47+
// it should go.
48+
patternDir = path.join(patternDir, pattern.patternPartial.split('-')[0]);
49+
patternName = pattern.patternPartial.split('-').slice(1).join('-');
50+
}
51+
52+
fs.outputFileSync(path.join(patternDir, patternName) + '.html', pattern.patternPartialCode);
53+
}
54+
4055
return {
4156
export_patterns: function (patternlab) {
4257
exportPatterns(patternlab);

patternlab-config.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
"patternStates": {
5454
},
5555
"patternExportAll": false,
56+
"patternExportPreserveDirectoryStructure": false,
5657
"patternExportPatternPartials": [],
5758
"patternExportDirectory": "./pattern_exports/",
5859
"cacheBust": true,

0 commit comments

Comments
 (0)