Moved file functions to internal lib with some tests as well

This commit is contained in:
Danielv123 committed 2017-05-21 22:00:30 +02:00
1 parent d9e897836b
commit 5f3a160b75
7 files changed
+218 -60

No files matched your search

+65
View File
@@ -0,0 +1,65 @@
var fs = require("fs");
var path = require("path");
// gets newest file in a directory
// dir is directory
// files is array of filenames
// callback(filename string);
var getNewestFile = function(dir, files, callback) {
if (!callback) return;
if (!files || (files && files.length === 0)) {
callback();
}
var newest = { file: files[0] };
var checked = 0;
fs.stat(dir + newest.file, function(err, stats) {
newest.mtime = stats.mtime;
for (var i = 0; i < files.length; i++) {
var file = files[i];
(function(file) {
fs.stat(file, function(err, stats) {
++checked;
if (stats.mtime.getTime() > newest.mtime.getTime()) {
newest = { file : file, mtime : stats.mtime };
}
if (checked == files.length) {
callback(newest);
}
});
})(dir + file);
}
});
}
// get all directories in folder
var getDirectories = function(srcpath) {
return fs.readdirSync(srcpath).filter(function (file) {
return fs.statSync(path.join(srcpath, file)).isDirectory();
});
}
var getFileNamesSync = function(srcpath) {
return fs.readdirSync(srcpath).filter(function (file) {
return !fs.statSync(path.join(srcpath, file)).isDirectory();
});
}
// does what it says on the tin
var deleteFolderRecursive = function(path) {
if (fs.existsSync(path)) {
fs.readdirSync(path).forEach(function (file, index) {
var curPath = path + "/" + file;
if (fs.lstatSync(curPath).isDirectory()) { // recurse
deleteFolderRecursive(curPath);
} else { // delete file
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(path);
}
};
module.exports = {
getNewestFile:getNewestFile,
getDirectories:getDirectories,
deleteFolderRecursive:deleteFolderRecursive,
getFileNamesSync:getFileNamesSync,
}
+43
View File
@@ -0,0 +1,43 @@
var assert = require('assert');
var fileOps = require('./fileOps');
describe("fileOps.js", function(){
describe("fileOps.getDirectories()", function(){
it("gets an array of directory names from a path", function(){
let directories = fileOps.getDirectories("lib/test");
// because there are no arrays for some reason
assert(typeof directories == "object");
// they are sorted alphabetically because thats the way it is
assert(directories[0] == "another folder" && directories[1] == "folder");
});
it("does not return any filenames", function(){
let directories = fileOps.getDirectories("lib/test");
// this is a function to return directories, not files. So yeah.
assert(!directories.includes("file.txt") && !directories.includes("another file.txt"));
});
});
describe("fileOps.getFileNamesSync(path)", function(){
it("gets names of files in directory", function(){
let files = fileOps.getFileNamesSync("lib/test");
assert(files.length > 0);
assert(files.includes("file.txt") && files.includes("another file.txt"));
});
it("throws when lacking first argument", function(){
assert.throws(function(){
let files = fileOps.getFileNamesSync();
}, Error);
});
});
/*describe("fileOps.getNewestFile()", function(){
it("gets the newest file in a directory async", function(done){
fileOps.getNewestFile("lib/test", fileOps.getFileNames("lib/test"), function(newest){
assert(newest);
done();
})
});
});*/
});
View File
Whitespace-only changes.
View File
Whitespace-only changes.