Skip to content

Commit

Permalink
feat: central colors theme
Browse files Browse the repository at this point in the history
  • Loading branch information
JackuB committed Jul 21, 2021
1 parent 2aecd40 commit e3c3ac0
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 86 deletions.
5 changes: 2 additions & 3 deletions src/cli/commands/protect/prompts.ts
Expand Up @@ -20,7 +20,7 @@ import * as config from '../../../lib/config';
import * as snykPolicy from 'snyk-policy';
import chalk from 'chalk';
import { AnnotatedIssue, SEVERITY } from '../../../lib/snyk-test/legacy';
import { getLegacySeveritiesColour } from '../../../lib/snyk-test/common';
import { colorTextBySeverity } from '../../../lib/snyk-test/common';
import { titleCaseText } from '../../../lib/formatters/legacy-format-issue';

const debug = debugModule('snyk');
Expand All @@ -47,8 +47,7 @@ function sort(prop) {

function createSeverityBasedIssueHeading(msg: string, severity: SEVERITY) {
// Example: ✗ Medium severity vulnerability found in xmldom
const severityColor = getLegacySeveritiesColour(severity);
return severityColor.colorFunc(msg);
return colorTextBySeverity(severity, msg);
}

function sortUpgradePrompts(a, b) {
Expand Down
7 changes: 3 additions & 4 deletions src/lib/formatters/iac-output.ts
Expand Up @@ -12,7 +12,7 @@ import { printPath } from './remediation-based-format-issues';
import { titleCaseText } from './legacy-format-issue';
import * as sarif from 'sarif';
import { SEVERITY } from '../../lib/snyk-test/legacy';
import { getSeveritiesColour } from '../../lib/snyk-test/common';
import { colorTextBySeverity } from '../../lib/snyk-test/common';
import { IacFileInDirectory } from '../../lib/types';
import { isLocalFolder } from '../../lib/detect';
import { getSeverityValue } from './get-severity-value';
Expand All @@ -33,10 +33,9 @@ function formatIacIssue(
introducedBy = `\n introduced by ${pathStr}`;
}

const severityColor = getSeveritiesColour(issue.severity);

return (
severityColor.colorFunc(
colorTextBySeverity(
issue.severity,
` ✗ ${chalk.bold(issue.title)}${newBadge} [${titleCaseText(
issue.severity,
)} Severity]`,
Expand Down
6 changes: 3 additions & 3 deletions src/lib/formatters/legacy-format-issue.ts
Expand Up @@ -18,7 +18,7 @@ import {
} from '../../lib/snyk-test/legacy';
import { formatLegalInstructions } from './legal-license-instructions';
import { getReachabilityText } from './format-reachability';
import { getLegacySeveritiesColour } from '../../lib/snyk-test/common';
import { colorTextBySeverity } from '../../lib/snyk-test/common';
import { PATH_SEPARATOR } from '../constants';

export function formatIssues(
Expand Down Expand Up @@ -97,15 +97,15 @@ function createSeverityBasedIssueHeading({
}: CreateSeverityBasedIssueHeading) {
// Example: ✗ Medium severity vulnerability found in xmldom
const vulnTypeText = type === 'license' ? 'issue' : 'vulnerability';
const severityColor = getLegacySeveritiesColour(severity);

let originalSeverityStr = '';
if (originalSeverity && originalSeverity !== severity) {
originalSeverityStr = ` (originally ${titleCaseText(originalSeverity)})`;
}

return (
severityColor.colorFunc(
colorTextBySeverity(
severity,
'✗ ' +
titleCaseText(severity) +
` severity${originalSeverityStr} ` +
Expand Down
7 changes: 3 additions & 4 deletions src/lib/formatters/remediation-based-format-issues.ts
Expand Up @@ -14,7 +14,7 @@ import {
SEVERITY,
UpgradeRemediation,
} from '../../lib/snyk-test/legacy';
import { getSeveritiesColour } from '../../lib/snyk-test/common';
import { colorTextBySeverity } from '../../lib/snyk-test/common';
import { formatLegalInstructions } from './legal-license-instructions';
import {
formatReachability,
Expand Down Expand Up @@ -434,8 +434,6 @@ export function formatIssue(
reachability?: REACHABILITY,
sampleReachablePaths?: SampleReachablePaths,
): string {
const severityColor = getSeveritiesColour(severity);

const newBadge = isNew ? ' (new)' : '';
const name = vulnerableModule ? ` in ${chalk.bold(vulnerableModule)}` : '';
let legalLicenseInstructionsText;
Expand Down Expand Up @@ -488,7 +486,8 @@ export function formatIssue(
}

return (
severityColor.colorFunc(
colorTextBySeverity(
severity,
` ✗ ${chalk.bold(title)}${newBadge} [${titleCaseText(
severity,
)} Severity${originalSeverityStr}]`,
Expand Down
21 changes: 8 additions & 13 deletions src/lib/plugins/sast/format/output-format.ts
@@ -1,7 +1,7 @@
import * as Sarif from 'sarif';
import * as Debug from 'debug';
import chalk from 'chalk';
import { getLegacySeveritiesColour, SEVERITY } from '../../../snyk-test/common';
import { colorTextBySeverity, SEVERITY } from '../../../snyk-test/common';
import { rightPadWithSpaces } from '../../../right-pad';
import { Options } from '../../../types';

Expand Down Expand Up @@ -43,19 +43,13 @@ export function getCodeDisplayedOutput(

function getCodeIssuesSummary(issues: { [index: string]: string[] }): string {
const lowSeverityText = issues.low.length
? getLegacySeveritiesColour(SEVERITY.LOW).colorFunc(
` ${issues.low.length} [Low] `,
)
? colorTextBySeverity(SEVERITY.LOW, ` ${issues.low.length} [Low] `)
: '';
const mediumSeverityText = issues.medium.length
? getLegacySeveritiesColour(SEVERITY.MEDIUM).colorFunc(
` ${issues.medium.length} [Medium] `,
)
? colorTextBySeverity(SEVERITY.MEDIUM, ` ${issues.medium.length} [Medium] `)
: '';
const highSeverityText = issues.high.length
? getLegacySeveritiesColour(SEVERITY.HIGH).colorFunc(
`${issues.high.length} [High] `,
)
? colorTextBySeverity(SEVERITY.HIGH, `${issues.high.length} [High] `)
: '';

const codeIssueCount =
Expand Down Expand Up @@ -93,9 +87,10 @@ function getIssues(
}
const ruleName =
rulesMap[ruleId].shortDescription?.text || rulesMap[ruleId].name;
const ruleIdSeverityText = getLegacySeveritiesColour(
severity.toLowerCase(),
).colorFunc(` ✗ [${severity}] ${ruleName}`);
const ruleIdSeverityText = colorTextBySeverity(
severity,
` ✗ [${severity}] ${ruleName}`,
);
const artifactLocationUri = location.artifactLocation.uri;
const startLine = location.region.startLine;
const text = res.message.text;
Expand Down
67 changes: 8 additions & 59 deletions src/lib/snyk-test/common.ts
@@ -1,5 +1,5 @@
import * as config from '../config';
import chalk from 'chalk';
import { color } from '../theme';

export function assembleQueryString(options) {
const org = options.org || config.org || null;
Expand Down Expand Up @@ -49,64 +49,13 @@ export const SEVERITIES: Array<{
},
];

export const severitiesColourMapping = {
low: {
colorFunc(text) {
return chalk.blueBright(text);
},
},
medium: {
colorFunc(text) {
return chalk.yellowBright(text);
},
},
high: {
colorFunc(text) {
return chalk.redBright(text);
},
},
critical: {
colorFunc(text) {
return chalk.magentaBright(text);
},
},
};

export const legacySeveritiesColourMapping = {
low: {
colorFunc(text) {
return chalk.bold.blue(text);
},
},
medium: {
colorFunc(text) {
return chalk.bold.yellow(text);
},
},
high: {
colorFunc(text) {
return chalk.bold.red(text);
},
},
critical: {
colorFunc(text) {
return chalk.bold.magenta(text);
},
},
};

export const defaultSeverityColor = {
colorFunc(text) {
return chalk.grey(text);
},
};

export function getSeveritiesColour(severity: string) {
return severitiesColourMapping[severity] || defaultSeverityColor;
}

export function getLegacySeveritiesColour(severity: string) {
return legacySeveritiesColourMapping[severity] || defaultSeverityColor;
export function colorTextBySeverity(
severity: string,
textToColor: string,
): string {
return (color.severity[(severity || '').toLowerCase()] || color.severity.low)(
textToColor,
);
}

export enum FAIL_ON {
Expand Down
22 changes: 22 additions & 0 deletions src/lib/theme.ts
@@ -0,0 +1,22 @@
import chalk from 'chalk';

export const icon = {
RUN: '►',
VALID: '✔',
ISSUE: '✗',
WARNING: '⚠',
INFO: 'ℹ',
};

export const color = {
status: {
error: (text: string) => chalk.red(text),
success: (text: string) => chalk.green(text),
},
severity: {
critical: (text: string) => chalk.magenta(text),
high: (text: string) => chalk.red(text),
medium: (text: string) => chalk.yellow(text),
low: (text: string) => text,
},
};

0 comments on commit e3c3ac0

Please sign in to comment.