-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Handle environment variables properly
- Adapt the regular expression used to match the first line. - Create a simple module that can convert shell style variable declarations: NODE_PATH=./lib:$NODE_PATH to the equivalent batch syntax: @set=NODE_PATH=./lib:%NODE_PATH%. Furthermore the structure of the generated shim now looks like this: @SETLOCAL <variable declarations> <original content> @endlocal - Note that the new segments are only added to the file if there were any variable declarations. - The generated shell script carries over the captured variable declaration as is. - Add some extra tests to validate the behavior. - Remove some unnecessary white-space in the generated shim. PR-URL: #2 Fix: npm/npm#3380 Credit: @basbossink Close: #2 Reviewed-by: @isaacs
- Loading branch information
1 parent
3bf6d4a
commit 9c93ac3
Showing
5 changed files
with
146 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
exports.replaceDollarWithPercentPair = replaceDollarWithPercentPair | ||
exports.convertToSetCommand = convertToSetCommand | ||
exports.convertToSetCommands = convertToSetCommands | ||
|
||
function convertToSetCommand(key, value) { | ||
var line = "" | ||
key = key || "" | ||
key = key.trim() | ||
value = value || "" | ||
value = value.trim() | ||
if(key && value && value.length > 0) { | ||
line = "@SET " + key + "=" + replaceDollarWithPercentPair(value) + "\r\n" | ||
} | ||
return line | ||
} | ||
|
||
function extractVariableValuePairs(declarations) { | ||
var pairs = {} | ||
declarations.map(function(declaration) { | ||
var split = declaration.split("=") | ||
pairs[split[0]]=split[1] | ||
}) | ||
return pairs | ||
} | ||
|
||
function convertToSetCommands(variableString) { | ||
var variableValuePairs = extractVariableValuePairs(variableString.split(" ")) | ||
var variableDeclarationsAsBatch = "" | ||
Object.keys(variableValuePairs).forEach(function (key) { | ||
variableDeclarationsAsBatch += convertToSetCommand(key, variableValuePairs[key]) | ||
}) | ||
return variableDeclarationsAsBatch | ||
} | ||
|
||
function replaceDollarWithPercentPair(value) { | ||
var dollarExpressions = /\$\{?([^\$@#\?\- \t{}:]+)\}?/g | ||
var result = "" | ||
var startIndex = 0 | ||
value = value || "" | ||
do { | ||
var match = dollarExpressions.exec(value) | ||
if(match) { | ||
var betweenMatches = value.substring(startIndex, match.index) || "" | ||
result += betweenMatches + "%" + match[1] + "%" | ||
startIndex = dollarExpressions.lastIndex | ||
} | ||
} while (dollarExpressions.lastIndex > 0) | ||
result += value.substr(startIndex) | ||
return result | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
var test = require('tap').test | ||
var toBatchSyntax = require('../lib/to-batch-syntax') | ||
|
||
test('replace $ expressions with % pair', function (t) { | ||
var assertReplacement = function(string, expected) { | ||
t.equal(toBatchSyntax.replaceDollarWithPercentPair(string), expected) | ||
} | ||
assertReplacement("$A", "%A%") | ||
assertReplacement("$A:$B", "%A%:%B%") | ||
assertReplacement("$A bla", "%A% bla") | ||
assertReplacement("${A}bla", "%A%bla") | ||
assertReplacement("$A $bla bla", "%A% %bla% bla") | ||
assertReplacement("${A}bla ${bla}bla", "%A%bla %bla%bla") | ||
assertReplacement("./lib:$NODE_PATH", "./lib:%NODE_PATH%") | ||
t.end() | ||
}) | ||
|
||
test('convert variable declaration to set command', function(t) { | ||
t.equal(toBatchSyntax.convertToSetCommand("A",".lib:$A "), "@SET A=.lib:%A%\r\n") | ||
t.equal(toBatchSyntax.convertToSetCommand("", ""), "") | ||
t.equal(toBatchSyntax.convertToSetCommand(" ", ""), "") | ||
t.equal(toBatchSyntax.convertToSetCommand(" ", " "), "") | ||
t.equal(toBatchSyntax.convertToSetCommand(" ou", " ou "), "@SET ou=ou\r\n") | ||
t.end() | ||
}) |