UNPKG

iobroker.vis-2

Version:

Next generation graphical user interface for ioBroker.

172 lines 7.31 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.syncWidgetSets = syncWidgetSets; const node_fs_1 = require("node:fs"); const node_path_1 = require("node:path"); const mime_1 = __importDefault(require("mime")); const node_crypto_1 = require("node:crypto"); const wwwDir = (0, node_fs_1.existsSync)(`${__dirname}/../../www/`) ? `${__dirname}/../../www/` : `${__dirname}/../www/`; const TEXT_TYPES = ['application/json', 'application/javascript', 'image/svg+xml']; const generic = ['basic', 'jqplot', 'jqui', 'swipe', 'tabs']; const widgetSetsDependencies = { jqui: ['basic'], }; function copyFileSync(source, target, forceBuild) { let targetFile = target; let changed = false; // if target is a directory a new file with the same name will be created if ((0, node_fs_1.existsSync)(target)) { if ((0, node_fs_1.lstatSync)(target).isDirectory()) { targetFile = (0, node_path_1.join)(target, (0, node_path_1.basename)(source)); } } if ((0, node_fs_1.existsSync)(targetFile)) { const newFile = (0, node_fs_1.readFileSync)(source); const oldFile = (0, node_fs_1.readFileSync)(targetFile); const type = mime_1.default.getType(source); !type && console.log(`Unknown file type: ${source}`); if (newFile.byteLength !== oldFile.byteLength || forceBuild) { changed = true; (0, node_fs_1.writeFileSync)(targetFile, newFile); } else if (type && (type.startsWith('text/') || TEXT_TYPES.includes(type))) { if (newFile.toString('utf8') !== oldFile.toString('utf8')) { changed = true; (0, node_fs_1.writeFileSync)(targetFile, newFile); } } else { const hashSumOld = (0, node_crypto_1.createHash)('sha256'); hashSumOld.update(oldFile); const hexOld = hashSumOld.digest('hex'); const hashSumNew = (0, node_crypto_1.createHash)('sha256'); hashSumNew.update(newFile); const hexNew = hashSumNew.digest('hex'); if (hexNew !== hexOld) { changed = true; (0, node_fs_1.writeFileSync)(targetFile, newFile); } } } else { changed = true; (0, node_fs_1.writeFileSync)(targetFile, (0, node_fs_1.readFileSync)(source)); } return changed; } function copyFolderRecursiveSync(source, target, forceBuild) { let oldFiles = []; let files = []; let changed = false; // check if folder needs to be created or integrated const targetFolder = (0, node_path_1.join)(target, (0, node_path_1.basename)(source)); if (!(0, node_fs_1.existsSync)(targetFolder)) { (0, node_fs_1.mkdirSync)(targetFolder); } // copy if ((0, node_fs_1.lstatSync)(source).isDirectory()) { files = (0, node_fs_1.readdirSync)(source); oldFiles = (0, node_fs_1.readdirSync)(targetFolder); files.forEach(file => { const curSource = (0, node_path_1.join)(source, file); if ((0, node_fs_1.lstatSync)(curSource).isDirectory()) { if (copyFolderRecursiveSync(curSource, targetFolder, forceBuild)) { changed = true; } } else if (copyFileSync(curSource, targetFolder, forceBuild)) { changed = true; } }); // Delete all old files if (target !== (0, node_path_1.normalize)(wwwDir)) { oldFiles.forEach(file => { const pathName = (0, node_path_1.join)(targetFolder, file); if (!files.includes(file) && !(0, node_fs_1.lstatSync)(pathName).isDirectory()) { (0, node_fs_1.unlinkSync)(pathName); changed = true; } }); } } return changed; } function deleteFolderRecursive(dirPath) { let files = []; if ((0, node_fs_1.existsSync)(dirPath)) { files = (0, node_fs_1.readdirSync)(dirPath); files.forEach(file => { const curPath = `${dirPath}/${file}`; if ((0, node_fs_1.lstatSync)(curPath).isDirectory()) { // recurse deleteFolderRecursive(curPath); } else { // delete file (0, node_fs_1.unlinkSync)(curPath); } }); (0, node_fs_1.rmdirSync)(dirPath); } } function syncWidgetSets(enabledList, forceBuild) { let filesChanged = false; let found; let name; const v2 = {}; const normalizedWwwDir = (0, node_path_1.normalize)(wwwDir); // Now we have the list of widgets => copy them all to widgets directory for (let d = 0; d < enabledList.length; d++) { const _changed = copyFolderRecursiveSync(`${enabledList[d].path}/widgets/`, normalizedWwwDir, forceBuild); if (_changed) { filesChanged = true; } console.log(`Check ${enabledList[d].path.replace(/\\/g, '/').split('/').pop()}... ${_changed ? 'COPIED.' : 'no changes.'}`); v2[enabledList[d].name.replace('iobroker.', '').replace('ioBroker.', '')] = !!enabledList[d].pack.common.visWidgets; } const widgetSets = []; // Read the list of installed widgets const installed = (0, node_fs_1.readdirSync)(`${wwwDir}widgets/`); for (let d = 0; d < installed.length; d++) { if (installed[d].match(/\.html$/)) { name = installed[d].replace('.html', ''); const isGeneric = generic.includes(name); if (!found) { found = enabledList.find(w => w.name.toLowerCase() === `iobroker.vis-${name}` || w.name.toLowerCase() === `iobroker.${name}`); } if (!found && !isGeneric) { filesChanged = true; // delete (0, node_fs_1.unlinkSync)(`${wwwDir}widgets/${name}.html`); if ((0, node_fs_1.existsSync)(`${wwwDir}widgets/${name}`)) { deleteFolderRecursive((0, node_path_1.normalize)(`${wwwDir}widgets/${name}`)); } } else if (isGeneric) { if ((Array.isArray(widgetSetsDependencies[name]) && widgetSetsDependencies[name].length) || widgetSetsDependencies[name] || typeof widgetSetsDependencies[name] === 'string') { widgetSets.push({ name, depends: widgetSetsDependencies[name], v2: false }); } else { widgetSets.push({ name, v2: false }); } } else if (found?.pack?.native?.always) { widgetSets.push({ name, always: true, v2: v2[name] }); } else if (found?.pack?.native?.dependencies?.length) { widgetSets.push({ name, depends: found.pack.native.dependencies, v2: v2[name] }); } else { widgetSets.push({ name, v2: v2[name] }); } } } return { widgetSets, filesChanged }; } //# sourceMappingURL=install.js.map