fix(ischanged) make dir first and then do things

This commit is contained in:
coderaiser 2014-08-15 09:16:30 -04:00
parent 9bc0d06d1b
commit aa430e9e08

View file

@ -4,8 +4,8 @@
var fs = require('fs'),
os = require('os'),
Util = require('../util'),
time = require('./timem'),
Util = require('util-io'),
time = require('timem'),
mkdir = fs.mkdir,
@ -13,16 +13,27 @@
Times = {},
TimesReaded,
DIR = getDir() || __dirname + '/../../json/',
NAME_SHORT = DIR + 'changes',
NAME = NAME_SHORT + '.json';
makeDir(DIR, function(error) {
if (!error)
Util.exec.try(function() {
Times = require(NAME_SHORT);
});
});
function getTimes(callback) {
if (TimesReaded)
callback(Times);
else
makeDir(DIR, function(error) {
if (!error)
Util.exec.try(function() {
Times = require(NAME_SHORT);
});
TimesReaded = true;
callback(Times);
});
}
function getDir() {
var dir,
@ -58,26 +69,29 @@
});
}
module.exports = function(name, callback) {
var readTime = Times[name];
Util.checkArgs(arguments, ['name', 'callback']);
time.get(name, 'raw', function(error, fileTime) {
var json, timeChanged;
getTimes(function(times) {
var readTime = times[name];
if (!error && readTime !== fileTime) {
timeChanged = true;
Times[name] = fileTime;
json = Util.stringifyJSON(Times);
time.get(name, 'raw', function(error, fileTime) {
var json, timeChanged;
fs.writeFile(NAME, json, function(error) {
if (error)
Util.log(error);
});
}
if (!error && readTime !== fileTime) {
timeChanged = true;
Times[name] = fileTime;
json = Util.stringifyJSON(Times);
fs.writeFile(NAME, json, function(error) {
if (error)
Util.log(error);
});
}
callback(error, timeChanged);
callback(error, timeChanged);
});
});
};
})();