Skip to content
This repository has been archived by the owner on Feb 25, 2022. It is now read-only.

Commit

Permalink
implemented logging
Browse files Browse the repository at this point in the history
  • Loading branch information
moruzerinho6 committed Jan 16, 2021
1 parent 3d6b353 commit 60050e7
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 18 deletions.
31 changes: 28 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
"main": "src/index.js",
"dependencies": {
"@ibm/plex": "^5.1.3",
"ini-parser": "^0.0.2"
"caller-id": "^0.1.0",
"ini-parser": "^0.0.2",
"simple-node-logger": "^18.12.24"
},
"scripts": {
"start": "electron src/index.js"
Expand All @@ -23,7 +25,7 @@
"url": "https://github.com/moruzerinho6/Webini/issues"
},
"homepage": "https://github.com/moruzerinho6/Webini#readme",
"bin": "electron src/index.js",
"bin": "electron src/index.js",
"pkg": {
"targets": [
"node14"
Expand Down
32 changes: 22 additions & 10 deletions pageFiles/themes/moondance/assets/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,20 @@ window.api.send('toMain', 'start')

const pages = [homeElem, aboutElem, downloadsElem, faqElem, controllersElem, noteskinsElem, toolsElem, historicalChangelogElem, communityPoliciesElem]
const pagesStr = ['home', 'about', 'downloads', 'faq', 'controllers', 'noteskins', 'tools', 'historicalChangelog', 'communityPolicies']
/**
*
* @param {string} arg The message to log info.
*/
const info = (arg) => {
window.api.send('toMain', ['logInfo', arg])
}
/**
*
* @param {string} arg The message to log warn.
*/
const warn = (arg) => {
window.api.send('toMain', ['logWarn', arg])
}
/**
*
* @param {string} page
Expand All @@ -105,18 +119,21 @@ const translate = (page, translation) => {
}

toggleStringsView.onclick = (() => {
info(`Switching keyView mode to ${!window.tinyWebGlobal.keyViewMode}`)
window.tinyWebGlobal.keyViewMode = !window.tinyWebGlobal.keyViewMode
window.api.send('toMain', 'translationUpdate')
// translate(window.tinyWebGlobal.actualPage, window.tinyWebGlobal.translation)
})

highlightElements.onclick = (() => {
info(`Switching highlight mode to ${!window.tinyWebGlobal.highlight}`)
window.tinyWebGlobal.highlight = !window.tinyWebGlobal.highlight
window.api.send('toMain', 'translationUpdate')
// translate(window.tinyWebGlobal.actualPage, window.tinyWebGlobal.translation)
})

generateFiles.onclick = (() => {
info('Generating HTMl Files')
const languageCode = window.tinyWebGlobal.translation.Common.LanguageCode === "en" ? "" : `-${window.tinyWebGlobal.translation.Common.LanguageCode}`
const htmlFiles = Object.keys(window.tinyWebGlobal.template)
let allReady = false
Expand Down Expand Up @@ -158,13 +175,6 @@ generateFiles.onclick = (() => {
}

window.api.send('toMain', ['mkdir', window.tinyWebGlobal.pathToGenerateFiles + '/' + 'static-pages' + languageCode])
/*
fs.mkdir(window.tinyWebGlobal.pathToGenerateFiles + '/' + 'static-pages' + languageCode, { recursive: true }, err => {
if (err) {
console.log(err)
}
})
*/

const files = ['static-pages-historical-changelog.htm', 'static-pages-about.htm', 'static-pages-addons.htm', 'static-pages-add-ons.noteskins.htm', 'static-pages-faq.htm', 'static-pages-help-support.htm', 'static-pages-community-policies.htm']

Expand Down Expand Up @@ -192,6 +202,7 @@ const translationReady = (translation) => {
for (let i = 0; i < pages.length; i++) {
// Special case for home page with jumbatron
if (i === 0) {
info('Switching to home page')
pages[0].onclick = (_) => {
jumbatronPlaceElem.innerHTML = jumbatron
contentDivElem.innerHTML = window.tinyWebGlobal.files[0]
Expand All @@ -201,16 +212,18 @@ const translationReady = (translation) => {
continue
}
pages[i].onclick = (_) => {
info(`Switching to page ${pagesStr[i]}`)
if (window.tinyWebGlobal.actualPage === 'home') {
jumbatronPlaceElem.innerHTML = ''
}
window.tinyWebGlobal.actualPage = pagesStr[i]
contentDivElem.innerHTML = 'hi' //`${window.tinyWebGlobal.files[i]}`
contentDivElem.innerHTML = `${window.tinyWebGlobal.files[i]}`
translate(pagesStr[i], translation)
}
}

translationUpdate.onclick = (() => {
info('Requesting translation update')
window.api.send('toMain', 'translationUpdate')
})
}
Expand All @@ -221,8 +234,7 @@ const translationReady = (translation) => {
* @param {string} [actualPage]
*/
const translationReload = (translation, actualPage) => {
// The default page is always available in every page, so always update it.
info('Reloading translation')
translate('default', translation)
// console.log(actualPage)
translate(actualPage, translation)
}
62 changes: 59 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,50 @@ const {
} = require("electron");
const path = require("path");
const fs = require("fs");
fs.truncate(path.join(__dirname, '../logs/info.txt'), 0, ()=>{})
const callerId = require('caller-id')
const logger = require('simple-node-logger').createSimpleFileLogger({ logFilePath: path.join(__dirname, '../logs/info.txt') })
const translation = parser.parseFileSync(path.join(__dirname, '../translated.ini'))
const template = require('../template.json')

let mainWindow;

/**
* Returns the name of the file from the file path.
* @param {String} path - The file Path
* @returns {String} - name of the file from the file path.
*/
function fileName (path) {
let pathSplit = path.split('\\')
if (pathSplit[0] === path) {
pathSplit = path.split('/')
}

return pathSplit[pathSplit.length - 1].replace(/.js/gi, '')
}

/**
* Executes log.info
* @param {String} msg - The message to info.
*/
const info = (msg) => {
const Data = callerId.getData()
logger.info(`[${fileName(Data.filePath)}] at line ${Data.lineNumber} - ${msg}`)
}

/**
* Executes log.warn
* @param {String} msg - The message to warn.
*/
const warn = (msg) => {
const Data = callerId.getData()
logger.warn(`[${fileName(Data.filePath)}] at line ${Data.lineNumber} - ${msg}`)
}

function createWindow () {
// Create the browser window.

info('Creating window')
mainWindow = new BrowserWindow({
width: 800,
height: 600,
Expand All @@ -29,36 +66,52 @@ function createWindow () {
})

// and load the index.html of the app.
info('Loading default html')
mainWindow.loadFile(path.join(__dirname, "../pageFiles/themes/moondance/assets/default.html"))
mainWindow.once('ready-to-show', () => {
info('Showing window')
mainWindow.show()
})
}

ipcMain.on("toMain", (event, args) => {
if (args.length === 2) {
info('Recieved a call to Main')
if (args[0] === 'writeFile') {
info(`Writing a file, arguments: ${[args[1][0], args[1][1]].join(', ')}`)
fs.writeFileSync(args[1][0], args[1][1])
}

if (args[0] === 'mkdir') {
info(`Writing a directory to ${args[1]}`)
fs.mkdir(args[1], { recursive: true }, err => {
if (err) console.log(err)
if (err) warn(`Error while writing a directory, error: ${err}`)
})
}

if (args[0] === 'rename') {
info(`Renaming (or moving) ${args[1][0]} to ${args[1][1]}`)
fs.rename(args[1][0], args[1][1], (err) => {
if (err) console.log(err)
if (err) warn(`Error while writing a renaming, error: ${err}`)
})
}

if (args[0] === 'logInfo') {
info(args[1])
}

if (args[0] === 'logwarn') {
warn(args[1])
}
return
}
if (args === 'translationUpdate') {
info('An translation update was requested, updating translation.')
mainWindow.webContents.send('fromMain', ['translationUpdate', parser.parseFileSync(path.join(__dirname, '../translated.ini'))])
return
}

info('Sending files, template, translation to isolated window.')
mainWindow.webContents.send('fromMain', ['translation', translation])
mainWindow.webContents.send('fromMain', ['template', template])
mainWindow.webContents.send('fromMain', ['pathGeneratedFiles', path.join(__dirname, '../generatedFiles')])
Expand All @@ -80,14 +133,17 @@ ipcMain.on("toMain", (event, args) => {
})

app.whenReady().then(() => {
info('App is ready, creating window')
createWindow()

app.on('activate', function () {
if (BrowserWindow.getAllWindows().length === 0) createWindow()
})
})


/*
We don't support mac at the moment.
app.on('window-all-closed', function () {
if (process.platform !== 'darwin') app.quit()
})
*/

0 comments on commit 60050e7

Please sign in to comment.