Dashboard watcher

Signed-off-by: Romein van Buren <romein@vburen.nl>
This commit is contained in:
Romein van Buren 2022-07-08 15:18:40 +02:00
parent 6241de18fe
commit de2b8cdba8
Signed by: romein
GPG Key ID: 0EFF8478ADDF6C49
4 changed files with 45 additions and 6 deletions

View File

@ -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();

32
dashboard/watcher.js Normal file
View File

@ -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;
}
});

View File

@ -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;
},

View File

@ -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"
}