-
Notifications
You must be signed in to change notification settings - Fork 9
/
hdyShellDomain.js
161 lines (128 loc) · 4.47 KB
/
hdyShellDomain.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/* globals require, process, exports */
(function() {
"use strict";
var _domainManager,
child,
kill = require("tree-kill");
/**
* @private
* Handler function for the simple.getMemory command.
* @param {boolean} total If true, return total memory;
if false, return free memory only.
* @return {number} The amount of memory.
*/
function _execute(cmd, cwd, isWin) {
var spawn = require("child_process").spawn,
args,
enddir = cwd,
tempdir;
cmd = cmd.trim();
// Are we changing directories? If so we need
// to handle that in a special way.
if (cmd.slice(0, 3).toLowerCase() === "cd ") {
try {
process.chdir(cwd);
tempdir = cmd.substring(2).trim();
process.chdir(tempdir);
enddir = process.cwd();
}
catch (e) {}
}
// clearing the console with clear or clr?
if ((cmd.toLowerCase() === "clear" && !isWin) ||
(cmd.toLowerCase() === "cls" && isWin)) {
_domainManager.emitEvent("hdyShellDomain", "clear");
}
if (isWin) {
args = ["/c", cmd];
cmd = "cmd.exe";
}
else {
args = ["-c", cmd];
cmd = "/bin/sh";
}
child = spawn(cmd, args, { cwd: cwd, env: process.env });
child.stdout.on("data", function (data) {
_domainManager.emitEvent("hdyShellDomain", "stdout", [data.toString()]);
});
child.stderr.on("data", function (data) {
_domainManager.emitEvent("hdyShellDomain", "stderr", [data.toString()]);
});
child.on("close", function () {
child.kill();
_domainManager.emitEvent("hdyShellDomain", "close", [enddir]);
});
}
function _kill() {
//SIGKILL, SIGTERM, SIGABRT, SIGHUP, SIGINT and SIGQUIT
if (child && child.pid) {
kill(child.pid);
}
}
function _detach() {
if (child) {
child.disconnect();
}
}
/**
* Initializes the test domain with several test commands.
* @param {DomainManager} domainManager The DomainManager for the server
*/
function _init(domainManager) {
if (!domainManager.hasDomain("hdyShellDomain")) {
domainManager.registerDomain("hdyShellDomain", {major: 0, minor: 2});
}
domainManager.registerCommand(
"hdyShellDomain", // domain name
"kill", // command name
_kill, // command handler function
true, // isAsync
"Kill the current executing process",
[]
);
domainManager.registerCommand(
"hdyShellDomain", // domain name
"detach", // command name
_detach, // command handler function
true, // isAsync
"Detach current running process from brackets shell",
[]
);
domainManager.registerCommand(
"hdyShellDomain", // domain name
"execute", // command name
_execute, // command handler function
true, // isAsync
"Execute the given command and return the results to the UI",
[{
name: "cmd",
type: "string",
description: "The command to be executed"
},
{
name: "cwd",
type: "string",
description: "Directory in which the command is executed"
},
{
name: "isWin",
type: "Boolean",
description: "Is Windows System ?"
}]
);
domainManager.registerEvent("hdyShellDomain",
"stdout",
[{name: "data", type: "string"}]);
domainManager.registerEvent("hdyShellDomain",
"stderr",
[{name: "err", type: "string"}]);
domainManager.registerEvent("hdyShellDomain",
"close",
[{name: "enddir", type: "string"}]);
domainManager.registerEvent("hdyShellDomain",
"clear",
[]);
_domainManager = domainManager;
}
exports.init = _init;
}());