diff --git a/js/DotEnvParser.js b/js/DotEnvParser.js index 37bd97c..1429362 100644 --- a/js/DotEnvParser.js +++ b/js/DotEnvParser.js @@ -12,10 +12,23 @@ class DotEnvParser { } } + static removeComments(str) { + if (str.includes('="')) { + if(str.endsWith('"')) { + return str; + } else { + const values = /(.*="(?:[^"\\]|\\.)*").*/.exec(str); + return values[1].replace('\\"','"'); + } + } else { + return str.replace(/#.*/, ''); + } + } + static parseEnvStr(str) { str = str.trim(); str = str.replace(/^export /, ''); - str = str.replace(/#.*/, ''); + str = DotEnvParser.removeComments(str); let parts = str.split('='); let name = parts[0].trim(); let valid = new RegExp(`^${config.regex}$`).test(name); diff --git a/test/_classes/optionsTestObjs.js b/test/_classes/optionsTestObjs.js index 08c3944..81675d4 100644 --- a/test/_classes/optionsTestObjs.js +++ b/test/_classes/optionsTestObjs.js @@ -135,6 +135,21 @@ let envsub = [ flags: `--env-file ${Tmp.ENVFILE8}`.split(' ') } }, + { + testName: '--env-file should remove quotes surrounding environment variables and let all character on it ', + preFunc: () => { + }, + templateFile: Tmp.ENV_TEMPLATE_NOT_A_COMMENT_FILE, + outputContents: Tmp.ENV_TEMPLATE_NOT_A_COMMENT_FILE_EXPECTED, + options: { + envFiles: [ + Tmp.ENVFILE9 + ] + }, + cli: { + flags: `--env-file ${Tmp.ENVFILE9}`.split(' ') + } + }, { testName: '--env-file should not substitute environment variables with invalid names', templateFile: Tmp.ENV_INVALID_TEMPLATE_FILE, diff --git a/test/_classes/templateFiles.js b/test/_classes/templateFiles.js index 5b953a2..0da98d2 100644 --- a/test/_classes/templateFiles.js +++ b/test/_classes/templateFiles.js @@ -10,12 +10,16 @@ const ENVFILE5 = `${envsubDir}/envFile5.env`; const ENVFILE6 = `${envsubDir}/envFile6.env`; const ENVFILE7 = `${envsubDir}/envFile7.env`; const ENVFILE8 = `${envsubDir}/envFile8.env`; +const ENVFILE9 = `${envsubDir}/envFile9.env`; const COMBINED_TEMPLATE_FILE = `${envsubDir}/templateFileCombined`; const COMBINED_TEMPLATE_FILE_EXPECTED = readFileSync(`${envsubDir}/templateFileCombined_E`, 'utf8'); const ENV_TEMPLATE_FILE = `${envsubDir}/templateFileEnv`; + const ENV_TEMPLATE_FILE_EXPECTED = readFileSync(`${envsubDir}/templateFileEnv_E`, 'utf8'); +const ENV_TEMPLATE_NOT_A_COMMENT_FILE = `${envsubDir}/templateFileNotAComment`; +const ENV_TEMPLATE_NOT_A_COMMENT_FILE_EXPECTED = readFileSync(`${envsubDir}/templateFileNotAComment_E`, 'utf8'); const ENV_FILE_TEMPLATE_FILE = `${envsubDir}/templateFileEnvFile`; const ENV_FILE_TEMPLATE_FILE_EXPECTED = readFileSync(`${envsubDir}/templateFileEnvFile_E`, 'utf8'); const ENV_INVALID_TEMPLATE_FILE = `${envsubDir}/templateFileEnvInvalid`; @@ -74,6 +78,9 @@ const templateFiles = { ENVFILE6, ENVFILE7, ENVFILE8, + ENVFILE9, + ENV_TEMPLATE_NOT_A_COMMENT_FILE, + ENV_TEMPLATE_NOT_A_COMMENT_FILE_EXPECTED, COMBINED_TEMPLATE_FILE, COMBINED_TEMPLATE_FILE_EXPECTED, ENV_TEMPLATE_FILE, diff --git a/test/envsub-global/envFile9.env b/test/envsub-global/envFile9.env new file mode 100644 index 0000000..897b35a --- /dev/null +++ b/test/envsub-global/envFile9.env @@ -0,0 +1 @@ +NOT_A_COMMENT="Daniel#not_a_comment\"" #comment diff --git a/test/envsub-global/templateFileNotAComment b/test/envsub-global/templateFileNotAComment new file mode 100644 index 0000000..7e5e1ac --- /dev/null +++ b/test/envsub-global/templateFileNotAComment @@ -0,0 +1,2 @@ +xxx${ NOT_A_COMMENT }xxx xxx${ NOT_A_COMMENT }xxx +xxx${NOT_A_COMMENT}xxx xxx${ NOT_A_COMMENT }xxx diff --git a/test/envsub-global/templateFileNotAComment_E b/test/envsub-global/templateFileNotAComment_E new file mode 100644 index 0000000..452b66a --- /dev/null +++ b/test/envsub-global/templateFileNotAComment_E @@ -0,0 +1,2 @@ +xxxDaniel#not_a_comment"xxx xxxDaniel#not_a_comment"xxx +xxxDaniel#not_a_comment"xxx xxxDaniel#not_a_comment"xxx