From 0dfb7aa43775fbc1f2cdeae850ec0b917a041284 Mon Sep 17 00:00:00 2001 From: horike37 Date: Sun, 23 Oct 2016 22:49:10 +0900 Subject: [PATCH 1/2] fixed issue 2490 --- lib/classes/Variables.js | 12 ++++++++---- lib/classes/Variables.test.js | 10 ++++++++++ 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/lib/classes/Variables.js b/lib/classes/Variables.js index 0ff2b4b82..2bf13e484 100644 --- a/lib/classes/Variables.js +++ b/lib/classes/Variables.js @@ -97,14 +97,14 @@ class Variables { let finalValue; const variableStringsArray = variableStringsString.split(','); variableStringsArray.find(variableString => { - finalValue = this.getValueFromSource(variableString); + finalValue = this.getValueFromSource(variableString, true); return (typeof finalValue !== 'undefined' && finalValue !== null); }); return finalValue; } - getValueFromSource(variableString) { + getValueFromSource(variableString, overwriteMode) { let valueToPopulate; if (variableString.match(this.envRefSyntax)) { valueToPopulate = this.getValueFromEnv(variableString); @@ -113,7 +113,7 @@ class Variables { } else if (variableString.match(this.selfRefSyntax)) { valueToPopulate = this.getValueFromSelf(variableString); } else if (variableString.match(this.fileRefSyntax)) { - valueToPopulate = this.getValueFromFile(variableString); + valueToPopulate = this.getValueFromFile(variableString, overwriteMode); } else { const errorMessage = [ `Invalid variable reference syntax for variable ${variableString}.`, @@ -144,13 +144,17 @@ class Variables { return valueToPopulate; } - getValueFromFile(variableString) { + getValueFromFile(variableString, overwriteMode) { const matchedFileRefString = variableString.match(this.fileRefSyntax)[0]; const referencedFileRelativePath = matchedFileRefString .replace(this.fileRefSyntax, (match, varName) => varName.trim()); const referencedFileFullPath = path.join(this.serverless.config.servicePath, referencedFileRelativePath); + if (overwriteMode === true && !this.serverless.utils.fileExistsSync(referencedFileFullPath)) { + return undefined; + } + let valueToPopulate = this.serverless.utils.readFileSync(referencedFileFullPath); if (matchedFileRefString !== variableString) { let deepProperties = variableString diff --git a/lib/classes/Variables.test.js b/lib/classes/Variables.test.js index bfef5ecd2..74910138d 100644 --- a/lib/classes/Variables.test.js +++ b/lib/classes/Variables.test.js @@ -359,6 +359,16 @@ describe('Variables', () => { expect(valueToPopulate).to.deep.equal(configYml); }); + it('should get undefined if non existing file and the second argument is true', () => { + const serverless = new Serverless(); + const tmpDirPath = testUtils.getTmpDirPath(); + + serverless.config.update({ servicePath: tmpDirPath }); + + const valueToPopulate = serverless.variables.getValueFromFile('file(./config.yml)', true); + expect(valueToPopulate).to.be.equal(undefined); + }); + it('should populate non json/yml files', () => { const serverless = new Serverless(); const SUtils = new Utils(); From cd260f074324332dc17cda95d4b59d0ede07dfbd Mon Sep 17 00:00:00 2001 From: "Eslam A. Hefnawy" Date: Fri, 28 Oct 2016 16:41:55 +0700 Subject: [PATCH 2/2] removed overwrite for file variable --- docs/providers/aws/examples/cron/node/handler.js | 2 +- lib/classes/Variables.js | 10 +++++----- lib/classes/Variables.test.js | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/docs/providers/aws/examples/cron/node/handler.js b/docs/providers/aws/examples/cron/node/handler.js index 5fb485788..980b35d28 100644 --- a/docs/providers/aws/examples/cron/node/handler.js +++ b/docs/providers/aws/examples/cron/node/handler.js @@ -2,5 +2,5 @@ module.exports.run = () => { const time = new Date(); - console.log(`Your cron ran ${time}`); + console.log(`Your cron ran ${time}`); // eslint-disable-line no-console }; diff --git a/lib/classes/Variables.js b/lib/classes/Variables.js index 99773e7b0..f8aa0039c 100644 --- a/lib/classes/Variables.js +++ b/lib/classes/Variables.js @@ -101,7 +101,7 @@ class Variables { let finalValue; const variableStringsArray = variableStringsString.split(','); variableStringsArray.find(variableString => { - finalValue = this.getValueFromSource(variableString, true); + finalValue = this.getValueFromSource(variableString); return (finalValue !== null && typeof finalValue !== 'undefined') && !(typeof finalValue === 'object' && _.isEmpty(finalValue)); }); @@ -109,7 +109,7 @@ class Variables { return finalValue; } - getValueFromSource(variableString, overwriteMode) { + getValueFromSource(variableString) { let valueToPopulate; if (variableString.match(this.envRefSyntax)) { valueToPopulate = this.getValueFromEnv(variableString); @@ -118,7 +118,7 @@ class Variables { } else if (variableString.match(this.selfRefSyntax)) { valueToPopulate = this.getValueFromSelf(variableString); } else if (variableString.match(this.fileRefSyntax)) { - valueToPopulate = this.getValueFromFile(variableString, overwriteMode); + valueToPopulate = this.getValueFromFile(variableString); } else { const errorMessage = [ `Invalid variable reference syntax for variable ${variableString}.`, @@ -149,14 +149,14 @@ class Variables { return valueToPopulate; } - getValueFromFile(variableString, overwriteMode) { + getValueFromFile(variableString) { const matchedFileRefString = variableString.match(this.fileRefSyntax)[0]; const referencedFileRelativePath = matchedFileRefString .replace(this.fileRefSyntax, (match, varName) => varName.trim()); const referencedFileFullPath = path.join(this.serverless.config.servicePath, referencedFileRelativePath); - if (overwriteMode === true && !this.serverless.utils.fileExistsSync(referencedFileFullPath)) { + if (!this.serverless.utils.fileExistsSync(referencedFileFullPath)) { return undefined; } diff --git a/lib/classes/Variables.test.js b/lib/classes/Variables.test.js index cfe5e0f15..2caec549a 100644 --- a/lib/classes/Variables.test.js +++ b/lib/classes/Variables.test.js @@ -391,7 +391,7 @@ describe('Variables', () => { serverless.config.update({ servicePath: tmpDirPath }); - const valueToPopulate = serverless.variables.getValueFromFile('file(./config.yml)', true); + const valueToPopulate = serverless.variables.getValueFromFile('file(./config.yml)'); expect(valueToPopulate).to.be.equal(undefined); });