forked from mirrors/Scribe.js
Refactor LogWriter history
This commit is contained in:
parent
0bf1253717
commit
7f706c5aa4
1 changed files with 54 additions and 35 deletions
|
@ -39,20 +39,52 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
//Init history
|
//Init history
|
||||||
|
this.initHistory();
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* LogWriter.prototype.initHistory
|
||||||
|
*
|
||||||
|
* Attach and init the history property
|
||||||
|
*/
|
||||||
|
LogWriter.prototype.initHistory = function () {
|
||||||
|
|
||||||
this.history = {
|
this.history = {
|
||||||
dates : {}
|
dates : {}
|
||||||
};
|
};
|
||||||
|
|
||||||
this.writeFile(
|
var historyPath = path.join(this.rootPath, 'history.json');
|
||||||
path.join(this.rootPath, 'history.json'),
|
|
||||||
this.history,
|
if (!fs.existsSync(historyPath)) { //create file if doesn't exist yet
|
||||||
function (err) {
|
|
||||||
|
this.writeFile(
|
||||||
|
historyPath,
|
||||||
|
this.history,
|
||||||
|
function (err) {
|
||||||
|
if (err) {
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
} else { //get history if file exists
|
||||||
|
|
||||||
|
var self = this;
|
||||||
|
|
||||||
|
fs.readFile(historyPath, function (err, data) {
|
||||||
if (err) {
|
if (err) {
|
||||||
throw err;
|
throw err;
|
||||||
|
} else {
|
||||||
|
try {
|
||||||
|
|
||||||
|
self.history = JSON.parse(data);
|
||||||
|
|
||||||
|
} catch (e) {
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
);
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -88,7 +120,7 @@
|
||||||
callback(err);
|
callback(err);
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
var newFile = fs.existsSync(pathToFile);
|
var newFile = !fs.existsSync(pathToFile);
|
||||||
|
|
||||||
fs.appendFile(pathToFile, content, function (err) {
|
fs.appendFile(pathToFile, content, function (err) {
|
||||||
|
|
||||||
|
@ -140,37 +172,24 @@
|
||||||
var historyPath = path.join(this.rootPath, 'history.json'),
|
var historyPath = path.join(this.rootPath, 'history.json'),
|
||||||
self = this;
|
self = this;
|
||||||
|
|
||||||
fs.readFile(
|
var today = moment().startOf('day').valueOf().toString();
|
||||||
historyPath,
|
|
||||||
function (err, data) {
|
//Save the path under today key
|
||||||
|
|
||||||
|
if (!self.history.dates[today]) {
|
||||||
|
self.history.dates[today] = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (self.history.dates[today].indexOf(pathToFile) === -1) {
|
||||||
|
|
||||||
|
self.history.dates[today].push(pathToFile);
|
||||||
|
|
||||||
|
self.writeFile(historyPath, self.history, function (err) {
|
||||||
if (err) {
|
if (err) {
|
||||||
throw 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
);
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue