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
54 changes: 54 additions & 0 deletions core/lib/exportData.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
"use strict";

const eol = require('os').EOL;
const path = require('path');
const _ = require('lodash');

const ae = require('./annotation_exporter');

let fs = require('fs-extra'); //eslint-disable-line prefer-const

/**
* Write out our pattern information for use by the front end
* @param patternlab - global data store
*/
module.exports = function (patternlab) {

const annotation_exporter = new ae(patternlab);

const paths = patternlab.config.paths;

//write out the data
let output = '';

//config
output += 'var config = ' + JSON.stringify(patternlab.config) + ';\n';

//ishControls
output += 'var ishControls = {"ishControlsHide":' + JSON.stringify(patternlab.config.ishControlsHide) + '};' + eol;

//navItems
output += 'var navItems = {"patternTypes": ' + JSON.stringify(patternlab.patternTypes) + ', "ishControlsHide": ' + JSON.stringify(patternlab.config.ishControlsHide) + '};' + eol;

//patternPaths
output += 'var patternPaths = ' + JSON.stringify(patternlab.patternPaths) + ';' + eol;

//viewAllPaths
output += 'var viewAllPaths = ' + JSON.stringify(patternlab.viewAllPaths) + ';' + eol;

//plugins
output += 'var plugins = ' + JSON.stringify(patternlab.plugins || []) + ';' + eol;

//smaller config elements
output += 'var defaultShowPatternInfo = ' + (patternlab.config.defaultShowPatternInfo ? patternlab.config.defaultShowPatternInfo : 'false') + ';' + eol;
output += 'var defaultPattern = "' + (patternlab.config.defaultPattern ? patternlab.config.defaultPattern : 'all') + '";' + eol;

//annotations
const annotationsJSON = annotation_exporter.gather();
const annotations = 'var comments = { "comments" : ' + JSON.stringify(annotationsJSON) + '};';
fs.outputFileSync(path.resolve(paths.public.annotations, 'annotations.js'), annotations);

//write all output to patternlab-data
fs.outputFileSync(path.resolve(paths.public.data, 'patternlab-data.js'), output);
return output;
};
45 changes: 1 addition & 44 deletions core/lib/ui_builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

const path = require('path');
const _ = require('lodash');
const eol = require('os').EOL;

const ae = require('./annotation_exporter');
const of = require('./object_factory');
const Pattern = of.Pattern;
const logger = require('./log');
Expand All @@ -13,6 +11,7 @@ const logger = require('./log');
let render = require('./render'); //eslint-disable-line prefer-const
let fs = require('fs-extra'); //eslint-disable-line prefer-const
let buildFooter = require('./buildFooter'); //eslint-disable-line prefer-const
let exportData = require('./exportData'); //eslint-disable-line prefer-const

const ui_builder = function () {

Expand Down Expand Up @@ -566,48 +565,6 @@ const ui_builder = function () {
});
}

/**
* Write out our pattern information for use by the front end
* @param patternlab - global data store
*/
function exportData(patternlab) {
const annotation_exporter = new ae(patternlab);
const paths = patternlab.config.paths;

//write out the data
let output = '';

//config
output += 'var config = ' + JSON.stringify(patternlab.config) + ';\n';

//ishControls
output += 'var ishControls = {"ishControlsHide":' + JSON.stringify(patternlab.config.ishControlsHide) + '};' + eol;

//navItems
output += 'var navItems = {"patternTypes": ' + JSON.stringify(patternlab.patternTypes) + ', "ishControlsHide": ' + JSON.stringify(patternlab.config.ishControlsHide) + '};' + eol;

//patternPaths
output += 'var patternPaths = ' + JSON.stringify(patternlab.patternPaths) + ';' + eol;

//viewAllPaths
output += 'var viewAllPaths = ' + JSON.stringify(patternlab.viewAllPaths) + ';' + eol;

//plugins
output += 'var plugins = ' + JSON.stringify(patternlab.plugins || []) + ';' + eol;

//smaller config elements
output += 'var defaultShowPatternInfo = ' + (patternlab.config.defaultShowPatternInfo ? patternlab.config.defaultShowPatternInfo : 'false') + ';' + eol;
output += 'var defaultPattern = "' + (patternlab.config.defaultPattern ? patternlab.config.defaultPattern : 'all') + '";' + eol;

//write all output to patternlab-data
fs.outputFileSync(path.resolve(paths.public.data, 'patternlab-data.js'), output);

//annotations
const annotationsJSON = annotation_exporter.gather();
const annotations = 'var comments = { "comments" : ' + JSON.stringify(annotationsJSON) + '};';
fs.outputFileSync(path.resolve(paths.public.annotations, 'annotations.js'), annotations);
}

/**
* Reset any global data we use between builds to guard against double adding things
*/
Expand Down
66 changes: 66 additions & 0 deletions test/exportData_tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
"use strict";

const path = require('path');
const tap = require('tap');
const rewire = require("rewire");

const exportData = rewire('../core/lib/exportData');
const util = require('./util/test_utils.js');

const testPatternsPath = path.resolve(__dirname, 'files', '_patterns');

const fsMock = {
outputFileSync: function (path, content) { /* INTENTIONAL NOOP */},
};

//set our mocks in place of usual require()
exportData.__set__({
'fs': fsMock,
});

const patternlab = util.fakePatternLab(testPatternsPath);
const result = exportData(patternlab);

tap.test('exportData exports config', function (test) {
test.equals(result.indexOf('config') > -1, true);
test.equals(result.indexOf('paths') > -1, true);
test.equals(result.indexOf('theme') > -1, true);
test.end();
});

tap.test('exportData exports ishControls', function (test) {
test.equals(result.indexOf('ishControlsHide') > -1, true);
test.end();
});

tap.test('exportData exports navItems', function (test) {
test.equals(result.indexOf('patternTypes') > -1, true);
test.end();
});

tap.test('exportData exports patternPaths', function (test) {
test.equals(result.indexOf('patternPaths') > -1, true);
test.end();
});

tap.test('exportData exports viewAllPaths', function (test) {
test.equals(result.indexOf('viewAllPaths') > -1, true);
test.end();
});

tap.test('exportData exports plugins', function (test) {
test.equals(result.indexOf('plugins') > -1, true);
test.end();
});

tap.test('exportData exports defaultShowPatternInfo', function (test) {
test.equals(result.indexOf('defaultShowPatternInfo') > -1, true);
test.equals(result.indexOf('"defaultShowPatternInfo":false') > -1, true);
test.end();
});

tap.test('exportData exports defaultPattern', function (test) {
test.equals(result.indexOf('defaultPattern') > -1, true);
test.equals(result.indexOf('"defaultPattern":"all"') > -1, true);
test.end();
});