|
6 | 6 | 'use strict';
|
7 | 7 |
|
8 | 8 | import * as nls from 'vscode-nls';
|
9 |
| -import { exec } from 'child_process'; |
| 9 | +import { spawn } from 'child_process'; |
10 | 10 | import * as vscode from 'vscode';
|
| 11 | +import { join } from 'path'; |
11 | 12 |
|
12 | 13 | const localize = nls.loadMessageBundle();
|
13 | 14 |
|
@@ -177,37 +178,45 @@ function findChildProcesses(rootPid: number, cb: (pid: number, cmd: string) => v
|
177 | 178 | }
|
178 | 179 | }
|
179 | 180 |
|
| 181 | + // returns a function that aggregates chunks of data until one or more complete lines are received and passes them to a callback. |
| 182 | + function lines(callback: (a: string) => void) { |
| 183 | + let unfinished = ''; // unfinished last line of chunk |
| 184 | + return (data: string | Buffer) => { |
| 185 | + const lines = data.toString().split(/\r?\n/); |
| 186 | + const finishedLines = lines.slice(0, lines.length - 1); |
| 187 | + finishedLines[0] = unfinished + finishedLines[0]; // complete previous unfinished line |
| 188 | + unfinished = lines[lines.length - 1]; // remember unfinished last line of this chunk for next round |
| 189 | + for (const s of finishedLines) { |
| 190 | + callback(s); |
| 191 | + } |
| 192 | + } |
| 193 | + } |
| 194 | + |
180 | 195 | if (process.platform === 'win32') {
|
181 | 196 |
|
182 |
| - const CMD = 'wmic process get CommandLine,ParentProcessId,ProcessId'; |
183 | 197 | const CMD_PAT = /^(.+)\s+([0-9]+)\s+([0-9]+)$/;
|
184 | 198 |
|
185 |
| - exec(CMD, { maxBuffer: 1000 * 1024 }, (err, stdout, stderr) => { |
186 |
| - if (!err && !stderr) { |
187 |
| - const lines = stdout.split('\r\n'); |
188 |
| - for (let line of lines) { |
189 |
| - let matches = CMD_PAT.exec(line.trim()); |
190 |
| - if (matches && matches.length === 4) { |
191 |
| - oneProcess(parseInt(matches[3]), parseInt(matches[2]), matches[1].trim()); |
192 |
| - } |
193 |
| - } |
| 199 | + const wmic = join(process.env['WINDIR'] || 'C:\\Windows', 'System32', 'wbem', 'WMIC.exe'); |
| 200 | + var proc = spawn(wmic, [ 'process', 'get', 'CommandLine,ParentProcessId,ProcessId' ]); |
| 201 | + proc.stdout.setEncoding('utf8'); |
| 202 | + proc.stdout.on('data', lines(line => { |
| 203 | + let matches = CMD_PAT.exec(line.trim()); |
| 204 | + if (matches && matches.length === 4) { |
| 205 | + oneProcess(parseInt(matches[3]), parseInt(matches[2]), matches[1].trim()); |
194 | 206 | }
|
195 |
| - }); |
| 207 | + })); |
| 208 | + |
196 | 209 | } else { // OS X & Linux
|
197 | 210 |
|
198 |
| - const CMD = 'ps -ax -o pid=,ppid=,command='; |
199 | 211 | const CMD_PAT = /^\s*([0-9]+)\s+([0-9]+)\s+(.+)$/;
|
200 | 212 |
|
201 |
| - exec(CMD, { maxBuffer: 1000 * 1024 }, (err, stdout, stderr) => { |
202 |
| - if (!err && !stderr) { |
203 |
| - const lines = stdout.toString().split('\n'); |
204 |
| - for (const line of lines) { |
205 |
| - let matches = CMD_PAT.exec(line.trim()); |
206 |
| - if (matches && matches.length === 4) { |
207 |
| - oneProcess(parseInt(matches[1]), parseInt(matches[2]), matches[3]); |
208 |
| - } |
209 |
| - } |
| 213 | + var proc = spawn('/bin/ps', [ '-ax', '-o', 'pid=,ppid=,command=' ]); |
| 214 | + proc.stdout.setEncoding('utf8'); |
| 215 | + proc.stdout.on('data', lines(line => { |
| 216 | + let matches = CMD_PAT.exec(line.trim()); |
| 217 | + if (matches && matches.length === 4) { |
| 218 | + oneProcess(parseInt(matches[1]), parseInt(matches[2]), matches[3]); |
210 | 219 | }
|
211 |
| - }); |
| 220 | + })); |
212 | 221 | }
|
213 | 222 | }
|
0 commit comments