Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ljacobsson committed Aug 7, 2023
1 parent bea91cb commit 834eaea
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 14 deletions.
26 changes: 24 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
};
60 changes: 58 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down Expand Up @@ -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",
Expand Down
23 changes: 19 additions & 4 deletions src/commands/local/lib/event-router.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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
Expand All @@ -33,6 +42,10 @@ export async function routeEvent(obj, stack, functionSources) {
});
}
});
// setTimeout(() => {
// watcher.close();
// resolve("Timeout");
// }, 10000);
});
}
} catch (error) {
Expand All @@ -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);
}
);
Expand Down
9 changes: 5 additions & 4 deletions src/commands/local/runtime-support/dotnet/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<object>();

foreach (JProperty prop in request?.obj["envVars"].Properties() ?? Enumerable.Empty<JProperty>())
{
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<object>();

// The method will have 0, 1 or 2 parameters. The first parameter is the payload, the second is the ILambdaContext
if (parameters?.Length > 0)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down

0 comments on commit 834eaea

Please sign in to comment.