-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathargs-tokenizer.ts
More file actions
67 lines (60 loc) · 1.38 KB
/
args-tokenizer.ts
File metadata and controls
67 lines (60 loc) · 1.38 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
const spaceRegex = /\s/;
type Options = {
loose?: boolean;
};
/**
* Tokenize a shell string into argv array
*/
export const tokenizeArgs = (
argsString: string,
options?: Options,
): string[] => {
const tokens = [];
let currentToken = "";
let openningQuote: undefined | string;
let escaped = false;
for (let index = 0; index < argsString.length; index += 1) {
const char = argsString[index];
if (escaped) {
escaped = false;
// escape newline inside of quotes
// ignore newline elsewhere
if (openningQuote || char !== "\n") {
currentToken += char;
}
continue;
}
if (char === "\\") {
escaped = true;
continue;
}
if (openningQuote === undefined && spaceRegex.test(char)) {
if (currentToken.length > 0) {
tokens.push(currentToken);
currentToken = "";
}
continue;
}
if (char === "'" || char === '"') {
if (openningQuote === undefined) {
openningQuote = char;
continue;
}
if (openningQuote === char) {
openningQuote = undefined;
continue;
}
}
currentToken += char;
}
if (currentToken.length > 0) {
tokens.push(currentToken);
}
if (options?.loose) {
return tokens;
}
if (openningQuote) {
throw Error("Unexpected end of string. Closing quote is missing.");
}
return tokens;
};