UNPKG

7.37 kBJavaScriptView Raw
1"use strict";
2/**
3 * Copyright (c) 2012-2015, Christopher Jeffrey, Peter Sunde (MIT License)
4 * Copyright (c) 2016, Daniel Imms (MIT License).
5 * Copyright (c) 2018, Microsoft Corporation (MIT License).
6 */
7var __extends = (this && this.__extends) || (function () {
8 var extendStatics = function (d, b) {
9 extendStatics = Object.setPrototypeOf ||
10 ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
11 function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
12 return extendStatics(d, b);
13 };
14 return function (d, b) {
15 extendStatics(d, b);
16 function __() { this.constructor = d; }
17 d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
18 };
19})();
20Object.defineProperty(exports, "__esModule", { value: true });
21var terminal_1 = require("./terminal");
22var windowsPtyAgent_1 = require("./windowsPtyAgent");
23var utils_1 = require("./utils");
24var DEFAULT_FILE = 'cmd.exe';
25var DEFAULT_NAME = 'Windows Shell';
26var WindowsTerminal = /** @class */ (function (_super) {
27 __extends(WindowsTerminal, _super);
28 function WindowsTerminal(file, args, opt) {
29 var _this = _super.call(this, opt) || this;
30 _this._checkType('args', args, 'string', true);
31 // Initialize arguments
32 args = args || [];
33 file = file || DEFAULT_FILE;
34 opt = opt || {};
35 opt.env = opt.env || process.env;
36 if (opt.encoding) {
37 console.warn('Setting encoding on Windows is not supported');
38 }
39 var env = utils_1.assign({}, opt.env);
40 _this._cols = opt.cols || terminal_1.DEFAULT_COLS;
41 _this._rows = opt.rows || terminal_1.DEFAULT_ROWS;
42 var cwd = opt.cwd || process.cwd();
43 var name = opt.name || env.TERM || DEFAULT_NAME;
44 var parsedEnv = _this._parseEnv(env);
45 // If the terminal is ready
46 _this._isReady = false;
47 // Functions that need to run after `ready` event is emitted.
48 _this._deferreds = [];
49 // Create new termal.
50 _this._agent = new windowsPtyAgent_1.WindowsPtyAgent(file, args, parsedEnv, cwd, _this._cols, _this._rows, false, opt.useConpty, opt.conptyInheritCursor);
51 _this._socket = _this._agent.outSocket;
52 // Not available until `ready` event emitted.
53 _this._pid = _this._agent.innerPid;
54 _this._fd = _this._agent.fd;
55 _this._pty = _this._agent.pty;
56 // The forked windows terminal is not available until `ready` event is
57 // emitted.
58 _this._socket.on('ready_datapipe', function () {
59 // These events needs to be forwarded.
60 ['connect', 'data', 'end', 'timeout', 'drain'].forEach(function (event) {
61 _this._socket.on(event, function () {
62 // Wait until the first data event is fired then we can run deferreds.
63 if (!_this._isReady && event === 'data') {
64 // Terminal is now ready and we can avoid having to defer method
65 // calls.
66 _this._isReady = true;
67 // Execute all deferred methods
68 _this._deferreds.forEach(function (fn) {
69 // NB! In order to ensure that `this` has all its references
70 // updated any variable that need to be available in `this` before
71 // the deferred is run has to be declared above this forEach
72 // statement.
73 fn.run();
74 });
75 // Reset
76 _this._deferreds = [];
77 }
78 });
79 });
80 // Shutdown if `error` event is emitted.
81 _this._socket.on('error', function (err) {
82 // Close terminal session.
83 _this._close();
84 // EIO, happens when someone closes our child process: the only process
85 // in the terminal.
86 // node < 0.6.14: errno 5
87 // node >= 0.6.14: read EIO
88 if (err.code) {
89 if (~err.code.indexOf('errno 5') || ~err.code.indexOf('EIO'))
90 return;
91 }
92 // Throw anything else.
93 if (_this.listeners('error').length < 2) {
94 throw err;
95 }
96 });
97 // Cleanup after the socket is closed.
98 _this._socket.on('close', function () {
99 _this.emit('exit', _this._agent.exitCode);
100 _this._close();
101 });
102 });
103 _this._file = file;
104 _this._name = name;
105 _this._readable = true;
106 _this._writable = true;
107 _this._forwardEvents();
108 return _this;
109 }
110 WindowsTerminal.prototype._write = function (data) {
111 this._defer(this._doWrite, data);
112 };
113 WindowsTerminal.prototype._doWrite = function (data) {
114 this._agent.inSocket.write(data);
115 };
116 /**
117 * openpty
118 */
119 WindowsTerminal.open = function (options) {
120 throw new Error('open() not supported on windows, use Fork() instead.');
121 };
122 /**
123 * TTY
124 */
125 WindowsTerminal.prototype.resize = function (cols, rows) {
126 var _this = this;
127 if (cols <= 0 || rows <= 0 || isNaN(cols) || isNaN(rows) || cols === Infinity || rows === Infinity) {
128 throw new Error('resizing must be done using positive cols and rows');
129 }
130 this._defer(function () {
131 _this._agent.resize(cols, rows);
132 _this._cols = cols;
133 _this._rows = rows;
134 });
135 };
136 WindowsTerminal.prototype.destroy = function () {
137 var _this = this;
138 this._defer(function () {
139 _this.kill();
140 });
141 };
142 WindowsTerminal.prototype.kill = function (signal) {
143 var _this = this;
144 this._defer(function () {
145 if (signal) {
146 throw new Error('Signals not supported on windows.');
147 }
148 _this._close();
149 _this._agent.kill();
150 });
151 };
152 WindowsTerminal.prototype._defer = function (deferredFn, arg) {
153 var _this = this;
154 // If the terminal is ready, execute.
155 if (this._isReady) {
156 deferredFn.call(this, arg);
157 return;
158 }
159 // Queue until terminal is ready.
160 this._deferreds.push({
161 run: function () { return deferredFn.call(_this, arg); }
162 });
163 };
164 Object.defineProperty(WindowsTerminal.prototype, "process", {
165 get: function () { return this._name; },
166 enumerable: true,
167 configurable: true
168 });
169 Object.defineProperty(WindowsTerminal.prototype, "master", {
170 get: function () { throw new Error('master is not supported on Windows'); },
171 enumerable: true,
172 configurable: true
173 });
174 Object.defineProperty(WindowsTerminal.prototype, "slave", {
175 get: function () { throw new Error('slave is not supported on Windows'); },
176 enumerable: true,
177 configurable: true
178 });
179 return WindowsTerminal;
180}(terminal_1.Terminal));
181exports.WindowsTerminal = WindowsTerminal;
182//# sourceMappingURL=windowsTerminal.js.map
\No newline at end of file