Skip to content

Commit

Permalink
Adds reason field to config and output table
Browse files Browse the repository at this point in the history
closes #81

- `cli-table` is no longer supported and has been replaced with `cli-table3`
  • Loading branch information
ominestre committed Apr 13, 2022
1 parent a086329 commit da678a9
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 18 deletions.
1 change: 1 addition & 0 deletions documentation/how-to/create-a-config-file.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,4 @@ This is a collection of rules for each dependency. The individual rules have the
- `dependencyName: string` The name of the dependency you're configuring a rule for
- OPTIONAL `ignore: boolean` Sets the dependency to be ignored. This will never trigger a fail due to being outdated.
- OPTIONAL `daysUntilExpiration: number` This sets the expiration period for the individual dependency
- OPTIONAL `reason: string` This sets the reason for the specific rule which will be output into the table. Useful for memorizing why you whitelisted something to make it more likely that you follow up and fix later.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
},
"dependencies": {
"cli-progress": "^3.10.0",
"cli-table": "~0.3.11",
"cli-table3": "^0.6.2",
"yargs": "~17.4.0"
}
}
17 changes: 14 additions & 3 deletions src/bin/rotten-deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
resolve as pathResolve,
} from 'path';
import cliProgress from 'cli-progress';
import Table from 'cli-table';
import Table from 'cli-table3';
import { existsSync } from 'fs';
import { configuration, generateReport } from '../lib/index';

Expand Down Expand Up @@ -78,7 +78,17 @@ yargs
let exitCode: ExitCode = 0;

const table = new Table({
head: ['name', 'current version', 'latest version', 'days allowed', 'days outdated', 'is outdated'],
wordWrap: true,
head: [
'name',
'current version',
'latest version',
'days allowed',
'days outdated',
'is outdated',
'reason',
],
colWidths: [50, 10, 10, 10, 10, 10, 50],
});

x.data.forEach(({
Expand All @@ -90,12 +100,13 @@ yargs
isIgnored,
isStale,
daysAllowed,
reason,
}) => {
// if the exit code is already 1 for error we don't want to downgrade to warn
if (!isIgnored && exitCode !== 1 && isStale) exitCode = 2;
if (!isIgnored && isOutdated) exitCode = 1;
const outdated = isIgnored ? 'ignored' : isOutdated;
table.push([name, current, latest, daysAllowed, daysOutdated, outdated]);
table.push([name, current, latest, daysAllowed, daysOutdated, outdated, reason]);
});

