diff --git a/src/functions/codeEditor.js b/src/functions/codeEditor.js index d175a496fca5bf65bc424b55ecef02c99fdd7c4e..cae386d66d3dc67508767ad9978add0414878aaa 100644 --- a/src/functions/codeEditor.js +++ b/src/functions/codeEditor.js @@ -6,7 +6,7 @@ import { showCheck } from "./visualValidation"; import { handlePipeBinding, getPipesForFilter } from "./pipeBinding"; let editorValues = { - doc: "const amqp = require('amqplib/callback_api');\n\nconst rabbitmqUrl = 'amqp://mquser:mqpass@rabbit:5672';\n\namqp.connect(rabbitmqUrl, (error0, connection) => {\n if (error0) {\n throw error0;\n }\n connection.createChannel((error1, channel) => {\n if (error1) {\n throw error1;\n }\n\n\n\n //TODO: Code logic here\n\t});\n});\n", + doc: "const amqp = require('amqplib/callback_api');\n\nconst rabbitmqUrl = 'amqp://mquser:mqpass@rabbit:5672';\n\namqp.connect(rabbitmqUrl, (error0, connection) => {\n if (error0) {\n throw error0;\n }\n connection.createChannel((error1, channel) => {\n if (error1) {\n throw error1;\n }\n\n // START pipe-binding\n // END pipe-binding\n\n // TODO: Code logic here\n\n\n\t});\n});\n", extensions: [basicSetup, javascript()], }; diff --git a/src/functions/pipeBinding.js b/src/functions/pipeBinding.js index 3d4a9443d691297fc12bf73e0173699c0e2d66ba..2fbd594f5bdf316c08035b2f0cd6ec0da269b555 100644 --- a/src/functions/pipeBinding.js +++ b/src/functions/pipeBinding.js @@ -49,34 +49,47 @@ export const getPipesForFilter = (instance) => { }; export const handlePipeBinding = (pipeMapping, editor) => { - let lineNumber = 14; - let editorCodeText = editor.state.doc.toString(); - pipeMapping.forEach((pipe) => { - if ( - editorCodeText.includes( - `const ${makeValidConstName(pipe.pipeName)} = "${pipe.pipeName}"` - ) - ) { - return; - } else { - let line = editor.state.doc.line(lineNumber); - let position = line.from; - let pipeNameUserGiven = pipe.pipeName; - let pipeNameDeklaration = makeValidConstName(pipeNameUserGiven); - let insertCode = `\t\tconst ${pipeNameDeklaration} = "${pipeNameUserGiven}";\n\t\tchannel.assert${ - pipe.pipeType === "Queue" ? "Queue" : "Exchange" - }(${pipeNameDeklaration}, ${ - pipe.pipeType === "Topic" ? `"topic", ` : "" - }{\n\t\t\tdurable: false\n\t\t});\n`; - let transaction = editor.state.update({ - changes: { - from: position, - insert: insertCode, + // clear pipe-binindg code + const codeArray = editor.state.doc.toString(); + const start = " // START pipe-binding"; + const end = " // END pipe-binding"; + + const startIndex = codeArray.indexOf(start); + const endIndex = codeArray.indexOf(end, startIndex + start.length); + + if (startIndex && endIndex) { + const transaction = editor.state.update({ + changes: [ + { + from: startIndex + start.length, + to: endIndex, + insert: "\n", }, - }); - editor.dispatch(transaction); - lineNumber = lineNumber + 4; - } + ], + }); + editor.dispatch(transaction); + } + + // insert pipe-binding-code + let lineNumber = 15; + pipeMapping.forEach((pipe) => { + let line = editor.state.doc.line(lineNumber); + let position = line.from; + let pipeNameUserGiven = pipe.pipeName; + let pipeNameDeklaration = makeValidConstName(pipeNameUserGiven); + let insertCode = `\t\tconst ${pipeNameDeklaration} = "${pipeNameUserGiven}";\n\t\tchannel.assert${ + pipe.pipeType === "Queue" ? "Queue" : "Exchange" + }(${pipeNameDeklaration}, ${ + pipe.pipeType === "Topic" ? `"topic", ` : "" + }{\n\t\t\tdurable: false\n\t\t});\n`; + let transaction = editor.state.update({ + changes: { + from: position, + insert: insertCode, + }, + }); + editor.dispatch(transaction); + lineNumber = lineNumber + 4; }); };