• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

oke-py / npm-audit-action / 14797115667

02 May 2025 02:24PM UTC coverage: 73.444% (-3.4%) from 76.866%
14797115667

Pull #223

github

web-flow
Merge 4427195a4 into a898de739
Pull Request #223: refactor(testing): migrate from Jest to Vitest for testing framework

33 of 42 branches covered (78.57%)

Branch coverage included in aggregate %.

144 of 199 relevant lines covered (72.36%)

4.54 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.08
/src/audit.ts
1
import {spawnSync, SpawnSyncReturns} from 'child_process'
2✔
2
import stripAnsi from 'strip-ansi'
2✔
3

4
const SPAWN_PROCESS_BUFFER_SIZE = 10485760 // 10MiB
2✔
5

6
export class Audit {
2✔
7
  stdout = ''
2✔
8
  private status: number | null = null
2✔
9

10
  public run(
2✔
11
    auditLevel: string,
14✔
12
    productionFlag: string,
14✔
13
    jsonFlag: string
14✔
14
  ): void {
14✔
15
    try {
14✔
16
      const auditOptions: Array<string> = ['audit', '--audit-level', auditLevel]
14✔
17

18
      const isWindowsEnvironment: boolean = process.platform == 'win32'
14✔
19
      const cmd: string = isWindowsEnvironment ? 'npm.cmd' : 'npm'
14!
20

21
      if (productionFlag === 'true') {
14✔
22
        auditOptions.push('--omit=dev')
2✔
23
      }
2✔
24

25
      if (jsonFlag === 'true') {
14✔
26
        auditOptions.push('--json')
2✔
27
      }
2✔
28

29
      const result: SpawnSyncReturns<string> = spawnSync(cmd, auditOptions, {
14✔
30
        encoding: 'utf-8',
14✔
31
        maxBuffer: SPAWN_PROCESS_BUFFER_SIZE
14✔
32
      })
14✔
33

34
      if (result.error) {
14✔
35
        throw result.error
2✔
36
      }
2✔
37
      if (result.status === null) {
14✔
38
        throw new Error('the subprocess terminated due to a signal.')
2✔
39
      }
2✔
40
      if (result.stderr && result.stderr.length > 0) {
14✔
41
        throw new Error(result.stderr)
2✔
42
      }
2✔
43

44
      this.status = result.status
8✔
45
      this.stdout = result.stdout
8✔
46
    } catch (error) {
14✔
47
      throw error
6✔
48
    }
6✔
49
  }
14✔
50

51
  public foundVulnerability(): boolean {
2✔
52
    // `npm audit` return 1 when it found vulnerabilities
53
    return this.status === 1
8✔
54
  }
8✔
55

56
  public strippedStdout(): string {
2✔
57
    return `\`\`\`\n${stripAnsi(this.stdout)}\n\`\`\``
×
58
  }
×
59
}
2✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc