diff --git a/scripts/utg.sh b/scripts/utg.sh index 669c964..bcc1cff 100644 --- a/scripts/utg.sh +++ b/scripts/utg.sh @@ -14,8 +14,23 @@ extension="${sourceFilePath##*.}" # If the file is a Python file, install pytest-cov if [ "$extension" = "py" ]; then - echo "Installing pytest-cov..." - pip3 install pytest-cov --break-system-packages + echo "Checking if pytest-cov is installed..." + if ! pip3 show pytest-cov > /dev/null 2>&1; then + echo "pytest-cov is not installed. Installing pytest-cov..." + pip3 install pytest-cov --break-system-packages + else + echo "pytest-cov is already installed." + fi +fi + +if [ "$extension" = "js" ] || [ "$extension" = "ts" ]; then + echo "Checking if Jest is installed..." + if ! npm list -g jest > /dev/null 2>&1; then + echo "Jest is not installed. Installing Jest..." + npm install jest --save-dev + else + echo "Jest is already installed." + fi fi if [ "$extension" = "go" ]; then diff --git a/src/SignIn.ts b/src/SignIn.ts index bd81c52..703d3dc 100644 --- a/src/SignIn.ts +++ b/src/SignIn.ts @@ -123,57 +123,6 @@ export async function SignInWithOthers() { const state = generateRandomState(); // Generate a secure random state const authUrl = `https://app.keploy.io/signin?vscode=true&state=${state}`; vscode.env.openExternal(vscode.Uri.parse(authUrl)); - - return new Promise((resolve, reject) => { - const server = http.createServer(async (req, res) => { - res.setHeader('Access-Control-Allow-Origin', '*'); - res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS'); - res.setHeader('Access-Control-Allow-Headers', 'Content-Type'); - if (req.method === 'OPTIONS') { - res.writeHead(200); - res.end(); - return; - } - - if (req && req.url && req.url.startsWith('/login/keploy/callback')) { - const url = new URL(req.url, `http://${req.headers.host}`); - const receivedState = url.searchParams.get('state'); - const token = url.searchParams.get('token'); - console.log("Received state:", receivedState); - - if (!receivedState || !token) { - res.writeHead(400, { 'Content-Type': 'application/json' }); - res.end(JSON.stringify({ error: 'Missing state or token' })); - reject(new Error('Missing state or token')); - server.close(); - return; - } - - try { - // Simulate processing the token - console.log("Processing token..."); - - res.writeHead(200, { 'Content-Type': 'application/json' }); - res.end(JSON.stringify({ message: 'Token received and processed', token, receivedState })); - - // Resolve the promise with the token - resolve(token.toString()); - } catch (err) { - console.error('Error processing token:', err); - res.writeHead(500, { 'Content-Type': 'application/json' }); - res.end(JSON.stringify({ error: 'Internal Server Error' })); - reject(err); - } finally { - server.close(); // Close the server once the request is handled - } - } else { - res.writeHead(404, { 'Content-Type': 'application/json' }); - res.end(JSON.stringify({ error: 'Not Found' })); - } - }).listen(12408, () => { - console.log('Server listening on port 12408'); - }); - }); } diff --git a/src/extension.ts b/src/extension.ts index 47bd7a4..43c8f80 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -132,10 +132,32 @@ export function activate(context: vscode.ExtensionContext) { const sidebarProvider = new SidebarProvider(context.extensionUri , context); context.subscriptions.push( vscode.window.registerUriHandler({ - handleUri(uri: vscode.Uri): vscode.ProviderResult { - vscode.window.showInformationMessage(`URI handler called: ${uri.toString()}`); + async handleUri(uri) { + // Extract the token and state from the URI query parameters + const token = uri.query.split('token=')[1]?.split('&')[0]; + const state = uri.query.split('state=')[1]; + + if (token) { + vscode.window.showInformationMessage(`You are now logged in!`); + + await context.globalState.update('JwtToken', token); + await context.globalState.update('SignedOthers', true); + + const serverUrl = "https://api.keploy.io/"; + const response = await ValidateSignInWithOthers(token, serverUrl); + + if (response) { + vscode.commands.executeCommand('setContext', 'keploy.signedIn', true); + vscode.commands.executeCommand('setContext', 'keploy.signedOut', false); + } else { + vscode.window.showInformationMessage('Token validation failed!'); + } + } else { + vscode.window.showInformationMessage('Login failed'); + } } - }), + }), + vscode.window.registerWebviewViewProvider( "Keploy-Sidebar", sidebarProvider @@ -228,31 +250,13 @@ export function activate(context: vscode.ExtensionContext) { let signInWithOthersCommand = vscode.commands.registerCommand('keploy.SignInWithOthers', async () => { try { - const result = await SignInWithOthers(); - console.log("result from the Sign in with other , ",result); - const accessToken = result as string; // Assert that result is a string - console.log('Jwt token:', accessToken); - await context.globalState.update('JwtToken', accessToken); - await context.globalState.update('SignedOthers', true); - const ServerUrl = "https://api.keploy.io/" - const reponse = await ValidateSignInWithOthers(accessToken , ServerUrl) - - - - if (Boolean(accessToken)) { - vscode.window.showInformationMessage('You are now signed in!'); - vscode.commands.executeCommand('setContext', 'keploy.signedIn', true); - vscode.commands.executeCommand('setContext', 'keploy.signedOut', false); - } else { - console.log('Validation failed for the user !'); - vscode.window.showInformationMessage('Failed to sign in Keploy!'); - } + // Call SignInWithOthers to open the browser for sign-in + await SignInWithOthers(); // The result will now be handled in the URI handler } catch (error) { - // console.error('Error during sign-in:', error); - vscode.window.showInformationMessage('Failed to sign in Keploy!'); + vscode.window.showInformationMessage('Failed to initiate the sign-in process!'); } }); - + // context.subscriptions.push(signInCommand); context.subscriptions.push(signInWithOthersCommand);