Skip to content

Instantly share code, notes, and snippets.

@matthiasg
Last active September 21, 2023 12:12
Show Gist options
  • Save matthiasg/de75e85dcc3ebc6796359d4b98e60d55 to your computer and use it in GitHub Desktop.
Save matthiasg/de75e85dcc3ebc6796359d4b98e60d55 to your computer and use it in GitHub Desktop.
common/scripts/audit.js - for @microsoft/rush
const fs = require('fs')
const path = require('path')
const child_process = require('child_process')
const pnpmPath = path.resolve(__dirname, '../temp/pnpm-local/node_modules/pnpm/bin/pnpm.cjs')
const tempPath = path.resolve(__dirname, '../temp')
let argv = process.argv.slice(2)
// Simple order fix to avoid building or required a full parser (only works with the two specific parameters basically)
if (argv[0] === '--audit-json') {
argv = [argv[1], argv[2], argv[0]]
}
const options = {
auditLevel: (argv[0] === '--audit-level' && argv[1]) || 'low',
json: (argv[2] === '--audit-json' && true ) || false
}
if (!fs.existsSync(pnpmPath)) {
console.error(`Could not find pnpm at ${pnpmPath}`)
process.exit(2)
}
const parameters = ['--audit-level', options.auditLevel]
if (options.json) {
parameters.push('--json')
}
const p = child_process.spawn('node', [pnpmPath, 'audit', ...parameters], {
shell: true,
cwd: tempPath,
stdio: 'inherit'
})
p.on('error', (e) => {
console.error(`Error running pnpm: ${e}`)
process.exit(100)
})
{
"commands": [
...
{
"commandKind": "global",
"name": "audit",
"summary": "Audit packages for security vulnerabilities. Only use after install, update, and link",
"description": "Run the pnpm audit command",
"shellCommand": "node common/scripts/audit.js"
},
]
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment