diff --git a/core/lib/exportData.js b/core/lib/exportData.js new file mode 100644 index 000000000..0503c7aad --- /dev/null +++ b/core/lib/exportData.js @@ -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; +}; diff --git a/core/lib/ui_builder.js b/core/lib/ui_builder.js index ea91ca4a6..c81d7679b 100644 --- a/core/lib/ui_builder.js +++ b/core/lib/ui_builder.js @@ -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'); @@ -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 () { @@ -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 */ diff --git a/test/exportData_tests.js b/test/exportData_tests.js new file mode 100644 index 000000000..29fdf71d6 --- /dev/null +++ b/test/exportData_tests.js @@ -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(); +});