-
-
Notifications
You must be signed in to change notification settings - Fork 340
Expand file tree
/
Copy pathparse-argv.ts
More file actions
39 lines (33 loc) · 1.5 KB
/
parse-argv.ts
File metadata and controls
39 lines (33 loc) · 1.5 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
import { collectConfigAccess } from '../config/analyse-config';
import type { Flag } from '../flags/flags.helpers';
import { parseGlobalFlags } from '../flags/parse-global-flags';
import { parseTaskFlags } from '../flags/parse-task-flags';
import type { Vulnerability } from '../vulnerabilities/vulnerability.types';
import { vulnerabilityAnalysis } from '../vulnerabilities/vulnerability-analysis';
import type { ParsedArgv, ParsedFlag } from './parse-argv.types';
/**
* Parse the tokens that would be forwarded to a `git` child-process and
* return a structured summary of what the invocation does.
*/
export function parseArgv(...tokens: readonly unknown[]): ParsedArgv {
const { flags, taskIndex } = parseGlobalFlags(tokens);
const task = taskIndex < tokens.length ? String(tokens[taskIndex]).toLowerCase() : null;
const taskTokens = task !== null ? tokens.slice(taskIndex + 1) : [];
const { positionals, pathspecs } = parseTaskFlags(taskTokens, task, flags);
const config = collectConfigAccess(task, flags, positionals);
return {
task,
flags: flags.map(toParsedFlag),
paths: pathspecs,
config,
vulnerabilities: vulnerabilityList(vulnerabilityAnalysis(task, flags, config)),
};
}
function vulnerabilityList(vulnerabilities: Vulnerability[]) {
return Object.defineProperty(vulnerabilities, 'vulnerabilities', {
value: vulnerabilities,
});
}
function toParsedFlag({ value, name }: Flag): ParsedFlag {
return value !== undefined ? { name, value } : { name };
}