Skip to content

Commit

Permalink
Merge branch 'jwir3-add-pattern-export-all' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
bmuenzenmeyer committed Oct 29, 2019
2 parents eb7ccb4 + b210d82 commit b33ff91
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 6 deletions.
47 changes: 41 additions & 6 deletions packages/core/src/lib/pattern_exporter.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,35 @@
'use strict';

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

function exportSinglePattern(patternlab, pattern) {
const preserveDirStructure =
patternlab.config.patternExportPreserveDirectoryStructure;
let patternName = pattern.patternPartial;
let patternDir = patternlab.config.patternExportDirectory;
let patternCode = pattern.patternPartialCode;
let 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 = `.${patternlab.config.patternExtension}`;
}

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

const pattern_exporter = function() {
/**
Expand All @@ -13,18 +42,24 @@ const pattern_exporter = function() {
function exportPatterns(patternlab) {
//read the config export options
const exportPartials = patternlab.config.patternExportPatternPartials;
const exportAll = patternlab.config.patternExportAll;

if (exportAll) {
for (let 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 (let i = 0; i < exportPartials.length; i++) {
for (let 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]);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,16 @@
"m": [500, 800],
"l": [800, 2600]
},
<<<<<<< HEAD:packages/development-edition-engine-react/patternlab-config.json
"logLevel": "info",
=======
"patternExportAll": false,
"patternExportPreserveDirectoryStructure": false,
"patternExportRaw": false,
"patternExportPatternPartials": [],
"patternExportDirectory": "./pattern_exports/",
"cacheBust": true,
>>>>>>> 436dd99e50d808cd14691593b927209f1ecb663e:patternlab-config.json
"outputFileSuffixes": {
"rendered": ".rendered",
"rawTemplate": "",
Expand Down

0 comments on commit b33ff91

Please sign in to comment.