-
Notifications
You must be signed in to change notification settings - Fork 402
Add pattern export all #601
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 () { | ||
|
|
||
|
|
@@ -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]); | ||
| } | ||
| } | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| //find the chosen patterns to export | ||
| for (var i = 0; i < exportPartials.length; i++) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have no idea what that JSON.parse is doing in there.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is no We do have a |
||
| } | ||
|
|
||
| fs.outputFileSync(path.join(patternDir, patternName) + patternFileExtension, patternCode); | ||
| } | ||
|
|
||
| return { | ||
| export_patterns: function (patternlab) { | ||
| exportPatterns(patternlab); | ||
|
|
||
There was a problem hiding this comment.
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===truebefore callingexportSinglePattern()to avoid having the meta head and meta foot from being exported.