UNPKG

4.75 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.macPathToParallelsWindows = macPathToParallelsWindows;
7exports.ParallelsVmManager = exports.parseVmList = void 0;
8
9function _bluebirdLst() {
10 const data = require("bluebird-lst");
11
12 _bluebirdLst = function () {
13 return data;
14 };
15
16 return data;
17}
18
19function _builderUtil() {
20 const data = require("builder-util");
21
22 _builderUtil = function () {
23 return data;
24 };
25
26 return data;
27}
28
29function _child_process() {
30 const data = require("child_process");
31
32 _child_process = function () {
33 return data;
34 };
35
36 return data;
37}
38
39function _vm() {
40 const data = require("./vm");
41
42 _vm = function () {
43 return data;
44 };
45
46 return data;
47}
48
49/** @internal */
50let parseVmList = (() => {
51 var _ref = (0, _bluebirdLst().coroutine)(function* (debugLogger) {
52 // do not log output if debug - it is huge, logged using debugLogger
53 let rawList = yield (0, _builderUtil().exec)("prlctl", ["list", "-i", "-s", "name"], undefined, false);
54 debugLogger.add("parallels.list", rawList);
55 rawList = rawList.substring(rawList.indexOf("ID:")); // let match: Array<string> | null
56
57 const result = [];
58
59 for (const info of rawList.split("\n\n").map(it => it.trim()).filter(it => it.length > 0)) {
60 const vm = {};
61
62 for (const line of info.split("\n")) {
63 const meta = /^([^:("]+): (.*)$/.exec(line);
64
65 if (meta == null) {
66 continue;
67 }
68
69 const key = meta[1].toLowerCase();
70
71 if (key === "id" || key === "os" || key === "name" || key === "state" || key === "name") {
72 vm[key] = meta[2].trim();
73 }
74 }
75
76 result.push(vm);
77 }
78
79 return result;
80 });
81
82 return function parseVmList(_x) {
83 return _ref.apply(this, arguments);
84 };
85})();
86/** @internal */
87
88
89exports.parseVmList = parseVmList;
90
91class ParallelsVmManager extends _vm().VmManager {
92 constructor(vm) {
93 super();
94 this.vm = vm;
95 this.isExitHookAdded = false;
96 this.startPromise = this.doStartVm();
97 }
98
99 get pathSep() {
100 return "/";
101 }
102
103 handleExecuteError(error) {
104 if (error.message.includes("Unable to open new session in this virtual machine")) {
105 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}`);
106 }
107
108 _builderUtil().log.warn("ensure that 'Share folders' is set to 'All Disks', see https://goo.gl/E6XphP");
109
110 throw error;
111 }
112
113 exec(file, args, options) {
114 var _this = this;
115
116 return (0, _bluebirdLst().coroutine)(function* () {
117 yield _this.ensureThatVmStarted(); // it is important to use "--current-user" to execute command under logged in user - to access certs.
118
119 return yield (0, _builderUtil().exec)("prlctl", ["exec", _this.vm.id, "--current-user", file.startsWith("/") ? macPathToParallelsWindows(file) : file].concat(args), options).catch(error => _this.handleExecuteError(error));
120 })();
121 }
122
123 spawn(file, args, options, extraOptions) {
124 var _this2 = this;
125
126 return (0, _bluebirdLst().coroutine)(function* () {
127 yield _this2.ensureThatVmStarted();
128 return yield (0, _builderUtil().spawn)("prlctl", ["exec", _this2.vm.id, file].concat(args), options, extraOptions).catch(error => _this2.handleExecuteError(error));
129 })();
130 }
131
132 doStartVm() {
133 var _this3 = this;
134
135 return (0, _bluebirdLst().coroutine)(function* () {
136 const vmId = _this3.vm.id;
137 const state = _this3.vm.state;
138
139 if (state === "running") {
140 return;
141 }
142
143 if (!_this3.isExitHookAdded) {
144 _this3.isExitHookAdded = true;
145
146 require("async-exit-hook")(callback => {
147 const stopArgs = ["suspend", vmId];
148
149 if (callback == null) {
150 (0, _child_process().execFileSync)("prlctl", stopArgs);
151 } else {
152 (0, _builderUtil().exec)("prlctl", stopArgs).then(callback).catch(callback);
153 }
154 });
155 }
156
157 yield (0, _builderUtil().exec)("prlctl", ["start", vmId]);
158 })();
159 }
160
161 ensureThatVmStarted() {
162 let startPromise = this.startPromise;
163
164 if (startPromise == null) {
165 startPromise = this.doStartVm();
166 this.startPromise = startPromise;
167 }
168
169 return startPromise;
170 }
171
172 toVmFile(file) {
173 // https://stackoverflow.com/questions/4742992/cannot-access-network-drive-in-powershell-running-as-administrator
174 return macPathToParallelsWindows(file);
175 }
176
177}
178
179exports.ParallelsVmManager = ParallelsVmManager;
180
181function macPathToParallelsWindows(file) {
182 if (file.startsWith("C:\\")) {
183 return file;
184 }
185
186 return "\\\\Mac\\Host\\" + file.replace(/\//g, "\\");
187}
188//# sourceMappingURL=ParallelsVm.js.map
\No newline at end of file