Skip to content

Commit

Permalink
fix(package): Allow .json extension on annotations file (issue patter…
Browse files Browse the repository at this point in the history
  • Loading branch information
engelfrost committed Jul 10, 2018
1 parent 7232cd4 commit b92e62b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,43 +6,49 @@ const _ = require('lodash');
const mp = require('./markdown_parser');
const logger = require('./log');

const annotations_exporter = function(pl) {
const annotationExporter = function(pl) {
const paths = pl.config.paths;
let oldAnnotations;

/**
* Parses JS annotations.
* @returns array of comments that used to be wrapped in raw JS
*/
function parseAnnotationsJS() {
function parseAnnotationsJSON() {
const jsonPath = path.resolve(paths.source.annotations, 'annotations.json');
let annotations;

//attempt to read the file
try {
oldAnnotations = fs.readFileSync(
path.resolve(paths.source.annotations, 'annotations.js'),
'utf8'
);
if (fs.pathExistsSync(jsonPath)) {
//read the new file
annotations = fs.readFileSync(jsonPath, 'utf8');
} else {
//read the old file
const jsPath = path.resolve(paths.source.annotations, 'annotations.js');

annotations = fs
.readFileSync(jsPath, 'utf8')
.replace(/^\s*var comments ?= ?/, '')
.replace(/};\s*$/, '}');

logger.info(
`Please convert ${jsPath} to JSON and rename it annotations.json.`
);
}
} catch (ex) {
logger.debug(
`annotations.js file missing from ${
`annotations.json file missing from ${
paths.source.annotations
}. This may be expected if you do not use annotations or are using markdown.`
);
return [];
}

//parse as JSON by removing the old wrapping js syntax. comments and the trailing semi-colon
oldAnnotations = oldAnnotations.replace('var comments = ', '');
oldAnnotations = oldAnnotations.replace('};', '}');

try {
const oldAnnotationsJSON = JSON.parse(oldAnnotations);
return oldAnnotationsJSON.comments;
const annotationsJSON = JSON.parse(annotations);
return annotationsJSON.comments;
} catch (ex) {
logger.error(
`There was an error parsing JSON for ${
paths.source.annotations
}annotations.js`
);
logger.error(`There was an error parsing JSON for ${jsonPath}`);
return [];
}
}
Expand Down Expand Up @@ -108,7 +114,7 @@ const annotations_exporter = function(pl) {
* @returns array of annotations
*/
function gatherAnnotations() {
const annotationsJS = parseAnnotationsJS();
const annotationsJS = parseAnnotationsJSON();
const annotationsMD = parseAnnotationsMD();
return _.unionBy(annotationsJS, annotationsMD, 'el');
}
Expand All @@ -117,13 +123,13 @@ const annotations_exporter = function(pl) {
gather: function() {
return gatherAnnotations();
},
gatherJS: function() {
return parseAnnotationsJS();
gatherJSON: function() {
return parseAnnotationsJSON();
},
gatherMD: function() {
return parseAnnotationsMD();
},
};
};

module.exports = annotations_exporter;
module.exports = annotationExporter;
6 changes: 3 additions & 3 deletions packages/core/src/lib/exportData.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const eol = require('os').EOL;
const path = require('path');
const _ = require('lodash');

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

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

Expand All @@ -13,7 +13,7 @@ let fs = require('fs-extra'); //eslint-disable-line prefer-const
* @param patternlab - global data store
*/
module.exports = function(patternlab) {
const annotation_exporter = new ae(patternlab);
const annotationExporter = new ae(patternlab);

const paths = patternlab.config.paths;

Expand Down Expand Up @@ -68,7 +68,7 @@ module.exports = function(patternlab) {
eol;

//annotations
const annotationsJSON = annotation_exporter.gather();
const annotationsJSON = annotationExporter.gather();
const annotations =
'var comments = { "comments" : ' + JSON.stringify(annotationsJSON) + '};';
_.each(patternlab.uikits, uikit => {
Expand Down
6 changes: 3 additions & 3 deletions packages/core/test/annotation_exporter_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ function createFakePatternLab(anPath, customProps) {
}

var patternlab = createFakePatternLab(anPath);
var ae = require('../src/lib/annotation_exporter')(patternlab);
var ae = require('../src/lib/annotationExporter')(patternlab);

tap.test('converts old JS annotations into new format', function(test) {
//arrange
//act
var annotations = ae.gatherJS();
var annotations = ae.gatherJSON();

//assert
test.equals(annotations.length, 2);
Expand Down Expand Up @@ -77,7 +77,7 @@ tap.test('merges both annotation methods into one array', function(test) {
tap.test('when there are 0 annotation files', function(test) {
var emptyAnPath = './test/files/empty/';
var patternlab2 = createFakePatternLab(emptyAnPath);
var ae2 = require('../src/lib/annotation_exporter')(patternlab2);
var ae2 = require('../src/lib/annotationExporter')(patternlab2);

var annotations = ae2.gather();
test.equals(annotations.length, 0);
Expand Down

0 comments on commit b92e62b

Please sign in to comment.