UNPKG

@moonstone78/npmaudit

Version:

A small hobby project that reformats npm audit output.

101 lines (92 loc) 2.73 kB
#!/usr/bin/env node import { execSync } from 'child_process' import { existsSync } from 'fs' import chalk from 'chalk' import { status, summary, draw } from './utils.js' const options = process.argv const hasAuditFiles = () => { if (!existsSync('package.json')) { console.log( chalk.red( 'No package.json found in this folder. Run this command in a Node project.' ) ) return false } if (!existsSync('package-lock.json')) { console.log( chalk.red( 'npm audit requires package-lock.json. Run: npm i --package-lock-only' ) ) return false } return true } if (options.includes('-version') || options.includes('-v')) { const version = execSync('npm ls -g @moonstone78/npmaudit', { encoding: 'utf-8', }) console.log( chalk.green( ' Global on your computer: V' + version.substring(version.lastIndexOf('@') + 1) ) ) } else if (options.includes('-latest') || options.includes('-l')) { const version = execSync('npm view @moonstone78/npmaudit version', { encoding: 'utf-8', }) console.log(chalk.green(' Latest on npm: V' + version)) } else if (options.includes('-fix') || options.includes('-f')) { if (!hasAuditFiles()) { process.exit(1) } let auditJsonFix, vulnerabilitieFix try { auditJsonFix = execSync('npm audit fix --json --silent', { encoding: 'utf-8', stdio: ['pipe', 'pipe', 'pipe'], }) auditJsonFix = JSON.parse(auditJsonFix) if (auditJsonFix.audit.metadata.vulnerabilities.total === 0) { console.log( chalk.white('Yes!! There are NO vulnerabilities to fix.\nTotal: ') + chalk.green(auditJsonFix.audit.metadata.vulnerabilities.total) ) } } catch (data) { auditJsonFix = draw(data, vulnerabilitieFix, auditJsonFix, true) if ( auditJsonFix && typeof auditJsonFix.added !== 'undefined' && typeof auditJsonFix.removed !== 'undefined' && typeof auditJsonFix.changed !== 'undefined' && typeof auditJsonFix.audited !== 'undefined' ) { status(auditJsonFix) } summary(auditJsonFix, true) } } else { if (!hasAuditFiles()) { process.exit(1) } let auditJson, vulnerabilitie try { auditJson = execSync('npm audit --json --silent', { encoding: 'utf-8', stdio: ['pipe', 'pipe', 'pipe'], }) auditJson = JSON.parse(auditJson) if (auditJson.metadata.vulnerabilities.total === 0) { console.log( chalk.white('Yes!! There are NO vulnerabilities.\nTotal: ') + chalk.green(auditJson.metadata.vulnerabilities.total) ) } } catch (data) { auditJson = draw(data, vulnerabilitie, auditJson) summary(auditJson) } }