Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 34 additions & 1 deletion core/lib/pattern_exporter.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"use strict";

var fs = require('fs-extra');
var path = require('path');

var pattern_exporter = function () {

Expand All @@ -14,18 +15,50 @@ var pattern_exporter = function () {
function exportPatterns(patternlab) {
//read the config export options
var exportPartials = patternlab.config.patternExportPatternPartials;
var exportAll = patternlab.config.patternExportAll;

if (exportAll) {
for (var i = 0; i < patternlab.patterns.length; i++) {
if (!patternlab.patterns[i].patternPartial.startsWith('-')) {
exportSinglePattern(patternlab, patternlab.patterns[i]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd suggest adding a check for to make sure patternlab.patterns[i].isPattern === true before calling exportSinglePattern() to avoid having the meta head and meta foot from being exported.

}
}

return;
}

//find the chosen patterns to export
for (var i = 0; i < exportPartials.length; i++) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I cannot think of a need to loop through a provided array if the user has chosen to export all. I suggest making these mutually exclusive. Should be simple to add this as an else block as part of the exportAll check

for (var j = 0; j < patternlab.patterns.length; j++) {
if (exportPartials[i] === patternlab.patterns[j].patternPartial) {
//write matches to the desired location
fs.outputFileSync(patternlab.config.patternExportDirectory + patternlab.patterns[j].patternPartial + '.html', patternlab.patterns[j].patternPartialCode);
exportSinglePattern(patternlab, patternlab.patterns[j]);
}
}
}
}

function exportSinglePattern(patternlab, pattern) {
var preserveDirStructure = patternlab.config.patternExportPreserveDirectoryStructure;
var patternName = pattern.patternPartial;
var patternDir = patternlab.config.patternExportDirectory;
var patternCode = pattern.patternPartialCode;
var patternFileExtension = ".html";
if (preserveDirStructure) {
// Extract the first part of the pattern partial as the directory in which
// it should go.
patternDir = path.join(patternDir, pattern.patternPartial.split('-')[0]);
patternName = pattern.patternPartial.split('-').slice(1).join('-');
}

if (patternlab.config.patternExportRaw) {
patternCode = pattern.extendedTemplate;
patternFileExtension = "." + JSON.parse(pattern.patternData).patternExtension;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have no idea what that JSON.parse is doing in there.

pattern.patternExtension suffices.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no pattern.patternExtension - "patternExtension" only lives within pattern.patternData - which does seem lto be a stringified JSON object (hence the parse @jwir3 added).

We do have a pattern.fileExtension available (which shows up with the period in it, such as .hbs. I'm guessing we could use this to clean it up, but I'm not sure to why we have this difference between fileExtension and patternExtension, and why patternExtension is only available in that weird string JSON patternData.

}

fs.outputFileSync(path.join(patternDir, patternName) + patternFileExtension, patternCode);
}

return {
export_patterns: function (patternlab) {
exportPatterns(patternlab);
Expand Down
3 changes: 3 additions & 0 deletions patternlab-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@
"patternStateCascade": ["inprogress", "inreview", "complete"],
"patternStates": {
},
"patternExportAll": false,
"patternExportPreserveDirectoryStructure": false,
"patternExportRaw": false,
"patternExportPatternPartials": [],
"patternExportDirectory": "./pattern_exports/",
"cacheBust": true,
Expand Down