Skip to content

Commit 5342213

Browse files
Merge pull request #773 from pattern-lab/exportData
feat(exportData): Broke out file with unit test coverage
2 parents 63117e2 + 9d788b7 commit 5342213

File tree

3 files changed

+121
-44
lines changed

3 files changed

+121
-44
lines changed

core/lib/exportData.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
"use strict";
2+
3+
const eol = require('os').EOL;
4+
const path = require('path');
5+
const _ = require('lodash');
6+
7+
const ae = require('./annotation_exporter');
8+
9+
let fs = require('fs-extra'); //eslint-disable-line prefer-const
10+
11+
/**
12+
* Write out our pattern information for use by the front end
13+
* @param patternlab - global data store
14+
*/
15+
module.exports = function (patternlab) {
16+
17+
const annotation_exporter = new ae(patternlab);
18+
19+
const paths = patternlab.config.paths;
20+
21+
//write out the data
22+
let output = '';
23+
24+
//config
25+
output += 'var config = ' + JSON.stringify(patternlab.config) + ';\n';
26+
27+
//ishControls
28+
output += 'var ishControls = {"ishControlsHide":' + JSON.stringify(patternlab.config.ishControlsHide) + '};' + eol;
29+
30+
//navItems
31+
output += 'var navItems = {"patternTypes": ' + JSON.stringify(patternlab.patternTypes) + ', "ishControlsHide": ' + JSON.stringify(patternlab.config.ishControlsHide) + '};' + eol;
32+
33+
//patternPaths
34+
output += 'var patternPaths = ' + JSON.stringify(patternlab.patternPaths) + ';' + eol;
35+
36+
//viewAllPaths
37+
output += 'var viewAllPaths = ' + JSON.stringify(patternlab.viewAllPaths) + ';' + eol;
38+
39+
//plugins
40+
output += 'var plugins = ' + JSON.stringify(patternlab.plugins || []) + ';' + eol;
41+
42+
//smaller config elements
43+
output += 'var defaultShowPatternInfo = ' + (patternlab.config.defaultShowPatternInfo ? patternlab.config.defaultShowPatternInfo : 'false') + ';' + eol;
44+
output += 'var defaultPattern = "' + (patternlab.config.defaultPattern ? patternlab.config.defaultPattern : 'all') + '";' + eol;
45+
46+
//annotations
47+
const annotationsJSON = annotation_exporter.gather();
48+
const annotations = 'var comments = { "comments" : ' + JSON.stringify(annotationsJSON) + '};';
49+
fs.outputFileSync(path.resolve(paths.public.annotations, 'annotations.js'), annotations);
50+
51+
//write all output to patternlab-data
52+
fs.outputFileSync(path.resolve(paths.public.data, 'patternlab-data.js'), output);
53+
return output;
54+
};

core/lib/ui_builder.js

Lines changed: 1 addition & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
const path = require('path');
44
const _ = require('lodash');
5-
const eol = require('os').EOL;
65

7-
const ae = require('./annotation_exporter');
86
const of = require('./object_factory');
97
const Pattern = of.Pattern;
108
const logger = require('./log');
@@ -13,6 +11,7 @@ const logger = require('./log');
1311
let render = require('./render'); //eslint-disable-line prefer-const
1412
let fs = require('fs-extra'); //eslint-disable-line prefer-const
1513
let buildFooter = require('./buildFooter'); //eslint-disable-line prefer-const
14+
let exportData = require('./exportData'); //eslint-disable-line prefer-const
1615

1716
const ui_builder = function () {
1817

@@ -566,48 +565,6 @@ const ui_builder = function () {
566565
});
567566
}
568567

569-
/**
570-
* Write out our pattern information for use by the front end
571-
* @param patternlab - global data store
572-
*/
573-
function exportData(patternlab) {
574-
const annotation_exporter = new ae(patternlab);
575-
const paths = patternlab.config.paths;
576-
577-
//write out the data
578-
let output = '';
579-
580-
//config
581-
output += 'var config = ' + JSON.stringify(patternlab.config) + ';\n';
582-
583-
//ishControls
584-
output += 'var ishControls = {"ishControlsHide":' + JSON.stringify(patternlab.config.ishControlsHide) + '};' + eol;
585-
586-
//navItems
587-
output += 'var navItems = {"patternTypes": ' + JSON.stringify(patternlab.patternTypes) + ', "ishControlsHide": ' + JSON.stringify(patternlab.config.ishControlsHide) + '};' + eol;
588-
589-
//patternPaths
590-
output += 'var patternPaths = ' + JSON.stringify(patternlab.patternPaths) + ';' + eol;
591-
592-
//viewAllPaths
593-
output += 'var viewAllPaths = ' + JSON.stringify(patternlab.viewAllPaths) + ';' + eol;
594-
595-
//plugins
596-
output += 'var plugins = ' + JSON.stringify(patternlab.plugins || []) + ';' + eol;
597-
598-
//smaller config elements
599-
output += 'var defaultShowPatternInfo = ' + (patternlab.config.defaultShowPatternInfo ? patternlab.config.defaultShowPatternInfo : 'false') + ';' + eol;
600-
output += 'var defaultPattern = "' + (patternlab.config.defaultPattern ? patternlab.config.defaultPattern : 'all') + '";' + eol;
601-
602-
//write all output to patternlab-data
603-
fs.outputFileSync(path.resolve(paths.public.data, 'patternlab-data.js'), output);
604-
605-
//annotations
606-
const annotationsJSON = annotation_exporter.gather();
607-
const annotations = 'var comments = { "comments" : ' + JSON.stringify(annotationsJSON) + '};';
608-
fs.outputFileSync(path.resolve(paths.public.annotations, 'annotations.js'), annotations);
609-
}
610-
611568
/**
612569
* Reset any global data we use between builds to guard against double adding things
613570
*/

test/exportData_tests.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
"use strict";
2+
3+
const path = require('path');
4+
const tap = require('tap');
5+
const rewire = require("rewire");
6+
7+
const exportData = rewire('../core/lib/exportData');
8+
const util = require('./util/test_utils.js');
9+
10+
const testPatternsPath = path.resolve(__dirname, 'files', '_patterns');
11+
12+
const fsMock = {
13+
outputFileSync: function (path, content) { /* INTENTIONAL NOOP */},
14+
};
15+
16+
//set our mocks in place of usual require()
17+
exportData.__set__({
18+
'fs': fsMock,
19+
});
20+
21+
const patternlab = util.fakePatternLab(testPatternsPath);
22+
const result = exportData(patternlab);
23+
24+
tap.test('exportData exports config', function (test) {
25+
test.equals(result.indexOf('config') > -1, true);
26+
test.equals(result.indexOf('paths') > -1, true);
27+
test.equals(result.indexOf('theme') > -1, true);
28+
test.end();
29+
});
30+
31+
tap.test('exportData exports ishControls', function (test) {
32+
test.equals(result.indexOf('ishControlsHide') > -1, true);
33+
test.end();
34+
});
35+
36+
tap.test('exportData exports navItems', function (test) {
37+
test.equals(result.indexOf('patternTypes') > -1, true);
38+
test.end();
39+
});
40+
41+
tap.test('exportData exports patternPaths', function (test) {
42+
test.equals(result.indexOf('patternPaths') > -1, true);
43+
test.end();
44+
});
45+
46+
tap.test('exportData exports viewAllPaths', function (test) {
47+
test.equals(result.indexOf('viewAllPaths') > -1, true);
48+
test.end();
49+
});
50+
51+
tap.test('exportData exports plugins', function (test) {
52+
test.equals(result.indexOf('plugins') > -1, true);
53+
test.end();
54+
});
55+
56+
tap.test('exportData exports defaultShowPatternInfo', function (test) {
57+
test.equals(result.indexOf('defaultShowPatternInfo') > -1, true);
58+
test.equals(result.indexOf('"defaultShowPatternInfo":false') > -1, true);
59+
test.end();
60+
});
61+
62+
tap.test('exportData exports defaultPattern', function (test) {
63+
test.equals(result.indexOf('defaultPattern') > -1, true);
64+
test.equals(result.indexOf('"defaultPattern":"all"') > -1, true);
65+
test.end();
66+
});

0 commit comments

Comments
 (0)