UNPKG

iobroker.javascript

Version:
163 lines 5.2 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const node_child_process_1 = require("node:child_process"); const adapter = { log: { error: (text) => console.error(text), info: (text) => console.log(text), warn: (text) => console.warn(text), debug: (text) => console.log(text), }, setState: (_id, val) => { try { val = JSON.parse(val); } catch (e) { console.error(e); } console.log(`FROM: ${JSON.stringify(val)}`); }, extendForeignObjectAsync: (id, obj) => { console.log(`EXTEND: ${id} ${JSON.stringify(obj)}`); return Promise.resolve(); }, }; const context = { objects: {}, }; const debugState = { scriptName: '', child: null, promiseOnEnd: null, paused: false, endTimeout: null, running: false, breakOnStart: false, started: 0, }; function stopDebug() { if (debugState.child) { sendToInspector({ cmd: 'end' }); debugState.endTimeout = setTimeout(() => { debugState.endTimeout = null; debugState.child?.kill('SIGTERM'); }); debugState.promiseOnEnd = debugState.promiseOnEnd || Promise.resolve(0); } else { debugState.promiseOnEnd = Promise.resolve(0); } return debugState.promiseOnEnd.then(() => { debugState.child = null; debugState.running = false; debugState.scriptName = ''; if (debugState.endTimeout) { clearTimeout(debugState.endTimeout); debugState.endTimeout = null; } }); } function disableScript(id) { const obj = context.objects[id]; if (obj?.common?.enabled) { return adapter.extendForeignObjectAsync(obj._id, { common: { enabled: false }, }); } return Promise.resolve(); } function sendToInspector(message) { if (typeof message === 'string') { try { message = JSON.parse(message); } catch { adapter.log.error(`Cannot parse message to inspector: ${JSON.stringify(message)}`); return adapter.setState('debug.from', JSON.stringify({ error: 'Cannot parse message to inspector' })); } } if (debugState.child) { debugState.child.send(JSON.stringify(message)); } else { adapter.log.error(`Cannot send command to terminated inspector`); return adapter.setState('debug.from', JSON.stringify({ error: `Cannot send command to terminated inspector` })); } } /* function childPrint(text: string): void { console.log( text .toString() .split(/\r\n|\r|\n/g) .filter(chunk => !!chunk) .map(chunk => `< ${chunk}`) .join('\n'), ); } */ function debugScript(data) { // stop a script if it is running return disableScript(data.scriptName) .then(() => stopDebug()) .then(() => { debugState.scriptName = data.scriptName; debugState.breakOnStart = data.breakOnStart; const options = { stdio: ['ignore', 'inherit', 'inherit', 'ipc'], }; debugState.child = (0, node_child_process_1.fork)(`${__dirname}/../inspect.js`, [], options); /* debugState.child.stdout.setEncoding('utf8'); debugState.child.stderr.setEncoding('utf8'); debugState.child.stdout.on('data', childPrint); debugState.child.stderr.on('data', childPrint); */ debugState.child.on('message', (message) => { let debugMessage; try { debugMessage = JSON.parse(message); } catch { return adapter.log.error(`Cannot parse message from inspector: ${message}`); } adapter.setState('debug.from', JSON.stringify(debugMessage)); switch (debugMessage.cmd) { case 'ready': { debugState.child?.send(JSON.stringify({ cmd: 'start', scriptName: debugState.scriptName })); break; } case 'watched': { console.log(`WATCHED: ${JSON.stringify(debugMessage)}`); break; } case 'paused': { debugState.paused = true; console.log(`PAUSED`); break; } case 'resumed': { debugState.paused = false; console.log(`STARTED`); break; } case 'log': { console.log(`[${debugMessage.severity}] ${debugMessage.text}`); break; } case 'readyToDebug': { console.log(`readyToDebug (set breakpoints): [${debugMessage.scriptId}] ${debugMessage.script}`); break; } } }); }); } debugScript({ scriptName: 'script.js.Skript_1' }) .then(() => { console.log('Debugging started'); }) .catch(e => { console.error(e); }); //# sourceMappingURL=debug.js.map