add devel device export

This commit is contained in:
Stewart Allen 2025-11-13 21:23:59 -05:00
commit 03f3211abe
2 changed files with 48 additions and 0 deletions

25
mods/devel/init.js Normal file
View file

@ -0,0 +1,25 @@
// back end for device export in dev mode
module.exports = function(server) {
server.inject("kiri", "kiri.js");
if (!server.env.debug) {
return;
}
const fs = require('fs');
server.api.postDevice = (req, res, next) => {
server.handler.decodePost(req, res, () => {
// console.log({decodePost: req.app.post});
const { device, process, profiles } = JSON.parse(req.app.post);
if (profiles) {
device.profiles = Object.entries(profiles).filter(v => v[0] !== 'default').map(v => v[1]);
} else {
device.profiles = [ process ];
}
const { mode } = device;
const { deviceName } = device;
const path = `${server.const.rootdir}/src/kiri-dev/${mode.toLowerCase()}/${deviceName.replace(/\ /g,'.')}`;
// console.log({ mode, device, process, path });
fs.writeFileSync(path, JSON.stringify(device, undefined, 4));
res.end('profile saved');
});
};
};

23
mods/devel/kiri.js Normal file
View file

@ -0,0 +1,23 @@
// provides a device export to local disk option
self.kiri.load(api => {
api.event.on('load-done', () => {
let deviceExport = api.device.export;
api.device.export = (exp, name, opt = {}) => {
const { event, record } = opt;
if (event && event.shiftKey & api.const.LOCAL) {
const { code, process, profiles } = record;
fetch("/api/postDevice", {
method: "POST",
body: JSON.stringify({ device: code, process, profiles })
})
.then(r => r.text())
.then(t => {
// console.log({server_said: t});
api.show.alert('profile saved to server');
});
} else {
deviceExport(exp, name);
}
};
});
});