Skip to content

Commit 920772a

Browse files
committed
Merge pull request #2 from apibyexample/nick/abe-json-builder-creation
ABE JSON builder creation
2 parents 46d1e3c + 3ef3b0c commit 920772a

3 files changed

Lines changed: 114 additions & 2 deletions

File tree

README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
[![Build Status](http://img.shields.io/travis/apibyexample/abe-json-builder/master.svg)](https://travis-ci.org/apibyexample/abe-json-builder)
2+
[![Dependency Status](https://david-dm.org/apibyexample/abe-json-builder/dev-status.svg)](https://david-dm.org/apibyexample/abe-json-builder#info=devDependencies)
3+
[![devDependency Status](https://david-dm.org/apibyexample/abe-json-builder/status.svg)](https://david-dm.org/apibyexample/abe-json-builder#info=dependencies)
4+
[![Monthly downloads](http://img.shields.io/npm/dm/abe-json-builder.svg)](https://www.npmjs.org/package/abe-json-builder)
5+
[![License](http://img.shields.io/npm/l/abe-json-builder.svg)](https://www.npmjs.org/package/abe-json-builder)
6+
17
abe-json-builder
28
================
39

@@ -63,3 +69,32 @@ Would be converted into two different files (using the file name and the example
6369
"payload": "Another Sample"
6470
}
6571
```
72+
73+
## Usage
74+
75+
It is expected that you are using ABE Spec's for this node module to be used, to
76+
take a look at the Spec's take a look at the repo [here](https://github.com/apibyexample/abe-spec).
77+
78+
``npm install --save-dev abe-json-builder``
79+
80+
You will need to run the JSON Builder prior to running your tests and/or server.
81+
82+
Example usage (file name createFeeds.js):
83+
84+
```js
85+
var createFeeds = require('abe-json-builder'),
86+
options = {
87+
'verbose': false,
88+
'location': 'myApp/mocks/**/*',
89+
'build': 'www/dist/json/'
90+
};
91+
92+
createFeeds.jsonBuilder(options);
93+
94+
```
95+
96+
To execute you would then need to run
97+
98+
``node createFeeds.js``
99+
100+
A suggestion would be to add this to your ``package.json`` as a script command.

package.json

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,33 @@
22
"name": "abe-json-builder",
33
"version": "0.1.0",
44
"description": "Strips ABE spec files to JSON files containing response body only.",
5-
"main": "src/index.js",
5+
"main": "src/abe-json-builder.js",
66
"scripts": {
77
"test": "grunt test"
88
},
9+
"keywords": [
10+
"abe",
11+
"mocks",
12+
"json"
13+
],
914
"repository": {
1015
"type": "git",
1116
"url": "git://github.com/apibyexample/abe-json-builder.git"
1217
},
13-
"author": "Rockabox <tech@rockabox.com> (http://rockabox.com)",
18+
"author": [
19+
"API By Example <apibyexample@gmail.com>",
20+
"Nick White <nickswhite89@gmail.com>"
21+
],
1422
"license": "MIT",
1523
"bugs": {
1624
"url": "https://github.com/apibyexample/abe-json-builder/issues"
1725
},
1826
"homepage": "https://github.com/apibyexample/abe-json-builder",
27+
"dependencies": {
28+
"colours": "~0.6.0-2",
29+
"glob": "~4.0.6",
30+
"lodash-node": "~2.4.1"
31+
},
1932
"devDependencies": {
2033
"grunt": "~0.4.5",
2134
"grunt-cli": "~0.1.13",

src/abe-json-builder.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
var colors = require('colours'),
2+
glob = require('glob'),
3+
path = require('path'),
4+
util = require('util'),
5+
lodash = require('lodash-node'),
6+
fs = require('fs'),
7+
errors = {
8+
'NOT_ABE': 'This file isn\'t valid ABE JSON format'
9+
},
10+
opt = {
11+
'verbose': false,
12+
'location': './',
13+
'build': 'tmp/'
14+
};
15+
16+
exports.jsonBuilder = function (options) {
17+
lodash.merge(opt, options);
18+
19+
glob
20+
.sync(opt.location + '.json', {
21+
mark: true
22+
})
23+
.forEach(function (match) {
24+
var json = require(process.cwd() + '/' + match);
25+
26+
// Check that the JSON passed to the function has examples / matches
27+
// ABE Spec
28+
if (!lodash.has(json, 'examples')) {
29+
return errors.NOT_ABE;
30+
}
31+
32+
var folderArr = path.dirname(match).split('/'),
33+
filePath = process.cwd() + '/' + opt.build,
34+
buildName = folderArr[folderArr.length - 1] + '-',
35+
baseName = path.basename(match, '.json');
36+
37+
if (!fs.existsSync(filePath)) {
38+
fs.mkdir(filePath, function (err) {
39+
if (err) {
40+
console.log(err.red);
41+
} else if (opt.verbose) {
42+
console.log(opt.build.yellow, ' created.');
43+
}
44+
});
45+
}
46+
47+
// Loop through each example within the JSON to create it's own
48+
// JSON file
49+
lodash.forEach(json.examples, function (obj, key) {
50+
var bodyData = json.examples[key].response.body,
51+
fileData = JSON.stringify(bodyData, null, 4),
52+
file = buildName + baseName + '-' + key + '.json';
53+
54+
fs.writeFile(filePath + file, fileData, function (err) {
55+
if (err) {
56+
console.log(err.red);
57+
} else if (opt.verbose) {
58+
console.log(file.green, ' file was saved.');
59+
}
60+
});
61+
});
62+
63+
});
64+
};

0 commit comments

Comments
 (0)