From ec704cbe3445ff5d5d2eddfaf6c2be16a8cd7be4 Mon Sep 17 00:00:00 2001 From: Simon Larsen Date: Tue, 9 Jul 2024 21:06:40 +0100 Subject: [PATCH] Refactor executeScript function to execute multiple commands sequentially --- Copilot/Init.ts | 3 +-- .../CopilotActions/CopilotActionsBase.ts | 6 ++---- Copilot/Service/CopilotActions/Index.ts | 6 ++---- Copilot/Utils/CodeRepository.ts | 18 ++++++++++++++++-- Copilot/nodemon.json | 13 +++++++++---- 5 files changed, 30 insertions(+), 16 deletions(-) diff --git a/Copilot/Init.ts b/Copilot/Init.ts index 69777a8e17..12145cd213 100644 --- a/Copilot/Init.ts +++ b/Copilot/Init.ts @@ -57,10 +57,9 @@ const init: PromiseVoidFunction = async (): Promise => { if (onAfterCloneScript) { logger.info("Executing on-after-clone script."); - const result: string = await CodeRepositoryUtil.executeScript({ + await CodeRepositoryUtil.executeScript({ script: onAfterCloneScript, }); - logger.info(result); logger.info("on-after-clone script executed successfully."); } diff --git a/Copilot/Service/CopilotActions/CopilotActionsBase.ts b/Copilot/Service/CopilotActions/CopilotActionsBase.ts index 7fcdf3c639..d7e703414d 100644 --- a/Copilot/Service/CopilotActions/CopilotActionsBase.ts +++ b/Copilot/Service/CopilotActions/CopilotActionsBase.ts @@ -152,10 +152,9 @@ If you have any feedback or suggestions, please let us know. We would love to h ); } else { logger.info("Executing on-before-copilot-action script."); - const result: string = await CodeRepositoryUtil.executeScript({ + await CodeRepositoryUtil.executeScript({ script: onBeforeExecuteActionScript, }); - logger.info(result); logger.info("on-before-copilot-action script executed successfully"); } @@ -199,10 +198,9 @@ If you have any feedback or suggestions, please let us know. We would love to h if (onAfterExecuteActionScript) { logger.info("Executing on-after-copilot-action script."); - const result: string = await CodeRepositoryUtil.executeScript({ + await CodeRepositoryUtil.executeScript({ script: onAfterExecuteActionScript, }); - logger.info(result); logger.info("on-after-copilot-action script executed successfully"); } diff --git a/Copilot/Service/CopilotActions/Index.ts b/Copilot/Service/CopilotActions/Index.ts index 138745fd6e..aa8cc79f3a 100644 --- a/Copilot/Service/CopilotActions/Index.ts +++ b/Copilot/Service/CopilotActions/Index.ts @@ -123,10 +123,9 @@ export default class CopilotActionService { logger.debug("No on-before-commit script found for this repository."); } else { logger.info("Executing on-before-commit script."); - const result: string = await CodeRepositoryUtil.executeScript({ + await CodeRepositoryUtil.executeScript({ script: onBeforeCommitScript, }); - logger.info(result); logger.info("on-before-commit script executed successfully."); } @@ -144,10 +143,9 @@ export default class CopilotActionService { if (onAfterCommitScript) { logger.info("Executing on-after-commit script."); - const result: string = await CodeRepositoryUtil.executeScript({ + await CodeRepositoryUtil.executeScript({ script: onAfterCommitScript, }); - logger.info(result); logger.info("on-after-commit script executed successfully."); } diff --git a/Copilot/Utils/CodeRepository.ts b/Copilot/Utils/CodeRepository.ts index bb99672f8c..14ed03cf5c 100644 --- a/Copilot/Utils/CodeRepository.ts +++ b/Copilot/Utils/CodeRepository.ts @@ -130,8 +130,22 @@ export default class CodeRepositoryUtil { } public static async executeScript(data: { script: string }): Promise { - await Execute.executeCommand(`cd ${this.getLocalRepositoryPath()}`); // change directory to the repository path - return await Execute.executeCommand(data.script); // execute the script + const commands: Array = data.script.split("\n"); + + const results: Array = []; + + for (const command of commands) { + logger.info(`Executing command: ${command}`); + const commandResult: string = await Execute.executeCommand( + `cd ${this.getLocalRepositoryPath()} && ${command}`, + ); + if (commandResult) { + logger.info(`Command result: ${commandResult}`); + results.push(commandResult); + } + } + + return results.join("\n"); } public static async getRepoScript(data: { diff --git a/Copilot/nodemon.json b/Copilot/nodemon.json index a7d154ee51..bf7a1b8a2f 100644 --- a/Copilot/nodemon.json +++ b/Copilot/nodemon.json @@ -1,5 +1,10 @@ { - "watch": ["./","../Common", "../CommonServer", "../Model"], - "ext": "ts,json,tsx,env,js,jsx,hbs", - "exec": "node --inspect=0.0.0.0:9229 --require ts-node/register Index.ts" - } \ No newline at end of file + "watch": [ + "./", + "../Common", + "../CommonServer", + "../Model" + ], + "ext": "ts,json,tsx,env,js,jsx,hbs", + "exec": "node --inspect=0.0.0.0:9229 --require ts-node/register Index.ts" +} \ No newline at end of file