-
-
Notifications
You must be signed in to change notification settings - Fork 259
Expand file tree
/
Copy pathexpand-arguments.ts
More file actions
46 lines (38 loc) · 1.67 KB
/
expand-arguments.ts
File metadata and controls
46 lines (38 loc) · 1.67 KB
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
import { quote } from 'shell-quote';
import type { CommandInfo } from '../command.js';
import type { CommandParser } from './command-parser.js';
/**
* Replace placeholders with additional arguments.
*/
export class ExpandArguments implements CommandParser {
constructor(private readonly additionalArguments: string[]) {}
parse(commandInfo: CommandInfo) {
const command = commandInfo.command.replace(
/\\?\{([@*]|[1-9]\d*)\}/g,
(match, placeholderTarget: string) => {
// Don't replace the placeholder if it is escaped by a backslash.
if (match.startsWith('\\')) {
return match.slice(1);
}
if (this.additionalArguments.length > 0) {
// Replace numeric placeholder if value exists in additional arguments.
if (+placeholderTarget <= this.additionalArguments.length) {
return quote([this.additionalArguments[+placeholderTarget - 1]]);
}
// Replace all arguments placeholder.
if (placeholderTarget === '@') {
return quote(this.additionalArguments);
}
// Replace combined arguments placeholder.
if (placeholderTarget === '*') {
return quote([this.additionalArguments.join(' ')]);
}
}
// Replace placeholder with empty string
// if value doesn't exist in additional arguments.
return '';
},
);
return { ...commandInfo, command };
}
}