Skip to content

Commit b95fba4

Browse files
committed
feature(writejson) add support of writeFile options
1 parent 7501c6d commit b95fba4

5 files changed

Lines changed: 55 additions & 17 deletions

File tree

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
language: node_js
22
node_js:
3-
- 4
4-
- 6
53
- 8
4+
- 10
65

76
script:
87
- npm run lint
@@ -14,3 +13,4 @@ notifications:
1413
on_failure: change
1514

1615
sudo: false
16+

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,12 @@ writejson('data.json', {hello: 'world'}, (error) => {
2424
});
2525

2626
const options = {
27-
replacer: ['hello'],// properties to put in json
28-
space: 4 // default space count
29-
eof: true // default new line at end of file
27+
replacer: ['hello'], // properties to put in json
28+
space: 4, // default space count
29+
eof: true, // default new line at end of file
30+
encoding: 'utf8', // default
31+
mode: '0o666', // default
32+
flag: 'w', // default
3033
};
3134

3235
writejson('data.json', {hello: 'world'}, options, (error) => {

lib/writejson.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
const fs = require('fs');
44
const tryCatch = require('try-catch');
55

6+
const getWriteOptions = (options) => ({
7+
encoding: options.encoding,
8+
mode: options.mode,
9+
flag: options.flag,
10+
});
11+
612
module.exports = (name, json, options, callback) => {
713
if (!callback) {
814
callback = options;
@@ -13,16 +19,19 @@ module.exports = (name, json, options, callback) => {
1319
checkCB(callback);
1420

1521
const str = stringify(json, options);
16-
fs.writeFile(name, str, callback);
22+
const writeOptions = getWriteOptions(options);
23+
24+
fs.writeFile(name, str, writeOptions, callback);
1725
};
1826

1927
module.exports.sync = sync;
2028

2129
function sync(name, data, options) {
22-
options = options || {};
23-
30+
options = options || {};
2431
check(name, data, options);
25-
fs.writeFileSync(name, stringify(data, options));
32+
33+
const writeOptions = getWriteOptions(options);
34+
fs.writeFileSync(name, stringify(data, options), writeOptions);
2635
}
2736

2837
module.exports.sync.try = (name, data, options) => {

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,13 @@
3030
},
3131
"devDependencies": {
3232
"coveralls": "^3.0.0",
33+
"es6-promisify": "^6.0.0",
3334
"eslint": "^4.19.1",
3435
"eslint-plugin-node": "^6.0.1",
3536
"nyc": "^12.0.2",
3637
"redrun": "^6.0.0",
37-
"tape": "^4.2.2"
38+
"tape": "^4.2.2",
39+
"try-to-catch": "^1.0.2"
3840
},
3941
"engines": {
4042
"node": ">=4.0.0"

test/writejson.js

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ const os = require('os');
44
const path = require('path');
55
const fs = require('fs');
66
const test = require('tape');
7+
const {promisify} = require('es6-promisify');
8+
const tryToCatch = require('try-to-catch');
9+
710
const writejson = require('..');
11+
const _writejson = promisify(writejson);
812

913
const tmp = os.tmpdir();
1014
const NAME = path.join(tmp, String(Math.random()));
@@ -21,7 +25,7 @@ test('writejson: should write json data to file', (t) => {
2125
t.notOk(error, 'no read error');
2226
t.deepEqual(json, JSON.parse(data), 'data should be equal');
2327

24-
fs.unlink(NAME, error => {
28+
fs.unlink(NAME, (error) => {
2529
t.notOk(error, 'no remove error');
2630
t.end();
2731
});
@@ -51,7 +55,7 @@ test('writejson: should write json data to file with options', (t) => {
5155
t.equal(resultStr, data, 'data should be equal');
5256
t.deepEqual(JSON.parse(data), result, 'objects should be equal');
5357

54-
fs.unlink(NAME, error => {
58+
fs.unlink(NAME, (error) => {
5559
t.notOk(error, 'no remove error');
5660
t.end();
5761
});
@@ -60,7 +64,7 @@ test('writejson: should write json data to file with options', (t) => {
6064
});
6165

6266
test('writejson: should write json data to file with default options', (t) => {
63-
const resultStr = JSON.stringify(json, null, 4) + '\n'
67+
const resultStr = JSON.stringify(json, null, 4) + '\n';
6468

6569
writejson(NAME, json, error => {
6670
t.notOk(error, 'no write error');
@@ -71,7 +75,7 @@ test('writejson: should write json data to file with default options', (t) => {
7175
t.equal(resultStr, data, 'data should be equal');
7276
t.deepEqual(JSON.parse(data), json, 'objects should be equal');
7377

74-
fs.unlink(NAME, error => {
78+
fs.unlink(NAME, (error) => {
7579
t.notOk(error, 'no remove error');
7680
t.end();
7781
});
@@ -80,12 +84,32 @@ test('writejson: should write json data to file with default options', (t) => {
8084
});
8185

8286
test('writejson: write error', (t) => {
83-
writejson('/hello.json', json, error => {
87+
writejson('/hello.json', json, (error) => {
8488
t.ok(error, 'should return error: ' + error.message);
8589
t.end();
8690
});
8791
});
8892

93+
test('writejson: write options', async (t) => {
94+
const json = {
95+
hello: 'world',
96+
};
97+
98+
const options = {
99+
mode: 0o600
100+
};
101+
102+
await tryToCatch(_writejson, NAME, json, options);
103+
104+
const {mode} = fs.statSync(NAME);
105+
const expected = Number(mode).toString(8).slice(3);
106+
107+
fs.unlinkSync(NAME);
108+
109+
t.equal(expected, '600', 'should equal');
110+
t.end();
111+
});
112+
89113
test('writejson.sync.try: write error', (t) => {
90114
const error = writejson.sync.try('/hello.json', json);
91115

@@ -100,14 +124,14 @@ test('writejson: no args', (t) => {
100124

101125
test('writejson: no json', (t) => {
102126
const fn = () => writejson('hello');
103-
127+
104128
t.throws(fn, /json should be object!/, 'json check');
105129
t.end();
106130
});
107131

108132
test('writejson: options not object', (t) => {
109133
const fn = () => writejson('hello', {}, 'options', () => {});
110-
134+
111135
t.throws(fn, /options should be object!/, 'options check');
112136
t.end();
113137
});

0 commit comments

Comments
 (0)