to-batch-syntax.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. exports.replaceDollarWithPercentPair = replaceDollarWithPercentPair
  2. exports.convertToSetCommand = convertToSetCommand
  3. exports.convertToSetCommands = convertToSetCommands
  4. function convertToSetCommand(key, value) {
  5. var line = ""
  6. key = key || ""
  7. key = key.trim()
  8. value = value || ""
  9. value = value.trim()
  10. if(key && value && value.length > 0) {
  11. line = "@SET " + key + "=" + replaceDollarWithPercentPair(value) + "\r\n"
  12. }
  13. return line
  14. }
  15. function extractVariableValuePairs(declarations) {
  16. var pairs = {}
  17. declarations.map(function(declaration) {
  18. var split = declaration.split("=")
  19. pairs[split[0]]=split[1]
  20. })
  21. return pairs
  22. }
  23. function convertToSetCommands(variableString) {
  24. var variableValuePairs = extractVariableValuePairs(variableString.split(" "))
  25. var variableDeclarationsAsBatch = ""
  26. Object.keys(variableValuePairs).forEach(function (key) {
  27. variableDeclarationsAsBatch += convertToSetCommand(key, variableValuePairs[key])
  28. })
  29. return variableDeclarationsAsBatch
  30. }
  31. function replaceDollarWithPercentPair(value) {
  32. var dollarExpressions = /\$\{?([^\$@#\?\- \t{}:]+)\}?/g
  33. var result = ""
  34. var startIndex = 0
  35. do {
  36. var match = dollarExpressions.exec(value)
  37. if(match) {
  38. var betweenMatches = value.substring(startIndex, match.index) || ""
  39. result += betweenMatches + "%" + match[1] + "%"
  40. startIndex = dollarExpressions.lastIndex
  41. }
  42. } while (dollarExpressions.lastIndex > 0)
  43. result += value.substr(startIndex)
  44. return result
  45. }