This repository has been archived by the owner on Jun 10, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
script-runner-process.js
207 lines (161 loc) · 4.5 KB
/
script-runner-process.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/** @babel */
const PTY = require('node-pty');
const OS = require('os');
const Path = require('path');
const Shellwords = require('shellwords');
const TempWrite = require('temp-write');
class ScriptRunnerProcess {
static run(view, cmd, env, editor) {
const scriptRunnerProcess = new ScriptRunnerProcess(view);
scriptRunnerProcess.execute(cmd, env, editor);
return scriptRunnerProcess;
}
static spawn(view, args, cwd, env) {
const scriptRunnerProcess = new ScriptRunnerProcess(view);
scriptRunnerProcess.spawn(args, cwd, env);
return scriptRunnerProcess;
}
constructor(view) {
this.view = view;
this.pty = null;
if (atom.config.get('script-runner.clearBeforeExecuting')) {
this.view.clear();
}
}
destroy() {
if (this.pty) {
this.pty.kill('SIGTERM')
this.pty.destroy();
}
}
kill(signal) {
if (signal == null) { signal = 'SIGINT'; }
if (this.pty) {
console.log("Sending", signal, "to child", this.pty, "pid", this.pty.pid);
this.pty.kill(signal);
if (this.view) {
this.view.log(`<Sending ${signal}>`, 'stdin');
}
}
}
resolvePath(editor, callback) {
if (editor.getPath()) {
const cwd = Path.dirname(editor.getPath());
// Save the file if it has been modified:
Promise.resolve(editor.save()).then(() => {
callback(editor.getPath(), cwd);
});
return true;
}
// Otherwise it was not handled:
return false;
}
resolveSelection(editor, callback) {
let cwd;
if (editor.getPath()) {
cwd = Path.dirname(editor.getPath());
} else {
cwd = atom.project.path;
}
const selection = editor.getLastSelection();
if (selection != null && !selection.isEmpty()) {
callback(selection.getText(), cwd);
return true;
}
// Otherwise it was not handled:
return false;
}
resolveBuffer(editor, callback) {
let cwd;
if (editor.getPath()) {
cwd = Path.dirname(editor.getPath());
} else {
cwd = atom.project.path;
}
callback(editor.getText(), cwd);
return true;
}
execute(cmd, env, editor) {
// Split the incoming command so we can modify it
const args = Shellwords.split(cmd);
if (this.resolveSelection(editor, (text, cwd) => {
args.push(TempWrite.sync(text));
return this.spawn(args, cwd, env);
})) { return true; }
if (this.resolvePath(editor, (path, cwd) => {
args.push(path);
return this.spawn(args, cwd, env);
})) { return true; }
if (this.resolveBuffer(editor, (text, cwd) => {
args.push(TempWrite.sync(text));
return this.spawn(args, cwd, env);
})) { return true; }
// something really has to go wrong for this.
return false;
}
spawn(args, cwd, env) {
// Spawn the child process:
console.log("spawn", args[0], args.slice(1), cwd, env);
env['TERM'] = 'xterm-256color';
this.pty = PTY.spawn(args[0], args.slice(1), {
cols: this.view.terminal.cols,
rows: this.view.terminal.rows,
cwd: cwd,
env: env,
name: 'xterm-color',
});
this.startTime = new Date;
// Update the status (*Shellwords.join doesn't exist yet):
//this.view.log(args.join(' ') + ' (pgid ' + this.pty.pid + ')');
if (this.view.process) {
this.view.process.destroy();
}
this.view.process = this;
const {terminal} = this.view;
this.view.on('paste', data => {
// console.log('view -> pty (paste)', data.length);
if (this.pty) {
this.pty.write(data);
}
});
terminal.on('data', data => {
// console.log('view -> pty (data)', data.length);
if (this.pty) {
this.pty.write(data);
}
});
terminal.on('resize', geometry => {
if (this.pty) {
// console.log('view -> pty (resize)', geometry);
this.pty.resize(geometry.cols, geometry.rows);
}
});
this.pty.on('exit', () => {
// console.log('pty (exit)')
});
// Handle various events relating to the child process:
this.pty.on('data', data => {
// console.log('pty -> view (data)', data.length);
terminal.write(data);
});
this.pty.on('error', what => {
console.log('pty (error)', what);
});
this.pty.on('exit', (code, signal) => {
console.log('pty (exit)', code, signal)
this.pty.destroy();
this.pty = null;
this.endTime = new Date;
if (this.view) {
const duration = ` after ${(this.endTime - this.startTime) / 1000} seconds`;
if (signal) {
this.view.log(`Exited with signal ${signal}${duration}`);
} else if (code && code != 0) {
this.view.log(`Exited with status ${code}${duration}`);
}
}
});
terminal.focus();
}
};
module.exports = ScriptRunnerProcess;