1
>
/*---------------------------------------------------------------------------------------------
commandLineHelpers.ts
2
>
* Copyright (c) Microsoft Corporation. All rights reserved.
3
>
* Licensed under the MIT License. See License.txt in the project root for license information.
4
>
*--------------------------------------------------------------------------------------------*/
5
>
6
>
import { extUriBiasedIgnorePathCase } from '../../../base/common/resources.js';
7
>
import { URI } from '../../../base/common/uri.js';
8
>
9
>
/**
10
>
* Result of {@link extractCdPrefix}: the directory the `cd` jumps to and the
11
>
* remaining command after the chain operator.
12
>
*/
13
>
export interface IExtractedCdPrefix {
14
>
readonly directory: string;
15
>
readonly command: string;
16
>
}
17
>
18
>
/**
19
>
* Extracts a `cd <dir> &&` (or PowerShell equivalent) prefix from a command
20
>
* line, returning the directory and remaining command. Does not check whether
21
>
* the directory matches anything — callers do that comparison themselves.
22
>
*
23
>
* The separator between the `cd` and the remaining command may be `&&` or a
24
>
* newline (bash treats a bare newline as a command separator like `;`).
25
>
* PowerShell additionally accepts `;`. The remaining command may span multiple
26
>
* lines (the model frequently emits `cd <dir>` on its own line followed by a
27
>
* multi-line script).
28
>
*
29
>
* Recognized forms:
30
>
* - bash: `cd <dir> && <suffix>`, `cd <dir>\n<suffix>`
31
>
* - powershell: `cd <dir> && <suffix>`, `cd <dir>; <suffix>`, `cd <dir>\n<suffix>`
32
>
* `cd /d <dir> && <suffix>`, `cd /d <dir>; <suffix>`
33
>
* `Set-Location <dir> && <suffix>`, `Set-Location <dir>; <suffix>`
34
>
* `Set-Location -Path <dir> && <suffix>`, `Set-Location -Path <dir>; <suffix>`
35
>
*
36
>
* Surrounding double quotes around `<dir>` are stripped.
37
>
*/
38
>
export function extractCdPrefix(commandLine: string, isPowerShell: boolean): IExtractedCdPrefix | undefined {
39
const cdPrefixMatch = commandLine.match(
40
isPowerShell