From 65d28eae27d71e22b2c6bc1ac3135622c14950ed Mon Sep 17 00:00:00 2001 From: jean-emmanuel Date: Wed, 7 Feb 2018 10:28:22 +0100 Subject: [PATCH] main: watch --theme paths and reload automatically --- app/main/callbacks.js | 1 - app/main/server.js | 2 +- app/main/settings.js | 55 +++++++++++++++++++++++++++++++++---------- 3 files changed, 43 insertions(+), 15 deletions(-) diff --git a/app/main/callbacks.js b/app/main/callbacks.js index ab6ff613..a0b38726 100644 --- a/app/main/callbacks.js +++ b/app/main/callbacks.js @@ -267,7 +267,6 @@ module.exports = { }, reloadCss:function(){ - if (settings.read('theme')) settings.reloadTheme() ipc.send('reloadCss') }, diff --git a/app/main/server.js b/app/main/server.js index a240c47e..acc3fffd 100644 --- a/app/main/server.js +++ b/app/main/server.js @@ -17,7 +17,7 @@ express.get('*', function(req, res){ if (req.path.indexOf('theme.css') != -1) { res.set('Content-Type', 'text/css'); if (settings.read('theme')) { - res.send(new Buffer(settings.read('theme'))) + res.send(new Buffer(settings.read('theme').join('\n'))) } else { res.send('') } diff --git a/app/main/settings.js b/app/main/settings.js index 8c2d05bc..e067b6e0 100644 --- a/app/main/settings.js +++ b/app/main/settings.js @@ -4,7 +4,8 @@ if (process.argv[1]&&process.argv[1].indexOf('-')==0) process.argv.unshift('') var baseDir = process.env[(process.platform == 'win32') ? 'USERPROFILE' : 'HOME'], configPath = require('path').join(baseDir, '.open-stage-control'), fs = require('fs'), - ifaces = require('os').networkInterfaces() + ifaces = require('os').networkInterfaces(), + chokidar = require('chokidar') var options = { 's':{alias:'send',type:'array',describe:'default targets for all widgets (ip:port pairs)', @@ -104,19 +105,50 @@ var configFile = function(){try {return JSON.parse(fs.readFileSync(configPath,'u config = JSON.parse(JSON.stringify(configFile)), defaultConfig -var loadTheme = function(t){ - if (!t) return + +var loadTheme = function(themes){ + if (!themes) return var style = [] - for (i in t) { - try {style.push(fs.readFileSync(__dirname + '/themes/' + t[i] + '.css','utf-8'))} - catch(err) { - try {style.push(fs.readFileSync(t[i],'utf-8'))} - catch(err) { - console.log('Theme "' + t[i] + '" not found.') + + for (let i in themes) { + let path = themes[i] + if (!fs.existsSync(path)) { + path = __dirname + '/themes/' + themes[i] + '.css' + if (!fs.existsSync(path)) { + path = false } } + + if (path) { + + let updateStyle = (i)=>{ + try { + style[i] = fs.readFileSync(path,'utf-8') + } catch(err) { + throw err + } + } + let watchFile = ()=>{ + var watcher = chokidar.watch(path) + watcher.on('change',()=>{ + updateStyle(i) + require('./callbacks').reloadCss() + watcher.close() + watchFile() + }) + } + + watchFile() + updateStyle(i) + + + } else { + console.error('Theme "' + themes[i] + '" not found.') + } + } - return style.join('\n') + + return style } var makeDefaultConfig = function(argv){ @@ -178,8 +210,5 @@ module.exports = { if (err) throw err }) }, - reloadTheme:function(){ - module.exports.write('theme',loadTheme(argv.t),true) - }, cli: cli }