From 763b7f614e09e3f1baf5278ac69d46b58afeacb8 Mon Sep 17 00:00:00 2001 From: Hermione Dadheech Date: Thu, 6 Apr 2023 09:56:54 +0530 Subject: [PATCH 1/2] feat(keploy.ts): to start keploy server from typescript sdk for all OS To start keploy server from typescript sdk for all OS. feat #352 Signed-off-by: Hermione Dadheech --- src/keploy.ts | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/src/keploy.ts b/src/keploy.ts index eeeae15..3353b7f 100644 --- a/src/keploy.ts +++ b/src/keploy.ts @@ -12,8 +12,10 @@ import { TestCase } from "../proto/services/TestCase"; import { StrArr } from "../proto/services/StrArr"; import assert = require("assert"); import { createExecutionContext, getExecutionContext } from "./context"; -import Mode, { MODE_OFF } from "./mode"; +import Mode, { MODE_OFF,MODE_RECORD,MODE_TEST } from "./mode"; +import { exec, spawn } from "child_process"; +const isWindows = process.platform === "win32"; const PROTO_PATH = "../proto/services.proto"; const packageDef = protoLoader.loadSync(path.resolve(__dirname, PROTO_PATH)); const grpcObj = grpc.loadPackageDefinition( @@ -85,6 +87,41 @@ export default class Keploy { this.responses = {}; this.dependencies = {}; this.mocks = {}; + if ( process.env.KEPLOY_MODE == MODE_RECORD || process.env.KEPLOY_MODE === MODE_TEST) { + const findKeploy = isWindows ? "where keploy" : "which keploy"; + + exec(findKeploy, (error, stdout, stderr) => { + if (error) { + console.error(`Error finding keploy: ${error.message}`); + return; + } + + if (!stdout) { + console.log( + "Keploy Server is not installed. Please install it using https://docs.keploy.io/docs/go/installation" + ); + return; + } + + const portCheckCommand = isWindows + ? 'netstat -ano | findstr "LISTENING" | findstr ":6789"' + : "lsof -i:6789"; + + exec(portCheckCommand, (error, stdout, stderr) => { + if (error) { + console.error(`Error checking port: ${error.message}`); + return; + } + + if (!stdout) { + const keployProcess = spawn("keploy", [], { env: { KEPLOY_PORT: "6789" } }); + keployProcess.on("error", (error) => { + console.error(`Error starting keploy: ${error.message}`); + }); + } + }); + }); + } } validateServerConfig({ From 30a55a26cbb1a514a049595c8ef30422741524d1 Mon Sep 17 00:00:00 2001 From: Hermione Dadheech Date: Fri, 7 Apr 2023 08:58:44 +0530 Subject: [PATCH 2/2] feat(keploy.ts): resolving error Added additional check for error code and will also start the server in that case too feature #352 Signed-off-by: Hermione Dadheech --- src/keploy.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/keploy.ts b/src/keploy.ts index 3353b7f..751928a 100644 --- a/src/keploy.ts +++ b/src/keploy.ts @@ -108,12 +108,12 @@ export default class Keploy { : "lsof -i:6789"; exec(portCheckCommand, (error, stdout, stderr) => { - if (error) { + if (error && error.code !== 1) { console.error(`Error checking port: ${error.message}`); return; } - if (!stdout) { + if (!stdout || (error && error.code === 1)) { const keployProcess = spawn("keploy", [], { env: { KEPLOY_PORT: "6789" } }); keployProcess.on("error", (error) => { console.error(`Error starting keploy: ${error.message}`);