1- var colors = require ( 'colours' ) ,
2- fs = require ( 'fs' ) ,
1+ var fs = require ( 'fs' ) ,
32 glob = require ( 'glob' ) ,
43 lodash = require ( 'lodash-node' ) ,
54 mkdirp = require ( 'mkdirp' ) ,
65 path = require ( 'path' ) ,
7- util = require ( 'util' ) ,
86 errors = {
97 'NOT_ABE' : 'This file is an invalid ABE JSON format'
108 } ,
@@ -16,13 +14,18 @@ var colors = require('colours'),
1614
1715exports . jsonBuilder = function ( options , callback ) {
1816 lodash . merge ( opt , options ) ;
17+ var folderPath = path . join ( process . cwd ( ) , opt . build ) ;
18+
19+ if ( ! fs . existsSync ( folderPath ) ) {
20+ mkdirp . sync ( folderPath ) ;
21+ }
1922
2023 glob
2124 . sync ( opt . location + '.json' , {
2225 mark : true
2326 } )
2427 . forEach ( function ( match ) {
25- var json = require ( process . cwd ( ) + '/' + match ) ;
28+ var json = require ( path . join ( process . cwd ( ) , match ) ) ;
2629
2730 // Check that the JSON passed to the function has examples / matches
2831 // ABE Spec
@@ -31,34 +34,18 @@ exports.jsonBuilder = function (options, callback) {
3134 }
3235
3336 var folderArr = path . dirname ( match ) . split ( '/' ) ,
34- filePath = process . cwd ( ) + '/' + opt . build ,
3537 buildName = folderArr [ folderArr . length - 1 ] + '-' ,
3638 baseName = path . basename ( match , '.json' ) ;
3739
38- if ( ! fs . existsSync ( filePath ) ) {
39- mkdirp ( filePath , function ( err ) {
40- if ( err ) {
41- console . log ( err ) ;
42- } else if ( opt . verbose ) {
43- console . log ( opt . build . yellow , ' created.' ) ;
44- }
45- } ) ;
46- }
47-
4840 // Loop through each example within the JSON to create it's own
4941 // JSON file
5042 lodash . forEach ( json . examples , function ( obj , key ) {
51- var bodyData = json . examples [ key ] . response . body ,
52- fileData = JSON . stringify ( bodyData , null , 4 ) ,
53- file = buildName + baseName + '-' + key + '.json' ;
43+ var data = JSON . stringify ( json
44+ . examples [ key ] . response . body , null , 4 ) ,
45+ file = buildName + baseName + '-' + key + '.json' ,
46+ filePath = path . join ( folderPath , file ) ;
5447
55- fs . writeFile ( filePath + file , fileData , function ( err ) {
56- if ( err ) {
57- console . log ( err ) ;
58- } else if ( opt . verbose ) {
59- console . log ( file . green , ' file was saved.' ) ;
60- }
61- } ) ;
48+ fs . writeFileSync ( filePath , data ) ;
6249 } ) ;
6350
6451 } ) ;
0 commit comments