-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathhandleInput.ts
57 lines (53 loc) · 1.93 KB
/
handleInput.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
import get from 'lodash.get';
import semver from 'semver';
import { AuditLevel, CommandOptions } from 'src/types';
import { getNpmVersion } from '../utils/npm';
import { readFile } from '../utils/file';
import { getExceptionsIds } from '../utils/vulnerability';
/**
* Get the `npm audit` flag to audit only production dependencies.
* @return {String} The flag.
*/
function getProductionOnlyOption() {
const npmVersion = getNpmVersion();
if (semver.satisfies(npmVersion, '<=8.13.2')) {
return '--production';
} else {
return '--omit=dev';
}
}
/**
* Handle user's input
* @param {Object} options User's command options or flags
* @param {Function} fn The function to handle the inputs
*/
export default function handleInput(
options: CommandOptions,
fn: (T1: string, T2: AuditLevel, T3: string[], T4: string[], T5: string[]) => void,
): void {
// Generate NPM Audit command
const auditCommand: string = [
'npm audit',
// flags
get(options, 'production') ? getProductionOnlyOption() : '',
get(options, 'registry') ? `--registry=${options.registry}` : '',
]
.filter(Boolean)
.join(' ');
// Taking the audit level from the command or environment variable
const envVar = process.env.NPM_CONFIG_AUDIT_LEVEL as AuditLevel;
const auditLevel: AuditLevel = get(options, 'level', envVar) || 'info';
// Get the exceptions
const nsprc = readFile('.nsprc');
const cmdExceptions: string[] = get(options, 'exclude', '')
.split(',')
.map((each) => each.trim())
.filter((each) => each !== '');
const exceptionIds: string[] = getExceptionsIds(nsprc, cmdExceptions);
const cmdModuleIgnore: string[] = get(options, 'moduleIgnore', '').split(',');
const cmdIncludeColumns: string[] = get(options, 'includeColumns', '')
.split(',')
.map((each: string) => each.trim())
.filter((each: string) => !!each);
fn(auditCommand, auditLevel, exceptionIds, cmdModuleIgnore, cmdIncludeColumns);
}