resolve({
Expand Down
19 changes: 17 additions & 2 deletions src/lib/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
* @module
*/

import Table from 'cli-table';
import Table from 'cli-table3';
import { readFileSync } from 'fs';


interface Rule {
readonly dependencyName: string;
readonly ignore?: boolean;
readonly daysUntilExpiration?: number;
readonly reason?: string;
}

export interface Config {
Expand Down Expand Up @@ -61,12 +62,18 @@ const parseRules = (config: Config): MaybeRules => {

let isValidConfig = true;

rules.forEach(({ dependencyName, ignore = false, daysUntilExpiration = 0 }, i) => {
rules.forEach(({
dependencyName,
ignore = false,
daysUntilExpiration = 0,
reason = '',
}, i) => {
const ruleNumber = i + 1;
const rule = {
dependencyName,
ignore,
daysUntilExpiration,
reason,
};

let isRuleValid = true;
Expand Down Expand Up @@ -96,6 +103,14 @@ const parseRules = (config: Config): MaybeRules => {
});
}

if (typeof reason !== 'string') {
isRuleValid = false;
issues.push({
error: `Rule ${ruleNumber} has a type mismatch for the reason property`,
recommendation: 'The value for reason must be a string',
});
}

if (!isRuleValid) {
isValidConfig = false;
problems.push({
Expand Down
2 changes: 2 additions & 0 deletions src/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ interface ReportData {
isIgnored: boolean,
isStale: boolean,
daysAllowed: number | 'inf',
reason: string,
}

interface Report {
Expand Down Expand Up @@ -174,6 +175,7 @@ export const generateReport = async (c: Config, r?: Reporter): Promise<ReportRes
isIgnored,
isStale,
daysAllowed,
reason: rule?.reason || '',
};

r?.report(data);
Expand Down
13 changes: 11 additions & 2 deletions test/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,15 @@ describe('Configuration library', () => {

assert.throws(d, /Configuration file contains invalid config/);

const e = configLib.createConfig.bind(null, {
rules: [
// @ts-ignore intentionally passing bad type
{ dependencyName: 'barforama', ignore: true, daysUntilExpiration: 30, reason: 12345 },
]
});

assert.throws(e, /Configuration file contains invalid config/);

sinon.restore();
});

Expand All @@ -71,8 +80,8 @@ describe('Configuration library', () => {
assert.equal(kind, 'config');
assert.deepEqual(maybeConfig, {
rules: [
{ dependencyName: 'trevorforget', ignore: false, daysUntilExpiration: 9000 },
{ dependencyName: 'banana', ignore: true, daysUntilExpiration: 13 },
{ dependencyName: 'trevorforget', ignore: false, daysUntilExpiration: 9000, reason: '' },
{ dependencyName: 'banana', ignore: true, daysUntilExpiration: 13, reason: '' },
],
});
});
Expand Down
22 changes: 12 additions & 10 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
# yarn lockfile v1


"@colors/[email protected]":
version "1.5.0"
resolved "https://registry.yarnpkg.com/@colors/colors/-/colors-1.5.0.tgz#bb504579c1cae923e6576a4f5da43d25f97bdbd9"
integrity sha512-ooWCrlZP11i8GImSjTHYHLkvFDP48nS4+204nGb1RiX/WXYHmJA2III9/e2DWVabCESdW7hBAEzHRqUn9OUVvQ==

"@cspotcode/[email protected]":
version "0.8.0"
resolved "https://registry.yarnpkg.com/@cspotcode/source-map-consumer/-/source-map-consumer-0.8.0.tgz#33bf4b7b39c178821606f669bbc447a6a629786b"
Expand Down Expand Up @@ -502,12 +507,14 @@ cli-progress@^3.10.0:
dependencies:
string-width "^4.2.0"

cli-table@~0.3.11:
version "0.3.11"
resolved "https://registry.yarnpkg.com/cli-table/-/cli-table-0.3.11.tgz#ac69cdecbe81dccdba4889b9a18b7da312a9d3ee"
integrity sha512-IqLQi4lO0nIB4tcdTpN4LCB9FI3uqrJZK7RC515EnhZ6qBaglkIgICb1wjeAqpdoOabm1+SuQtkXIPdYC93jhQ==
cli-table3@^0.6.2:
version "0.6.2"
resolved "https://registry.yarnpkg.com/cli-table3/-/cli-table3-0.6.2.tgz#aaf5df9d8b5bf12634dc8b3040806a0c07120d2a"
integrity sha512-QyavHCaIC80cMivimWu4aWHilIpiDpfm3hGmqAmXVL1UsnbLuBSMd21hTX6VY4ZSDSM73ESLeF8TOYId3rBTbw==
dependencies:
colors "1.0.3"
string-width "^4.2.0"
optionalDependencies:
"@colors/colors" "1.5.0"

cliui@^7.0.2:
version "7.0.4"
Expand All @@ -530,11 +537,6 @@ color-name@~1.1.4:
resolved "https://registry.yarnpkg.com/color-name/-/color-name-1.1.4.tgz#c2a09a87acbde69543de6f63fa3995c826c536a2"
integrity sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==

[email protected]:
version "1.0.3"
resolved "https://registry.yarnpkg.com/colors/-/colors-1.0.3.tgz#0433f44d809680fdeb60ed260f1b0c262e82a40b"
integrity sha1-BDP0TYCWgP3rYO0mDxsMJi6CpAs=

[email protected]:
version "0.0.1"
resolved "https://registry.yarnpkg.com/concat-map/-/concat-map-0.0.1.tgz#d8a96bd77fd68df7793a73036a3ba0d5405d477b"
Expand Down

0 comments on commit da678a9

Please sign in to comment.