Skip to content

Commit d905bcd

Browse files
committed
Update: Make internals consistent
1 parent 9fd1689 commit d905bcd

18 files changed

+147
-111
lines changed

lib/default-value.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
'use strict';
22

3-
module.exports = function defaultValue(defaultValue, value) {
4-
return value === null ? defaultValue : value;
5-
};
3+
function defaultValue(defVal, value) {
4+
// Double equal to support null & undefined
5+
return value == null ? defVal : value;
6+
}
7+
8+
module.exports = defaultValue;

lib/dest/index.js

+8-6
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@ function dest(outFolder, opt) {
1717
var sourcemapsOpt = valueOrFunction(
1818
['boolean', 'string', 'object'], opt.sourcemaps);
1919

20-
function saveFile(file, enc, cb) {
21-
prepareWrite(outFolder, file, opt, function(err) {
22-
if (err) {
23-
return cb(err);
20+
function saveFile(file, enc, callback) {
21+
prepareWrite(outFolder, file, opt, onPrepare);
22+
23+
function onPrepare(prepareErr) {
24+
if (prepareErr) {
25+
return callback(prepareErr);
2426
}
25-
writeContents(file, cb);
26-
});
27+
writeContents(file, callback);
28+
}
2729
}
2830

2931
var saveStream = through2.obj(opt, saveFile);

lib/dest/write-contents/index.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ function writeContents(file, callback) {
3333

3434
// This is invoked by the various writeXxx modules when they've finished
3535
// writing the contents.
36-
function onWritten(err) {
37-
if (isErrorFatal(err)) {
38-
return callback(err);
36+
function onWritten(writeErr) {
37+
if (isErrorFatal(writeErr)) {
38+
return callback(writeErr);
3939
}
4040

4141
callback(null, file);
@@ -48,7 +48,7 @@ function writeContents(file, callback) {
4848

4949
if (err.code === 'EEXIST' && file.flag === 'wx') {
5050
// Handle scenario for file overwrite failures.
51-
return false; // "These aren't the droids you're looking for"
51+
return false;
5252
}
5353

5454
// Otherwise, this is a fatal error

lib/dest/write-contents/write-buffer.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ function writeBuffer(file, onWritten) {
1717

1818
fo.updateMetadata(fd, file, onUpdate);
1919

20-
function onUpdate(statErr) {
21-
fo.closeFd(statErr, fd, onWritten);
20+
function onUpdate(updateErr) {
21+
fo.closeFd(updateErr, fd, onWritten);
2222
}
2323
}
2424

lib/dest/write-contents/write-dir.js

+10-10
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,23 @@ function writeDir(file, onWritten) {
2727

2828
fo.updateMetadata(fd, file, onUpdate);
2929

30-
function onUpdate(statErr) {
31-
fo.closeFd(statErr, fd, onWritten);
30+
function onUpdate(updateErr) {
31+
fo.closeFd(updateErr, fd, onWritten);
3232
}
3333
}
3434

3535
}
3636

3737
function isInaccessible(err) {
38-
if (!err) {
39-
return false;
40-
}
41-
42-
if (err.code === 'EACCES') {
43-
return true;
44-
}
45-
38+
if (!err) {
4639
return false;
4740
}
4841

42+
if (err.code === 'EACCES') {
43+
return true;
44+
}
45+
46+
return false;
47+
}
48+
4949
module.exports = writeDir;

lib/dest/write-contents/write-symbolic-link.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ var fs = require('graceful-fs');
44

55
function writeSymbolicLink(file, onWritten) {
66
// TODO handle symlinks properly
7-
fs.symlink(file.symlink, file.path, function(err) {
8-
if (isFatalError(err)) {
9-
return onWritten(err);
7+
fs.symlink(file.symlink, file.path, function(symlinkErr) {
8+
if (isFatalError(symlinkErr)) {
9+
return onWritten(symlinkErr);
1010
}
1111

1212
onWritten();

lib/file-operations.js

+18-19
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,9 @@ function updateMetadata(fd, file, callback) {
141141

142142
fs.fstat(fd, onStat);
143143

144-
function onStat(err, stat) {
145-
if (err) {
146-
return callback(err);
144+
function onStat(statErr, stat) {
145+
if (statErr) {
146+
return callback(statErr);
147147
}
148148

149149
// Check if mode needs to be updated
@@ -160,13 +160,13 @@ function updateMetadata(fd, file, callback) {
160160

161161
// Nothing to do
162162
if (!modeDiff && !timesDiff && !ownerDiff) {
163-
return callback(null);
163+
return callback();
164164
}
165165

166-
// Check access, `futimes` and `fchmod` only work if we own the file,
167-
// or if we are effectively root.
166+
// Check access, `futimes`, `fchmod` & `fchown` only work if we own
167+
// the file, or if we are effectively root (`fchown` only when root).
168168
if (!isOwner(stat)) {
169-
return callback(null);
169+
return callback();
170170
}
171171

172172
if (modeDiff) {
@@ -196,7 +196,7 @@ function updateMetadata(fd, file, callback) {
196196
}
197197
}
198198

199-
function times(fchmodErr) {
199+
function times(propagatedErr) {
200200
fs.futimes(fd, timesDiff.atime, timesDiff.mtime, onFutimes);
201201

202202
function onFutimes(futimesErr) {
@@ -205,21 +205,21 @@ function updateMetadata(fd, file, callback) {
205205
file.stat.mtime = timesDiff.mtime;
206206
}
207207
if (ownerDiff) {
208-
return owner(fchmodErr || futimesErr);
208+
return owner(propagatedErr || futimesErr);
209209
}
210-
callback(fchmodErr || futimesErr);
210+
callback(propagatedErr || futimesErr);
211211
}
212212
}
213213

214-
function owner(earlierErr) {
214+
function owner(propagatedErr) {
215215
fs.fchown(fd, ownerDiff.uid, ownerDiff.gid, onFchown);
216216

217217
function onFchown(fchownErr) {
218218
if (!fchownErr) {
219219
file.stat.uid = ownerDiff.uid;
220220
file.stat.gid = ownerDiff.gid;
221221
}
222-
callback(earlierErr || fchownErr);
222+
callback(propagatedErr || fchownErr);
223223
}
224224
}
225225
}
@@ -237,8 +237,7 @@ function writeFile(filepath, data, options, callback) {
237237
}
238238

239239
if (!Buffer.isBuffer(data)) {
240-
callback(new TypeError('Data must be a Buffer'));
241-
return;
240+
return callback(new TypeError('Data must be a Buffer'));
242241
}
243242

244243
if (!options) {
@@ -252,15 +251,15 @@ function writeFile(filepath, data, options, callback) {
252251

253252
fs.open(filepath, flag, mode, onOpen);
254253

255-
function onOpen(err, fd) {
256-
if (err) {
257-
return onComplete(err);
254+
function onOpen(openErr, fd) {
255+
if (openErr) {
256+
return onComplete(openErr);
258257
}
259258

260259
fs.write(fd, data, 0, data.length, position, onComplete);
261260

262-
function onComplete(err) {
263-
callback(err, fd);
261+
function onComplete(writeErr) {
262+
callback(writeErr, fd);
264263
}
265264
}
266265
}

lib/filter-since.js

+8-6
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@
22

33
var filter = require('through2-filter');
44

5-
module.exports = function(d) {
6-
var isValid = typeof d === 'number' ||
7-
d instanceof Number ||
8-
d instanceof Date;
5+
function filterSince(date) {
6+
var isValid = typeof date === 'number' ||
7+
date instanceof Number ||
8+
date instanceof Date;
99

1010
if (!isValid) {
11-
throw new Error('expected since option to be a date or a number');
11+
throw new Error('expected since option to be a date or timestamp');
1212
}
1313
return filter.obj(function(file) {
14-
return file.stat && file.stat.mtime > d;
14+
return file.stat && file.stat.mtime > date;
1515
});
1616
};
17+
18+
module.exports = filterSince;

lib/prepare-write.js

+8-6
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ var boolean = valueOrFunction.boolean;
1212
var number = valueOrFunction.number;
1313
var string = valueOrFunction.string;
1414

15-
function prepareWrite(outFolder, file, opt, cb) {
15+
function prepareWrite(outFolder, file, opt, callback) {
1616
if (!opt) {
1717
opt = {};
1818
}
@@ -47,12 +47,14 @@ function prepareWrite(outFolder, file, opt, cb) {
4747
file.base = basePath;
4848
file.path = writePath;
4949

50-
fo.mkdirp(writeFolder, options.dirMode, function(err) {
51-
if (err) {
52-
return cb(err);
50+
fo.mkdirp(writeFolder, options.dirMode, onMkdirp);
51+
52+
function onMkdirp(mkdirpErr) {
53+
if (mkdirpErr) {
54+
return callback(mkdirpErr);
5355
}
54-
cb(null);
55-
});
56+
callback();
57+
}
5658
}
5759

5860
module.exports = prepareWrite;

lib/sink.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ function sink(stream) {
1414
var sinkAdded = false;
1515
var sinkStream = new Writable({
1616
objectMode: true,
17-
write: function(file, enc, cb) {
18-
cb();
17+
write: function(file, enc, callback) {
18+
callback();
1919
},
2020
});
2121

lib/src/get-contents/index.js

+7-6
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ var readBuffer = require('./read-buffer');
77
var readSymbolicLink = require('./read-symbolic-link');
88

99
function getContents(opt) {
10-
return through2.obj(opt, function(file, enc, callback) {
10+
11+
function readFile(file, enc, callback) {
1112
// Don't fail to read a directory
1213
if (file.isDirectory()) {
1314
return readDir(file, opt, onRead);
@@ -28,12 +29,12 @@ function getContents(opt) {
2829

2930
// This is invoked by the various readXxx modules when they've finished
3031
// reading the contents.
31-
function onRead(err) {
32-
callback(err, file);
32+
function onRead(readErr) {
33+
callback(readErr, file);
3334
}
34-
});
35-
}
36-
35+
}
3736

37+
return through2.obj(opt, readFile);
38+
}
3839

3940
module.exports = getContents;

lib/src/get-contents/read-buffer.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ var fs = require('graceful-fs');
44
var stripBom = require('strip-bom');
55

66
function bufferFile(file, opt, onRead) {
7-
fs.readFile(file.path, function(err, data) {
8-
if (err) {
9-
return onRead(err);
7+
fs.readFile(file.path, onReadFile);
8+
9+
function onReadFile(readErr, data) {
10+
if (readErr) {
11+
return onRead(readErr);
1012
}
1113

1214
if (opt.stripBOM) {
@@ -15,8 +17,8 @@ function bufferFile(file, opt, onRead) {
1517
file.contents = data;
1618
}
1719

18-
onRead(null);
19-
});
20+
onRead();
21+
}
2022
}
2123

2224
module.exports = bufferFile;

lib/src/get-contents/read-dir.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
function readDir(file, opt, onRead) {
44
// Do nothing for now
5-
onRead(null);
5+
onRead();
66
}
77

88
module.exports = readDir;

lib/src/get-contents/read-stream.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ function streamFile(file, opt, onRead) {
2020
file.contents = file.contents.pipe(stripBom());
2121
}
2222

23-
onRead(null);
23+
onRead();
2424
}
2525

2626
module.exports = streamFile;

lib/src/get-contents/read-symbolic-link.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,18 @@
33
var fs = require('graceful-fs');
44

55
function readLink(file, opt, onRead) {
6-
fs.readlink(file.path, function(err, target) {
7-
if (err) {
8-
return onRead(err);
6+
fs.readlink(file.path, onReadlink);
7+
8+
function onReadlink(readErr, target) {
9+
if (readErr) {
10+
return onRead(readErr);
911
}
1012

1113
// Store the link target path
1214
file.symlink = target;
1315

14-
return onRead(null);
15-
});
16+
onRead();
17+
}
1618
}
1719

1820
module.exports = readLink;

0 commit comments

Comments
 (0)