installed pty
[VSoRC/.git] / node_modules / node-pty / lib / windowsTerminal.js
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  */
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);
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 })();
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
31         args = args || [];
32         file = file || DEFAULT_FILE;
33         opt = opt || {};
34         opt.env = opt.env || process.env;
35         if (opt.encoding) {
36             console.warn('Setting encoding on Windows is not supported');
37         }
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 = [];
48         // Create new termal.
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
56         // emitted.
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
64                         // calls.
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
71                             // statement.
72                             fn.run();
73                         });
74                         // Reset
75                         _this._deferreds = [];
76                     }
77                 });
78             });
79             // Shutdown if `error` event is emitted.
80             _this._socket.on('error', function (err) {
81                 // Close terminal session.
82                 _this._close();
83                 // EIO, happens when someone closes our child process: the only process
84                 // in the terminal.
85                 // node < 0.6.14: errno 5
86                 // node >= 0.6.14: read EIO
87                 if (err.code) {
88                     if (~err.code.indexOf('errno 5') || ~err.code.indexOf('EIO'))
89                         return;
90                 }
91                 // Throw anything else.
92                 if (_this.listeners('error').length < 2) {
93                     throw err;
94                 }
95             });
96             // Cleanup after the socket is closed.
97             _this._socket.on('close', function () {
98                 _this.emit('exit', _this._agent.exitCode);
99                 _this._close();
100             });
101         });
102         _this._file = file;
103         _this._name = name;
104         _this._readable = true;
105         _this._writable = true;
106         _this._forwardEvents();
107         return _this;
108     }
109     WindowsTerminal.prototype._write = function (data) {
110         this._defer(this._doWrite, data);
111     };
112     WindowsTerminal.prototype._doWrite = function (data) {
113         this._agent.inSocket.write(data);
114     };
115     /**
116      * openpty
117      */
118     WindowsTerminal.open = function (options) {
119         throw new Error('open() not supported on windows, use Fork() instead.');
120     };
121     /**
122      * TTY
123      */
124     WindowsTerminal.prototype.resize = function (cols, rows) {
125         var _this = this;
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');
128         }
129         this._defer(function () {
130             _this._agent.resize(cols, rows);
131             _this._cols = cols;
132             _this._rows = rows;
133         });
134     };
135     WindowsTerminal.prototype.destroy = function () {
136         var _this = this;
137         this._defer(function () {
138             _this.kill();
139         });
140     };
141     WindowsTerminal.prototype.kill = function (signal) {
142         var _this = this;
143         this._defer(function () {
144             if (signal) {
145                 throw new Error('Signals not supported on windows.');
146             }
147             _this._close();
148             _this._agent.kill();
149         });
150     };
151     WindowsTerminal.prototype._defer = function (deferredFn, arg) {
152         var _this = this;
153         // If the terminal is ready, execute.
154         if (this._isReady) {
155             deferredFn.call(this, arg);
156             return;
157         }
158         // Queue until terminal is ready.
159         this._deferreds.push({
160             run: function () { return deferredFn.call(_this, arg); }
161         });
162     };
163     Object.defineProperty(WindowsTerminal.prototype, "process", {
164         get: function () { return this._name; },
165         enumerable: true,
166         configurable: true
167     });
168     Object.defineProperty(WindowsTerminal.prototype, "master", {
169         get: function () { throw new Error('master is not supported on Windows'); },
170         enumerable: true,
171         configurable: true
172     });
173     Object.defineProperty(WindowsTerminal.prototype, "slave", {
174         get: function () { throw new Error('slave is not supported on Windows'); },
175         enumerable: true,
176         configurable: true
177     });
178     return WindowsTerminal;
179 }(terminal_1.Terminal));
180 exports.WindowsTerminal = WindowsTerminal;
181 //# sourceMappingURL=windowsTerminal.js.map