forked from nrigaudiere/yarn-recursive
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patharguments.ts
93 lines (82 loc) · 2.94 KB
/
arguments.ts
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import {notNull} from "@softwareventures/nullable";
export interface Options {
readonly skipRoot: boolean;
readonly includeHidden: boolean;
readonly concurrent: boolean;
readonly command: ReadonlyArray<string>;
readonly deprecatedCmdOpt: boolean;
}
export function readArguments(args: ReadonlyArray<string>): Options {
let skipRoot = false;
let includeHidden = false;
let concurrent = false;
let command: string[] = [];
let deprecatedCmdOpt = false;
let mode: "bare" | "cmd" | "opt" | "passthrough" = "bare";
for (const arg of args) {
if (mode === "passthrough") {
command.push(arg);
} else if (mode === "cmd") {
command = arg.split(" ").concat(command);
mode = "bare";
} else if (mode === "opt") {
command = command.concat(arg.split(" "));
mode = "bare";
} else {
const argument = readArgument(arg);
if (argument === "--") {
mode = "passthrough";
} else if (typeof argument === "string") {
command.push(argument);
} else if (argument.name === "skip-root") {
skipRoot = !!argument.value;
} else if (argument.name === "include-hidden") {
includeHidden = !!argument.value;
} else if (argument.name === "concurrent") {
concurrent = !!argument.value;
} else if (argument.name === "cmd") {
deprecatedCmdOpt = true;
if (typeof argument.value === "string") {
command = argument.value.split(" ").concat(command);
} else if (argument.value) {
mode = "cmd";
}
} else if (argument.name === "opt") {
deprecatedCmdOpt = true;
if (typeof argument.value === "string") {
command = command.concat(argument.value.split(" "));
} else if (argument.value) {
mode = "opt";
}
} else {
command.push(arg);
}
}
}
return {skipRoot, includeHidden, concurrent, command, deprecatedCmdOpt};
}
export type Argument = Option | string;
export interface Option {
readonly type: "option";
readonly name: string;
readonly value: string | boolean;
}
export function readArgument(arg: string): Argument {
if (arg === "--") {
return arg;
}
const matches = /^--([^=]*)(?:=(.*))?$/.exec(arg);
if (matches) {
let name = notNull(matches[1]).replace(/[A-Z]/g, s => "-" + s.toLowerCase());
let value = matches[2] == null
? true
: matches[2];
if (value === true && name.match(/^no-[a-z]/)) {
name = name.replace(/^no-[a-z]/, s => s.charAt(3));
value = false;
}
return {type: "option", name, value};
} else {
return arg;
}
}