From baca0db2a0f8929ffefbef217f9432eb8aeb308d Mon Sep 17 00:00:00 2001 From: ljacobsson Date: Tue, 5 Sep 2023 16:17:13 +0200 Subject: [PATCH] add --template option in samp local --- package.json | 2 +- src/commands/local/index.js | 1 + src/commands/local/local.js | 27 +++++++++++++++---- .../nodejs/ide-support/vscode/setup.js | 3 +++ src/shared/parser.js | 3 +++ 5 files changed, 30 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index bed7ce4..611ea31 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "samp-cli", - "version": "1.0.53", + "version": "1.0.54", "description": "CLI tool for extended productivity with AWS Serverless Application Model (SAM)", "main": "index.js", "scripts": { diff --git a/src/commands/local/index.js b/src/commands/local/index.js index 5eaa78e..a00ced8 100644 --- a/src/commands/local/index.js +++ b/src/commands/local/index.js @@ -12,6 +12,7 @@ program .option("-i --ide [ide]", "IDE to configure for debugging. Default behaviour will attempt to figure it out automatically or prompt you to select one. Valid values: vscode, visualstudio, jetbrains (for WebStorm, Rider or PyCharm)") .option("-d, --debug", "Configure debug for vscode. This only needs to be run once per project", false) .option("-p, --profile [profile]", "AWS profile to use") + .option("-t, --template [template]", "The path to your template path. Default will try to find it in your cuurrent working directory. Only use this if your template is not in the root of your project") .option("--region [region]", "The AWS region to use. Falls back on AWS_REGION environment variable if not specified") .action(async (cmd) => { cmd.construct = cmd.construct || `./lib/${cmd.stackName}.ts`; diff --git a/src/commands/local/local.js b/src/commands/local/local.js index 617ca58..14da682 100644 --- a/src/commands/local/local.js +++ b/src/commands/local/local.js @@ -16,12 +16,13 @@ function setEnvVars(cmd) { process.env.SAMP_REGION = cmd.region || process.env.AWS_REGION || process.env.AWS_DEFAULT_REGION || ''; process.env.SAMP_STACKNAME = process.env.SAMP_STACKNAME || cmd.stackName || ''; process.env.SAMP_CDK_STACK_PATH = cmd.construct || process.env.SAMP_CDK_STACK_PATH || ''; + process.env.SAMP_TEMPLATE_PATH = cmd.template || process.env.SAMP_TEMPLATE_PATH || undefined; } async function run(cmd) { - env = runtimeEnvFinder.determineRuntime(); setEnvVars(cmd); + env = runtimeEnvFinder.determineRuntime(); if (cmd.mergePackageJsons) { await mergePackageJsons(); } @@ -215,8 +216,20 @@ async function setupDebug(cmd) { functions.push(...response.StackResourceSummaries.filter(r => r.ResourceType === "AWS::Lambda::Function")); token = response.NextToken; } catch (e) { - console.log(`Failed to list stack resources for stack '${stack}' in '${region}' using profile '${profile}'.`, e.message); - process.exit(1); + if (!stack) { + console.log("Stack name could not be found. Please rerun the command and specify the stack name using --stack-name "); + return; + } + if (!region) { + console.log("AWS region could not be found. Please rerun the command and specify the region using --region "); + return; + } + if (!profile) { + profile = "default"; + } else { + console.log(`Failed to list stack resources for stack '${stack}' in '${region}' using profile '${profile}'.`, e.message); + process.exit(1); + } } } while (token); functionNames = functions.map(f => f.LogicalResourceId); @@ -228,7 +241,11 @@ async function setupDebug(cmd) { const selectedFunctionsText = selectedFunctions.length === functionNames.length ? "all functions" : selectedFunctions.join(","); name = await inputUtil.text("Enter a name for the configuration", "Debug " + selectedFunctionsText); selectedFunctionsCsv = selectedFunctions.join(",") - args.push("--functions", selectedFunctionsCsv, "--profile", profile); + args.push("--functions", selectedFunctionsCsv, "--profile", profile, "--region", region, "--stack-name", stack); + } + + if (process.env.SAMP_TEMPLATE_PATH) { + args.push("--template", process.env.SAMP_TEMPLATE_PATH); } const runtime = env.isNodeJS ? "nodejs" : env.functionLanguage; @@ -288,7 +305,7 @@ function validate(env) { if (!fs.existsSync("samconfig.toml") && !fs.existsSync("cdk.json")) { console.log("No samconfig.toml found. Please make sure you have deployed your functions before running this command. You can deploy your functions by running 'sam deploy --guided'"); - return false; + //return false; } if (fs.existsSync("cdk.json") && !fs.existsSync("cdk.out")) { diff --git a/src/commands/local/runtime-support/nodejs/ide-support/vscode/setup.js b/src/commands/local/runtime-support/nodejs/ide-support/vscode/setup.js index 191ecb7..ad54840 100644 --- a/src/commands/local/runtime-support/nodejs/ide-support/vscode/setup.js +++ b/src/commands/local/runtime-support/nodejs/ide-support/vscode/setup.js @@ -39,6 +39,9 @@ function copyConfig(name, args) { } const task = taskConfig.tasks[0]; + if (process.env.SAMP_TEMPLATE_PATH) { + task.command += ` --template ${process.env.SAMP_TEMPLATE_PATH}`; + } let tasksJson; if (fs.existsSync(`${pwd}/.vscode/tasks.json`)) { tasksJson = commentJson.parse(fs.readFileSync(`${pwd}/.vscode/tasks.json`, "utf8")); diff --git a/src/shared/parser.js b/src/shared/parser.js index 6d4b882..ea87e5b 100644 --- a/src/shared/parser.js +++ b/src/shared/parser.js @@ -22,6 +22,9 @@ function stringify(identifier, obj) { } function findSAMTemplateFile(directory) { + if (process.env.SAMP_TEMPLATE_PATH && process.env.SAMP_TEMPLATE_PATH !== 'undefined') { + return process.env.SAMP_TEMPLATE_PATH; + } if (fs.existsSync("./cdk.json")) { return ".samp-out/mock-template.yaml"; }