Save each new file in history.json

This commit is contained in:
Guillaume Wuip 2014-10-30 18:48:33 +01:00
parent c7ea009ac5
commit 0bf1253717

View file

@ -8,57 +8,6 @@
mkdirp = require('mkdirp'),
path = require('path');
/**
* createDir
*
* Create a dir if it doesn't exist yet
*
* @param {String} path The dir
* @param {Function} callback
*/
var createDir = function (path, callback) {
mkdirp(path, function (err) {
callback(err);
});
};
/**
* appendFile
*
* Append content to a file
*
* @param {String} pathToFile The file
* @param {String} content
* @param {Function} callback
*/
var appendFile = function (pathToFile, content, callback) {
createDir(path.dirname(pathToFile), function (err) {
if (err) {
callback(err);
} else {
fs.appendFile(pathToFile, content, callback);
}
});
};
/**
* writeFile
*
* Write content into a file (erase old one)
*
* @param {String} pathToFile The file
* @param {String} content
* @param {Function} callback
*/
var writeFile = function (pathToFile, content, callback) {
createDir(path.dirname(pathToFile), function (err) {
if (err) {
callback(err);
} else {
fs.writeFile(pathToFile, content, callback);
}
});
};
/**
* rootPaths
@ -88,6 +37,141 @@
} else {
rootPaths.push(this.rootPath);
}
//Init history
this.history = {
dates : {}
};
this.writeFile(
path.join(this.rootPath, 'history.json'),
this.history,
function (err) {
if (err) {
throw err;
}
}
);
};
/**
* LogWriter.prototype.createDir
*
* Create a dir if it doesn't exist yet
*
* @param {String} path The dir
* @param {Function} callback
*/
LogWriter.prototype.createDir = function (path, callback) {
mkdirp(path, function (err) {
callback(err);
});
};
/**
* LogWriter.prototype.appendFile
*
* Append content to a file
*
* @param {String} pathToFile The file
* @param {String} content
* @param {Function} callback
*/
LogWriter.prototype.appendFile = function (pathToFile, content, callback) {
var self = this;
self.createDir(path.dirname(pathToFile), function (err) {
if (err) {
callback(err);
} else {
var newFile = fs.existsSync(pathToFile);
fs.appendFile(pathToFile, content, function (err) {
if (err) {
throw err;
} else if (newFile) {
self.newFileHistory(pathToFile);
callback();
}
});
}
});
};
/**
* LogWriter.prototype.writeFile
*
* Write content into a file (erase old one)
*
* @param {String} pathToFile The file
* @param {String} content
* @param {Function} callback
*/
LogWriter.prototype.writeFile = function (pathToFile, content, callback) {
if (typeof content !== 'string') {
content = JSON.stringify(content);
}
this.createDir(path.dirname(pathToFile), function (err) {
if (err) {
callback(err);
} else {
fs.writeFile(pathToFile, content, callback);
}
});
};
/**
* LogWriter.prototype.newFileHistory
*
* Save the new file path in history according to the date
*
* @param {String} pathToFile
*/
LogWriter.prototype.newFileHistory = function (pathToFile) {
var historyPath = path.join(this.rootPath, 'history.json'),
self = this;
fs.readFile(
historyPath,
function (err, data) {
if (err) {
throw err;
} else {
try {
var today = moment().startOf('day').valueOf().toString();
//Save the path under today key
if (!self.history.dates[today]) {
self.history.dates[today] = [];
}
self.history.dates[today].push(pathToFile);
self.writeFile(historyPath, self.history, function (err) {
if (err) {
throw err;
}
});
} catch (e) {
throw e;
}
}
}
);
};
/**
@ -189,7 +273,7 @@
var json = JSON.stringify(log);
appendFile(this.path(opt), json + '\n', function (err) {
this.appendFile(this.path(opt), json + '\n', function (err) {
if (err) {
throw err;
}
@ -207,7 +291,7 @@
var filePath = path.join(this.rootPath, logger.name + '.json');
writeFile(filePath, JSON.stringify(logger), function (err) {
this.writeFile(filePath, JSON.stringify(logger), function (err) {
if (err) {
throw err;
}