diff --git a/README.md b/README.md index 37c8386..bc27b8c 100644 --- a/README.md +++ b/README.md @@ -75,7 +75,7 @@ Options: ### samp invoke -Invokes a Lambda function or StepFunctions state machine in your CloudFormation stack. If a samconfig.toml file is present, it will use the stack name and region from that file. Otherwise you will have to specify them using the `--stack-name` and `--region` flags. +Invokes a Lambda function or StepFunctions state machine in your CloudFormation stack. If a `samconfig.[toml/yaml]` file is present, it will use the stack name and region from that file. Otherwise you will have to specify them using the `--stack-name` and `--region` flags. You can pass a variety of inputs to the function / state machine: * A path to a local JSON file @@ -101,7 +101,7 @@ Options: ### samp local This feature is inspired by and works similarly to [SST](https://twitter.com/SST_dev)'s [Live Lambda Development](https://docs.sst.dev/live-lambda-development). -It lets you test your Lambda functions locally with real events from your AWS account. You can step through your code using breakpoints and get sub-second code reloads on changes. If a `samconfig.toml` file is present, it will use the stack name and region from that file. Otherwise you will have to specify them using the `--stack-name` and `--region` flags. +It lets you test your Lambda functions locally with real events from your AWS account. You can step through your code using breakpoints and get sub-second code reloads on changes. If a `samconfig.[toml/yaml]` file is present, it will use the stack name and region from that file. Otherwise you will have to specify them using the `--stack-name` and `--region` flags. * NOTE #1: this command temporarily replaces your function code in the cloud with a proxy function that relays events to your local machine over AWS IoT (MQTT). Please only use on development stacks. Never use on production functions! * * NOTE #2: this command does not fully support Lambda layers. If you use layers to bundle your dependencies, you will have to manually install them locally as well. If you use layers to share custom code between functions, create a symlink from `/opt/nodejs` to the layer folder in your function folder. @@ -228,7 +228,7 @@ Options: ### samp console -Launches the AWS console for the selected SAM resource. If a samconfig.toml file is present, it will use the stack name and region from that file. Otherwise you will have to specify them using the `--stack-name` and `--region` flags. +Launches the AWS console for the selected SAM resource. If a `samconfig.[toml/yaml]` file is present, it will use the stack name and region from that file. Otherwise you will have to specify them using the `--stack-name` and `--region` flags. ``` Usage: sampat console|c [options] diff --git a/docs/samp-local-dotnet.md b/docs/samp-local-dotnet.md index a39cf22..9eca73a 100644 --- a/docs/samp-local-dotnet.md +++ b/docs/samp-local-dotnet.md @@ -3,7 +3,7 @@ ### Prerequisites: * Function runtime should be `dotnet6` * Has to be a SAM project. CDK might be supported later. -* It assumes a folder structure where the SAM template is in the root, next to the `samconfig.toml` file +* It assumes a folder structure where the SAM template is in the root, next to the `samconfig.[toml/yaml]` file * The SAM template can be in either JSON or YAML format and can be called anything with a `json`, `yml`, `yaml` or `.template` extension * You need to have npm/node installed on your system. diff --git a/src/commands/console/console.js b/src/commands/console/console.js index 56dd190..bf69d0b 100644 --- a/src/commands/console/console.js +++ b/src/commands/console/console.js @@ -6,11 +6,12 @@ const parser = require("../../shared/parser"); const fs = require("fs"); const ini = require('ini'); const link2aws = require('link2aws'); +const samConfigParser = require('../../shared/samConfigParser'); const open = import('open'); let region; async function run(cmd) { - if (fs.existsSync("samconfig.toml")) { - const config = ini.parse(fs.readFileSync("samconfig.toml", "utf8")); + if (samConfigParser.configExists()) { + const config = samConfigParser.parse(); const params = config?.default?.deploy?.parameters; if (!cmd.stackName && params.stack_name) { console.log("Using stack name from config:", params.stack_name); diff --git a/src/commands/invoke/invoke.js b/src/commands/invoke/invoke.js index 6e471b9..723230f 100644 --- a/src/commands/invoke/invoke.js +++ b/src/commands/invoke/invoke.js @@ -6,6 +6,7 @@ const { fromSSO } = require("@aws-sdk/credential-provider-sso"); const lambdaInvoker = require("./lambdaInvoker"); const stepFunctionsInvoker = require("./stepFunctionsInvoker"); const inputUtil = require('../../shared/inputUtil'); +const samConfigParser = require('../../shared/samConfigParser'); const parser = require("../../shared/parser"); const fs = require("fs"); const ini = require('ini'); @@ -42,8 +43,8 @@ async function run(cmd) { console.log("Using default profile. Override by setting --profile "); } } - else if (fs.existsSync("samconfig.toml")) { - const config = ini.parse(fs.readFileSync("samconfig.toml", "utf8")); + else if (samConfigParser.configExists()) { + const config = samConfigParser.parse(); const params = { ...config?.default?.deploy?.parameters, ...(config?.default?.global?.parameters || {}) }; if (!cmd.stackName && params.stack_name) { console.log("Using stack name from config:", params.stack_name); diff --git a/src/commands/local/lib/connect.js b/src/commands/local/lib/connect.js index 7711edf..0c945b1 100644 --- a/src/commands/local/lib/connect.js +++ b/src/commands/local/lib/connect.js @@ -34,7 +34,7 @@ if (fs.existsSync(`${homedir()}/.lambda-debug/uuid`)) { } let targetConfig = {}; -if (fs.existsSync(`samconfig.toml`)) { +if (samConfigParser.configExists()) { targetConfig = samConfigParser.parse(); } targetConfig = { diff --git a/src/commands/local/local.js b/src/commands/local/local.js index 14da682..fede3f5 100644 --- a/src/commands/local/local.js +++ b/src/commands/local/local.js @@ -303,9 +303,9 @@ 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; + if (!samConfigParser.configExists() && !fs.existsSync("cdk.json")) { + console.log("No samconfig 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; } if (fs.existsSync("cdk.json") && !fs.existsSync("cdk.out")) { diff --git a/src/commands/local/runtime-env-finder.js b/src/commands/local/runtime-env-finder.js index 32c6cd9..a2aae0d 100644 --- a/src/commands/local/runtime-env-finder.js +++ b/src/commands/local/runtime-env-finder.js @@ -1,6 +1,7 @@ const fs = require('fs'); const path = require('path'); const { parse, findSAMTemplateFile } = require('../../shared/parser'); +const samConfigParser = require('../../shared/samConfigParser'); function determineRuntime() { const templateFile = findSAMTemplateFile(process.cwd()); @@ -41,7 +42,7 @@ function determineRuntime() { if (csprojFiles.length > 0) return { iac: "sam", functionLanguage: "dotnet" };; if (fs.existsSync('nuget.config')) return { iac: "sam", functionLanguage: "dotnet" };; - if (fs.existsSync('samconfig.toml')) return { iac: "sam", functionLanguage: "js", isNodeJS: true };; + if (samConfigParser.configExists()) return { iac: "sam", functionLanguage: "js", isNodeJS: true };; } module.exports = { diff --git a/src/shared/samConfigParser.js b/src/shared/samConfigParser.js index b0a67f7..e1fa405 100644 --- a/src/shared/samConfigParser.js +++ b/src/shared/samConfigParser.js @@ -1,17 +1,27 @@ const toml = require('toml'); const fs = require('fs'); const yaml = require('yaml'); +const path = require('path'); + function parse() { - if (!fs.existsSync(`samconfig.toml`)) { + const foundSamconfigFile = findConfigFile(); + if (foundSamconfigFile === null) { return {}; } + const configEnv = 'default'; let config; - try { - config = toml.parse(fs.readFileSync(`samconfig.toml`, 'utf-8')); - } catch (e) { - config = yaml.parse(fs.readFileSync(`samconfig.yaml`, 'utf-8')); + const type = path.extname(foundSamconfigFile); + + switch(type) { + case '.toml': + config = toml.parse(fs.readFileSync(foundSamconfigFile, 'utf-8')); + break; + case '.yaml': + config = yaml.parse(fs.readFileSync(foundSamconfigFile, 'utf-8')); + break; } + const envConfig = config[configEnv].deploy.parameters; envConfig.configEnv = process.env.configEnv || 'default'; envConfig.stack_name = envConfig.stack_name || config[configEnv].global.parameters.stack_name @@ -20,6 +30,22 @@ function parse() { return envConfig; } +function findConfigFile() { + const defaultSamconfigFiles = ['samconfig.toml', 'samconfig.yaml']; + for (const file of defaultSamconfigFiles) { + if (fs.existsSync(file)){ + return file; + } + } + return null; +} + +function configExists() { + return findConfigFile() ? true : false; +} + module.exports = { - parse + parse, + configExists, + findConfigFile } \ No newline at end of file