ParallelsVm.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.macPathToParallelsWindows = exports.ParallelsVmManager = exports.parseVmList = void 0;
  4. const builder_util_1 = require("builder-util");
  5. const child_process_1 = require("child_process");
  6. const vm_1 = require("./vm");
  7. /** @internal */
  8. async function parseVmList(debugLogger) {
  9. // do not log output if debug - it is huge, logged using debugLogger
  10. let rawList = await (0, builder_util_1.exec)("prlctl", ["list", "-i", "-s", "name"], undefined, false);
  11. debugLogger.add("parallels.list", rawList);
  12. rawList = rawList.substring(rawList.indexOf("ID:"));
  13. // let match: Array<string> | null
  14. const result = [];
  15. for (const info of rawList
  16. .split("\n\n")
  17. .map(it => it.trim())
  18. .filter(it => it.length > 0)) {
  19. const vm = {};
  20. for (const line of info.split("\n")) {
  21. const meta = /^([^:("]+): (.*)$/.exec(line);
  22. if (meta == null) {
  23. continue;
  24. }
  25. const key = meta[1].toLowerCase();
  26. if (key === "id" || key === "os" || key === "name" || key === "state" || key === "name") {
  27. vm[key] = meta[2].trim();
  28. }
  29. }
  30. result.push(vm);
  31. }
  32. return result;
  33. }
  34. exports.parseVmList = parseVmList;
  35. /** @internal */
  36. class ParallelsVmManager extends vm_1.VmManager {
  37. constructor(vm) {
  38. super();
  39. this.vm = vm;
  40. this.isExitHookAdded = false;
  41. this.startPromise = this.doStartVm();
  42. }
  43. get pathSep() {
  44. return "/";
  45. }
  46. handleExecuteError(error) {
  47. if (error.message.includes("Unable to open new session in this virtual machine")) {
  48. throw new Error(`Please ensure that your are logged in "${this.vm.name}" parallels virtual machine. In the future please do not stop VM, but suspend.\n\n${error.message}`);
  49. }
  50. builder_util_1.log.warn("ensure that 'Share folders' is set to 'All Disks', see https://goo.gl/E6XphP");
  51. throw error;
  52. }
  53. async exec(file, args, options) {
  54. await this.ensureThatVmStarted();
  55. // it is important to use "--current-user" to execute command under logged in user - to access certs.
  56. return await (0, builder_util_1.exec)("prlctl", ["exec", this.vm.id, "--current-user", file.startsWith("/") ? macPathToParallelsWindows(file) : file].concat(args), options).catch(error => this.handleExecuteError(error));
  57. }
  58. async spawn(file, args, options, extraOptions) {
  59. await this.ensureThatVmStarted();
  60. return await (0, builder_util_1.spawn)("prlctl", ["exec", this.vm.id, file].concat(args), options, extraOptions).catch(error => this.handleExecuteError(error));
  61. }
  62. async doStartVm() {
  63. const vmId = this.vm.id;
  64. const state = this.vm.state;
  65. if (state === "running") {
  66. return;
  67. }
  68. if (!this.isExitHookAdded) {
  69. this.isExitHookAdded = true;
  70. // eslint-disable-next-line @typescript-eslint/no-var-requires
  71. require("async-exit-hook")((callback) => {
  72. const stopArgs = ["suspend", vmId];
  73. if (callback == null) {
  74. (0, child_process_1.execFileSync)("prlctl", stopArgs);
  75. }
  76. else {
  77. (0, builder_util_1.exec)("prlctl", stopArgs).then(callback).catch(callback);
  78. }
  79. });
  80. }
  81. await (0, builder_util_1.exec)("prlctl", ["start", vmId]);
  82. }
  83. ensureThatVmStarted() {
  84. let startPromise = this.startPromise;
  85. if (startPromise == null) {
  86. startPromise = this.doStartVm();
  87. this.startPromise = startPromise;
  88. }
  89. return startPromise;
  90. }
  91. toVmFile(file) {
  92. // https://stackoverflow.com/questions/4742992/cannot-access-network-drive-in-powershell-running-as-administrator
  93. return macPathToParallelsWindows(file);
  94. }
  95. }
  96. exports.ParallelsVmManager = ParallelsVmManager;
  97. function macPathToParallelsWindows(file) {
  98. if (file.startsWith("C:\\")) {
  99. return file;
  100. }
  101. return "\\\\Mac\\Host\\" + file.replace(/\//g, "\\");
  102. }
  103. exports.macPathToParallelsWindows = macPathToParallelsWindows;
  104. //# sourceMappingURL=ParallelsVm.js.map