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).
7 var __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);
14 return function (d, b) {
16 function __() { this.constructor = d; }
17 d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
20 Object.defineProperty(exports, "__esModule", { value: true });
21 var terminal_1 = require("./terminal");
22 var windowsPtyAgent_1 = require("./windowsPtyAgent");
23 var utils_1 = require("./utils");
24 var DEFAULT_FILE = 'cmd.exe';
25 var DEFAULT_NAME = 'Windows Shell';
26 var WindowsTerminal = /** @class */ (function (_super) {
27 __extends(WindowsTerminal, _super);
28 function WindowsTerminal(file, args, opt) {
29 var _this = _super.call(this, opt) || this;
30 // Initialize arguments
32 file = file || DEFAULT_FILE;
34 opt.env = opt.env || process.env;
36 console.warn('Setting encoding on Windows is not supported');
38 var env = utils_1.assign({}, opt.env);
39 _this._cols = opt.cols || terminal_1.DEFAULT_COLS;
40 _this._rows = opt.rows || terminal_1.DEFAULT_ROWS;
41 var cwd = opt.cwd || process.cwd();
42 var name = opt.name || env.TERM || DEFAULT_NAME;
43 var parsedEnv = _this._parseEnv(env);
44 // If the terminal is ready
45 _this._isReady = false;
46 // Functions that need to run after `ready` event is emitted.
47 _this._deferreds = [];
49 _this._agent = new windowsPtyAgent_1.WindowsPtyAgent(file, args, parsedEnv, cwd, _this._cols, _this._rows, false, opt.useConpty, opt.conptyInheritCursor);
50 _this._socket = _this._agent.outSocket;
51 // Not available until `ready` event emitted.
52 _this._pid = _this._agent.innerPid;
53 _this._fd = _this._agent.fd;
54 _this._pty = _this._agent.pty;
55 // The forked windows terminal is not available until `ready` event is
57 _this._socket.on('ready_datapipe', function () {
58 // These events needs to be forwarded.
59 ['connect', 'data', 'end', 'timeout', 'drain'].forEach(function (event) {
60 _this._socket.on(event, function () {
61 // Wait until the first data event is fired then we can run deferreds.
62 if (!_this._isReady && event === 'data') {
63 // Terminal is now ready and we can avoid having to defer method
65 _this._isReady = true;
66 // Execute all deferred methods
67 _this._deferreds.forEach(function (fn) {
68 // NB! In order to ensure that `this` has all its references
69 // updated any variable that need to be available in `this` before
70 // the deferred is run has to be declared above this forEach
75 _this._deferreds = [];
79 // Shutdown if `error` event is emitted.
80 _this._socket.on('error', function (err) {
81 // Close terminal session.
83 // EIO, happens when someone closes our child process: the only process
85 // node < 0.6.14: errno 5
86 // node >= 0.6.14: read EIO
88 if (~err.code.indexOf('errno 5') || ~err.code.indexOf('EIO'))
91 // Throw anything else.
92 if (_this.listeners('error').length < 2) {
96 // Cleanup after the socket is closed.
97 _this._socket.on('close', function () {
98 _this.emit('exit', _this._agent.exitCode);
104 _this._readable = true;
105 _this._writable = true;
106 _this._forwardEvents();
109 WindowsTerminal.prototype._write = function (data) {
110 this._defer(this._doWrite, data);
112 WindowsTerminal.prototype._doWrite = function (data) {
113 this._agent.inSocket.write(data);
118 WindowsTerminal.open = function (options) {
119 throw new Error('open() not supported on windows, use Fork() instead.');
124 WindowsTerminal.prototype.resize = function (cols, rows) {
126 if (cols <= 0 || rows <= 0 || isNaN(cols) || isNaN(rows) || cols === Infinity || rows === Infinity) {
127 throw new Error('resizing must be done using positive cols and rows');
129 this._defer(function () {
130 _this._agent.resize(cols, rows);
135 WindowsTerminal.prototype.destroy = function () {
137 this._defer(function () {
141 WindowsTerminal.prototype.kill = function (signal) {
143 this._defer(function () {
145 throw new Error('Signals not supported on windows.');
151 WindowsTerminal.prototype._defer = function (deferredFn, arg) {
153 // If the terminal is ready, execute.
155 deferredFn.call(this, arg);
158 // Queue until terminal is ready.
159 this._deferreds.push({
160 run: function () { return deferredFn.call(_this, arg); }
163 Object.defineProperty(WindowsTerminal.prototype, "process", {
164 get: function () { return this._name; },
168 Object.defineProperty(WindowsTerminal.prototype, "master", {
169 get: function () { throw new Error('master is not supported on Windows'); },
173 Object.defineProperty(WindowsTerminal.prototype, "slave", {
174 get: function () { throw new Error('slave is not supported on Windows'); },
178 return WindowsTerminal;
179 }(terminal_1.Terminal));
180 exports.WindowsTerminal = WindowsTerminal;
181 //# sourceMappingURL=windowsTerminal.js.map