Code-Cleanup and package.json

- Added package.json with dependencies
- Cleaned up unreachable code
- Moved a function definition in caching.js to the top of the file
- Added some comments
pull/4/head
Trivernis 6 years ago
parent e0c3feaf86
commit cf9f51013c

@ -3,8 +3,17 @@ const fs = require("fs"),
config_path = "./config/caching_dump.json", config_path = "./config/caching_dump.json",
cache_dump = JSON.parse(fs.readFileSync(config_path)), cache_dump = JSON.parse(fs.readFileSync(config_path)),
cache_dir = "./.cache", cache_dir = "./.cache",
cache = {}; // TODO: Must read from dump again cache = {};
if (cache_dump != null && cache_dump["last"] != null) cache = cache_dump["last"]; var logger = require("winston");
if (cache_dump != null && cache_dump["last"] != null) cache = cache_dump["last"]; // read the data from the file dump
/**
* Sets the logger for logging
* @param {Winston Logger} newLogger
*/
exports.setLogger = function(newLogger) {
logger = newLogger;
}
/** /**
* Returns the data from files that were cached * Returns the data from files that were cached
@ -56,15 +65,6 @@ exports.cache = function(filename, data) {
}); });
}); // write the data asynchronously to the file }); // write the data asynchronously to the file
}; };
var logger = require("winston");
/**
* Sets the logger for logging
* @param {Winston Logger} newLogger
*/
exports.setLogger = function(newLogger) {
logger = newLogger;
}
/** /**
* Returns if the file is already cached * Returns if the file is already cached
@ -94,6 +94,6 @@ exports.isCached = function(filename) {
*/ */
exports.cleanup = function() { exports.cleanup = function() {
logger.verbose("Dumping cache into cache_dump file"); logger.verbose("Dumping cache into cache_dump file");
cache_dump["last"] = cache; cache_dump["last"] = cache; // append the cache to the dump object
fs.writeFileSync(config_path, JSON.stringify(cache_dump)); fs.writeFileSync(config_path, JSON.stringify(cache_dump)); // write the dump data to the file
} }

@ -30,28 +30,28 @@ exports.setLogger = function(newLogger) {
exports.getProcessed = function(filename) { exports.getProcessed = function(filename) {
try { try {
logger.debug("Processing File %s", filename); logger.debug("Processing File %s", filename);
var extension = utils.getExtension(filename); var extension = utils.getExtension(filename); // use the utils function to get the files extension
var data = null; var data = null;
if (caching.isCached(filename)) return caching.getCached(filename) if (caching.isCached(filename)) return caching.getCached(filename) // return the cached file if it exists
logger.debug("File is not cached. Processing..."); logger.debug("File is not cached. Processing...");
switch (pp_config[extension]) { switch (pp_config[extension]) {
case "sass": case "sass":
logger.debug("Processing sass %s", filename); logger.debug("Processing sass %s", filename);
data = Buffer.from(pp_sass.renderSync({ data = Buffer.from(pp_sass.renderSync({ // use the sass preprocessor
file: filename file: filename
}).css).toString("utf-8"); }).css).toString("utf-8");
break; break;
case "html": case "html":
logger.debug("Processing html %s", filename); logger.debug("Processing html %s", filename);
data = pp_html.formatHtml(filename); data = pp_html.formatHtml(filename); // use the html-preprocessor
break; break;
default: default:
logger.debug("No processor found for %s. Returning data.", filename); logger.debug("No processor found for %s. Returning data.", filename);
return fs.readFileSync(filename); return fs.readFileSync(filename); // just read the data from the file
} }
caching.cache(filename, data); caching.cache(filename, data); // cache the file for faster access next time
logger.debug("Cached file %s", filename); logger.debug("Cached file %s", filename);
return data; return data; // return the data
} catch (error) { } catch (error) {
logger.error(error); logger.error(error);
return "Processing Error"; return "Processing Error";

@ -0,0 +1,14 @@
{
"license": "GPL-v3",
"dependencies": [
"args-parser",
"https",
"jquery",
"jsdom",
"node-sass",
"perfy",
"vuejs",
"winston-daily-rotate-file",
"winston"
]
}

@ -136,9 +136,6 @@ function getResponse(uri) {
logger.verbose("Found route: "+JSON.stringify(route)); logger.verbose("Found route: "+JSON.stringify(route));
if (!route) return ["Not Allowed", "text/plain"]; // return not allowed if no route was found if (!route) return ["Not Allowed", "text/plain"]; // return not allowed if no route was found
return [gp(mount || path.join(route["path"],uri)), route["mime"]]; // get processed output (done by preprocessor) return [gp(mount || path.join(route["path"],uri)), route["mime"]]; // get processed output (done by preprocessor)
// test the extension for differend file types.
logger.verbose({'msg': 'Error', 'path': uri});
return ["Error with url", "text/plain"]; // return an error if above has not returned
} catch (error) { } catch (error) {
logger.error(error); logger.error(error);
return ["Error", "text/plain"]; return ["Error", "text/plain"];

Loading…
Cancel
Save