diff --git a/dashboard.js b/dashboard/index.js similarity index 72% rename from dashboard.js rename to dashboard/index.js index 2e2227e..f034db4 100644 --- a/dashboard.js +++ b/dashboard/index.js @@ -1,11 +1,13 @@ 'use strict'; const { build: viteBuild } = require('vite'); -const { svelte } = require('@sveltejs/vite-plugin-svelte'); const fs = require('fs').promises; +const { svelte } = require('@sveltejs/vite-plugin-svelte'); + +const path = __dirname + '/../gui/dashboard'; const build = (prefix = '') => viteBuild({ - root: __dirname + '/gui/dashboard/', + root: path, base: `${prefix}/statusdashboard/asset/`, plugins: [ svelte() ], build: { @@ -16,14 +18,14 @@ const build = (prefix = '') => viteBuild({ chunkFileNames: '[hash].js', }, }, - outDir: __dirname + '/gui/dashboard/build', + outDir: path + '/build', }, }); -const cleanup = () => fs.rm(__dirname + '/gui/dashboard/build', { +const cleanup = () => fs.rm(path + '/build', { recursive: true, force: true, }); -module.exports = { build, cleanup }; +module.exports = { build, cleanup, path }; build(); diff --git a/dashboard/watcher.js b/dashboard/watcher.js new file mode 100644 index 0000000..3b27f98 --- /dev/null +++ b/dashboard/watcher.js @@ -0,0 +1,32 @@ +'use strict'; + +const chokidar = require('chokidar'); +const dashboard = require('./index'); + +let watcher; +const handler = async () => { + console.log('status dashboard watcher triggered, rebuilding...'); + await dashboard.cleanup(); + await dashboard.build(); + process.send({ command: 'reload' }); +}; + +process.on('message', message => { + switch (message.command) { + case 'start': + if (message.path) { + watcher = chokidar.watch(message.path, { + ignored: [ /node_modules/, /build/ ], + }); + + watcher.on('add', handler); + watcher.on('change', handler); + watcher.on('unlink', handler); + } + break; + + default: + console.log(`Status dashboard watcher received unknown command ${message.command}`); + break; + } +}); diff --git a/index.js b/index.js index 16b04b5..1d04b66 100644 --- a/index.js +++ b/index.js @@ -127,6 +127,10 @@ module.exports = { await dashboard.cleanup(); await dashboard.build(server.settings.prefix); + + const dashWatcher = fork(__dirname + '/dashboard/watcher.js'); + dashWatcher.send({ command: 'start', path: dashboard.path }); + return true; }, diff --git a/package.json b/package.json index d73d407..53a0b0a 100644 --- a/package.json +++ b/package.json @@ -22,8 +22,9 @@ "url": "https://github.com/smartyellow/status/issues" }, "homepage": "https://github.com/smartyellow/status#readme", - "devDependencies": { + "dependencies": { "@sveltejs/vite-plugin-svelte": "^1.0.0-next.49", + "chokidar": "^3.5.3", "svelte": "^3.49.0", "vite": "^2.9.13" }