(rCommand: string, cwd?: string | URL, fallback?: string | ((e: Error) => string))
| 362 | // Single quotes are ok. |
| 363 | // |
| 364 | export async function executeRCommand(rCommand: string, cwd?: string | URL, fallback?: string | ((e: Error) => string)): Promise<string | undefined> { |
| 365 | const rPath = await getRpath(); |
| 366 | if (!rPath) { |
| 367 | return undefined; |
| 368 | } |
| 369 | |
| 370 | const options: cp.CommonOptions = { |
| 371 | cwd: cwd, |
| 372 | }; |
| 373 | |
| 374 | const lim = '---vsc---'; |
| 375 | const args = [ |
| 376 | '--silent', |
| 377 | '--no-echo', |
| 378 | '--no-save', |
| 379 | '--no-restore', |
| 380 | '-e', `cat('${lim}')`, |
| 381 | '-e', rCommand, |
| 382 | '-e', `cat('${lim}')` |
| 383 | ]; |
| 384 | |
| 385 | let ret: string | undefined = undefined; |
| 386 | |
| 387 | try { |
| 388 | const result = await spawnAsync(rPath, args, options); |
| 389 | if (result.status !== 0) { |
| 390 | throw result.error || new Error(result.stderr); |
| 391 | } |
| 392 | const re = new RegExp(`${lim}(.*)${lim}`, 'ms'); |
| 393 | const match = re.exec(result.stdout); |
| 394 | if (!match || match.length !== 2) { |
| 395 | throw new Error('Could not parse R output.'); |
| 396 | } |
| 397 | ret = match[1]; |
| 398 | } catch (e) { |
| 399 | if (fallback) { |
| 400 | ret = (typeof fallback === 'function' ? fallback(catchAsError(e)) : fallback); |
| 401 | } else { |
| 402 | console.warn(e); |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | return ret; |
| 407 | } |
| 408 | |
| 409 | |
| 410 | // This class is a wrapper around Map<string, any> that implements vscode.Memento |
no test coverage detected