forked from jeemok/better-npm-audit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
executable file
·62 lines (50 loc) · 2.26 KB
/
index.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
#!/usr/bin/env node
import { Command } from 'commander';
import { exec } from 'child_process';
import { AuditLevel, CommandOptions } from 'src/types';
import handleInput from './src/handlers/handleInput';
import handleFinish from './src/handlers/handleFinish';
import packageJson from './package.json';
const MAX_BUFFER_SIZE = 1024 * 1000 * 50; // 50 MB
const program = new Command();
/**
* Run audit
* @param {String} auditCommand The NPM audit command to use (with flags)
* @param {String} auditLevel The level of vulnerabilities we care about
* @param {Array} exceptionIds List of vulnerability IDs to exclude
* @param {Array} modulesToIgnore List of vulnerable modules to ignore in audit results
* @param {Array} columnsToInclude List of columns to include in audit results
*/
export function callback(
auditCommand: string,
auditLevel: AuditLevel,
exceptionIds: string[],
modulesToIgnore: string[],
columnsToInclude: string[],
): void {
// Increase the default max buffer size (1 MB)
const audit = exec(`${auditCommand} --json`, { maxBuffer: MAX_BUFFER_SIZE });
// Grab the data in chunks and buffer it as we're unable to parse JSON straight from stdout
let jsonBuffer = '';
if (audit.stdout) {
audit.stdout.on('data', (data: string) => (jsonBuffer += data));
}
// Once the stdout has completed, process the output
if (audit.stderr) {
audit.stderr.on('close', () => handleFinish(jsonBuffer, auditLevel, exceptionIds, modulesToIgnore, columnsToInclude));
// stderr
audit.stderr.on('data', console.error);
}
}
program.name(packageJson.name).version(packageJson.version);
program
.command('audit')
.description('execute npm audit')
.option('-x, --exclude <ids>', 'Exceptions or the vulnerabilities ID(s) to exclude.')
.option('-m, --module-ignore <moduleNames>', 'Names of modules to ignore.')
.option('-l, --level <auditLevel>', 'The minimum audit level to validate.')
.option('-p, --production', 'Skip checking the devDependencies.')
.option('-r, --registry <url>', 'The npm registry url to use.')
.option('-i, --include-columns <columnName1>,<columnName2>,..,<columnNameN>', 'Columns to include in report.')
.action((options: CommandOptions) => handleInput(options, callback));
program.parse(process.argv);