diff --git a/app.js b/app.js index 0c46fa6..eadea0e 100755 --- a/app.js +++ b/app.js @@ -69,12 +69,14 @@ global.APPLICATION_ROOT = argv.path || process.cwd(); process.env = require("./app/env.js")(argv); global.WIKI_PATH = process.env.WIKI_PATH global.WIKI_NAME = argv.name || process.env.GIT_REPO_URL.split("/").pop().replace(".git", "").toUpperCase() +global.WIKI_CONTENTS_DIRECTORY_NAME = "_contents"; -global.logger = require("./app/log/logger.js"); -global.git = require("./app/git.js"); +global.logger = require("./app/log/logger.js") +global.git = require("./app/git.js") global.markdown = require("./app/markdown.js") -global.wikiMap = require("./app/map.js"); -global.wikiPage = require("./app/page.js"); +global.wikiMap = require("./app/map.js") +global.wikiPage = require("./app/page.js") +global.wikiContents = require('./app/content.js') logger.debug("Using root path: "+APPLICATION_ROOT+" and wiki path: "+WIKI_PATH) logger.debug("Parchment currently running as user: "+require('os').userInfo().username) diff --git a/app/content.js b/app/content.js new file mode 100755 index 0000000..f853e0a --- /dev/null +++ b/app/content.js @@ -0,0 +1,39 @@ +const path = require("path"); +const fs = require("fs") + +let contents = {}; +const contentsDirName = WIKI_CONTENTS_DIRECTORY_NAME; +const units = ["css", "favicon", "header", "background"]; + +module.exports = function(){ + updateContents(); + + setInterval(updateContents, process.env.WIKI_CONTENT_UPDATE_INTERVAL * 60 * 1000); + + return function(){return contents;}() +}() + +function updateContents(){ + logger.debug("Updating contents...") + contents = { + "css": "/style.css", + "favicon": "/res/img/favicon.png", + "header": false, + "background": false + } + + for (k in units){ + const v = units[k] + const dirPath = path.join(WIKI_PATH, contentsDirName, v); + if (fs.existsSync(dirPath)){ + const files = fs.readdirSync(dirPath) + if (files.length > 0){ + contents[v] = path.join(dirPath, files[0]).replace(WIKI_PATH, "") + logger.debug("Found custom content for "+v+"! ("+files[0]+")") + continue + } + } + } + + logger.debug("Done!") +} \ No newline at end of file diff --git a/app/defaultenv.js b/app/defaultenv.js index 645220f..76678e4 100755 --- a/app/defaultenv.js +++ b/app/defaultenv.js @@ -6,5 +6,8 @@ module.exports = { GIT_PEM_FILE: "git.pem", GIT_REPO_URL: "git@github.com:louvekingdoms/Wiki.git", GIT_REPO_BRANCH: "auto", - WIKI_PATH: require("path").join(APPLICATION_ROOT, "wiki") + GIT_PULL_INTERVAL: 1, // minutes + GIT_PUSH_INTERVAL: 0, // minutes + WIKI_PATH: require("path").join(APPLICATION_ROOT, "wiki"), + WIKI_CONTENT_UPDATE_INTERVAL: 1 //minutes } diff --git a/app/env.js b/app/env.js index 66acb86..a967ee8 100755 --- a/app/env.js +++ b/app/env.js @@ -1,12 +1,16 @@ module.exports = function(env){ - cEnv = process.env; + cEnv = {}; defaultEnv = require("./defaultenv.js"); cEnv.DEBUG_LEVEL = env.debuglvl || process.env.DEBUG_LEVEL || defaultEnv.DEBUG_LEVEL - cEnv.PORT = env.port || process.env.PORT || defaultEnv.PORT - cEnv.LOG_PORT = env.logport || process.env.LOG_PORT || defaultEnv.LOG_PORT + cEnv.PORT = parseInt(env.port || process.env.PORT || defaultEnv.PORT) + cEnv.LOG_PORT = parseInt(env.logport || process.env.LOG_PORT || defaultEnv.LOG_PORT) cEnv.GIT_PEM_FILE = env.pemfile || process.env.GIT_PEM_FILE || defaultEnv.GIT_PEM_FILE cEnv.GIT_REPO_URL = env.repo || process.env.GIT_REPO_URL || defaultEnv.GIT_REPO_URL cEnv.GIT_REPO_BRANCH = env.branch || process.env.GIT_REPO_BRANCH || defaultEnv.GIT_REPO_BRANCH + cEnv.GIT_PULL_INTERVAL = parseInt(process.env.GIT_PULL_INTERVAL || defaultEnv.GIT_PULL_INTERVAL) + cEnv.GIT_PUSH_INTERVAL = parseInt(process.env.GIT_PUSH_INTERVAL || defaultEnv.GIT_PUSH_INTERVAL) cEnv.WIKI_PATH = env.wikipath || process.env.WIKI_PATH || defaultEnv.WIKI_PATH + cEnv.GIT_PUSH_INTERVAL = process.env.GIT_PUSH_INTERVAL || defaultEnv.GIT_PUSH_INTERVAL + cEnv.WIKI_CONTENT_UPDATE_INTERVAL = parseInt(process.env.WIKI_CONTENT_UPDATE_INTERVAL || defaultEnv.WIKI_CONTENT_UPDATE_INTERVAL) return cEnv; -} \ No newline at end of file +} diff --git a/app/express.js b/app/express.js index 73a54ed..e7837b1 100755 --- a/app/express.js +++ b/app/express.js @@ -47,6 +47,11 @@ module.exports = function(port){ ) }) + // Wiki contents route + app.get('/'+WIKI_CONTENTS_DIRECTORY_NAME+"/*", function(req, res){ + res.sendFile(path.join(WIKI_PATH, req.path)) + }) + // Public directory app.use(express.static(path.join(APPLICATION_ROOT, 'public'))); @@ -59,12 +64,14 @@ module.exports = function(port){ } function getPageInfo(req){ + return { header: getHeaderInfo(), navigation: getNavigationInfo(req), footer: getFooterInfo(), website: { - name: WIKI_NAME + name: WIKI_NAME, + links: wikiContents } } } diff --git a/app/git.js b/app/git.js index 33e0c93..546f810 100755 --- a/app/git.js +++ b/app/git.js @@ -58,11 +58,19 @@ module.exports = async function(){ process.exit(1) }); - logger.info("Git client OK!") + logger.info("Git client OK! I will pull every "+ + process.env.GIT_PULL_INTERVAL+" minute" + +(process.env.GIT_PUSH_INTERVAL >0? " and push every "+process.env.GIT_PUSH_INTERVAL+" minute": "") + + "!") await pull(); - setInterval(async function(){ await pull() }, 1000 * 60) + if (process.env.GIT_PULL_INTERVAL > 0) setInterval(async function(){ await pull() }, process.env.GIT_PULL_INTERVAL * 1000 * 60) + else{ + logger.error("Impossible pull interval ("+process.env.GIT_PULL_INTERVAL+"), aborting to avoid damage."); + process.exit(1); + } + if (process.env.GIT_PUSH_INTERVAL > 0) setInterval(async function(){ await checkAndUploadModifications() }, process.env.GIT_PUSH_INTERVAL * 1000 * 60) return { checkAndUploadModifications: checkAndUploadModifications @@ -85,13 +93,43 @@ async function pull(){ if (!error) logger.info("Done!") } -async function checkAndUploadModifications(){ - if (isOperating) return; +async function checkAndUploadModifications(changesName=null){ + if(isOperating){ + const wait = 2; + logger.debug("Could not check and upload modifications because of another operation, waiting for "+wait+" seconds"); + setTimeout(async function(){await checkAndUploadModifications(changesName)}, wait * 1000); + return; + } isOperating = true; + // 1. git add -A // 2. git commit // 3. git pull // 4. commit the merge // 5. git push origin branch + + logger.info("Checking for modifications...") + await gitClient.add("-A") + .then(()=>{ + logger.debug("Committing...") + return gitClient.commit(changesName || ("Update at "+new Date().toString())) + }) + .then(() => { + logger.debug("Pulling...") + return gitClient.pull("origin", process.env.GIT_REPO_BRANCH); + }) + .then(() =>{ + logger.debug("Merging...") + return gitClient.commit("Merge") + }) + .catch((e) =>{ + logger.debug("There was an error during the merge, but this is probably because there is nothing to merge.") + }) + .then(() => { + logger.debug("Pushing...") + return gitClient.push("origin", process.env.GIT_REPO_BRANCH) + }); + logger.info("Done!") + isOperating = false; } \ No newline at end of file diff --git a/app/layout/default.pug b/app/layout/default.pug index 0802b1f..f5a85b8 100755 --- a/app/layout/default.pug +++ b/app/layout/default.pug @@ -9,11 +9,11 @@ mixin recursive_loop(array, index, current, prefix) doctype html html head - title #{website.name} wiki - link(rel="stylesheet", href="/style.css") - link(rel="icon", type="image/png", href="/res/img/favicon.png") + title #{website.name} + link(rel="stylesheet", href=website.links.css) + link(rel="icon", type="image/png", href=website.links.favicon) body - .header + .header(style=(website.links.background ? "background-image:url('"+website.links.background+"')" : "")) include elements/header.pug .mainContainer .navigation diff --git a/public/style.css b/public/style.css index cba8906..f61dc9e 100755 --- a/public/style.css +++ b/public/style.css @@ -33,13 +33,18 @@ body{ } .header{ - padding: 5px; - padding-left:20px; border-bottom:1px dotted grey; + background-size:cover; + background-repeat: no-repeat; + background-origin: content-box; + background-position: center; } .header h1{ + text-indent: 1%; font-weight:normal; + color: rgb(255, 255, 255); + mix-blend-mode: difference; } .mainContainer{ diff --git a/start.sh b/start.sh new file mode 100644 index 0000000..ba2b7db --- /dev/null +++ b/start.sh @@ -0,0 +1 @@ +node app.js run