Skip to content

Commit

Permalink
Update: Make internals consistent
Browse files Browse the repository at this point in the history
  • Loading branch information
phated committed Nov 28, 2017
1 parent 5fa9328 commit c24970f
Show file tree
Hide file tree
Showing 18 changed files with 147 additions and 111 deletions.
9 changes: 6 additions & 3 deletions lib/default-value.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
'use strict';

module.exports = function defaultValue(defaultValue, value) {
return value === null ? defaultValue : value;
};
function defaultValue(defVal, value) {
// Double equal to support null & undefined
return value == null ? defVal : value;
}

module.exports = defaultValue;
14 changes: 8 additions & 6 deletions lib/dest/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ function dest(outFolder, opt) {
var sourcemapsOpt = valueOrFunction(
['boolean', 'string', 'object'], opt.sourcemaps);

function saveFile(file, enc, cb) {
prepareWrite(outFolder, file, opt, function(err) {
if (err) {
return cb(err);
function saveFile(file, enc, callback) {
prepareWrite(outFolder, file, opt, onPrepare);

function onPrepare(prepareErr) {
if (prepareErr) {
return callback(prepareErr);
}
writeContents(file, cb);
});
writeContents(file, callback);
}
}

var saveStream = through2.obj(opt, saveFile);
Expand Down
8 changes: 4 additions & 4 deletions lib/dest/write-contents/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ function writeContents(file, callback) {

// This is invoked by the various writeXxx modules when they've finished
// writing the contents.
function onWritten(err) {
if (isErrorFatal(err)) {
return callback(err);
function onWritten(writeErr) {
if (isErrorFatal(writeErr)) {
return callback(writeErr);
}

callback(null, file);
Expand All @@ -48,7 +48,7 @@ function writeContents(file, callback) {

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

// Otherwise, this is a fatal error
Expand Down
4 changes: 2 additions & 2 deletions lib/dest/write-contents/write-buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ function writeBuffer(file, onWritten) {

fo.updateMetadata(fd, file, onUpdate);

function onUpdate(statErr) {
fo.closeFd(statErr, fd, onWritten);
function onUpdate(updateErr) {
fo.closeFd(updateErr, fd, onWritten);
}
}

Expand Down
20 changes: 10 additions & 10 deletions lib/dest/write-contents/write-dir.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,23 @@ function writeDir(file, onWritten) {

fo.updateMetadata(fd, file, onUpdate);

function onUpdate(statErr) {
fo.closeFd(statErr, fd, onWritten);
function onUpdate(updateErr) {
fo.closeFd(updateErr, fd, onWritten);
}
}

}

function isInaccessible(err) {
if (!err) {
return false;
}

if (err.code === 'EACCES') {
return true;
}

if (!err) {
return false;
}

if (err.code === 'EACCES') {
return true;
}

return false;
}

module.exports = writeDir;
6 changes: 3 additions & 3 deletions lib/dest/write-contents/write-symbolic-link.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ var fs = require('graceful-fs');

function writeSymbolicLink(file, onWritten) {
// TODO handle symlinks properly
fs.symlink(file.symlink, file.path, function(err) {
if (isFatalError(err)) {
return onWritten(err);
fs.symlink(file.symlink, file.path, function(symlinkErr) {
if (isFatalError(symlinkErr)) {
return onWritten(symlinkErr);
}

onWritten();
Expand Down
37 changes: 18 additions & 19 deletions lib/file-operations.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,9 @@ function updateMetadata(fd, file, callback) {

fs.fstat(fd, onStat);

function onStat(err, stat) {
if (err) {
return callback(err);
function onStat(statErr, stat) {
if (statErr) {
return callback(statErr);
}

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

// Nothing to do
if (!modeDiff && !timesDiff && !ownerDiff) {
return callback(null);
return callback();
}

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

if (modeDiff) {
Expand Down Expand Up @@ -196,7 +196,7 @@ function updateMetadata(fd, file, callback) {
}
}

function times(fchmodErr) {
function times(propagatedErr) {
fs.futimes(fd, timesDiff.atime, timesDiff.mtime, onFutimes);

function onFutimes(futimesErr) {
Expand All @@ -205,21 +205,21 @@ function updateMetadata(fd, file, callback) {
file.stat.mtime = timesDiff.mtime;
}
if (ownerDiff) {
return owner(fchmodErr || futimesErr);
return owner(propagatedErr || futimesErr);
}
callback(fchmodErr || futimesErr);
callback(propagatedErr || futimesErr);
}
}

function owner(earlierErr) {
function owner(propagatedErr) {
fs.fchown(fd, ownerDiff.uid, ownerDiff.gid, onFchown);

function onFchown(fchownErr) {
if (!fchownErr) {
file.stat.uid = ownerDiff.uid;
file.stat.gid = ownerDiff.gid;
}
callback(earlierErr || fchownErr);
callback(propagatedErr || fchownErr);
}
}
}
Expand All @@ -237,8 +237,7 @@ function writeFile(filepath, data, options, callback) {
}

if (!Buffer.isBuffer(data)) {
callback(new TypeError('Data must be a Buffer'));
return;
return callback(new TypeError('Data must be a Buffer'));
}

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

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

function onOpen(err, fd) {
if (err) {
return onComplete(err);
function onOpen(openErr, fd) {
if (openErr) {
return onComplete(openErr);
}

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

function onComplete(err) {
callback(err, fd);
function onComplete(writeErr) {
callback(writeErr, fd);
}
}
}
Expand Down
14 changes: 8 additions & 6 deletions lib/filter-since.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@

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

module.exports = function(d) {
var isValid = typeof d === 'number' ||
d instanceof Number ||
d instanceof Date;
function filterSince(date) {
var isValid = typeof date === 'number' ||
date instanceof Number ||
date instanceof Date;

if (!isValid) {
throw new Error('expected since option to be a date or a number');
throw new Error('expected since option to be a date or timestamp');
}
return filter.obj(function(file) {
return file.stat && file.stat.mtime > d;
return file.stat && file.stat.mtime > date;
});
};

module.exports = filterSince;
14 changes: 8 additions & 6 deletions lib/prepare-write.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ var boolean = valueOrFunction.boolean;
var number = valueOrFunction.number;
var string = valueOrFunction.string;

function prepareWrite(outFolder, file, opt, cb) {
function prepareWrite(outFolder, file, opt, callback) {
if (!opt) {
opt = {};
}
Expand Down Expand Up @@ -47,12 +47,14 @@ function prepareWrite(outFolder, file, opt, cb) {
file.base = basePath;
file.path = writePath;

fo.mkdirp(writeFolder, options.dirMode, function(err) {
if (err) {
return cb(err);
fo.mkdirp(writeFolder, options.dirMode, onMkdirp);

function onMkdirp(mkdirpErr) {
if (mkdirpErr) {
return callback(mkdirpErr);
}
cb(null);
});
callback();
}
}

module.exports = prepareWrite;
4 changes: 2 additions & 2 deletions lib/sink.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ function sink(stream) {
var sinkAdded = false;
var sinkStream = new Writable({
objectMode: true,
write: function(file, enc, cb) {
cb();
write: function(file, enc, callback) {
callback();
},
});

Expand Down
13 changes: 7 additions & 6 deletions lib/src/get-contents/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ var readBuffer = require('./read-buffer');
var readSymbolicLink = require('./read-symbolic-link');

function getContents(opt) {
return through2.obj(opt, function(file, enc, callback) {

function readFile(file, enc, callback) {
// Don't fail to read a directory
if (file.isDirectory()) {
return readDir(file, opt, onRead);
Expand All @@ -28,12 +29,12 @@ function getContents(opt) {

// This is invoked by the various readXxx modules when they've finished
// reading the contents.
function onRead(err) {
callback(err, file);
function onRead(readErr) {
callback(readErr, file);
}
});
}

}

return through2.obj(opt, readFile);
}

module.exports = getContents;
12 changes: 7 additions & 5 deletions lib/src/get-contents/read-buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ var fs = require('graceful-fs');
var stripBom = require('strip-bom');

function bufferFile(file, opt, onRead) {
fs.readFile(file.path, function(err, data) {
if (err) {
return onRead(err);
fs.readFile(file.path, onReadFile);

function onReadFile(readErr, data) {
if (readErr) {
return onRead(readErr);
}

if (opt.stripBOM) {
Expand All @@ -15,8 +17,8 @@ function bufferFile(file, opt, onRead) {
file.contents = data;
}

onRead(null);
});
onRead();
}
}

module.exports = bufferFile;
2 changes: 1 addition & 1 deletion lib/src/get-contents/read-dir.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

function readDir(file, opt, onRead) {
// Do nothing for now
onRead(null);
onRead();
}

module.exports = readDir;
2 changes: 1 addition & 1 deletion lib/src/get-contents/read-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function streamFile(file, opt, onRead) {
file.contents = file.contents.pipe(stripBom());
}

onRead(null);
onRead();
}

module.exports = streamFile;
12 changes: 7 additions & 5 deletions lib/src/get-contents/read-symbolic-link.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@
var fs = require('graceful-fs');

function readLink(file, opt, onRead) {
fs.readlink(file.path, function(err, target) {
if (err) {
return onRead(err);
fs.readlink(file.path, onReadlink);

function onReadlink(readErr, target) {
if (readErr) {
return onRead(readErr);
}

// Store the link target path
file.symlink = target;

return onRead(null);
});
onRead();
}
}

module.exports = readLink;
Loading

0 comments on commit c24970f

Please sign in to comment.