From 61f31a1b6b8a26caa5227a74e29bdce940dbe6b5 Mon Sep 17 00:00:00 2001 From: Nate Fischer Date: Sat, 27 Feb 2016 21:56:07 -0800 Subject: [PATCH] Simplifying environmental variable handling. --- dist/preparser.js | 22 +++------------------- packages/cat/dist/preparser.js | 22 +++------------------- packages/cp/dist/preparser.js | 22 +++------------------- packages/kill/dist/preparser.js | 22 +++------------------- packages/ls/dist/preparser.js | 22 +++------------------- packages/mkdir/dist/preparser.js | 22 +++------------------- packages/mv/dist/preparser.js | 22 +++------------------- packages/pwd/dist/preparser.js | 22 +++------------------- packages/rm/dist/preparser.js | 22 +++------------------- packages/sort/dist/preparser.js | 22 +++------------------- packages/touch/dist/preparser.js | 22 +++------------------- src/preparser.js | 21 ++------------------- test/preparser.js | 2 +- 13 files changed, 36 insertions(+), 229 deletions(-) diff --git a/dist/preparser.js b/dist/preparser.js index 880839b..b8eed39 100755 --- a/dist/preparser.js +++ b/dist/preparser.js @@ -1,30 +1,14 @@ 'use strict'; -var os = require('os'); -var windows = os.platform() === 'win32'; - // Replace out env variables. + var parseEnvVariables = function parseEnvVariables(input) { var referenceRegex = /\${([a-zA-Z_][a-zA-Z0-9_]*)}|\$([a-zA-Z_][a-zA-Z0-9_]*)/g; return input.replace(referenceRegex, function (varRef, capture1, capture2, capture3) { var varName = capture1 || capture2 || capture3; - var value = ''; - if (windows) { - for (var name in process.env) { - if (process.env.hasOwnProperty(name)) { - // Windows is case insensitive - if (String(name).toLowerCase() === varName.toLowerCase()) { - value = process.env[name]; - break; - } - } - } - } else { - // default to empty string on Unix - value = process.env.hasOwnProperty(varName) ? process.env[varName] : ''; - } - return value; + // Return the value of the variable, or the empty string if not there + return process.env.hasOwnProperty(varName) ? process.env[varName] : ''; }); }; diff --git a/packages/cat/dist/preparser.js b/packages/cat/dist/preparser.js index 880839b..b8eed39 100755 --- a/packages/cat/dist/preparser.js +++ b/packages/cat/dist/preparser.js @@ -1,30 +1,14 @@ 'use strict'; -var os = require('os'); -var windows = os.platform() === 'win32'; - // Replace out env variables. + var parseEnvVariables = function parseEnvVariables(input) { var referenceRegex = /\${([a-zA-Z_][a-zA-Z0-9_]*)}|\$([a-zA-Z_][a-zA-Z0-9_]*)/g; return input.replace(referenceRegex, function (varRef, capture1, capture2, capture3) { var varName = capture1 || capture2 || capture3; - var value = ''; - if (windows) { - for (var name in process.env) { - if (process.env.hasOwnProperty(name)) { - // Windows is case insensitive - if (String(name).toLowerCase() === varName.toLowerCase()) { - value = process.env[name]; - break; - } - } - } - } else { - // default to empty string on Unix - value = process.env.hasOwnProperty(varName) ? process.env[varName] : ''; - } - return value; + // Return the value of the variable, or the empty string if not there + return process.env.hasOwnProperty(varName) ? process.env[varName] : ''; }); }; diff --git a/packages/cp/dist/preparser.js b/packages/cp/dist/preparser.js index 880839b..b8eed39 100755 --- a/packages/cp/dist/preparser.js +++ b/packages/cp/dist/preparser.js @@ -1,30 +1,14 @@ 'use strict'; -var os = require('os'); -var windows = os.platform() === 'win32'; - // Replace out env variables. + var parseEnvVariables = function parseEnvVariables(input) { var referenceRegex = /\${([a-zA-Z_][a-zA-Z0-9_]*)}|\$([a-zA-Z_][a-zA-Z0-9_]*)/g; return input.replace(referenceRegex, function (varRef, capture1, capture2, capture3) { var varName = capture1 || capture2 || capture3; - var value = ''; - if (windows) { - for (var name in process.env) { - if (process.env.hasOwnProperty(name)) { - // Windows is case insensitive - if (String(name).toLowerCase() === varName.toLowerCase()) { - value = process.env[name]; - break; - } - } - } - } else { - // default to empty string on Unix - value = process.env.hasOwnProperty(varName) ? process.env[varName] : ''; - } - return value; + // Return the value of the variable, or the empty string if not there + return process.env.hasOwnProperty(varName) ? process.env[varName] : ''; }); }; diff --git a/packages/kill/dist/preparser.js b/packages/kill/dist/preparser.js index 880839b..b8eed39 100755 --- a/packages/kill/dist/preparser.js +++ b/packages/kill/dist/preparser.js @@ -1,30 +1,14 @@ 'use strict'; -var os = require('os'); -var windows = os.platform() === 'win32'; - // Replace out env variables. + var parseEnvVariables = function parseEnvVariables(input) { var referenceRegex = /\${([a-zA-Z_][a-zA-Z0-9_]*)}|\$([a-zA-Z_][a-zA-Z0-9_]*)/g; return input.replace(referenceRegex, function (varRef, capture1, capture2, capture3) { var varName = capture1 || capture2 || capture3; - var value = ''; - if (windows) { - for (var name in process.env) { - if (process.env.hasOwnProperty(name)) { - // Windows is case insensitive - if (String(name).toLowerCase() === varName.toLowerCase()) { - value = process.env[name]; - break; - } - } - } - } else { - // default to empty string on Unix - value = process.env.hasOwnProperty(varName) ? process.env[varName] : ''; - } - return value; + // Return the value of the variable, or the empty string if not there + return process.env.hasOwnProperty(varName) ? process.env[varName] : ''; }); }; diff --git a/packages/ls/dist/preparser.js b/packages/ls/dist/preparser.js index 880839b..b8eed39 100755 --- a/packages/ls/dist/preparser.js +++ b/packages/ls/dist/preparser.js @@ -1,30 +1,14 @@ 'use strict'; -var os = require('os'); -var windows = os.platform() === 'win32'; - // Replace out env variables. + var parseEnvVariables = function parseEnvVariables(input) { var referenceRegex = /\${([a-zA-Z_][a-zA-Z0-9_]*)}|\$([a-zA-Z_][a-zA-Z0-9_]*)/g; return input.replace(referenceRegex, function (varRef, capture1, capture2, capture3) { var varName = capture1 || capture2 || capture3; - var value = ''; - if (windows) { - for (var name in process.env) { - if (process.env.hasOwnProperty(name)) { - // Windows is case insensitive - if (String(name).toLowerCase() === varName.toLowerCase()) { - value = process.env[name]; - break; - } - } - } - } else { - // default to empty string on Unix - value = process.env.hasOwnProperty(varName) ? process.env[varName] : ''; - } - return value; + // Return the value of the variable, or the empty string if not there + return process.env.hasOwnProperty(varName) ? process.env[varName] : ''; }); }; diff --git a/packages/mkdir/dist/preparser.js b/packages/mkdir/dist/preparser.js index 880839b..b8eed39 100755 --- a/packages/mkdir/dist/preparser.js +++ b/packages/mkdir/dist/preparser.js @@ -1,30 +1,14 @@ 'use strict'; -var os = require('os'); -var windows = os.platform() === 'win32'; - // Replace out env variables. + var parseEnvVariables = function parseEnvVariables(input) { var referenceRegex = /\${([a-zA-Z_][a-zA-Z0-9_]*)}|\$([a-zA-Z_][a-zA-Z0-9_]*)/g; return input.replace(referenceRegex, function (varRef, capture1, capture2, capture3) { var varName = capture1 || capture2 || capture3; - var value = ''; - if (windows) { - for (var name in process.env) { - if (process.env.hasOwnProperty(name)) { - // Windows is case insensitive - if (String(name).toLowerCase() === varName.toLowerCase()) { - value = process.env[name]; - break; - } - } - } - } else { - // default to empty string on Unix - value = process.env.hasOwnProperty(varName) ? process.env[varName] : ''; - } - return value; + // Return the value of the variable, or the empty string if not there + return process.env.hasOwnProperty(varName) ? process.env[varName] : ''; }); }; diff --git a/packages/mv/dist/preparser.js b/packages/mv/dist/preparser.js index 880839b..b8eed39 100755 --- a/packages/mv/dist/preparser.js +++ b/packages/mv/dist/preparser.js @@ -1,30 +1,14 @@ 'use strict'; -var os = require('os'); -var windows = os.platform() === 'win32'; - // Replace out env variables. + var parseEnvVariables = function parseEnvVariables(input) { var referenceRegex = /\${([a-zA-Z_][a-zA-Z0-9_]*)}|\$([a-zA-Z_][a-zA-Z0-9_]*)/g; return input.replace(referenceRegex, function (varRef, capture1, capture2, capture3) { var varName = capture1 || capture2 || capture3; - var value = ''; - if (windows) { - for (var name in process.env) { - if (process.env.hasOwnProperty(name)) { - // Windows is case insensitive - if (String(name).toLowerCase() === varName.toLowerCase()) { - value = process.env[name]; - break; - } - } - } - } else { - // default to empty string on Unix - value = process.env.hasOwnProperty(varName) ? process.env[varName] : ''; - } - return value; + // Return the value of the variable, or the empty string if not there + return process.env.hasOwnProperty(varName) ? process.env[varName] : ''; }); }; diff --git a/packages/pwd/dist/preparser.js b/packages/pwd/dist/preparser.js index 880839b..b8eed39 100755 --- a/packages/pwd/dist/preparser.js +++ b/packages/pwd/dist/preparser.js @@ -1,30 +1,14 @@ 'use strict'; -var os = require('os'); -var windows = os.platform() === 'win32'; - // Replace out env variables. + var parseEnvVariables = function parseEnvVariables(input) { var referenceRegex = /\${([a-zA-Z_][a-zA-Z0-9_]*)}|\$([a-zA-Z_][a-zA-Z0-9_]*)/g; return input.replace(referenceRegex, function (varRef, capture1, capture2, capture3) { var varName = capture1 || capture2 || capture3; - var value = ''; - if (windows) { - for (var name in process.env) { - if (process.env.hasOwnProperty(name)) { - // Windows is case insensitive - if (String(name).toLowerCase() === varName.toLowerCase()) { - value = process.env[name]; - break; - } - } - } - } else { - // default to empty string on Unix - value = process.env.hasOwnProperty(varName) ? process.env[varName] : ''; - } - return value; + // Return the value of the variable, or the empty string if not there + return process.env.hasOwnProperty(varName) ? process.env[varName] : ''; }); }; diff --git a/packages/rm/dist/preparser.js b/packages/rm/dist/preparser.js index 880839b..b8eed39 100755 --- a/packages/rm/dist/preparser.js +++ b/packages/rm/dist/preparser.js @@ -1,30 +1,14 @@ 'use strict'; -var os = require('os'); -var windows = os.platform() === 'win32'; - // Replace out env variables. + var parseEnvVariables = function parseEnvVariables(input) { var referenceRegex = /\${([a-zA-Z_][a-zA-Z0-9_]*)}|\$([a-zA-Z_][a-zA-Z0-9_]*)/g; return input.replace(referenceRegex, function (varRef, capture1, capture2, capture3) { var varName = capture1 || capture2 || capture3; - var value = ''; - if (windows) { - for (var name in process.env) { - if (process.env.hasOwnProperty(name)) { - // Windows is case insensitive - if (String(name).toLowerCase() === varName.toLowerCase()) { - value = process.env[name]; - break; - } - } - } - } else { - // default to empty string on Unix - value = process.env.hasOwnProperty(varName) ? process.env[varName] : ''; - } - return value; + // Return the value of the variable, or the empty string if not there + return process.env.hasOwnProperty(varName) ? process.env[varName] : ''; }); }; diff --git a/packages/sort/dist/preparser.js b/packages/sort/dist/preparser.js index 880839b..b8eed39 100755 --- a/packages/sort/dist/preparser.js +++ b/packages/sort/dist/preparser.js @@ -1,30 +1,14 @@ 'use strict'; -var os = require('os'); -var windows = os.platform() === 'win32'; - // Replace out env variables. + var parseEnvVariables = function parseEnvVariables(input) { var referenceRegex = /\${([a-zA-Z_][a-zA-Z0-9_]*)}|\$([a-zA-Z_][a-zA-Z0-9_]*)/g; return input.replace(referenceRegex, function (varRef, capture1, capture2, capture3) { var varName = capture1 || capture2 || capture3; - var value = ''; - if (windows) { - for (var name in process.env) { - if (process.env.hasOwnProperty(name)) { - // Windows is case insensitive - if (String(name).toLowerCase() === varName.toLowerCase()) { - value = process.env[name]; - break; - } - } - } - } else { - // default to empty string on Unix - value = process.env.hasOwnProperty(varName) ? process.env[varName] : ''; - } - return value; + // Return the value of the variable, or the empty string if not there + return process.env.hasOwnProperty(varName) ? process.env[varName] : ''; }); }; diff --git a/packages/touch/dist/preparser.js b/packages/touch/dist/preparser.js index 880839b..b8eed39 100755 --- a/packages/touch/dist/preparser.js +++ b/packages/touch/dist/preparser.js @@ -1,30 +1,14 @@ 'use strict'; -var os = require('os'); -var windows = os.platform() === 'win32'; - // Replace out env variables. + var parseEnvVariables = function parseEnvVariables(input) { var referenceRegex = /\${([a-zA-Z_][a-zA-Z0-9_]*)}|\$([a-zA-Z_][a-zA-Z0-9_]*)/g; return input.replace(referenceRegex, function (varRef, capture1, capture2, capture3) { var varName = capture1 || capture2 || capture3; - var value = ''; - if (windows) { - for (var name in process.env) { - if (process.env.hasOwnProperty(name)) { - // Windows is case insensitive - if (String(name).toLowerCase() === varName.toLowerCase()) { - value = process.env[name]; - break; - } - } - } - } else { - // default to empty string on Unix - value = process.env.hasOwnProperty(varName) ? process.env[varName] : ''; - } - return value; + // Return the value of the variable, or the empty string if not there + return process.env.hasOwnProperty(varName) ? process.env[varName] : ''; }); }; diff --git a/src/preparser.js b/src/preparser.js index e463e1b..67cd56f 100755 --- a/src/preparser.js +++ b/src/preparser.js @@ -1,8 +1,5 @@ 'use strict'; -const os = require('os'); -const windows = (os.platform() === 'win32'); - // Replace out env variables. const parseEnvVariables = function (input) { const referenceRegex = @@ -10,22 +7,8 @@ const parseEnvVariables = function (input) { return input.replace(referenceRegex, function (varRef, capture1, capture2, capture3) { const varName = capture1 || capture2 || capture3; - let value = ''; - if (windows) { - for (const name in process.env) { - if (process.env.hasOwnProperty(name)) { - // Windows is case insensitive - if (String(name).toLowerCase() === varName.toLowerCase()) { - value = process.env[name]; - break; - } - } - } - } else { - // default to empty string on Unix - value = process.env.hasOwnProperty(varName) ? process.env[varName] : ''; - } - return value; + // Return the value of the variable, or the empty string if not there + return process.env.hasOwnProperty(varName) ? process.env[varName] : ''; }); }; diff --git a/test/preparser.js b/test/preparser.js index 4475a92..830f7a9 100755 --- a/test/preparser.js +++ b/test/preparser.js @@ -76,7 +76,7 @@ describe('preparser', function () { it('should have proper case sensitivity', function () { if (windows) { // Case insensitive - cash('echo %path%.%PATH%').should.equal(`${path}.${path}\n`); + cash('echo $path.$PATH').should.equal(`${path}.${path}\n`); } else { // Case sensitive cash('echo $path.$PATH').should.equal(`.${path}\n`);