From 834eaeac295c8eb836856a2f7e8ece7f77108fe4 Mon Sep 17 00:00:00 2001 From: ljacobsson Date: Mon, 7 Aug 2023 18:37:35 +0200 Subject: [PATCH] fixes --- index.js | 26 +++++++- package-lock.json | 60 ++++++++++++++++++- package.json | 3 +- src/commands/local/lib/event-router.js | 23 +++++-- .../local/runtime-support/dotnet/Program.cs | 9 +-- .../dotnet/ide-support/vscode/launch.json | 2 +- 6 files changed, 109 insertions(+), 14 deletions(-) diff --git a/index.js b/index.js index cfc7a1b..7a9195a 100755 --- a/index.js +++ b/index.js @@ -4,6 +4,7 @@ const program = require("commander"); const package = require("./package.json"); const fs = require("fs"); const path = require("path"); +const axios = require("axios").default; const commands = fs.readdirSync(path.join(__dirname, "src", "commands")); for (const command of commands) { @@ -12,12 +13,33 @@ for (const command of commands) { program.version(package.version, "-v, --vers", "output the current version"); +(async () => { + console.log("Checking for updates..."); + const latestVersion = await + console.log("tag name", latestVersion); + if (latestVersion.data.tag_name !== package.version) { + console.log(`\nUpdate available: ${latestVersion.data.tag_name}. Run "npm i -g samp-cli" to update.\n`); + } +}); + +(async () => { + try { + const latestVersion = await axios.get("https://api.github.com/repos/ljacobsson/samp-cli/releases/latest", { timeout: 1000 }) + if (latestVersion.data.tag_name !== package.version) { + console.log(`\nUpdate available: ${latestVersion.data.tag_name}. Run "npm i -g samp-cli" to update.\n`); + } + + } catch (error) { + } +})(); + + program.parse(process.argv); if (process.argv.length < 3) { - program.help(); + program.help(); } // nodejs<15 compatability String.prototype.replaceAll = function (target, replacement) { - return this.split(target).join(replacement); + return this.split(target).join(replacement); }; \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 474f98d..eb25dd7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "samp-cli", - "version": "1.0.27", + "version": "1.0.28", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "samp-cli", - "version": "1.0.27", + "version": "1.0.28", "license": "ISC", "dependencies": { "@aws-sdk/client-cloudformation": "^3.319.0", @@ -20,6 +20,7 @@ "@octokit/rest": "^18.5.2", "archiver": "^5.3.1", "aws-cdk-lib": "^2.87.0", + "axios": "^1.4.0", "chokidar": "^3.5.3", "cli-spinner": "^0.2.10", "clipboardy": "^2.2.0", @@ -4850,6 +4851,29 @@ "uuid": "dist/bin/uuid" } }, + "node_modules/axios": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/axios/-/axios-1.4.0.tgz", + "integrity": "sha512-S4XCWMEmzvo64T9GfvQDOXgYRDJ/wsSZc7Jvdgx5u1sd0JwsuPLqb3SYmusag+edF6ziyMensPVqLTSc1PiSEA==", + "dependencies": { + "follow-redirects": "^1.15.0", + "form-data": "^4.0.0", + "proxy-from-env": "^1.1.0" + } + }, + "node_modules/axios/node_modules/form-data": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.0.tgz", + "integrity": "sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==", + "dependencies": { + "asynckit": "^0.4.0", + "combined-stream": "^1.0.8", + "mime-types": "^2.1.12" + }, + "engines": { + "node": ">= 6" + } + }, "node_modules/babel-jest": { "version": "26.6.3", "resolved": "https://registry.npmjs.org/babel-jest/-/babel-jest-26.6.3.tgz", @@ -9397,6 +9421,11 @@ "node": ">= 6" } }, + "node_modules/proxy-from-env": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/proxy-from-env/-/proxy-from-env-1.1.0.tgz", + "integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==" + }, "node_modules/ps-tree": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/ps-tree/-/ps-tree-1.2.0.tgz", @@ -15515,6 +15544,28 @@ } } }, + "axios": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/axios/-/axios-1.4.0.tgz", + "integrity": "sha512-S4XCWMEmzvo64T9GfvQDOXgYRDJ/wsSZc7Jvdgx5u1sd0JwsuPLqb3SYmusag+edF6ziyMensPVqLTSc1PiSEA==", + "requires": { + "follow-redirects": "^1.15.0", + "form-data": "^4.0.0", + "proxy-from-env": "^1.1.0" + }, + "dependencies": { + "form-data": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.0.tgz", + "integrity": "sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==", + "requires": { + "asynckit": "^0.4.0", + "combined-stream": "^1.0.8", + "mime-types": "^2.1.12" + } + } + } + }, "babel-jest": { "version": "26.6.3", "resolved": "https://registry.npmjs.org/babel-jest/-/babel-jest-26.6.3.tgz", @@ -18959,6 +19010,11 @@ "sisteransi": "^1.0.5" } }, + "proxy-from-env": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/proxy-from-env/-/proxy-from-env-1.1.0.tgz", + "integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==" + }, "ps-tree": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/ps-tree/-/ps-tree-1.2.0.tgz", diff --git a/package.json b/package.json index 3890770..e2cb036 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "samp-cli", - "version": "1.0.28", + "version": "1.0.29", "description": "CLI tool for extended productivity with AWS Serverless Application Model (SAM)", "main": "index.js", "scripts": { @@ -41,6 +41,7 @@ "@octokit/rest": "^18.5.2", "archiver": "^5.3.1", "aws-cdk-lib": "^2.87.0", + "axios": "^1.4.0", "chokidar": "^3.5.3", "cli-spinner": "^0.2.10", "clipboardy": "^2.2.0", diff --git a/src/commands/local/lib/event-router.js b/src/commands/local/lib/event-router.js index ff54b0e..afa0e94 100644 --- a/src/commands/local/lib/event-router.js +++ b/src/commands/local/lib/event-router.js @@ -5,8 +5,16 @@ export async function routeEvent(obj, stack, functionSources) { const event = obj.event; const context = obj.context; process.env = { ...obj.envVars, LOCAL_DEBUG: true }; + let logicalId; + try { + logicalId = stack.StackResourceSummaries.find(resource => resource.PhysicalResourceId === context.functionName).LogicalResourceId; + } catch (e) { + } + + if (!logicalId) { + return { error: "Could not find function in stack", skipResponse: true }; + } - const logicalId = stack.StackResourceSummaries.find(resource => resource.PhysicalResourceId === context.functionName).LogicalResourceId; if (functionSources[logicalId].runtime.startsWith("nodejs")) { const modulePath = functionSources[logicalId].module; const module = await import(modulePath); @@ -18,9 +26,10 @@ export async function routeEvent(obj, stack, functionSources) { if (!fs.existsSync(".samp-out/responses")) fs.mkdirSync(".samp-out/responses"); fs.writeFileSync(".samp-out/requests/" + obj.context.awsRequestId, JSON.stringify({ func: functionSources[logicalId].handler, obj }, null, 2)); // await file to be written to .samp-out/responses + return await new Promise((resolve, reject) => { const filePath = `.samp-out/responses/${context.awsRequestId}`; - + const watcher = fs.watch(".samp-out/responses", (eventType, filename) => { if (filename === context.awsRequestId) { watcher.close(); // Close the watcher to stop monitoring changes @@ -33,6 +42,10 @@ export async function routeEvent(obj, stack, functionSources) { }); } }); + // setTimeout(() => { + // watcher.close(); + // resolve("Timeout"); + // }, 10000); }); } } catch (error) { @@ -45,8 +58,10 @@ if (process.argv.length > 3) { const stack = JSON.parse(process.argv[3]); const functionSources = JSON.parse(process.argv[4]); routeEvent(obj, stack, functionSources).then((result) => { - const response = typeof result === "string" ? result : JSON.stringify(result, null, 2); - process.send(response || ""); + if (!result.skipResponse) { + const response = typeof result === "string" ? result : JSON.stringify(result, null, 2); + process.send(response || ""); + } process.exit(0); } ); diff --git a/src/commands/local/runtime-support/dotnet/Program.cs b/src/commands/local/runtime-support/dotnet/Program.cs index 4ee10af..ab1b34a 100644 --- a/src/commands/local/runtime-support/dotnet/Program.cs +++ b/src/commands/local/runtime-support/dotnet/Program.cs @@ -79,16 +79,17 @@ static void InvokeFunctionThread(object? data) Console.WriteLine("Class or method not found."); return; } - var instance = Activator.CreateInstance(classType); - var methodInfo = classType.GetMethod(method); - var parameters = methodInfo?.GetParameters(); - var parameterList = new List(); foreach (JProperty prop in request?.obj["envVars"].Properties() ?? Enumerable.Empty()) { Environment.SetEnvironmentVariable(prop.Name, prop.Value.ToString()); } + var instance = Activator.CreateInstance(classType); + var methodInfo = classType.GetMethod(method); + var parameters = methodInfo?.GetParameters(); + var parameterList = new List(); + // The method will have 0, 1 or 2 parameters. The first parameter is the payload, the second is the ILambdaContext if (parameters?.Length > 0) { diff --git a/src/commands/local/runtime-support/dotnet/ide-support/vscode/launch.json b/src/commands/local/runtime-support/dotnet/ide-support/vscode/launch.json index 5f8543a..1efc177 100644 --- a/src/commands/local/runtime-support/dotnet/ide-support/vscode/launch.json +++ b/src/commands/local/runtime-support/dotnet/ide-support/vscode/launch.json @@ -8,7 +8,7 @@ "preLaunchTask": "build", "program": "${workspaceFolder}/.samp-out/bin/Debug/net6.0/dotnet.dll", "args": [], - "cwd": "${workspaceFolder}", + "cwd": "${workspaceFolder}/.samp-out", "stopAtEntry": false, "console": "internalConsole" }