Dashboard watcher

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

31
dashboard/index.js Normal file
View File

@ -0,0 +1,31 @@
'use strict';
const { build: viteBuild } = require('vite');
const fs = require('fs').promises;
const { svelte } = require('@sveltejs/vite-plugin-svelte');
const path = __dirname + '/../gui/dashboard';
const build = (prefix = '') => viteBuild({
root: path,
base: `${prefix}/statusdashboard/asset/`,
plugins: [ svelte() ],
build: {
rollupOptions: {
output: {
assetFileNames: '[hash].[ext]',
entryFileNames: '[hash].js',
chunkFileNames: '[hash].js',
},
},
outDir: path + '/build',
},
});
const cleanup = () => fs.rm(path + '/build', {
recursive: true,
force: true,
});
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;
}
});