Skip to content

Commit 5dc0705

Browse files
Add performing npm audit locally (#21151)
* Add performing npm audit locally * Remove the fix attribute from the npm audit output * Add the option to bypass the auditing step * Add a command example to the contribute documentation * Add the process exiting when we catch exception during auditing
1 parent d65d7c5 commit 5dc0705

File tree

2 files changed

+61
-12
lines changed

2 files changed

+61
-12
lines changed

docs/contribute.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,12 @@ npm run build
100100
node make.js build --task ShellScript
101101
```
102102

103+
## Build task with the bypassed auditing step
104+
105+
```bash
106+
node make.js build --task ShellScript --BypassNpmAudit
107+
```
108+
103109
## Run Tests
104110

105111
Tests for each task are located in Tests folder for each task. To get additional debugging when you are running your tests, set the environment variable TASK_TEST_TRACE to 1. This will cause additional logging to be printed to STDOUT.

make-util.js

Lines changed: 55 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
1-
var check = require('validator').default;
2-
var fs = require('fs');
3-
var makeOptions = require('./make-options.json');
4-
var minimatch = require('minimatch');
5-
var ncp = require('child_process');
6-
var os = require('os');
7-
var path = require('path');
8-
var process = require('process');
9-
var semver = require('semver');
10-
var shell = require('shelljs');
11-
const { XMLParser } = require("fast-xml-parser");
12-
const Downloader = require("nodejs-file-downloader");
1+
const ncp = require('child_process');
2+
const fs = require('fs');
3+
const os = require('os');
4+
const path = require('path');
5+
const process = require('process');
6+
7+
const { XMLParser } = require('fast-xml-parser');
8+
const minimatch = require('minimatch');
9+
const minimist = require('minimist');
10+
const Downloader = require('nodejs-file-downloader');
11+
const check = require('validator').default;
12+
const semver = require('semver');
13+
const shell = require('shelljs');
14+
15+
const makeOptions = require('./make-options.json');
16+
17+
const args = minimist(process.argv.slice(2));
1318

1419
// global paths
1520
var repoPath = __dirname;
@@ -154,6 +159,42 @@ var getCommonPackInfo = function (modOutDir) {
154159
}
155160
exports.getCommonPackInfo = getCommonPackInfo;
156161

162+
function performNpmAudit(taskPath) {
163+
console.log('\n🛫 Running npm audit...');
164+
165+
if (process.env['TF_BUILD']) {
166+
console.log(`\x1b[A\x1b[K⏭️ Skipping npm audit in build pipeline because it is not supported in the pipeline.`);
167+
return;
168+
}
169+
170+
if (args.BypassNpmAudit) {
171+
console.log(`\x1b[A\x1b[K⏭️ Skipping npm audit because --BypassNpmAudit argument is set.`);
172+
return;
173+
}
174+
175+
try {
176+
const auditResult = ncp.spawnSync('npm', ['audit', '--prefix', taskPath, '--audit-level=high'], {
177+
stdio: 'pipe',
178+
encoding: 'utf8',
179+
});
180+
181+
if (auditResult.error) {
182+
console.log(`\x1b[A\x1b[K❌ npm audit failed because the build task at "${taskPath}" has vulnerable dependencies.`);
183+
console.log('👉 Please see details by running the command');
184+
console.log(`\tnpm audit --prefix ${taskPath}`);
185+
console.log('or execute the command with --BypassNpmAudit argument to skip the auditing');
186+
console.log(`\tnode make.js --build --task ${args.task} --BypassNpmAudit`);
187+
process.exit(1);
188+
} else {
189+
console.log('\x1b[A\x1b[K✅ npm audit completed successfully.');
190+
}
191+
} catch (error) {
192+
console.error('\x1b[A\x1b[K❌ "performNpmAudit" failed.');
193+
console.error(error.message);
194+
process.exit(1);
195+
}
196+
}
197+
157198
var buildNodeTask = function (taskPath, outDir, isServerBuild) {
158199
var originalDir = shell.pwd().toString();
159200
cd(taskPath);
@@ -191,6 +232,8 @@ var buildNodeTask = function (taskPath, outDir, isServerBuild) {
191232
cd(taskPath);
192233
}
193234

235+
performNpmAudit(taskPath);
236+
194237
// Use the tsc version supplied by the task if it is available, otherwise use the global default.
195238
if (overrideTscPath) {
196239
var tscExec = path.join(overrideTscPath, "bin", "tsc");

0 commit comments

Comments
 (0)