|
1 | 1 | // Copyright (c) Microsoft Corporation. All rights reserved.
|
2 | 2 | // Licensed under the MIT License.
|
3 | 3 |
|
4 |
| -import { exec, execSync, spawn } from 'child_process'; |
| 4 | +import { execSync, spawn } from 'child_process'; |
5 | 5 | import { Readable } from 'stream';
|
6 |
| -import { Observable } from 'rxjs/Observable'; |
7 | 6 | import { IDisposable } from '../types';
|
8 | 7 | import { createDeferred } from '../utils/async';
|
9 | 8 | import { EnvironmentVariables } from '../variables/types';
|
10 | 9 | import { DEFAULT_ENCODING } from './constants';
|
11 |
| -import { ExecutionResult, ObservableExecutionResult, Output, ShellOptions, SpawnOptions, StdErrError } from './types'; |
| 10 | +import { ExecutionResult, ShellOptions, SpawnOptions, StdErrError } from './types'; |
12 | 11 | import { noop } from '../utils/misc';
|
13 | 12 | import { decodeBuffer } from './decoder';
|
14 |
| -import { traceVerbose } from '../log/logging'; |
15 | 13 |
|
16 | 14 | function getDefaultOptions<T extends ShellOptions | SpawnOptions>(options: T, defaultEnv?: EnvironmentVariables): T {
|
17 | 15 | const defaultOptions = { ...options };
|
@@ -45,42 +43,6 @@ function getDefaultOptions<T extends ShellOptions | SpawnOptions>(options: T, de
|
45 | 43 | return defaultOptions;
|
46 | 44 | }
|
47 | 45 |
|
48 |
| -export function shellExec( |
49 |
| - command: string, |
50 |
| - options: ShellOptions = {}, |
51 |
| - defaultEnv?: EnvironmentVariables, |
52 |
| - disposables?: Set<IDisposable>, |
53 |
| -): Promise<ExecutionResult<string>> { |
54 |
| - const shellOptions = getDefaultOptions(options, defaultEnv); |
55 |
| - traceVerbose(`Shell Exec: ${command} with options: ${JSON.stringify(shellOptions, null, 4)}`); |
56 |
| - return new Promise((resolve, reject) => { |
57 |
| - // eslint-disable-next-line @typescript-eslint/no-explicit-any |
58 |
| - const callback = (e: any, stdout: any, stderr: any) => { |
59 |
| - if (e && e !== null) { |
60 |
| - reject(e); |
61 |
| - } else if (shellOptions.throwOnStdErr && stderr && stderr.length) { |
62 |
| - reject(new Error(stderr)); |
63 |
| - } else { |
64 |
| - stdout = filterOutputUsingCondaRunMarkers(stdout); |
65 |
| - // Make sure stderr is undefined if we actually had none. This is checked |
66 |
| - // elsewhere because that's how exec behaves. |
67 |
| - resolve({ stderr: stderr && stderr.length > 0 ? stderr : undefined, stdout }); |
68 |
| - } |
69 |
| - }; |
70 |
| - const proc = exec(command, shellOptions, callback); // NOSONAR |
71 |
| - const disposable: IDisposable = { |
72 |
| - dispose: () => { |
73 |
| - if (!proc.killed) { |
74 |
| - proc.kill(); |
75 |
| - } |
76 |
| - }, |
77 |
| - }; |
78 |
| - if (disposables) { |
79 |
| - disposables.add(disposable); |
80 |
| - } |
81 |
| - }); |
82 |
| -} |
83 |
| - |
84 | 46 | export function plainExec(
|
85 | 47 | file: string,
|
86 | 48 | args: string[],
|
@@ -162,96 +124,6 @@ function filterOutputUsingCondaRunMarkers(stdout: string) {
|
162 | 124 | return filteredOut !== undefined ? filteredOut : stdout;
|
163 | 125 | }
|
164 | 126 |
|
165 |
| -function removeCondaRunMarkers(out: string) { |
166 |
| - out = out.replace('>>>PYTHON-EXEC-OUTPUT\r\n', '').replace('>>>PYTHON-EXEC-OUTPUT\n', ''); |
167 |
| - return out.replace('<<<PYTHON-EXEC-OUTPUT\r\n', '').replace('<<<PYTHON-EXEC-OUTPUT\n', ''); |
168 |
| -} |
169 |
| - |
170 |
| -export function execObservable( |
171 |
| - file: string, |
172 |
| - args: string[], |
173 |
| - options: SpawnOptions = {}, |
174 |
| - defaultEnv?: EnvironmentVariables, |
175 |
| - disposables?: Set<IDisposable>, |
176 |
| -): ObservableExecutionResult<string> { |
177 |
| - const spawnOptions = getDefaultOptions(options, defaultEnv); |
178 |
| - const encoding = spawnOptions.encoding ? spawnOptions.encoding : 'utf8'; |
179 |
| - const proc = spawn(file, args, spawnOptions); |
180 |
| - let procExited = false; |
181 |
| - const disposable: IDisposable = { |
182 |
| - dispose() { |
183 |
| - if (proc && !proc.killed && !procExited) { |
184 |
| - killPid(proc.pid!); |
185 |
| - } |
186 |
| - if (proc) { |
187 |
| - proc.unref(); |
188 |
| - } |
189 |
| - }, |
190 |
| - }; |
191 |
| - disposables?.add(disposable); |
192 |
| - |
193 |
| - const output = new Observable<Output<string>>((subscriber) => { |
194 |
| - const internalDisposables: IDisposable[] = []; |
195 |
| - |
196 |
| - // eslint-disable-next-line @typescript-eslint/ban-types |
197 |
| - const on = (ee: Readable | null, name: string, fn: Function) => { |
198 |
| - // eslint-disable-next-line @typescript-eslint/no-explicit-any |
199 |
| - ee?.on(name, fn as any); |
200 |
| - // eslint-disable-next-line @typescript-eslint/no-explicit-any |
201 |
| - internalDisposables.push({ dispose: () => ee?.removeListener(name, fn as any) as any }); |
202 |
| - }; |
203 |
| - |
204 |
| - if (options.token) { |
205 |
| - internalDisposables.push( |
206 |
| - options.token.onCancellationRequested(() => { |
207 |
| - if (!procExited && !proc.killed) { |
208 |
| - proc.kill(); |
209 |
| - procExited = true; |
210 |
| - } |
211 |
| - }), |
212 |
| - ); |
213 |
| - } |
214 |
| - |
215 |
| - const sendOutput = (source: 'stdout' | 'stderr', data: Buffer) => { |
216 |
| - let out = decodeBuffer([data], encoding); |
217 |
| - if (source === 'stderr' && options.throwOnStdErr) { |
218 |
| - subscriber.error(new StdErrError(out)); |
219 |
| - } else { |
220 |
| - // Because all of output is not retrieved at once, filtering out the |
221 |
| - // actual output using markers is not possible. Hence simply remove |
222 |
| - // the markers and return original output. |
223 |
| - out = removeCondaRunMarkers(out); |
224 |
| - subscriber.next({ source, out }); |
225 |
| - } |
226 |
| - }; |
227 |
| - |
228 |
| - on(proc.stdout, 'data', (data: Buffer) => sendOutput('stdout', data)); |
229 |
| - on(proc.stderr, 'data', (data: Buffer) => sendOutput('stderr', data)); |
230 |
| - |
231 |
| - proc.once('close', () => { |
232 |
| - procExited = true; |
233 |
| - subscriber.complete(); |
234 |
| - internalDisposables.forEach((d) => d.dispose()); |
235 |
| - }); |
236 |
| - proc.once('exit', () => { |
237 |
| - procExited = true; |
238 |
| - subscriber.complete(); |
239 |
| - internalDisposables.forEach((d) => d.dispose()); |
240 |
| - }); |
241 |
| - proc.once('error', (ex) => { |
242 |
| - procExited = true; |
243 |
| - subscriber.error(ex); |
244 |
| - internalDisposables.forEach((d) => d.dispose()); |
245 |
| - }); |
246 |
| - }); |
247 |
| - |
248 |
| - return { |
249 |
| - proc, |
250 |
| - out: output, |
251 |
| - dispose: disposable.dispose, |
252 |
| - }; |
253 |
| -} |
254 |
| - |
255 | 127 | export function killPid(pid: number): void {
|
256 | 128 | try {
|
257 | 129 | if (process.platform === 'win32') {
|
|
0 commit comments