Skip to content

Commit

Permalink
add --template option in samp local
Browse files Browse the repository at this point in the history
  • Loading branch information
ljacobsson committed Sep 5, 2023
1 parent fd11eae commit baca0db
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
1 change: 1 addition & 0 deletions src/commands/local/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`;
Expand Down
27 changes: 22 additions & 5 deletions src/commands/local/local.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -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 <stack-name>");
return;
}
if (!region) {
console.log("AWS region could not be found. Please rerun the command and specify the region using --region <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);
Expand All @@ -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;
Expand Down Expand Up @@ -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")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand Down
3 changes: 3 additions & 0 deletions src/shared/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
Expand Down

0 comments on commit baca0db

Please sign in to comment.