Skip to content

Commit

Permalink
Merge pull request #153 from BoatsAreRockable/issue-127
Browse files Browse the repository at this point in the history
Add file copy and rename methods
  • Loading branch information
Charlie Lee committed May 1, 2016
2 parents 0918b97 + 9df2ef0 commit 572fe49
Showing 1 changed file with 151 additions and 93 deletions.
244 changes: 151 additions & 93 deletions app/js/file.js
Original file line number Diff line number Diff line change
@@ -1,100 +1,158 @@
module.exports = {};

(function() {
"use strict";
var fs = require("fs");

/**
* Update the callback object to add missing repsonse methods.
* This works by using an empty function for the empty property.
*
* @param {Object} callback - The original callback object.
* @returns {Object} The revised callback object.
*/
function update(callback) {
function noop() {}

if (!callback.success) {
callback.success = noop;
}
if (!callback.error) {
callback.error = noop;
}

return callback;
}

/**
* Delete a file from the hard drive.
*
* @param {String} file - Absolute path to the file to be deleted.
* @param {Object} callback - The callback object.
* @param {Function} callback.success - The success callback.
* @param {Function} callback.error - The error callback.
*/
function deleteFile(file, callback) {
callback = update(callback);

fs.unlink(file, function(err) {
if (err) {
console.error(err);
callback.error();
} else {
console.log(`Successfully deleted ${file}`);
callback.success();
}
});
}
"use strict";
var fs = require("fs");

/**
* Read a file from the hard drive.
*
* @param {String} file - Absolute path to the file to be read.
* @param {Object} callback - The callback object.
* @param {Function} callback.success - The success callback,
* which will recieve the
* file contents.
* @param {Function} callback.error - The error callback.
*/
function readFile(file, callback) {
callback = update(callback);

fs.readFile(file, "utf8", function(err, data) {
if (err) {
console.error(err);
callback.error();
} else {
console.log(`Successfully read ${file}`);
callback.success(data);
}
});
}
/**
* Update the callback object to add missing repsonse methods.
* This works by using an empty function for the empty property.
*
* @param {!Object} callback - The original callback object.
* @returns {Object} The revised callback object.
*/
function update(callback) {
function noop() {}

/**
* Write a file to the hard drive.
*
* @param {String} file - Absolute path to the file to be saved.
* @param {Binary} data - The file data to br written.
* @param {Object} callback - The callback object.
* @param {Function} callback.success - The success callback.
* @param {Function} callback.error - The error callback.
*/
function writeFile(file, data, callback) {
callback = update(callback);

fs.writeFile(file, data, function(err) {
if (err) {
console.error(err);
callback.error();
} else {
console.log(`Successfully written ${file}`);
callback.success();
}
});
if (!callback) {
callback = {};
}
if (!callback.success) {
callback.success = noop;
}
if (!callback.error) {
callback.error = noop;
}

return callback;
}

/**
* Copy a file on the hard drive.
*
* @param {String} oldPath - Absolute path to the file to be copied.
* @param {String} newPath - Absolute path to the file to be created.
* @param {Object} callback - The callback object.
* @param {Function} callback.success - The success callback.
* @param {Function} callback.error - The error callback.
*/
function copyFile(oldPath, newPath, callback) {
callback = update(callback);

var from = fs.createReadStream(oldPath),
to = fs.createWriteStream(newPath);

from.pipe(to);

// An error occurred
from.on("error", function(err) {
console.error(err);
console.error(`Unable to copy ${oldPath} to ${newPath}`);
});

// Copy process was a success
to.on("close", function() {
console.log(`Successfully copied ${oldPath} to ${newPath}`);
callback.success();
});
}

/**
* Delete a file from the hard drive.
*
* @param {String} file - Absolute path to the file to be deleted.
* @param {Object} callback - The callback object.
* @param {Function} callback.success - The success callback.
* @param {Function} callback.error - The error callback.
*/
function deleteFile(file, callback) {
callback = update(callback);

fs.unlink(file, function(err) {
if (err) {
console.error(err);
callback.error();
} else {
console.log(`Successfully deleted ${file}`);
callback.success();
}
});
}

/**
* Read a file from the hard drive.
*
* @param {String} file - Absolute path to the file to be read.
* @param {Object} callback - The callback object.
* @param {Function} callback.success - The success callback,
* which will recieve the
* file contents.
* @param {Function} callback.error - The error callback.
*/
function readFile(file, callback) {
callback = update(callback);

fs.readFile(file, "utf8", function(err, data) {
if (err) {
console.error(err);
callback.error();
} else {
console.log(`Successfully read ${file}`);
callback.success(data);
}
});
}

/**
* Rename a file on the hard drive.
*
* @param {String} oldName - Absolute path to the file to the current file.
* @param {String} newName - Absolute path to the file to the new file.
* @param {Object} callback - The callback object.
* @param {Function} callback.success - The success callback.
* @param {Function} callback.error - The error callback.
*/
function renameFile(oldName, newName, callback) {
callback = update(callback);

fs.rename(oldName, newName, function(err) {
if (err) {
console.error(err);
callback.error();
} else {
console.log(`Successfully renamed ${oldName} to ${newName}`);
callback.success();
}
});
}

/**
* Write a file to the hard drive.
*
* @param {String} file - Absolute path to the file to be saved.
* @param {Binary} data - The file data to br written.
* @param {Object} callback - The callback object.
* @param {Function} callback.success - The success callback.
* @param {Function} callback.error - The error callback.
*/
function writeFile(file, data, callback) {
callback = update(callback);

fs.writeFile(file, data, function(err) {
if (err) {
console.error(err);
callback.error();
} else {
console.log(`Successfully written ${file}`);
callback.success();
}
});
}

// Public exports
module.exports.read = readFile;
module.exports.write = writeFile;
module.exports.delete = deleteFile;
}());
// Public exports
module.exports.copy = copyFile;
module.exports.read = readFile;
module.exports.write = writeFile;
module.exports.rename = renameFile;
module.exports.delete = deleteFile;
}());

0 comments on commit 572fe49

Please sign in to comment.