executeWinCommand.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.executeCommand = void 0;
  6. function _fs() {
  7. const data = require("fs");
  8. _fs = function () {
  9. return data;
  10. };
  11. return data;
  12. }
  13. function _os() {
  14. const data = require("os");
  15. _os = function () {
  16. return data;
  17. };
  18. return data;
  19. }
  20. function _path() {
  21. const data = require("path");
  22. _path = function () {
  23. return data;
  24. };
  25. return data;
  26. }
  27. function _execa() {
  28. const data = _interopRequireDefault(require("execa"));
  29. _execa = function () {
  30. return data;
  31. };
  32. return data;
  33. }
  34. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  35. /** Runs a command requestion permission to run elevated. */
  36. const runElevated = command => {
  37. // TODO: escape double quotes in args
  38. // https://www.winhelponline.com/blog/vbscripts-and-uac-elevation/
  39. /**
  40. * Need to use a couple of intermediary files to make this work as
  41. * `ShellExecute` only accepts a command so
  42. */
  43. // prettier-ignore
  44. const script = `If WScript.Arguments.length = 0 Then
  45. Set objShell = CreateObject("Shell.Application")
  46. 'Pass a bogus argument, say [ uac]
  47. objShell.ShellExecute "wscript.exe", Chr(34) & _
  48. WScript.ScriptFullName & Chr(34) & " uac", "", "runas", 1
  49. Else
  50. Dim oShell
  51. Set oShell = WScript.CreateObject ("WSCript.shell")
  52. oShell.run "${command}"
  53. Set oShell = Nothing
  54. End If`;
  55. const elevatedPath = (0, _path().join)((0, _os().tmpdir)(), `elevated-${Math.random()}.vbs`);
  56. (0, _fs().writeFileSync)(elevatedPath, script, 'utf-8');
  57. return (0, _execa().default)(elevatedPath);
  58. };
  59. /**
  60. * Groups all string arguments into a single one. E.g.:
  61. * ```js
  62. * ['-m', '"Upgrade:', 'to', 'latest', 'version"'] --> ['-m', '"Upgrade: to latest version"']`
  63. * ```
  64. * @param args The arguments
  65. * © webhint project
  66. * (https://github.com/webhintio/hint/blob/30b8ba74f122d8b66fc5596d788dd1c7738f2d83/release/lib/utils.ts#L82)
  67. * License: Apache-2
  68. */
  69. const groupArgs = args => {
  70. let isStringArgument = false;
  71. const newArgs = args.reduce((acum, current) => {
  72. if (isStringArgument) {
  73. const last = acum[acum.length - 1];
  74. acum[acum.length - 1] = `${last} ${current}`;
  75. if (current.endsWith('"')) {
  76. isStringArgument = false;
  77. }
  78. return acum;
  79. }
  80. if (current.startsWith('"')) {
  81. /**
  82. * Argument is split. I.e.: `['"part1', 'part2"'];`
  83. */
  84. if (!current.endsWith('"')) {
  85. isStringArgument = true;
  86. acum.push(current);
  87. return acum;
  88. }
  89. /**
  90. * Argument is surrounded by "" that need to be removed.
  91. * We just remove all the quotes because we don't escape any in our commands
  92. */
  93. acum.push(current.replace(/"/g, ''));
  94. return acum;
  95. }
  96. acum.push(current);
  97. return acum;
  98. }, []);
  99. return newArgs;
  100. };
  101. /**
  102. * Executes the given `command` on a shell taking care of slicing the parameters
  103. * if needed.
  104. */
  105. const executeShellCommand = (command, elevated = false) => {
  106. const args = groupArgs(command.split(' '));
  107. const program = args.shift();
  108. if (elevated) {
  109. return runElevated(command);
  110. }
  111. return (0, _execa().default)(program, args, {
  112. shell: true
  113. });
  114. };
  115. exports.executeCommand = executeShellCommand;
  116. //# sourceMappingURL=executeWinCommand.js.map