Skip to content

Commit dd7b006

Browse files
author
Ben Gowers
committed
added support for option variable in the serverless YAML parsing
1 parent 4a25411 commit dd7b006

File tree

5 files changed

+41
-10
lines changed

5 files changed

+41
-10
lines changed

src/guardian/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
getStackResources,
1313
getLambdaFunctions,
1414
} from "../services";
15-
import Serverless from "../services/serverless";
15+
import ServerlessConfigParser from "../services/serverlessConfigParser";
1616

1717
const infoLog = chalk.greenBright;
1818
const titleLog = chalk.greenBright.underline.bold;
@@ -57,7 +57,7 @@ class GuardianCI {
5757
this.ignoreConfig = this.config.ignore;
5858
}
5959

60-
this.SLS = new Serverless(program.location);
60+
this.SLS = new ServerlessConfigParser(program.location);
6161
}
6262

6363
async getAllLambdaFunctions() {

src/index.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/usr/bin/env node
2-
import Serverless from "./services/serverless";
2+
import ServerlessConfigParser from "./services/serverlessConfigParser";
33
import GuardianCI from "./guardian/index";
44
import Main from "./CLIMain";
55

@@ -33,11 +33,11 @@ program
3333
.option("--sam", "use the SAM framework to execute commands")
3434
.option("-c, --ci", "ci mode for sls-dev-guardian checks")
3535
.option("--mfa <mfa>", "mfa token for profiles with mfa authentication")
36-
.ignoreUnknownOptions()
36+
.allowUnknownOption()
3737
.parse(process.argv);
3838

3939
program.location = program.location || process.cwd();
40-
const SLS = new Serverless(program);
40+
const SLS = new ServerlessConfigParser(program);
4141
if (!program.stage) {
4242
program.stage = SLS.getStage();
4343
}

src/services/serverless.js src/services/serverlessConfigParser.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,29 @@
11
import transformArgsToDict from "../utils/transformArgsToDict";
2+
import replaceVariablesInYml from "../utils/replaceVariablesInYml";
23

34
const fs = require("fs");
45
const path = require("path");
56
const YAML = require("js-yaml");
67

7-
class Serverless {
8+
class ServerlessConfigParser {
89
constructor(program) {
9-
const { argv, location } = program;
10-
const options = transformArgsToDict(argv);
10+
const { args, location } = program;
11+
const options = transformArgsToDict(args);
1112
const ymlPath = path.join(location, "serverless.yml");
1213
const yamlPath = path.join(location, "serverless.yaml");
1314
const jsonPath = path.join(location, "serverless.json");
1415

1516
if (fs.existsSync(ymlPath)) {
1617
this.config = YAML.load(fs.readFileSync(ymlPath).toString("utf8"));
17-
return;
1818
}
1919
if (fs.existsSync(yamlPath)) {
2020
this.config = YAML.load(fs.readFileSync(yamlPath).toString("utf8"));
2121
}
2222
if (fs.existsSync(jsonPath)) {
2323
this.config = JSON.parse(fs.readFileSync(jsonPath).toString("utf8"));
2424
}
25+
26+
replaceVariablesInYml(this.config, options);
2527
}
2628

2729
getFunctionConfig(functionName) {
@@ -95,4 +97,4 @@ class Serverless {
9597
}
9698
}
9799

98-
export default Serverless;
100+
export default ServerlessConfigParser;

src/utils/replaceVariablesInYml.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const replaceVariablesInYml = (config, cmdOptions) => {
2+
const optRegex = /\$\{opt:.*\}/;
3+
4+
if (config.service.name.match(optRegex)) {
5+
let variable = config.service.name.match(optRegex)[0];
6+
7+
variable = variable.replace(/[${}]/g, "");
8+
const varName = variable.split(":")[1];
9+
10+
config.service.name = config.service.name.replace(
11+
/\$\{opt:.*\}/,
12+
cmdOptions[varName]
13+
);
14+
}
15+
};
16+
17+
export default replaceVariablesInYml;

src/utils/transformArgsToDict.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const tranformArgsToDict = (args) => {
2+
const options = {};
3+
4+
for (let i = 0; i < args.length; i += 2) {
5+
const key = args[i].replace(/^-+/, "");
6+
options[key] = args[i + 1];
7+
}
8+
9+
return options;
10+
};
11+
12+
export default tranformArgsToDict;

0 commit comments

Comments
 (0)