-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
53 lines (47 loc) · 1.54 KB
/
index.js
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
import { cwd, from } from '@nodesecure/scanner'
import { gatherLockFileSafetyMetric, getMetrics } from './src/metrics.js'
import { createReport } from './src/report.js'
import { findLockFilePath, loadConfig, loadIgnoreFile, resolveMode } from './src/utils.js'
/**
* @typedef {import('./index').check} check
* @type check
*/
export async function check({
config,
ignoredPackages,
packageName = '',
rootDir = '',
version = ''
}) {
try {
if (!rootDir && !packageName) {
throw new Error('You have not specified rootDir or packageName')
}
let mode = resolveMode(rootDir)
let lockfilePath = await findLockFilePath(mode, rootDir)
config = config || (await loadConfig(mode, rootDir))
ignoredPackages = ignoredPackages || (await loadIgnoreFile(rootDir))
let nssOutput =
mode === 'internal'
? await cwd(rootDir, {
forceRootAnalysis: true,
vulnerabilityStrategy: 'npm'
})
: await from(version ? `${packageName}@${version}` : packageName, {
forceRootAnalysis: true,
vulnerabilityStrategy: 'npm'
})
return createReport({
config,
findings: await getMetrics(nssOutput, config, rootDir),
ignoredPackages,
lockFileIsNotSafe: await gatherLockFileSafetyMetric(lockfilePath)
})
} catch (error) {
/* eslint-disable no-console */
console.error(error)
console.error('ERROR: Could not perform sdc-check audit')
/* eslint-enable no-console */
return { errors: [], type: 'none', warnings: [] }
}
}