Use constants and CWD-independent paths for resource directories.

pull/344/head
Chad Weider 2012-01-17 02:16:25 -08:00
parent c3acdbe329
commit 53549b7422
1 changed files with 13 additions and 11 deletions

View File

@ -32,6 +32,10 @@ var gzip = require('gzip');
var server = require('../server'); var server = require('../server');
var os = require('os'); var os = require('os');
var ROOT_DIR = path.normalize(__dirname + "/../" );
var JS_DIR = ROOT_DIR + '../static/js/';
var CSS_DIR = ROOT_DIR + '../static/css/';
var CACHE_DIR = ROOT_DIR + '../var/';
var TAR_PATH = path.join(__dirname, 'tar.json'); var TAR_PATH = path.join(__dirname, 'tar.json');
var tar = JSON.parse(fs.readFileSync(TAR_PATH, 'utf8')); var tar = JSON.parse(fs.readFileSync(TAR_PATH, 'utf8'));
@ -52,8 +56,6 @@ exports.minifyJS = function(req, res, next)
return next(); return next();
} }
var rootPath = path.normalize(__dirname + "/../../" );
res.header("Content-Type","text/javascript"); res.header("Content-Type","text/javascript");
//minifying is enabled //minifying is enabled
@ -67,7 +69,7 @@ exports.minifyJS = function(req, res, next)
//find out the highest modification date //find out the highest modification date
function(callback) function(callback)
{ {
var folders2check = [rootPath + "static/css", rootPath + "static/js"]; var folders2check = [CSS_DIR, JS_DIR];
//go trough this two folders //go trough this two folders
async.forEach(folders2check, function(path, callback) async.forEach(folders2check, function(path, callback)
@ -106,7 +108,7 @@ exports.minifyJS = function(req, res, next)
function(callback) function(callback)
{ {
//check the modification time of the minified js //check the modification time of the minified js
fs.stat(rootPath + "var/minified_" + jsFilename, function(err, stats) fs.stat(CACHE_DIR + "/minified_" + jsFilename, function(err, stats)
{ {
if(err && err.code != "ENOENT") if(err && err.code != "ENOENT")
{ {
@ -131,7 +133,7 @@ exports.minifyJS = function(req, res, next)
{ {
async.forEach(jsFiles, function (item, callback) async.forEach(jsFiles, function (item, callback)
{ {
fs.readFile(rootPath + "static/js/" + item, "utf-8", function(err, data) fs.readFile(JS_DIR + item, "utf-8", function(err, data)
{ {
if(ERR(err, callback)) return; if(ERR(err, callback)) return;
fileValues[item] = data; fileValues[item] = data;
@ -160,7 +162,7 @@ exports.minifyJS = function(req, res, next)
var type = item.match(/INCLUDE_[A-Z]+/g)[0].substr("INCLUDE_".length); var type = item.match(/INCLUDE_[A-Z]+/g)[0].substr("INCLUDE_".length);
//read the included file //read the included file
fs.readFile(filename, "utf-8", function(err, data) fs.readFile(ROOT_DIR + filename, "utf-8", function(err, data)
{ {
if(ERR(err, callback)) return; if(ERR(err, callback)) return;
@ -209,7 +211,7 @@ exports.minifyJS = function(req, res, next)
//write the results plain in a file //write the results plain in a file
function(callback) function(callback)
{ {
fs.writeFile(rootPath + "var/minified_" + jsFilename, result, "utf8", callback); fs.writeFile(CACHE_DIR + "minified_" + jsFilename, result, "utf8", callback);
}, },
//write the results compressed in a file //write the results compressed in a file
function(callback) function(callback)
@ -223,7 +225,7 @@ exports.minifyJS = function(req, res, next)
if(ERR(err, callback)) return; if(ERR(err, callback)) return;
fs.writeFile(rootPath + "var/minified_" + jsFilename + ".gz", compressedResult, callback); fs.writeFile(CACHE_DIR + "minified_" + jsFilename + ".gz", compressedResult, callback);
}); });
} }
//skip this step on windows //skip this step on windows
@ -247,12 +249,12 @@ exports.minifyJS = function(req, res, next)
var pathStr; var pathStr;
if(gzipSupport && os.type().indexOf("Windows") == -1) if(gzipSupport && os.type().indexOf("Windows") == -1)
{ {
pathStr = path.normalize(rootPath + "var/minified_" + jsFilename + ".gz"); pathStr = path.normalize(CACHE_DIR + "minified_" + jsFilename + ".gz");
res.header('Content-Encoding', 'gzip'); res.header('Content-Encoding', 'gzip');
} }
else else
{ {
pathStr = path.normalize(rootPath + "var/minified_" + jsFilename ); pathStr = path.normalize(CACHE_DIR + "minified_" + jsFilename );
} }
res.sendfile(pathStr, { maxAge: server.maxAge }); res.sendfile(pathStr, { maxAge: server.maxAge });
@ -266,7 +268,7 @@ exports.minifyJS = function(req, res, next)
//read all js files //read all js files
async.forEach(jsFiles, function (item, callback) async.forEach(jsFiles, function (item, callback)
{ {
fs.readFile(rootPath + "static/js/" + item, "utf-8", function(err, data) fs.readFile(JS_DIR + item, "utf-8", function(err, data)
{ {
if(ERR(err, callback)) return; if(ERR(err, callback)) return;
fileValues[item] = data; fileValues[item] = data;