forked from npm/cmd-shim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathto-batch-syntax.js
51 lines (45 loc) · 1.61 KB
/
to-batch-syntax.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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
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
}