@@ -6,43 +6,49 @@ const _ = require('lodash');
6
6
const mp = require ( './markdown_parser' ) ;
7
7
const logger = require ( './log' ) ;
8
8
9
- const annotations_exporter = function ( pl ) {
9
+ const annotationExporter = function ( pl ) {
10
10
const paths = pl . config . paths ;
11
- let oldAnnotations ;
12
11
13
12
/**
14
13
* Parses JS annotations.
15
14
* @returns array of comments that used to be wrapped in raw JS
16
15
*/
17
- function parseAnnotationsJS ( ) {
16
+ function parseAnnotationsJSON ( ) {
17
+ const jsonPath = path . resolve ( paths . source . annotations , 'annotations.json' ) ;
18
+ let annotations ;
19
+
18
20
//attempt to read the file
19
21
try {
20
- oldAnnotations = fs . readFileSync (
21
- path . resolve ( paths . source . annotations , 'annotations.js' ) ,
22
- 'utf8'
23
- ) ;
22
+ if ( fs . pathExistsSync ( jsonPath ) ) {
23
+ //read the new file
24
+ annotations = fs . readFileSync ( jsonPath , 'utf8' ) ;
25
+ } else {
26
+ //read the old file
27
+ const jsPath = path . resolve ( paths . source . annotations , 'annotations.js' ) ;
28
+
29
+ annotations = fs
30
+ . readFileSync ( jsPath , 'utf8' )
31
+ . replace ( / ^ \s * v a r c o m m e n t s ? = ? / , '' )
32
+ . replace ( / } ; \s * $ / , '}' ) ;
33
+
34
+ logger . info (
35
+ `Please convert ${ jsPath } to JSON and rename it annotations.json.`
36
+ ) ;
37
+ }
24
38
} catch ( ex ) {
25
39
logger . debug (
26
- `annotations.js file missing from ${
40
+ `annotations.json file missing from ${
27
41
paths . source . annotations
28
42
} . This may be expected if you do not use annotations or are using markdown.`
29
43
) ;
30
44
return [ ] ;
31
45
}
32
46
33
- //parse as JSON by removing the old wrapping js syntax. comments and the trailing semi-colon
34
- oldAnnotations = oldAnnotations . replace ( 'var comments = ' , '' ) ;
35
- oldAnnotations = oldAnnotations . replace ( '};' , '}' ) ;
36
-
37
47
try {
38
- const oldAnnotationsJSON = JSON . parse ( oldAnnotations ) ;
39
- return oldAnnotationsJSON . comments ;
48
+ const annotationsJSON = JSON . parse ( annotations ) ;
49
+ return annotationsJSON . comments ;
40
50
} catch ( ex ) {
41
- logger . error (
42
- `There was an error parsing JSON for ${
43
- paths . source . annotations
44
- } annotations.js`
45
- ) ;
51
+ logger . error ( `There was an error parsing JSON for ${ jsonPath } ` ) ;
46
52
return [ ] ;
47
53
}
48
54
}
@@ -108,7 +114,7 @@ const annotations_exporter = function(pl) {
108
114
* @returns array of annotations
109
115
*/
110
116
function gatherAnnotations ( ) {
111
- const annotationsJS = parseAnnotationsJS ( ) ;
117
+ const annotationsJS = parseAnnotationsJSON ( ) ;
112
118
const annotationsMD = parseAnnotationsMD ( ) ;
113
119
return _ . unionBy ( annotationsJS , annotationsMD , 'el' ) ;
114
120
}
@@ -117,13 +123,13 @@ const annotations_exporter = function(pl) {
117
123
gather : function ( ) {
118
124
return gatherAnnotations ( ) ;
119
125
} ,
120
- gatherJS : function ( ) {
121
- return parseAnnotationsJS ( ) ;
126
+ gatherJSON : function ( ) {
127
+ return parseAnnotationsJSON ( ) ;
122
128
} ,
123
129
gatherMD : function ( ) {
124
130
return parseAnnotationsMD ( ) ;
125
131
} ,
126
132
} ;
127
133
} ;
128
134
129
- module . exports = annotations_exporter ;
135
+ module . exports = annotationExporter ;
0 commit comments