Skip to content

Commit

Permalink
fix: Get audit-ci version from package.json (#120)
Browse files Browse the repository at this point in the history
* fix: Get audit-ci version from package.json

Instead of performing a network call to a registry,
get the audit-ci version from the package.json

* fix: Remove redundant comment

* fix: Clean up test function definitions
  • Loading branch information
quinnturner authored Feb 8, 2020
1 parent b1515e4 commit 6330e21
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 23 deletions.
10 changes: 10 additions & 0 deletions lib/audit-ci-version.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const { version: auditCiVersion } = require('../package.json');

if (!auditCiVersion) {
console.log(
'\x1b[33m%s\x1b[0m',
'Could not identify audit-ci version. Please report this issue to https://github.com/IBM/audit-ci/issues.'
);
}

module.exports = { auditCiVersion };
12 changes: 1 addition & 11 deletions lib/npm-auditer.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,10 @@
*
* SPDX-License-Identifier: Apache-2.0
*/
const childProcess = require('child_process');
const { auditCiVersion } = require('./audit-ci-version');
const { runProgram, reportAudit } = require('./common');
const Model = require('./Model');

function getAuditCiVersion() {
const version = childProcess
.execSync('npm show audit-ci version')
.toString()
.replace('\n', '');
return version;
}

function runNpmAudit(config) {
const { directory, registry, _npm } = config;
const npmExec = _npm || 'npm';
Expand Down Expand Up @@ -48,8 +40,6 @@ function runNpmAudit(config) {
}

function printReport(parsedOutput, levels, reportType) {
const auditCiVersion = getAuditCiVersion();

function printReportObj(text, obj) {
console.log('\x1b[36m%s\x1b[0m', text);
console.log(JSON.stringify(obj, null, 2));
Expand Down
9 changes: 1 addition & 8 deletions lib/yarn-auditer.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
const childProcess = require('child_process');
const semver = require('semver');
const { auditCiVersion } = require('./audit-ci-version');
const { reportAudit, runProgram } = require('./common');
const Model = require('./Model');

Expand All @@ -24,13 +25,6 @@ function getYarnVersion() {
return version;
}

function getAuditCiVersion() {
const version = JSON.parse(
childProcess.execSync('yarn info audit-ci version --json')
).data;
return version;
}

function yarnSupportsAudit(yarnVersion) {
return semver.gte(yarnVersion, MINIMUM_YARN_VERSION);
}
Expand Down Expand Up @@ -67,7 +61,6 @@ function audit(config, reporter = reportAudit) {
const model = new Model(config);

const yarnVersion = getYarnVersion();
const auditCiVersion = getAuditCiVersion();
const isYarnVersionSupported = yarnSupportsAudit(yarnVersion);
if (!isYarnVersionSupported) {
throw new Error(
Expand Down
10 changes: 10 additions & 0 deletions test/audit-ci-version.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const semver = require('semver');
const { auditCiVersion } = require('../lib/audit-ci-version');

describe('audit-ci package', () => {
it('gets the version of the audit-ci package', () => {
const packageVersion = auditCiVersion;
semver.valid(packageVersion);
semver.gte(packageVersion, '2.4.2');
});
});
3 changes: 1 addition & 2 deletions test/npm-auditer.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ function testDir(s) {

// To modify what slow times are, need to use
// function() {} instead of () => {}
// eslint-disable-next-line func-names
describe('npm-auditer', function() {
describe('npm-auditer', function testNpmAuditer() {
this.slow(6000);
it('prints full report with critical severity', () => {
return audit(
Expand Down
3 changes: 1 addition & 2 deletions test/yarn-auditer.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ function testDir(s) {

// To modify what slow times are, need to use
// function() {} instead of () => {}
// eslint-disable-next-line func-names
describe('yarn-auditer', function() {
describe('yarn-auditer', function testYarnAuditer() {
this.slow(3000);
it('prints full report with critical severity', () => {
return audit(
Expand Down

0 comments on commit 6330e21

Please sign in to comment.