Code-Cleanup

- Migrated to Webstorm and cleaned the code
pull/4/head
Trivernis 6 years ago
parent cf9f51013c
commit 2b7c88596a

1
.gitignore vendored

@ -31,3 +31,4 @@ node_modules
.log .log
.cache .cache
package-lock.json package-lock.json
.idea

@ -2,18 +2,18 @@ const fs = require("fs"),
path = require("path"), path = require("path"),
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 = {}; let cache = {};
var logger = require("winston"); let logger = require("winston");
if (cache_dump != null && cache_dump["last"] != null) cache = cache_dump["last"]; // read the data from the file dump if (cache_dump != null && cache_dump["last"] != null) cache = cache_dump["last"]; // read the data from the file dump
/** /**
* Sets the logger for logging * Sets the logger for logging
* @param {Winston Logger} newLogger * @param {Object} newLogger
*/ */
exports.setLogger = function (newLogger) { exports.setLogger = function (newLogger) {
logger = newLogger; logger = newLogger;
} };
/** /**
* Returns the data from files that were cached * Returns the data from files that were cached
@ -44,12 +44,14 @@ exports.getCached = function(filename) {
exports.cache = function (filename, data) { exports.cache = function (filename, data) {
logger.verbose("Creating cache entry for %s", filename); logger.verbose("Creating cache entry for %s", filename);
if (!fs.existsSync("./.cache")) fs.mkdirSync("./.cache"); // if the cache folder doesn't exist, create it if (!fs.existsSync("./.cache")) fs.mkdirSync("./.cache"); // if the cache folder doesn't exist, create it
let cache_fn = filename.replace(/[^\w\.]/g, "__"); // remove invalid path characters let cache_fn = filename.replace(/[^\w.]/g, "__"); // remove invalid path characters
let count = 0; let count = 0;
while (fs.existsSync(filename + count + ".cache")) count++; // check if a file with the same name already exists and increase count while (fs.existsSync(filename + count + ".cache")) count++; // check if a file with the same name already exists and increase count
let cache_path = path.join(cache_dir, cache_fn + count + ".cache"); // create the final file path. Cachedir + cached filename (without invalid) + count + .cache let cache_path = path.join(cache_dir, cache_fn + count + ".cache"); // create the final file path. Cachedir + cached filename (without invalid) + count + .cache
logger.debug("Creating file %s", cache_path); logger.debug("Creating file %s", cache_path);
fs.writeFile(cache_path, data, (error) => { fs.writeFile(cache_path, data, (err) => {
if (err !== null) logger.error(err);
else {
logger.debug("Created file cache entry for %s", filename); logger.debug("Created file cache entry for %s", filename);
cache[filename] = { // create a cache-entry with the file's path when the file is written (so it won't be accessed before) cache[filename] = { // create a cache-entry with the file's path when the file is written (so it won't be accessed before)
"path": cache_path, // the last call to the file, the count of calls and an "path": cache_path, // the last call to the file, the count of calls and an
@ -58,22 +60,19 @@ exports.cache = function(filename, data) {
"data": null, "data": null,
"creation_time": Date.now(), "creation_time": Date.now(),
"changed": false "changed": false
}; }
}
fs.watch(filename, (eventType) => { // watch the file for changes fs.watch(filename, (eventType) => { // watch the file for changes
logger.debug("Change detected on %s", filename); logger.debug("Change detected on %s", filename);
if (eventType == 'change') cache[filename].changed = true; // if the event change is detected set the change attribute to true if (eventType === 'change') cache[filename].changed = true; // if the event change is detected set the change attribute to true
}); });
}); // write the data asynchronously to the file }); // write the data asynchronously to the file
}; };
/** /**
* Returns if the file is already cached * Returns if the file is already cached. It returns false if changes were detected in the file
* @param {String} filename The filename to check * @param {String} filename The filename to check
* @return {Boolean} Is it cached or not * @return {Boolean} Is it cached or not
* TODO: Use last access or use creation_time property to check if the file might
* be too old. If the function returns false, a new cache-file will be created which
* has a different name from the old. On each startup a function could check if
* there are cache-files that are not listet in the cache_dump and delete them.
*/ */
exports.isCached = function (filename) { exports.isCached = function (filename) {
let cached_entry = cache[filename]; let cached_entry = cache[filename];
@ -96,4 +95,4 @@ exports.cleanup = function() {
logger.verbose("Dumping cache into cache_dump file"); logger.verbose("Dumping cache into cache_dump file");
cache_dump["last"] = cache; // append the cache to the dump object cache_dump["last"] = cache; // append the cache to the dump object
fs.writeFileSync(config_path, JSON.stringify(cache_dump)); // write the dump data to the file fs.writeFileSync(config_path, JSON.stringify(cache_dump)); // write the dump data to the file
} };

@ -2,9 +2,7 @@
* Preprocesses html-files * Preprocesses html-files
*/ */
const fs = require("fs"), const fs = require("fs"),
{ { JSDOM } = require('jsdom'),
JSDOM
} = require('jsdom')
// ressources // ressources
defaultCss = "/glob/style.sass", // the default style that is embedded in every html defaultCss = "/glob/style.sass", // the default style that is embedded in every html
defaultJs = "/glob/script.js"; // the default script that is embedded in every html defaultJs = "/glob/script.js"; // the default script that is embedded in every html
@ -38,11 +36,11 @@ function createScriptLinkElement(document, src) {
/** /**
* Formats the html string by adding a link to the standard css and to the standard javascript file. * Formats the html string by adding a link to the standard css and to the standard javascript file.
* @param {String} htmlstring A string read from an html file or a html document string itself.
* @return {String} A html-string that represents a document. * @return {String} A html-string that represents a document.
* @param filename
*/ */
exports.formatHtml = function (filename) { exports.formatHtml = function (filename) {
var htmlstring = fs.readFileSync(filename); let htmlstring = fs.readFileSync(filename);
try { try {
let dom = new JSDOM(htmlstring); // creates a dom from the html string let dom = new JSDOM(htmlstring); // creates a dom from the html string
let document = dom.window.document; let document = dom.window.document;
@ -56,4 +54,4 @@ exports.formatHtml = function(filename) {
console.error(error); console.error(error);
return htmlstring; return htmlstring;
} }
} };

@ -11,16 +11,16 @@ const fs = require("fs"),
".html": "html", ".html": "html",
".htm": "hmtl" ".htm": "hmtl"
}; };
var logger = require("winston"); let logger = require("winston");
/** /**
* Sets the logger for logging * Sets the logger for logging
* @param {Winston Logger} newLogger * @param {Object} newLogger
*/ */
exports.setLogger = function (newLogger) { exports.setLogger = function (newLogger) {
logger = newLogger; logger = newLogger;
caching.setLogger(logger); caching.setLogger(logger);
} };
/** /**
* Returns the data for the file. In some cases the data is processed or loaded from cache. * Returns the data for the file. In some cases the data is processed or loaded from cache.
@ -30,8 +30,8 @@ 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); // use the utils function to get the files extension let extension = utils.getExtension(filename); // use the utils function to get the files extension
var data = null; let data = null;
if (caching.isCached(filename)) return caching.getCached(filename) // return the cached file if it exists 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]) {
@ -56,4 +56,11 @@ exports.getProcessed = function(filename) {
logger.error(error); logger.error(error);
return "Processing Error"; return "Processing Error";
} }
} };
/**
* Cleanup function that calls the cleanup for the caching module
*/
exports.cleanup = function() {
caching.cleanup();
};

@ -14,7 +14,7 @@ exports.getExtension = function(filename) {
if (exts) return exts[exts.length - 1]; // return the found extension if (exts) return exts[exts.length - 1]; // return the found extension
else return null; // return null if no extension could be found else return null; // return null if no extension could be found
} catch (error) { } catch (error) {
logger.warn(error); console.error(error);
return null; return null;
} }
} }

@ -106,7 +106,7 @@ function main() {
} catch (error) { } catch (error) {
logger.error(error); logger.error(error);
logger.info("Shutting Down..."); logger.info("Shutting Down...");
caching.cleanup(); prepro.cleanup();
winston.end(); winston.end();
return false; return false;
} }
@ -114,20 +114,20 @@ function main() {
/** /**
* Returns a string that depends on the uri It gets the data from the routes variable. * Returns a string that depends on the uri It gets the data from the routes variable.
* @param {String} uriNormally a file-name. Depending on the extension, an other root-uriis choosen. * @param uri
* @return {String} An Array containing (Either the files content or an error message) and the mime-type. * @return {string[]} An Array containing (Either the files content or an error message) and the mime-type.
*/ */
function getResponse(uri) { function getResponse(uri) {
if (!uri || uri == "/") uri = "/index.html"; // uri redirects to the index.html if it is not set or if it is root if (!uri || uri === "/") uri = "/index.html"; // uri redirects to the index.html if it is not set or if it is root
logger.verbose({'msg': 'calculating response', 'path': uri}); logger.verbose({'msg': 'calculating response', 'path': uri});
let gp = prepro.getProcessed; let gp = prepro.getProcessed;
try { try {
// get the file extension // get the file extension
let extension = utils.getExtension(uri); let extension = utils.getExtension(uri);
// returns the global script or css if the extension is css or js and the root-uriis glob. // returns the global script or css if the extension is css or js and the root-uriis glob.
if (uri.includes("/glob") && (extension == ".sass" || extension == ".js")) { if (uri.includes("/glob") && (extension === ".sass" || extension === ".js")) {
logger.verbose("Using global uri"); logger.verbose("Using global uri");
if (extension == ".sass") return [gp("." + uri), "text/css"]; if (extension === ".sass") return [gp("." + uri), "text/css"];
else return [gp("." + uri), "text/javascript"]; else return [gp("." + uri), "text/javascript"];
} }
let mount = getMount(uri); // get mount for uri it will be uses as path later instead of route let mount = getMount(uri); // get mount for uri it will be uses as path later instead of route
@ -149,7 +149,7 @@ function getResponse(uri) {
*/ */
function getMount(uri) { function getMount(uri) {
if (mounts) { // if mounts are set if (mounts) { // if mounts are set
for (var mount of mounts){ // for each set mount for (let mount of mounts) { // for each set mount
if (mount.mount && mount.path) { // if the mount has the mount parameter and the path parameter if (mount.mount && mount.path) { // if the mount has the mount parameter and the path parameter
let regEx = RegExp(mount.mount); // create a regex from the mount let regEx = RegExp(mount.mount); // create a regex from the mount
if (uri.match(regEx)) { // if there is a match if (uri.match(regEx)) { // if there is a match
@ -162,7 +162,7 @@ function getMount(uri) {
} }
// Executing the main function // Executing the main function
if (typeof require != 'undefined' && require.main == module) { if (typeof require !== 'undefined' && require.main === module) {
logger.exceptions.handle( logger.exceptions.handle(
new winston.transports.File({ new winston.transports.File({
filename: './.log/frontserver-exceptions.log' filename: './.log/frontserver-exceptions.log'

Loading…
Cancel
Save