Skip to content

Commit 84fcc8f

Browse files
committed
updated specs and error handling
1 parent 536cfa6 commit 84fcc8f

5 files changed

Lines changed: 43 additions & 20 deletions

File tree

lib/create.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,14 @@ function create (pathString, filename, content, callback) {
2727
}
2828

2929
if (typeof content === 'object') {
30-
if (!check('object', content)) {
31-
return callback({
32-
message: 'JSON is not valid',
33-
error: 'NOJSON'
34-
});
35-
}
36-
3730
content = JSON.stringify(content);
3831
}
3932

4033
if (typeof content !== 'string') {
41-
return callback({err: 'Json string or object required'});
34+
return callback({
35+
message: 'JSON is not valid',
36+
error: 'NOJSON'
37+
});
4238
}
4339

4440
if (!check(content)) {

lib/createSync.js

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,12 @@ function createSync(pathString, filename, content) {
1717
content = typeof content === 'undefined' ? '{}' : content;
1818

1919
if (typeof content === 'object') {
20-
if (!check('object', content)) {
21-
return false;
22-
}
23-
2420
content = JSON.stringify(content);
2521
}
2622

27-
if (typeof content !== 'string') {
28-
console.log('Error. Please enter a json string as content.'.bold.red);
29-
return false;
30-
}
23+
if (typeof content !== 'string') return false;
3124

32-
if (!check(content)) {
33-
return false;
34-
}
25+
if (!check(content)) return false;
3526

3627
fs.ensureDirSync(pathString);
3728
fs.writeFileSync(combinePath, content, 'utf8');

lib/toPath.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
'use strict';
22

3-
var mkdirp = require('mkdirp');
43
var colors = require('colors');
54
var path = require('path');
65
var json = require('../index');

test/create.spec.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,35 @@ describe('create.js', function () {
3232
});
3333
});
3434

35+
it('should generate an empty json file', function (done) {
36+
base.create(testCwd, 'test.json', function (err) {
37+
expect(err).to.not.exist;
38+
39+
expect(fs.existsSync(path.join(testCwd, 'test.json'))).to.be.true;
40+
41+
done();
42+
});
43+
});
44+
45+
it('should set the extname to json if it is not set to it', function (done) {
46+
base.create(testCwd, 'test.jayson', '{"test": "string"}', function (err) {
47+
expect(err).to.not.exist;
48+
49+
expect(fs.existsSync(path.join(testCwd, 'test.json'))).to.be.true;
50+
expect(fs.existsSync(path.join(testCwd, 'test.jayson'))).to.be.false;
51+
52+
done();
53+
});
54+
});
55+
56+
it('should fail', function (done) {
57+
base.create(testCwd, 'test.jayson', 123123, function (err) {
58+
expect(err.error).to.equal('NOJSON');
59+
60+
done();
61+
});
62+
});
63+
3564
it('should fail', function (done) {
3665
base.create(testCwd, 'test.json', '{"test"; "string"}', function (err) {
3766
expect(err.error).to.equal('NOJSON');

test/createSync.spec.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,12 @@ describe('createSync.js', function () {
4343

4444
done();
4545
});
46+
47+
it('should fail', function (done) {
48+
var file = base.createSync(testCwd, 'test.json', 123123);
49+
50+
expect(file).to.be.false;
51+
52+
done();
53+
});
4654
});

0 commit comments

Comments
 (0)