Skip to content

Commit 6bda3bc

Browse files
committed
Merge pull request #10 from apibyexample/nick/bugfix-mkdir-sync
Bugfix mkdir sync
2 parents 3a902f7 + 455957c commit 6bda3bc

2 files changed

Lines changed: 12 additions & 26 deletions

File tree

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
},
2626
"homepage": "https://github.com/apibyexample/abe-json-builder",
2727
"dependencies": {
28-
"colours": "~0.6.0-2",
2928
"glob": "~4.0.6",
3029
"lodash-node": "~2.4.1",
3130
"mkdirp": "~0.5.0"

src/abe-json-builder.js

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
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

1715
exports.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

Comments
 (0)